CLI/Command/Init.pm

filter: Statementstmt 47.6% 30 / 63 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 10 / 14 Podpod 75.0% 3 / 4 totaltotal 39.0% 43 / 110 CC 13 SCAR 38.3
CC
13
Cov
34%
execute
110.0
render
20.0
abstract
2.0
CRAP 45.9
« CLI/Command/Add.pm ↑ Summary CLI/Command/List.pm »
1 package Luv::CLI::Command::Init;
2  
3 # ABSTRACT: initialize a new luv project
4  
5 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use App::Cmd::Setup -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Cwd qw(getcwd chdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Luv::CLI::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub abstract {
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
19 0 "Initializes a new Love2D project.";
20 }
21  
22 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
23 return (
24 0 [ "name|n=s" =>
25 "Project name (defaults to the current directory's name)."
26 ],
27 [ "force|f" =>
28 "Overwrites an existing luv.json file within the project directory."
29 ],
30 );
31 }
32  
33 0 ▶sub execute( $self, $opt, $args ) {
Subroutine execute: not called CC=10 SCAR=47.0
Pod documented
34 0 ▶ my $project_name = $opt->{name} // basename( getcwd() );
Condition0%0/3
$opt->{"name"} // basename(getcwd())
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"name"} // basename(getcwd())
$opt->{"name"} basename(getcwd())
Truth table0%0/3
A // B
A= $opt->{"name"}B= basename(getcwd())
ABresult
1X1
011
000
35  
36 0 ▶ if ( $opt->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"name"}) 0 0 0
37 0 ▶ make_path($project_name) unless -d $project_name;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $project_name 0 0 0
38 0 ▶ chdir($project_name)
Branch: 1 branch
BranchTrueFalseTotal
Luv::CLI::Command::Init::chdir($project_name) or die "Cannot access directory '${project_name}': $!\n" 0 0 0
39 or die "Cannot access directory '$project_name': $!\n";
40 }
41  
42 0 my $manifest_path = "luv.json";
43  
44 0 ▶ if ( -e $manifest_path && !$opt->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $manifest_path and not $opt->{"force"}) 0 0 0
Condition0%0/3
-e $manifest_path and not $opt->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $manifest_path and not $opt->{"force"}
-e $manifest_path not $opt->{"force"}
Truth table0%0/3
A and not(B)
A= -e $manifest_pathB= not $opt->{"force"}
ABresult
0X0
100
111
45 0 die
46 "luv.json already exist: use --force|-f to overwrite the current file.\n";
47 }
48  
49 0 my $manifest = Luv::CLI::Manifest->new(
50 path => $manifest_path,
51 project_name => $project_name
52 );
53  
54 0 $manifest->save;
55  
56 0 make_path(
57 $manifest->source_dir, $manifest->library_dir,
58 $manifest->assets_dir, $manifest->build_dir
59 );
60  
61 0 ▶ my $share_dir = eval { dist_dir("Luv-CLI") } // "$Bin/../share";
Condition0%0/2
eval { do { dist_dir("Luv-CLI") } } // "$Luv::CLI::Command::Init::Bin/../share"
l!l
00
MC/DC0%0/1
eval { do { dist_dir("Luv-CLI") } } // "$Luv::CLI::Command::Init::Bin/../share"
eval { do { dist_dir("Luv-CLI") } }
Truth table0%0/2
A
A= eval { do { dist_dir("Luv-CLI") } }
Aresult
11
00
62 0 my %vars = ( project_name => $project_name );
63  
64 0 render( "$share_dir/templates/conf.lua.tpl", "conf.lua", %vars );
65 0 ▶ copy( "$share_dir/templates/main.lua.tpl", "main.lua" )
Branch: 1 branch
BranchTrueFalseTotal
copy("$share_dir/templates/main.lua.tpl", "main.lua") or die "Cannot copy main.lua template: $!\n" 0 0 0
66 or die "Cannot copy main.lua template: $!\n";
67 0 ▶ copy( "$share_dir/templates/gitignore.tpl", ".gitignore" )
Branch: 1 branch
BranchTrueFalseTotal
copy("$share_dir/templates/gitignore.tpl", ".gitignore") or die "Cannot copy .gitignore template: $!\n" 0 0 0
68 or die "Cannot copy .gitignore template: $!\n";
69  
70 0 print "Initialized Love2D project '$project_name'\n";
71  
72 0 return;
73 }
74  
75 0 ▶sub render( $template_path, $destination_path, %vars ) {
Subroutine render: not called CC=4 SCAR=30.0
Pod undocumented
76 0 ▶ open my $fh, '<', $template_path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $template_path or die "Cannot read from ${template_path}: $!\n" 0 0 0
77 or die "Cannot read from $template_path: $!\n";
78 0 local $/;
79 0 my $content = <$fh>;
80 0 close $fh;
81  
82 0 for my $key ( keys %vars ) {
83 0 my $value = $vars{$key};
84 0 $content =~ s/\{\{\Q$key\E\}\}/$value/ge;
85 }
86  
87 0 ▶ open my $out, '>', $destination_path
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $destination_path or die "Cannot write to ${destination_path}: $!\n" 0 0 0
88 or die "Cannot write to $destination_path: $!\n";
89 0 print {$out} $content;
90 0 close $out;
91  
92 0 return;
93 }
94  
95 1 1;
96  
97 __END__
« CLI/Command/Add.pm ↑ Summary CLI/Command/List.pm »