blib/lib/Dist/Setup.pm

filter: Statementstmt 37.5% 42 / 112 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 2 totaltotal 27.8% 54 / 194 CC 30 SCAR 45.3
CC
30
Cov
24%
__ANON__
156.0
setup
110.0
setup_file
110.0
CRAP 92.8
↑ Summary
1 package Dist::Setup;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Eval::Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Basename 'basename', 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec::Functions 'abs2rel', 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = '0.18';
18  
19 1 our %conf; # to be shared with the Eval::Safe object.
20 1 my $tt;
21 my $tt_raw;
22 1 my $target_dir;
23  
24 1 my $footer_marker = '# End of the template. You can add custom content below this line.';
25  
26 0 ▶sub setup {
Subroutine setup: not called CC=10 SCAR=47.0
Pod undocumented
27 0 my ($data_dir, $target_dir_local) = @_;
28 0 $target_dir = $target_dir_local;
29  
30 0 my $conf_file_name = 'dist_setup.conf';
31 0 my $conf_file = catfile($target_dir, $conf_file_name);
32 0 ▶ if (!-e $conf_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $conf_file) 0 0 0
33 0 print STDERR "No ${conf_file_name} in target directory.\n";
34 0 ▶ copy(catfile($data_dir, $conf_file_name), $conf_file)
Branch: 1 branch
BranchTrueFalseTotal
copy(catfile($data_dir, $conf_file_name), $conf_file) or die "Cannot copy ${conf_file_name}: $!\n" 0 0 0
35 or die "Cannot copy ${conf_file_name}: $!\n";
36 0 print STDERR "Created a new configuration file. Modify it then run this tool again.\n";
37 0 exit 0;
38 }
39  
40 0 my $eval = Eval::Safe->new();
41 0 ▶ %conf = %{$eval->do($conf_file)}
Branch: 1 branch
BranchTrueFalseTotal
%Dist::Setup::conf = %{$eval->do($conf_file);} or die "Cannot parse the $conf_file_name configuration: $@\n" 0 0 0
42 or die "Cannot parse the ${conf_file_name} configuration: $@\n";
43 0 $eval->share('%conf');
44  
45 0 $conf{auto}{date}{year} = localtime->year() + 1900; ## no critic (ProhibitMagicNumbers)
46 0 ▶ $conf{dist_name} //= $conf{name} =~ s/::/-/gr;
Condition0%0/3
$Dist::Setup::conf{"dist_name"} //= $Dist::Setup::conf{"name"} =~ s/::/-/gru
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dist::Setup::conf{"dist_name"} //= $Dist::Setup::conf{"name"} =~ s/::/-/gru
$Dist::Setup::conf{"dist_name"} $Dist::Setup::conf{"name"} =~ s/::/-/gru
Truth table0%0/3
A //= B
A= $Dist::Setup::conf{"dist_name"}B= $Dist::Setup::conf{"name"} =~ s/::/-/gru
ABresult
1X1
011
000
47 0 ▶ $conf{dist_package} //= $conf{name} =~ s/::/\//gr; # Undocumented for now, used only by the `make exe` target
Condition0%0/3
$Dist::Setup::conf{"dist_package"} //= $Dist::Setup::conf{"name"} =~ s[::][/]gru
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dist::Setup::conf{"dist_package"} //= $Dist::Setup::conf{"name"} =~ s[::][/]gru
$Dist::Setup::conf{"dist_package"} $Dist::Setup::conf{"name"} =~ s[::][/]gru
Truth table0%0/3
A //= B
A= $Dist::Setup::conf{"dist_package"}B= $Dist::Setup::conf{"name"} =~ s[::][/]gru
ABresult
1X1
011
000
48 0 ▶ $conf{base_package} //= 'lib/'.($conf{name} =~ s{::}{/}gr).'.pm';
Condition0%0/2
$Dist::Setup::conf{"base_package"} //= 'lib/' . $Dist::Setup::conf{"name"} =~ s[::][/]gru . '.pm'
l!l
00
MC/DC0%0/1
$Dist::Setup::conf{"base_package"} //= 'lib/' . $Dist::Setup::conf{"name"} =~ s[::][/]gru . '.pm'
$Dist::Setup::conf{"base_package"}
Truth table0%0/2
A
A= $Dist::Setup::conf{"base_package"}
Aresult
11
00
49 0 $conf{footer_marker} = $footer_marker;
50 $conf{short_min_perl_version} =
51 0 version->parse($conf{min_perl_version})->normal =~ s/^v(\d+\.\d+)\..*$/$1/r;
52 $conf{dotted_min_perl_version} =
53 0 version->parse($conf{min_perl_version})->normal =~ s/^v(\d+(?:\.\d+)*).*$/$1/r;
54  
55 0 ▶ if ($conf{github}{use_ci}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dist::Setup::conf{"github"}{"use_ci"}) 0 0 0
56 0 ▶ $conf{github}{use_ci} = {} unless ref $conf{github}{use_ci};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Dist::Setup::conf{"github"}{"use_ci"} 0 0 0
57 $conf{github}{use_ci}{runners} = [qw(ubuntu windows macos)]
58 0 ▶ unless exists $conf{github}{use_ci}{runners};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Dist::Setup::conf{"github"}{"use_ci"}{"runners"} 0 0 0
59 }
60  
61 0 $tt = Template->new({
62 INCLUDE_PATH => $data_dir,
63 OUTPUT_PATH => $target_dir,
64 ENCODING => 'utf8',
65 EVAL_PERL => 1,
66 PRE_PROCESS => 'tt_header',
67 POST_PROCESS => 'tt_footer',
68 });
69 0 $tt_raw = Template->new({
70 INCLUDE_PATH => $data_dir,
71 OUTPUT_PATH => $target_dir,
72 ENCODING => 'utf8',
73 EVAL_PERL => 1,
74 });
75  
76 find({
77 no_chdir => 1,
78 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
79 0 my $f = basename($_);
80 0 ▶ if ($f =~ m/^\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /^\./u) 0 0 0
81 0 $File::Find::prune = 1;
82 0 return;
83 }
84 0 ▶ return if $f eq 'dist_setup.conf';
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "dist_setup.conf" 0 0 0
85 0 ▶ return if $f =~ m/^tt_/;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^tt_/u 0 0 0
86 0 ▶ return if $f =~ m/\.cond$/;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /\.cond$/u 0 0 0
87  
88 0 my $src = abs2rel($_, $data_dir);
89 0 my $out = $src;
90 0 $out =~ s/(^|\/)dot_/${1}./g;
91  
92 0 ▶ my $cond_file = -d $_ ? catfile($_, '.cond') : $_.'.cond';
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
93 0 ▶ if (-f $cond_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cond_file) 0 0 0
94 0 my $ret = $eval->do($cond_file);
95 0 ▶ die "Cannot evaluate ${cond_file}: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
96 0 ▶ die "Cannot read ${cond_file}: $!\n" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
97 0 ▶ if (!$ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
98 0 $File::Find::prune = 1;
99 0 print "Skipped ${out}\n";
100 0 return;
101 }
102 }
103 0 ▶ return if -d $_;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
104 0 ▶ die "Cannot read $_: $!\n" unless -r $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_ 0 0 0
105  
106 0 print "Processing ${out}\n";
107 0 setup_file($src, $out);
108 },
109 },
110 0 $data_dir);
111 0 return;
112 }
113  
114 0 ▶sub setup_file {
Subroutine setup_file: not called CC=10 SCAR=47.0
Pod undocumented
115 0 my ($src_file, $target_file) = @_;
116 0 my $footer = q{};
117  
118 0 my $dest_tt;
119  
120 0 ▶ if ($src_file =~ m/\.raw$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_file =~ /\.raw$/u) { } 0 0 0
121 0 $dest_tt = $tt_raw;
122 0 $target_file =~ s/\.raw$//;
123 } else {
124 0 $dest_tt = $tt;
125 0 my $dest_file = catfile($target_dir, $target_file);
126 0 ▶ if (-e $dest_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dest_file) 0 0 0
127 0 ▶ open my $f, '<:encoding(UTF-8)', $dest_file
Branch: 1 branch
BranchTrueFalseTotal
open my $f, "<:encoding(UTF-8)", $dest_file or die "Cannot open '${dest_file}': $!\n" 0 0 0
128 or die "Cannot open '$dest_file': $!\n";
129 0 ▶ while (<$f>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $f)) 0 0 0
130 0 ▶ last if /^${footer_marker}$/m;
Branch: 1 branch
BranchTrueFalseTotal
if /^$footer_marker$/mu 0 0 0
131 }
132 0 ▶ $footer = do { local $/ = undef; <$f> } unless eof($f);
Branch: 1 branch
BranchTrueFalseTotal
unless eof $f 0 0 0
133 0 $footer =~ s/\n+$//;
134 0 ▶ $footer = "\n".$footer if $footer; # There is no new line before footer in the template.
Branch: 1 branch
BranchTrueFalseTotal
if $footer 0 0 0
135 0 ▶ close $f or die "Cannot close '$dest_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $f or die "Cannot close '${dest_file}': $!\n" 0 0 0
136 }
137 }
138  
139 0 ▶ $dest_tt->process($src_file, {%conf, footer_content => $footer},
Branch: 1 branch
BranchTrueFalseTotal
$dest_tt->process($src_file, {%Dist::Setup::conf, "footer_content", $footer}, $target_file, {"binmode", ":utf8"}) or die "Cannot process template ${src_file}: " . $dest_tt->error . "\n" 0 0 0
140 $target_file, {binmode => ':utf8'})
141 or die "Cannot process template ${src_file}: ".$dest_tt->error()."\n";
142  
143 0 return;
144 }
145  
146 1 1;
147  
148 __END__
↑ Summary