| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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'
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"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 56 | 0 | ▶ | $conf{github}{use_ci} = {} unless ref $conf{github}{use_ci}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | $conf{github}{use_ci}{runners} = [qw(ubuntu windows macos)] | ||||||||||||||||||||
| 58 | 0 | ▶ | unless exists $conf{github}{use_ci}{runners}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 81 | 0 | $File::Find::prune = 1; | |||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 0 | ▶ | return if $f eq 'dist_setup.conf'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | ▶ | return if $f =~ m/^tt_/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | ▶ | return if $f =~ m/\.cond$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 93 | 0 | ▶ | if (-f $cond_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | my $ret = $eval->do($cond_file); | |||||||||||||||||||
| 95 | 0 | ▶ | die "Cannot evaluate ${cond_file}: $@\n" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | ▶ | die "Cannot read ${cond_file}: $!\n" if $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | if (!$ret) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 104 | 0 | ▶ | die "Cannot read $_: $!\n" unless -r $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 127 | 0 | ▶ | open my $f, '<:encoding(UTF-8)', $dest_file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | or die "Cannot open '$dest_file': $!\n"; | ||||||||||||||||||||
| 129 | 0 | ▶ | while (<$f>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 0 | ▶ | last if /^${footer_marker}$/m; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 0 | ▶ | $footer = do { local $/ = undef; <$f> } unless eof($f); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 135 | 0 | ▶ | close $f or die "Cannot close '$dest_file': $!\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | ▶ | $dest_tt->process($src_file, {%conf, footer_content => $footer}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||