.github/dzil-binary/publish.pl

filter: Statementstmt 99.3% 155 / 156 Branchbran 74.3% 55 / 74 Conditioncond 51.1% 23 / 45 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 80.0% 256 / 320 CC 61 SCAR 21.6
CC
61
Cov
85%
latest_rule
13.5
main
13.3
render
10.4
CRAP 8.7
« blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Binary.pm ↑ Summary blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Docker.pm »
1 #!/usr/bin/env perl
2 # Publish-job helper of the binary release workflow.
3 # publish.pl render <META.json> <owner> <repo> <tag> <dir> -> <dir>/install.sh, <dir>/install.ps1
4 # publish.pl expected <META.json> <version> -> expected asset names, one per line
5 # publish.pl sums <dir> -> <dir>/SHA256SUMS over the binaries found there
6 # publish.pl check <META.json> <version> <dl-dir> <release-json> <latest-json>
7 # <latest-json>: the releases/latest response, empty when that answered 404
8 package DzilBinary::Publish;
9 98 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 98 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 98 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 98 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 98 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 98 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 53 my @PLATFORMS = qw( linux-x86_64 linux-arm64 macos-arm64 macos-x86_64 windows-x86_64 );
17 # what the workflow's publish step uploads beside the binaries; keep in step with its `gh release upload`
18 53 my @BESIDE = qw( SHA256SUMS install.sh install.ps1 provenance.json );
19  
20 84 ▶sub read_meta {
Subroutine read_meta: called CC=4 SCAR=14.2
21 84 my ($file) = @_;
22 84 ▶ open my $fh, '<:raw', $file or die "cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "cannot read ${file}: $!\n" 84 0 84
23 84 my $meta = eval { JSON::PP->new->decode(do { local $/; <$fh> }) };
24 84 ▶ die "$file is not valid JSON: $@" unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 84 84
25 84 ▶ die "$file has no x_binaries\n" unless ref $meta->{x_binaries} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meta->{"x_binaries"} eq "HASH" 0 84 84
26 84 return $meta;
27 }
28  
29 107 ▶sub platforms_of {
Subroutine platforms_of: called CC=5 SCAR=16.7
30 107 my ($x) = @_;
31 107 my $want = $x->{platforms};
32 107 ▶ $want = [@PLATFORMS] unless ref $want eq 'ARRAY' && @$want;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $want eq "ARRAY" and @$want 0 107 107
Condition33%1/3
ref $want eq "ARRAY" and @$want
!ll&&!rl&&r
00107
MC/DC0%0/2
ref $want eq "ARRAY" and @$want
ref $want eq "ARRAY" @$want
Truth table33%1/3
A and B
A= ref $want eq "ARRAY"B= @$want
ABresult
0X0
100
111
33 107 my %seen = map { $_ => 1 } @$want;
34 107 ▶ for my $p (keys %seen) { die "x_binaries: unknown platform '$p'\n" unless grep { $_ eq $p } @PLATFORMS }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $p;} @PLATFORMS 0 262 262
35 107 return grep { $seen{$_} } @PLATFORMS;
36 }
37  
38 107 ▶sub binaries_of {
Subroutine binaries_of: called CC=3 SCAR=11.8
39 107 my ($x) = @_;
40 107 ▶ die "x_binaries has no binaries\n" unless ref $x->{binaries} eq 'HASH' && %{ $x->{binaries} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x->{"binaries"} eq "HASH" and %{$x->{"binaries"};} 0 107 107
Condition33%1/3
ref $x->{"binaries"} eq "HASH" and %{$x->{"binaries"};}
!ll&&!rl&&r
00107
MC/DC0%0/2
ref $x->{"binaries"} eq "HASH" and %{$x->{"binaries"};}
ref $x->{"binaries"} eq "HASH" %{$x->{"binaries"};}
Truth table33%1/3
A and B
A= ref $x->{"binaries"} eq "HASH"B= %{$x->{"binaries"};}
ABresult
0X0
100
111
41 107 return sort keys %{ $x->{binaries} };
42 }
43  
44 # binaries sorted by name, platforms in constraint order within each
45 54 ▶sub expected_names {
Subroutine expected_names: called CC=2 SCAR=6.9
46 54 my ($x, $version) = @_;
47 54 my @p = platforms_of($x);
48 54 ▶ return map { my $n = $_; map { "$n-$version-$_" . ($_ eq 'windows-x86_64' ? '.exe' : '') } @p } binaries_of($x);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'windows-x86_64' ? : 106 108 214
49 }
50  
51 417 ▶sub slurp { open my $fh, '<:raw', $_[0] or die "cannot read $_[0]: $!\n"; local $/; return scalar <$fh> }
Subroutine slurp: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $_[0] or die "cannot read $_[0]: $!\n" 417 0 417
52  
53 51 ▶sub spew {
Subroutine spew: called CC=3 SCAR=11.4
54 51 my ($path, $data) = @_;
55 51 ▶ open my $fh, '>:raw', $path or die "cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $path or die "cannot write ${path}: $!\n" 51 0 51
56 51 print $fh $data;
57 51 ▶ close $fh or die "cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "cannot write ${path}: $!\n" 51 0 51
58 }
59  
60 26 ▶sub render {
Subroutine render: called CC=10 SCAR=23.4
61 26 my ($meta_file, $owner, $repo, $tag, $dir) = @_;
62 26 my $meta = read_meta($meta_file);
63 26 my $x = $meta->{x_binaries};
64 26 my ($name, $version) = ($meta->{name}, $meta->{version});
65 26 ▶ die "$meta_file has no name or version\n" unless defined $name && defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $version 0 26 26
Condition33%1/3
defined $name and defined $version
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $name and defined $version
defined $name defined $version
Truth table33%1/3
A and B
A= defined $nameB= defined $version
ABresult
0X0
100
111
66 26 ▶ die "tag '$tag' does not end with version '$version'\n" unless length $tag >= length $version && substr($tag, -length $version) eq $version;
Branch: 1 branch
BranchTrueFalseTotal
unless length $tag >= length $version and substr($tag, -length($version)) eq $version 1 25 26
Condition66%2/3
length $tag >= length $version and substr($tag, -length($version)) eq $version
!ll&&!rl&&r
0125
MC/DC50%1/2
length $tag >= length $version and substr($tag, -length($version)) eq $version
length $tag >= length $version substr($tag, -length($version)) eq $version
Truth table66%2/3
A and B
A= length $tag >= length $versionB= substr($tag, -length($version)) eq $version
ABresult
0X0
100
111
67 25 my $prefix = substr($tag, 0, length($tag) - length $version);
68 25 ▶ die "tag '$tag' does not end with version '$version' after a digit-free prefix (prefix '$prefix' contains a digit)\n" if $prefix =~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix =~ /\d/ 1 24 25
69 24 my @bins = binaries_of($x);
70 24 my @plat = platforms_of($x);
71 24 for my $v ($name, $owner, $repo, $tag, $version, @bins) {
72 168 ▶ die "refusing '$v': only letters, digits, '.', '_', '-', '+' are allowed\n" unless $v =~ /\A[A-Za-z0-9._+-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\A[A-Za-z0-9._+-]+\z/ 0 168 168
73 }
74 24 (my $env = uc $name) =~ s/-/_/g;
75 24 ▶ die "dist name '$name' gives no valid environment prefix\n" unless $env =~ /\A[A-Z][A-Z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $env =~ /\A[A-Z][A-Z0-9_]*\z/ 0 24 24
76 24 my %v = (
77 DIST => $name, OWNER => $owner, REPO => $repo, TAG => $tag, VERSION => $version,
78 BINARIES => join(' ', @bins), PLATFORMS => join(' ', @plat), ENV => $env,
79 );
80 24 my $here = File::Basename::dirname(__FILE__);
81 24 for my $f (qw( install.sh install.ps1 )) {
82 48 my $c = slurp(File::Spec->catfile($here, "$f.in"));
83 48 $c =~ s/\@(DIST|OWNER|REPO|TAG|VERSION|BINARIES|PLATFORMS|ENV)\@/$v{$1}/g;
84 48 my $out = File::Spec->catfile($dir, $f);
85 48 spew($out, $c);
86 48 chmod 0644, $out;
87 }
88 24 return 0;
89 }
90  
91 # The binaries are recognised by their asset name (<name>-<version>-<platform>[.exe]),
92 # so the tarball and strays in the directory get no line.
93 3 ▶sub sums {
Subroutine sums: called CC=10 SCAR=23.2
94 3 my ($dir) = @_;
95 3 ▶ opendir my $dh, $dir or die "cannot read $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "cannot read ${dir}: $!\n" 3 0 3
96 3 my $alt = join '|', map { quotemeta } @PLATFORMS;
97 3 my %idx; @idx{@PLATFORMS} = (0 .. $#PLATFORMS);
98 3 my @found;
99 3 for my $f (readdir $dh) {
100 23 ▶ next unless -f File::Spec->catfile($dir, $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -f "File::Spec"->catfile($dir, $f) 6 17 23
101 17 ▶ next unless $f =~ /\A(.+)-[0-9][^-]*-($alt)(?:\.exe)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /\A(.+)-[0-9][^-]*-($alt)(?:\.exe)?\z/ 5 12 17
102 12 my ($n, $plat) = ($1, $2);
103 12 ▶ next unless ($plat eq 'windows-x86_64') == ($f =~ /\.exe\z/ ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
unless ($plat eq "windows-x86_64") == ($f =~ /\.exe\z/ ? 1 : 0) 0 12 12
$f =~ /\.exe\z/ ? : 6 6 12
104 12 push @found, [ $n, $idx{$plat}, $f ];
105 }
106 3 closedir $dh;
107 3 ▶ die "no binaries found in $dir\n" unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 0 3 3
108 3 my $out = '';
109 3 ▶ for my $e (sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] } @found) {
Condition66%2/3
$DzilBinary::Publish::a->[0] cmp $DzilBinary::Publish::b->[0] || $DzilBinary::Publish::a->[1] <=> $DzilBinary::Publish::b->[1]
l!l&&r!l&&!r
760
MC/DC0%0/2
$DzilBinary::Publish::a->[0] cmp $DzilBinary::Publish::b->[0] || $DzilBinary::Publish::a->[1] <=> $DzilBinary::Publish::b->[1]
$DzilBinary::Publish::a->[0] cmp $DzilBinary::Publish::b->[0] $DzilBinary::Publish::a->[1] <=> $DzilBinary::Publish::b->[1]
Truth table66%2/3
A || B
A= $DzilBinary::Publish::a->[0] cmp $DzilBinary::Publish::b->[0]B= $DzilBinary::Publish::a->[1] <=> $DzilBinary::Publish::b->[1]
ABresult
1X1
011
000
110 12 $out .= Digest::SHA->new(256)->addfile(File::Spec->catfile($dir, $e->[2]), 'b')->hexdigest . " $e->[2]\n";
111 }
112 3 spew(File::Spec->catfile($dir, 'SHA256SUMS'), $out);
113 3 return 0;
114 }
115  
116 22 ▶sub check {
Subroutine check: called CC=7 SCAR=19.6
117 22 my ($meta_file, $version, $dl, $release_file, $latest_file) = @_;
118 22 my $meta = read_meta($meta_file);
119 22 my @want = expected_names($meta->{x_binaries}, $version);
120 22 my $rel = eval { JSON::PP->new->decode(slurp($release_file)) };
121 22 ▶ die "$release_file is not valid JSON: $@" unless ref $rel eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rel eq "HASH" 0 22 22
122 22 ▶ my %have = map { $_->{name} => 1 } grep { ref $_ eq 'HASH' && defined $_->{name} } @{ $rel->{assets} || [] };
Condition40%2/5
ref $_ eq 'HASH' && defined $_->{'name'}
!ll&&!rl&&r
0037
$rel->{'assets'} || []
l!l
220
MC/DC0%0/2
ref $_ eq 'HASH' && defined $_->{'name'}
ref $_ eq 'HASH' defined $_->{'name'}
MC/DC0%0/1
$rel->{'assets'} || []
$rel->{'assets'}
Truth table33%1/3
A && B
A= ref $_ eq 'HASH'B= defined $_->{'name'}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $rel->{'assets'}
Aresult
11
00
123 22 my @bad;
124 22 push @bad, map { "missing binary: $_" } grep { !-f File::Spec->catfile($dl, $_) } @want;
125 # every name the upload sends: a clash would fail it after check said go, and
126 # overwriting (--clobber) SHA256SUMS would swap the pins' trust root
127 22 push @bad, map { "release already has asset: $_" } grep { $have{$_} } @want, @BESIDE;
128 22 ▶ if (@bad) { print STDERR "$_\n" for @bad; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) 9 13 22
129 13 ▶ print 'latest=', (latest_rule($rel, $latest_file) ? 'true' : 'false'), "\n";
Branch: 1 branch
BranchTrueFalseTotal
latest_rule($rel, $latest_file) ? : 7 3 10
130 10 return 0;
131 }
132  
133 # Never rolls releases/latest back: GitHub orders "latest" by created_at, so an
134 # older release finishing (or re-run) after a newer one is not marked latest.
135 13 ▶sub latest_rule {
Subroutine latest_rule: called CC=13 SCAR=26.0
136 13 my ($rel, $latest_file) = @_;
137 13 ▶ return 0 if $rel->{prerelease};
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"prerelease"} 2 11 13
138 11 my $raw = slurp($latest_file);
139 11 ▶ return 1 unless length $raw; # 404: no latest release yet
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 4 7 11
140 7 my $latest = eval { JSON::PP->new->decode($raw) };
141 7 ▶ die "$latest_file is not valid JSON: $@" unless ref $latest eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $latest eq "HASH" 1 6 7
142 6 ▶ return 1 if defined $rel->{id} && defined $latest->{id} && $rel->{id} eq $latest->{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rel->{"id"} and defined $latest->{"id"} and $rel->{"id"} eq $latest->{"id"} 1 5 6
Condition50%3/6
defined $rel->{"id"} and defined $latest->{"id"} and $rel->{"id"} eq $latest->{"id"}
!ll&&!rl&&r
051
defined $rel->{"id"} and defined $latest->{"id"}
!ll&&!rl&&r
006
MC/DC33%1/3
defined $rel->{"id"} and defined $latest->{"id"} and $rel->{"id"} eq $latest->{"id"}
defined $rel->{"id"} defined $latest->{"id"} $rel->{"id"} eq $latest->{"id"}
Truth table50%2/4
A and B and C
A= defined $rel->{"id"}B= defined $latest->{"id"}C= $rel->{"id"} eq $latest->{"id"}
ABCresult
0XX0
10X0
1100
1111
143 5 my %at;
144 5 for ([ this => $rel, 'the release' ], [ latest => $latest, $latest_file ]) {
145 9 my ($k, $r, $what) = @$_;
146 9 my $t = $r->{created_at};
147 9 ▶ die "$what has no created_at\n" unless defined $t && !ref $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and not ref $t 2 7 9
Condition66%2/3
defined $t and not ref $t
!ll&&!rl&&r
207
MC/DC50%1/2
defined $t and not ref $t
defined $t not ref $t
Truth table66%2/3
A and not(B)
A= defined $tB= not ref $t
ABresult
0X0
100
111
148 7 ▶ die "$what has created_at '$t', not YYYY-MM-DDThh:mm:ssZ\n" unless $t =~ /\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ\z/ 0 7 7
149 7 $at{$k} = $t;
150 }
151 3 ▶ return 1 if $at{latest} le $at{this};
Branch: 1 branch
BranchTrueFalseTotal
if $at{"latest"} le $at{"this"} 2 1 3
152 1 ▶ print STDERR 'not marking latest: ', ($latest->{tag_name} // "release $latest->{id}"),
Condition50%1/2
$latest->{"tag_name"} // qq[release $latest->{"id"}]
l!l
10
MC/DC0%0/1
$latest->{"tag_name"} // qq[release $latest->{"id"}]
$latest->{"tag_name"}
Truth table50%1/2
A
A= $latest->{"tag_name"}
Aresult
11
00
153 " (created $at{latest}) is newer than this release (created $at{this})\n";
154 1 return 0;
155 }
156  
157 53 ▶sub main {
Subroutine main: called CC=12 SCAR=25.9
158 53 my ($cmd, @a) = @_;
159 53 ▶ $cmd //= '';
Condition50%1/2
$cmd //= ""
l!l
530
MC/DC0%0/1
$cmd //= ""
$cmd
Truth table50%1/2
A
A= $cmd
Aresult
11
00
160 53 my $rc = eval {
161 53 ▶ if ($cmd eq 'render' && @a == 5) { render(@a) }
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'render' and @a == 5) { } 26 27 53
elsif ($cmd eq 'expected' and @a == 2) { } 2 25 27
elsif ($cmd eq 'sums' and @a == 1) { } 3 22 25
elsif ($cmd eq 'check' and @a == 5) { } 22 0 22
Condition66%2/3
$cmd eq "render" and @a == 5
!ll&&!rl&&r
27026
MC/DC50%1/2
$cmd eq "render" and @a == 5
$cmd eq "render" @a == 5
Truth table66%2/3
A and B
A= $cmd eq "render"B= @a == 5
ABresult
0X0
100
111
162 ▶ elsif ($cmd eq 'expected' && @a == 2) {
Condition66%2/3
$cmd eq "expected" and @a == 2
!ll&&!rl&&r
2502
MC/DC50%1/2
$cmd eq "expected" and @a == 2
$cmd eq "expected" @a == 2
Truth table66%2/3
A and B
A= $cmd eq "expected"B= @a == 2
ABresult
0X0
100
111
163 2 print "$_\n" for expected_names(read_meta($a[0])->{x_binaries}, $a[1]);
164 2 0;
165 }
166 3 ▶ elsif ($cmd eq 'sums' && @a == 1) { sums(@a) }
Condition66%2/3
$cmd eq "sums" and @a == 1
!ll&&!rl&&r
2203
MC/DC50%1/2
$cmd eq "sums" and @a == 1
$cmd eq "sums" @a == 1
Truth table66%2/3
A and B
A= $cmd eq "sums"B= @a == 1
ABresult
0X0
100
111
167 22 ▶ elsif ($cmd eq 'check' && @a == 5) { check(@a) }
Condition33%1/3
$cmd eq "check" and @a == 5
!ll&&!rl&&r
0022
MC/DC0%0/2
$cmd eq "check" and @a == 5
$cmd eq "check" @a == 5
Truth table33%1/3
A and B
A= $cmd eq "check"B= @a == 5
ABresult
0X0
100
111
168 0 else { die "usage: publish.pl render|expected|sums|check ...\n" }
169 };
170 53 ▶ if (!defined $rc) { print STDERR $@; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 5 48 53
171 48 return $rc;
172 }
173  
174 53 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 53 0 53
175 1;
« blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Binary.pm ↑ Summary blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Docker.pm »