| 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())
MC/DC0%0/2
$opt->{"name"} // basename(getcwd())
$opt->{"name"}
basename(getcwd())
Truth table0%0/3
A // B
A= $opt->{"name"}B= basename(getcwd())
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ( $opt->{name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | make_path($project_name) unless -d $project_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | chdir($project_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
-e $manifest_path and not $opt->{"force"}
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"}
| |||||||||||||||||||||||||||||
| 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"
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")
}
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | or die "Cannot copy main.lua template: $!\n"; | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | copy( "$share_dir/templates/gitignore.tpl", ".gitignore" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||