| 1 | 2 | ▶ | use v5.40; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | |||||||||||||
| 3 | package Minima::Project; | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use File::Share; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Path::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Template; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | our $tdir = path( | |||||||||||
| 10 | File::Share::dist_dir('Minima') | ||||||||||||
| 11 | )->child('/templates'); | ||||||||||||
| 12 | 2 | our $verbose = 0; | |||||||||||
| 13 | |||||||||||||
| 14 | 7 | ▶ | sub create ($dir, $user_config = {}) | ||||||||||
|
Subroutine
create: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 15 | 7 | { | |||||||||||
| 16 | 7 | ▶ | my $project = path($dir // '.')->absolute; | ||||||||||
|
Condition100%2/2
$dir // "."
MC/DC100%1/1
$dir // "."
$dir
Truth table100%2/2
A
A= $dir
| |||||||||||||
| 17 | 7 | my %config = ( | |||||||||||
| 18 | 'cpanfile' => 0, | ||||||||||||
| 19 | 'static' => 1, | ||||||||||||
| 20 | 'verbose' => 0, | ||||||||||||
| 21 | %$user_config | ||||||||||||
| 22 | ); | ||||||||||||
| 23 | 7 | $verbose = $config{verbose}; | |||||||||||
| 24 | |||||||||||||
| 25 | # Test if directory can be used | ||||||||||||
| 26 | 7 | ▶ | if ($project->exists) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 4 | ▶ | die "Project destination must be a directory\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | unless $project->is_dir; | ||||||||||||
| 29 | 3 | ▶ | die "Project destination must be empty.\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | if $project->children; | ||||||||||||
| 31 | } else { | ||||||||||||
| 32 | 3 | $project->mkdir; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 5 | chdir $project; | |||||||||||
| 36 | |||||||||||||
| 37 | # Create files | ||||||||||||
| 38 | 5 | for my ($file, $content) (get_templates(\%config)) { | |||||||||||
| 39 | 56 | my $dest = path($file); | |||||||||||
| 40 | 56 | my $dir = $dest->parent; | |||||||||||
| 41 | |||||||||||||
| 42 | 56 | ▶ | unless ($dir->is_dir) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 20 | _info("mkdir $dir"); | |||||||||||
| 44 | 20 | $dir->mkdir; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 56 | _info(" spew $dest"); | |||||||||||
| 48 | 56 | $dest->spew_utf8($content); | |||||||||||
| 49 | } | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 5 | ▶ | sub get_templates ($config) | ||||||||||
|
Subroutine
get_templates: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 53 | { | ||||||||||||
| 54 | 5 | my %files; | |||||||||||
| 55 | 2 | ▶ | use Template::Constants qw/ :debug /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 5 | my $tt = Template->new( | |||||||||||
| 57 | INCLUDE_PATH => $tdir, | ||||||||||||
| 58 | OUTLINE_TAG => '@@', | ||||||||||||
| 59 | TAG_STYLE => 'star', | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | |||||||||||||
| 62 | 5 | $config->{version} = Minima->VERSION; | |||||||||||
| 63 | |||||||||||||
| 64 | 2 | ▶ | foreach (glob "$tdir/*.[sd]tpl") { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | 60 | my $content = path($_)->slurp_utf8; | |||||||||||
| 66 | 60 | $_ = path($_)->basename; | |||||||||||
| 67 | 60 | tr|-|/|; | |||||||||||
| 68 | 60 | tr|+|.|; | |||||||||||
| 69 | 60 | ▶ | if (/\.dtpl$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | # Process .d(ynamic) template | ||||||||||||
| 71 | 20 | my $template = $content; | |||||||||||
| 72 | 20 | my $processed; | |||||||||||
| 73 | 20 | $tt->process(\$template, $config, \$processed); | |||||||||||
| 74 | 20 | $content = $processed; | |||||||||||
| 75 | } | ||||||||||||
| 76 | 60 | s/\.\w+$//; | |||||||||||
| 77 | 60 | ▶ | $files{$_} = $content if $content; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 5 | map { $_, $files{$_} } sort keys %files; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 76 | ▶ | sub _info ($m) | ||||||||||
|
Subroutine
_info: called CC=2 SCAR=6.9
| |||||||||||||
| 84 | { | ||||||||||||
| 85 | 76 | ▶ | say $m if ($verbose); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 2 | 1; | |||||||||||
| 89 | |||||||||||||
| 90 | __END__ | ||||||||||||