bin/kelp-generator

filter: Statementstmt 88.6% 47 / 53 Branchbran 62.5% 10 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 78.8% 67 / 85 CC 2 SCAR 3.1
CC
2
Cov
80%
_say
2.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
« blib/lib/Kelp/Module/Logger.pm ↑ Summary blib/lib/Kelp/Module/Routes.pm »
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
BranchTrueFalseTotal
if (not $name or $help) 1 2 3
Condition66%2/3
not $name or $help
l!l&&r!l&&!r
102
MC/DC50%1/2
not $name or $help
not $name $help
Truth table66%2/3
not(A) or B
A= not $nameB= $help
ABresult
1X1
011
000
29 1 ▶ say 'Error - no application name.' unless $help;
Branch: 1 branch
BranchTrueFalseTotal
unless $help 0 1 1
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
BranchTrueFalseTotal
$help ? : 1 0 1
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
BranchTrueFalseTotal
if $tabs 0 10 10
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
BranchTrueFalseTotal
unless (-d $dir) 5 5 10
53 5 _say("Creating folder: $dir");
54 5 path($dir)->mkpath;
55 }
56  
57 10 ▶ if ($file->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_dir) 0 10 10
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
BranchTrueFalseTotal
if ($file->exists and not $force) 0 10 10
Condition33%1/3
$file->exists and not $force
!ll&&!rl&&r
1000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($verbose) 0 15 15
74 0 say $what;
75 }
76 }
77  
78 __END__
« blib/lib/Kelp/Module/Logger.pm ↑ Summary blib/lib/Kelp/Module/Routes.pm »