blib/lib/Dist/Zilla/Plugin/Author/GETTY/Binary.pm

filter: Statementstmt 100% 145 / 145 Branchbran 94.8% 55 / 58 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 5 totaltotal 95.9% 236 / 246 CC 37 SCAR 14.9
CC
37
Cov
98%
_pp_args_output
8.0
__ANON__
8.0
metadata
5.0
CRAP 4.4
« blib/lib/Pod/Weaver/Section/Author/GETTY/Support.pm ↑ Summary .github/dzil-binary/require-gate.pl »
1 package Dist::Zilla::Plugin::Author::GETTY::Binary;
2 # ABSTRACT: Declare one standalone binary of the distribution in META.json
3 32 our $VERSION = '0.328';
4 32 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 with 'Dist::Zilla::Role::MetaProvider',
6 'Dist::Zilla::Role::AfterBuild',
7 'Dist::Zilla::Role::BeforeRelease';
8  
9 32 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 32 my @PLATFORMS = qw( linux-x86_64 linux-arm64 macos-arm64 macos-x86_64 windows-x86_64 );
15  
16 32 has binary_name => ( is => 'ro', isa => 'Str', required => 1 );
17  
18 32 has script => ( is => 'ro', isa => 'Str', required => 1 );
19  
20 has modules => (
21 is => 'ro',
22 isa => 'ArrayRef[Str]',
23 26 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 32 );
25  
26 32 has pp_args => ( is => 'ro', isa => 'Maybe[Str]' );
27  
28 has require_modules => (
29 is => 'ro',
30 isa => 'ArrayRef[Str]',
31 init_arg => 'require',
32 33 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 32 );
34  
35 32 has verify => ( is => 'ro', isa => 'Maybe[Str]' );
36  
37 32 has smoke => ( is => 'ro', isa => 'Maybe[Str]' );
38  
39 has platforms => (
40 is => 'ro',
41 isa => 'ArrayRef[Str]',
42 30 ▶ default => sub { [ @PLATFORMS ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 32 );
44  
45 32 has prepare => ( is => 'ro', isa => 'Maybe[Str]' );
46  
47 35 ▶sub mvp_multivalue_args { qw( modules require platforms ) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
48  
49 35 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
50 35 my ($self) = @_;
51  
52 35 ▶ Carp::croak("[" . $self->plugin_name . "] invalid binary name '" . $self->binary_name
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binary_name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/ 1 34 35
53 . "': a binary name starts with a letter or digit and holds only letters, digits, '.', '_' and '-'")
54 unless $self->binary_name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/;
55  
56 34 my %known = map { $_ => 1 } @PLATFORMS;
57 34 for my $p (@{ $self->platforms }) {
58 Carp::croak("[" . $self->plugin_name . "] unknown platform $p (known: @PLATFORMS)")
59 154 ▶ unless $known{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$p} 1 153 154
60 }
61 }
62  
63 # Each instance contributes its own binary, keyed by name. The platform list
64 # and the prepare command are dist-wide (equal in every instance), and
65 # CPAN::Meta::Merge refuses a duplicated array, so only the first instance
66 # writes them.
67 31 ▶sub metadata {
Subroutine metadata: called CC=5 SCAR=16.1
Pod undocumented
68 31 my ($self) = @_;
69  
70 31 my $script = $self->script;
71 Carp::croak("[" . $self->plugin_name . "] script $script of binary "
72 . $self->binary_name . " is not part of the distribution")
73 31 ▶ unless grep { $_->name eq $script } @{ $self->zilla->files };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->name eq $script;} @{$self->zilla->files;} 1 30 31
74  
75 my %bin = (
76 script => $script,
77 30 modules => [ @{ $self->modules } ],
78 30 require => [ @{ $self->require_modules } ],
79 30 ▶ map { defined $self->$_ ? ( $_ => $self->$_ ) : () } qw( pp_args verify smoke ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 18 72 90
80 );
81  
82 30 my @all = grep { $_->isa(__PACKAGE__) } @{ $self->zilla->plugins };
83 30 my %dist_wide;
84 30 ▶ if ($all[0] == $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all[0] == $self) 28 2 30
85 28 my %want = map { $_ => 1 } @{ $self->platforms };
86 %dist_wide = (
87 28 ▶ platforms => [ grep { $want{$_} } @PLATFORMS ],
Branch: 1 branch
BranchTrueFalseTotal
defined $self->prepare ? : 2 26 28
88 defined $self->prepare ? ( prepare => $self->prepare ) : (),
89 );
90 }
91  
92 30 return { x_binaries => { %dist_wide, binaries => { $self->binary_name => \%bin } } };
93 }
94  
95 # The line rule for the output of pp_args (ruling R4), the same as in the
96 # binary workflow's pp-args.pl: split on "\n" only, strip one trailing "\r" per
97 # line, drop empty lines. A line is one argument and is never word-split.
98 5 ▶sub _pp_args_lines {
Subroutine _pp_args_lines: called CC=1 SCAR=0.0
99 5 my ($output) = @_;
100 5 return grep { length } map { s/\r\z//r } split /\n/, $output;
101 }
102  
103 # The -M rule, the same as in the binary workflow's pp-args.pl: each module as
104 # -M, a trailing ::* as ::**, every module below at any depth (pp takes one
105 # level for ::*), the namespace require-gate.pl checks for Glob::*.
106 483 ▶sub _pp_module_args {
Subroutine _pp_module_args: called CC=1 SCAR=0.0
107 483 my (@modules) = @_;
108 483 return map { ('-M', s/::\*\z/::**/r) } @modules;
109 }
110  
111 # Win32: -x is true there only for .exe, .bat, .cmd and .com (win32.c,
112 # win32_stat_low), never for pp, a Perl script without an extension next to its
113 # pp.bat; _run_pp starts it with $^X.
114 27 ▶sub _find_pp {
Subroutine _find_pp: called CC=5 SCAR=16.1
115 27 for my $dir (File::Spec->path) {
116 27 my $pp = File::Spec->catfile($dir, 'pp');
117 27 ▶ return $pp if -f $pp && ($^O eq 'MSWin32' || -x _);
Branch: 1 branch
BranchTrueFalseTotal
if -f $pp and $^O eq "MSWin32" || -x _ 22 5 27
Condition100%6/6
-f $pp and $^O eq "MSWin32" || -x _
!ll&&!rl&&r
4122
$^O eq "MSWin32" or -x _
l!l&&r!l&&!r
9131
MC/DC100%3/3
-f $pp and $^O eq "MSWin32" || -x _
-f $pp $^O eq "MSWin32" -x _
Truth table100%4/4
A and B or C
A= -f $ppB= $^O eq "MSWin32"C= -x _
ABCresult
0XX0
1000
11X1
1011
118 }
119 5 return;
120 }
121  
122 # Runs pp. Win32: Perl joins a list into one CreateProcess command line and
123 # never escapes an embedded " (win32.c, create_command_line: an argument is
124 # wrapped in " only when it has whitespace and no "), but passes one that starts
125 # and ends in " unchanged. So pp runs as `$^X pp ARG...` (not through pp.bat
126 # and cmd.exe), each argument quoted for the C runtime's parser of perl.exe:
127 # backslashes before a " or at the end doubled, " escaped.
128 22 ▶sub _run_pp {
Subroutine _run_pp: called CC=2 SCAR=6.9
129 22 my (@cmd) = @_;
130 22 ▶ return system(@cmd) unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 13 9 22
131 9 return system($^X, map { _win32_arg($_) } @cmd);
132 }
133  
134 99 ▶sub _win32_arg {
Subroutine _win32_arg: called CC=1 SCAR=0.0
135 99 my ($arg) = @_;
136 99 $arg =~ s/(\\*)"/$1$1\\"/g;
137 99 $arg =~ s/(\\+)\z/$1$1/;
138 99 return qq{"$arg"};
139 }
140  
141 # Win32, sh (_pp_args_output, _run_hook): the same command line, and the MSYS2
142 # sh splits it again by rules of its own (' quotes too, \\ becomes \, words are
143 # globbed). So the script and its argument go in the environment, and the fixed
144 # script on the command line holds neither " nor \: with IFS empty and globbing
145 # off each variable expands to exactly one word, a fresh sh -c SCRIPT [sh ARG],
146 # as on Unix. The same fix as run_in_win32 in .github/dzil-binary/pp-args.pl.
147  
148 # The output of `sh -c CMD`, the binary's pp_args; fails the build when it fails.
149 13 ▶sub _pp_args_output {
Subroutine _pp_args_output: called CC=8 SCAR=20.8
150 13 my ($self, $cmd) = @_;
151 13 my $name = $self->binary_name;
152 13 my ($ph, $why);
153 13 ▶ if ($^O ne 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 6 7 13
elsif (not length $cmd) { } 1 6 7
154 6 ▶ open $ph, '-|', 'sh', '-c', $cmd or $why = "$!";
Branch: 1 branch
BranchTrueFalseTotal
open $ph, "-|", "sh", "-c", $cmd or $why = "$!" 6 0 6
155 }
156 elsif (!length $cmd) {
157 1 return ''; # sh -c '' prints nothing; exec sh -c without a word fails
158 }
159 else {
160 6 local $ENV{DZIL_BINARY_PP_ARGS} = $cmd;
161 # $! read before the local is undone: restoring %ENV may clobber errno
162 6 ▶ open $ph, '-|', 'sh', '-c', 'IFS=; set -f; exec sh -c $DZIL_BINARY_PP_ARGS' or $why = "$!";
Branch: 1 branch
BranchTrueFalseTotal
open $ph, "-|", "sh", "-c", "IFS=; set -f; exec sh -c \$DZIL_BINARY_PP_ARGS" or $why = "$!" 5 1 6
163 }
164 12 ▶ $self->log_fatal("cannot run binary_pp_args of $name: $why") if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 1 11 12
165 11 my $output = do { local $/; <$ph> };
166 11 ▶ close $ph or $self->log_fatal("binary_pp_args of $name failed: $cmd");
Branch: 1 branch
BranchTrueFalseTotal
close $ph or $self->log_fatal("binary_pp_args of $name failed: $cmd") 8 3 11
167 8 ▶ return $output // '';
Condition50%1/2
$output // ""
l!l
80
MC/DC0%0/1
$output // ""
$output
Truth table50%1/2
A
A= $output
Aresult
11
00
168 }
169  
170 # Runs `sh -c 'CMD "$@"' sh ARG`, CMD with ARG as its argument, without
171 # PERL5LIB. A defined PERL5LIB goes in DZIL_BINARY_PERL5LIB; without, that is
172 # unset too.
173 30 ▶sub _run_hook {
Subroutine _run_hook: called CC=3 SCAR=11.0
174 30 my ($cmd, $arg, $perl5lib) = @_;
175 30 my $script = "$cmd \"\$@\"";
176 30 delete local $ENV{PERL5LIB};
177 30 delete local $ENV{DZIL_BINARY_PERL5LIB};
178 30 ▶ $ENV{DZIL_BINARY_PERL5LIB} = $perl5lib if defined $perl5lib;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl5lib 15 15 30
179 30 ▶ return system('sh', '-c', $script, 'sh', $arg) unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 17 13 30
180 13 local $ENV{DZIL_BINARY_HOOK} = $script;
181 13 local $ENV{DZIL_BINARY_OUT} = $arg;
182 13 return system('sh', '-c', 'IFS=; set -f; exec sh -c $DZIL_BINARY_HOOK sh $DZIL_BINARY_OUT');
183 }
184  
185 # Runs in the build root; dies naming the command when it fails.
186 20 ▶sub _in_dir {
Subroutine _in_dir: called CC=4 SCAR=14.0
187 20 my ($dir, $code) = @_;
188 20 my $start = Cwd::getcwd();
189 20 ▶ chdir $dir or Carp::croak("cannot chdir to $dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or Carp::croak("cannot chdir to ${dir}: $!") 20 0 20
190 20 my @r = eval { $code->() };
191 20 my $err = $@;
192 20 ▶ chdir $start or Carp::croak("cannot chdir back to $start: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $start or Carp::croak("cannot chdir back to ${start}: $!") 20 0 20
193 20 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 8 12 20
194 12 return @r;
195 }
196  
197 42 ▶sub after_build {
Subroutine after_build: called CC=4 SCAR=13.9
Pod undocumented
198 42 my ($self, $arg) = @_;
199 42 ▶ return if $ENV{DZIL_BINARY_SKIP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DZIL_BINARY_SKIP"} 20 22 42
200  
201 22 my $name = $self->binary_name;
202 22 ▶ my $pp = _find_pp()
Branch: 1 branch
BranchTrueFalseTotal
my $pp = _find_pp() or $self->log_fatal("pp not found in PATH: install PAR::Packer, or set DZIL_BINARY_SKIP=1 to skip the local check of binary $name") 20 2 22
203 or $self->log_fatal("pp not found in PATH: install PAR::Packer, or set DZIL_BINARY_SKIP=1 to skip the local check of binary $name");
204  
205 20 my $build = Path::Tiny::path($arg->{build_root})->absolute->stringify;
206 20 my $sib = "$build-binaries";
207 20 Path::Tiny::path($sib)->mkpath;
208 # Win32: NAME.exe, like the release asset: cmd.exe, PowerShell and the PATH
209 # search of CreateProcess start a bare NAME only as NAME.exe
210 20 ▶ my $out = "$sib/$name" . ($^O eq 'MSWin32' ? '.exe' : '');
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 8 12 20
211  
212 20 ▶ _in_dir($build, sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
213 20 my @extra;
214 20 ▶ if (defined $self->pp_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pp_args) 6 14 20
215 6 @extra = _pp_args_lines($self->_pp_args_output($self->pp_args));
216 }
217 my @cmd = ( $pp, '-o', $out, '-I', "$build/lib",
218 19 _pp_module_args(@{ $self->modules }), @extra, "$build/" . $self->script );
219 19 $self->log("packing $name");
220 19 local $ENV{PAR_VERBATIM} = 1;
221 19 my $rc = _run_pp(@cmd);
222 19 my $why = "$!"; # pp did not start ($? == -1): $! read before other code clobbers it
223 19 ▶ $self->log_fatal("cannot run pp for binary $name: $why") if $rc == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == -1 2 17 19
224 17 ▶ $rc == 0 or $self->log_fatal("pp failed for binary $name (exit " . ($? >> 8) . ")");
Branch: 1 branch
BranchTrueFalseTotal
$rc == 0 or $self->log_fatal("pp failed for binary $name (exit " . ($? >> 8) . ')') 15 2 17
225  
226 # As in the workflow: verify gets the PERL5LIB pp ran under, the caller's
227 # (empty without one), in DZIL_BINARY_PERL5LIB; smoke gets neither.
228 15 ▶ my %perl5lib = ( verify => $ENV{PERL5LIB} // '' );
Condition100%2/2
$ENV{"PERL5LIB"} // ""
l!l
132
MC/DC100%1/1
$ENV{"PERL5LIB"} // ""
$ENV{"PERL5LIB"}
Truth table100%2/2
A
A= $ENV{"PERL5LIB"}
Aresult
11
00
229 15 for my $hook (qw( verify smoke )) {
230 28 my $cmd = $self->$hook;
231 28 ▶ next unless defined $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmd 8 20 28
232 20 $self->log("$hook $name");
233 20 ▶ _run_hook($cmd, $out, $perl5lib{$hook}) == 0
Branch: 1 branch
BranchTrueFalseTotal
_run_hook($cmd, $out, $perl5lib{$hook}) == 0 or $self->log_fatal("binary_$hook failed for ${name}: $cmd") 17 3 20
234 or $self->log_fatal("binary_$hook failed for $name: $cmd");
235 }
236 20 });
237 }
238  
239 32 my $CALLER = qr{uses:\s*Getty/p5-dist-zilla-pluginbundle-author-getty/\.github/workflows/dzil-binary\.yml\@\S};
240  
241 7 ▶sub before_release {
Subroutine before_release: called CC=3 SCAR=11.0
Pod undocumented
242 7 my ($self) = @_;
243 7 my $wf = Path::Tiny::path($self->zilla->root)->child('.github', 'workflows', 'binaries.yml');
244 7 ▶ $self->log_fatal("release refused: .github/workflows/binaries.yml is missing; the dist declares binaries and needs a caller of dzil-binary.yml")
Branch: 1 branch
BranchTrueFalseTotal
unless $wf->is_file 1 6 7
245 unless $wf->is_file;
246 6 ▶ $self->log_fatal("release refused: .github/workflows/binaries.yml does not call Getty/p5-dist-zilla-pluginbundle-author-getty/.github/workflows/dzil-binary.yml\@<ref>")
Branch: 1 branch
BranchTrueFalseTotal
unless $wf->slurp_utf8 =~ /$CALLER/ 4 2 6
247 unless $wf->slurp_utf8 =~ $CALLER;
248 }
249  
250 32 __PACKAGE__->meta->make_immutable;
251 32 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 32 1;
253  
254 __END__
« blib/lib/Pod/Weaver/Section/Author/GETTY/Support.pm ↑ Summary .github/dzil-binary/require-gate.pl »