| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Kelp::Base -strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Kelp::Generator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | my $path = '.'; | |||||||||||||||||||||||||||
| 10 | 3 | my $verbose = 1; | |||||||||||||||||||||||||||
| 11 | 3 | my $force = 0; | |||||||||||||||||||||||||||
| 12 | 3 | my $type = 'kelp'; | |||||||||||||||||||||||||||
| 13 | 3 | my $tabs = 0; | |||||||||||||||||||||||||||
| 14 | 3 | my $help = 0; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | GetOptions( | |||||||||||||||||||||||||||
| 17 | "path=s" => \$path, | ||||||||||||||||||||||||||||
| 18 | "type=s" => \$type, | ||||||||||||||||||||||||||||
| 19 | "verbose!" => \$verbose, | ||||||||||||||||||||||||||||
| 20 | "force!" => \$force, | ||||||||||||||||||||||||||||
| 21 | "tabs!" => \$tabs, | ||||||||||||||||||||||||||||
| 22 | "help" => \$help | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | my $name = $ARGV[0]; | |||||||||||||||||||||||||||
| 26 | 3 | my $generator = Kelp::Generator->new; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | if (!$name || $help) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $name or $help
MC/DC50%1/2
not $name or $help
not $name
$help
Truth table66%2/3
not(A) or B
A= not $nameB= $help
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | say 'Error - no application name.' unless $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 1 | pod2usage(-verbose => 1, -exitval => 'NOEXIT'); | |||||||||||||||||||||||||||
| 31 | 1 | say 'Available application types:'; | |||||||||||||||||||||||||||
| 32 | 1 | say for map { ' ' x 4 . $_ } $generator->list_templates; | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | exit ($help ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Remove the slash at the end | ||||||||||||||||||||||||||||
| 37 | 2 | $path =~ s{/$}{}; | |||||||||||||||||||||||||||
| 38 | 2 | my $files = $generator->get_template($type, $name); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | for my $filedata (@$files) { | |||||||||||||||||||||||||||
| 41 | 10 | my ($filename, $contents) = @$filedata; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # replace spaces with tabs | ||||||||||||||||||||||||||||
| 44 | # each 4 spaces will become a tab character | ||||||||||||||||||||||||||||
| 45 | # last 2 spaces will also become a tab due to rounding (+0.5) | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $contents =~ s{ ^ ((?: [ ]{4} | [ ]{2} )+) }{ "\t" x (length($1) / 4 + 0.5) }xmeg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | if $tabs; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 10 | my $dir = $path . path("/$filename")->parent; | |||||||||||||||||||||||||||
| 50 | 10 | my $file = path($path . '/' . $filename); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 10 | ▶ | if (!-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 5 | _say("Creating folder: $dir"); | |||||||||||||||||||||||||||
| 54 | 5 | path($dir)->mkpath; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | if ($file->is_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | say "$filename is a directory - manual action required. Skipping..."; | |||||||||||||||||||||||||||
| 59 | 0 | next; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ($file->exists && !$force) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file->exists and not $force
MC/DC0%0/2
$file->exists and not $force
$file->exists
not $force
Truth table33%1/3
A and not(B)
A= $file->existsB= not $force
| |||||||||||||||||||||||||||||
| 63 | 0 | say "File $filename exists. Use --force to overwrite. Skipping..."; | |||||||||||||||||||||||||||
| 64 | 0 | next; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | _say("Writing file: $filename"); | |||||||||||||||||||||||||||
| 68 | 10 | $file->spew({binmode => ':encoding(UTF-8)'}, $contents); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 15 | ▶ | sub _say { | ||||||||||||||||||||||||||
|
Subroutine
_say: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 72 | 15 | my $what = shift; | |||||||||||||||||||||||||||
| 73 | 15 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | say $what; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||