.github/dzil-binary/publish.pl

filter: Statementstmt 99.3% 154 / 155 Branchbran 74.3% 55 / 74 Conditioncond 51.1% 23 / 45 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 79.9% 255 / 319 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 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 44 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 44 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 44 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 44 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 43 my @PLATFORMS = qw( linux-x86_64 linux-arm64 macos-arm64 macos-x86_64 windows-x86_64 );
17  
18 42 ▶sub read_meta {
Subroutine read_meta: called CC=4 SCAR=14.2
19 42 my ($file) = @_;
20 42 ▶ 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" 42 0 42
21 42 my $meta = eval { JSON::PP->new->decode(do { local $/; <$fh> }) };
22 42 ▶ die "$file is not valid JSON: $@" unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 42 42
23 42 ▶ 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 42 42
24 42 return $meta;
25 }
26  
27 41 ▶sub platforms_of {
Subroutine platforms_of: called CC=5 SCAR=16.7
28 41 my ($x) = @_;
29 41 my $want = $x->{platforms};
30 41 ▶ $want = [@PLATFORMS] unless ref $want eq 'ARRAY' && @$want;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $want eq "ARRAY" and @$want 0 41 41
Condition33%1/3
ref $want eq "ARRAY" and @$want
!ll&&!rl&&r
0041
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
31 41 my %seen = map { $_ => 1 } @$want;
32 41 ▶ 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 130 130
33 41 return grep { $seen{$_} } @PLATFORMS;
34 }
35  
36 41 ▶sub binaries_of {
Subroutine binaries_of: called CC=3 SCAR=11.8
37 41 my ($x) = @_;
38 41 ▶ 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 41 41
Condition33%1/3
ref $x->{"binaries"} eq "HASH" and %{$x->{"binaries"};}
!ll&&!rl&&r
0041
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
39 41 return sort keys %{ $x->{binaries} };
40 }
41  
42 # binaries sorted by name, platforms in constraint order within each
43 17 ▶sub expected_names {
Subroutine expected_names: called CC=2 SCAR=6.9
44 17 my ($x, $version) = @_;
45 17 my @p = platforms_of($x);
46 17 ▶ return map { my $n = $_; map { "$n-$version-$_" . ($_ eq 'windows-x86_64' ? '.exe' : '') } @p } binaries_of($x);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'windows-x86_64' ? : 32 34 66
47 }
48  
49 72 ▶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" 72 0 72
50  
51 49 ▶sub spew {
Subroutine spew: called CC=3 SCAR=11.4
52 49 my ($path, $data) = @_;
53 49 ▶ 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" 49 0 49
54 49 print $fh $data;
55 49 ▶ close $fh or die "cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "cannot write ${path}: $!\n" 49 0 49
56 }
57  
58 26 ▶sub render {
Subroutine render: called CC=10 SCAR=23.4
59 26 my ($meta_file, $owner, $repo, $tag, $dir) = @_;
60 26 my $meta = read_meta($meta_file);
61 26 my $x = $meta->{x_binaries};
62 26 my ($name, $version) = ($meta->{name}, $meta->{version});
63 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
64 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
65 25 my $prefix = substr($tag, 0, length($tag) - length $version);
66 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
67 24 my @bins = binaries_of($x);
68 24 my @plat = platforms_of($x);
69 24 for my $v ($name, $owner, $repo, $tag, $version, @bins) {
70 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
71 }
72 24 (my $env = uc $name) =~ s/-/_/g;
73 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
74 24 my %v = (
75 DIST => $name, OWNER => $owner, REPO => $repo, TAG => $tag, VERSION => $version,
76 BINARIES => join(' ', @bins), PLATFORMS => join(' ', @plat), ENV => $env,
77 );
78 24 my $here = File::Basename::dirname(__FILE__);
79 24 for my $f (qw( install.sh install.ps1 )) {
80 48 my $c = slurp(File::Spec->catfile($here, "$f.in"));
81 48 $c =~ s/\@(DIST|OWNER|REPO|TAG|VERSION|BINARIES|PLATFORMS|ENV)\@/$v{$1}/g;
82 48 my $out = File::Spec->catfile($dir, $f);
83 48 spew($out, $c);
84 48 chmod 0644, $out;
85 }
86 24 return 0;
87 }
88  
89 # The binaries are recognised by their asset name (<name>-<version>-<platform>[.exe]),
90 # so the tarball and strays in the directory get no line.
91 1 ▶sub sums {
Subroutine sums: called CC=10 SCAR=23.2
92 1 my ($dir) = @_;
93 1 ▶ opendir my $dh, $dir or die "cannot read $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "cannot read ${dir}: $!\n" 1 0 1
94 1 my $alt = join '|', map { quotemeta } @PLATFORMS;
95 1 my %idx; @idx{@PLATFORMS} = (0 .. $#PLATFORMS);
96 1 my @found;
97 1 for my $f (readdir $dh) {
98 8 ▶ next unless -f File::Spec->catfile($dir, $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -f "File::Spec"->catfile($dir, $f) 2 6 8
99 6 ▶ next unless $f =~ /\A(.+)-[0-9][^-]*-($alt)(?:\.exe)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /\A(.+)-[0-9][^-]*-($alt)(?:\.exe)?\z/ 2 4 6
100 4 my ($n, $plat) = ($1, $2);
101 4 ▶ 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 4 4
$f =~ /\.exe\z/ ? : 2 2 4
102 4 push @found, [ $n, $idx{$plat}, $f ];
103 }
104 1 closedir $dh;
105 1 ▶ die "no binaries found in $dir\n" unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 0 1 1
106 1 my $out = '';
107 1 ▶ 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
220
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
108 4 $out .= Digest::SHA->new(256)->addfile(File::Spec->catfile($dir, $e->[2]), 'b')->hexdigest . " $e->[2]\n";
109 }
110 1 spew(File::Spec->catfile($dir, 'SHA256SUMS'), $out);
111 1 return 0;
112 }
113  
114 14 ▶sub check {
Subroutine check: called CC=7 SCAR=19.6
115 14 my ($meta_file, $version, $dl, $release_file, $latest_file) = @_;
116 14 my $meta = read_meta($meta_file);
117 14 my @want = expected_names($meta->{x_binaries}, $version);
118 14 my $rel = eval { JSON::PP->new->decode(slurp($release_file)) };
119 14 ▶ die "$release_file is not valid JSON: $@" unless ref $rel eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rel eq "HASH" 0 14 14
120 14 ▶ my %have = map { $_->{name} => 1 } grep { ref $_ eq 'HASH' && defined $_->{name} } @{ $rel->{assets} || [] };
Condition40%2/5
ref $_ eq 'HASH' && defined $_->{'name'}
!ll&&!rl&&r
0015
$rel->{'assets'} || []
l!l
140
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
121 14 my @bad;
122 14 push @bad, map { "missing binary: $_" } grep { !-f File::Spec->catfile($dl, $_) } @want;
123 14 push @bad, map { "release already has asset: $_" } grep { $have{$_} } @want;
124 14 ▶ if (@bad) { print STDERR "$_\n" for @bad; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) 2 12 14
125 12 ▶ print 'latest=', (latest_rule($rel, $latest_file) ? 'true' : 'false'), "\n";
Branch: 1 branch
BranchTrueFalseTotal
latest_rule($rel, $latest_file) ? : 6 3 9
126 9 return 0;
127 }
128  
129 # Never rolls releases/latest back: GitHub orders "latest" by created_at, so an
130 # older release finishing (or re-run) after a newer one is not marked latest.
131 12 ▶sub latest_rule {
Subroutine latest_rule: called CC=13 SCAR=26.0
132 12 my ($rel, $latest_file) = @_;
133 12 ▶ return 0 if $rel->{prerelease};
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"prerelease"} 2 10 12
134 10 my $raw = slurp($latest_file);
135 10 ▶ return 1 unless length $raw; # 404: no latest release yet
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 3 7 10
136 7 my $latest = eval { JSON::PP->new->decode($raw) };
137 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
138 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
139 5 my %at;
140 5 for ([ this => $rel, 'the release' ], [ latest => $latest, $latest_file ]) {
141 9 my ($k, $r, $what) = @$_;
142 9 my $t = $r->{created_at};
143 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
144 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
145 7 $at{$k} = $t;
146 }
147 3 ▶ return 1 if $at{latest} le $at{this};
Branch: 1 branch
BranchTrueFalseTotal
if $at{"latest"} le $at{"this"} 2 1 3
148 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
149 " (created $at{latest}) is newer than this release (created $at{this})\n";
150 1 return 0;
151 }
152  
153 43 ▶sub main {
Subroutine main: called CC=12 SCAR=25.9
154 43 my ($cmd, @a) = @_;
155 43 ▶ $cmd //= '';
Condition50%1/2
$cmd //= ""
l!l
430
MC/DC0%0/1
$cmd //= ""
$cmd
Truth table50%1/2
A
A= $cmd
Aresult
11
00
156 43 my $rc = eval {
157 43 ▶ if ($cmd eq 'render' && @a == 5) { render(@a) }
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'render' and @a == 5) { } 26 17 43
elsif ($cmd eq 'expected' and @a == 2) { } 2 15 17
elsif ($cmd eq 'sums' and @a == 1) { } 1 14 15
elsif ($cmd eq 'check' and @a == 5) { } 14 0 14
Condition66%2/3
$cmd eq "render" and @a == 5
!ll&&!rl&&r
17026
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
158 ▶ elsif ($cmd eq 'expected' && @a == 2) {
Condition66%2/3
$cmd eq "expected" and @a == 2
!ll&&!rl&&r
1502
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
159 2 print "$_\n" for expected_names(read_meta($a[0])->{x_binaries}, $a[1]);
160 2 0;
161 }
162 1 ▶ elsif ($cmd eq 'sums' && @a == 1) { sums(@a) }
Condition66%2/3
$cmd eq "sums" and @a == 1
!ll&&!rl&&r
1401
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
163 14 ▶ elsif ($cmd eq 'check' && @a == 5) { check(@a) }
Condition33%1/3
$cmd eq "check" and @a == 5
!ll&&!rl&&r
0014
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
164 0 else { die "usage: publish.pl render|expected|sums|check ...\n" }
165 };
166 43 ▶ if (!defined $rc) { print STDERR $@; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 5 38 43
167 38 return $rc;
168 }
169  
170 43 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 43 0 43
171 1;
« blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Binary.pm ↑ Summary blib/lib/Dist/Zilla/PluginBundle/Author/GETTY/Docker.pm »