cpan2arch/MergePrereqs.pm

filter: Statementstmt 17.4% 27 / 155 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 4 totaltotal 11.3% 34 / 299 CC 51 SCAR 60.7
CC
51
Cov
11%
merge_prereqs
812.0
_get_dists
342.0
__ANON__
20.0
CRAP 433.4
« cpan2arch/WritePkgbuild.pm ↑ Summary cpan2arch/GetMetadata.pm »
1 1 ▶use v5.42.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶no source::encoding; # Avoid dying on v5.42.0 (non-ASCII char in POD).
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package App::cpan2arch::MergePrereqs; # For toolchain compatibility.
10 role App::cpan2arch::MergePrereqs;
11  
12 1 ▶use Scalar::Util qw< looks_like_number >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util qw< any >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = 'v1.1.2';
16  
17 0 ▶field %_cpan_prereqs :reader :writer;
Subroutine cpan_prereqs: not called CC=1 SCAR=4.1
Subroutine set_cpan_prereqs: not called CC=1 SCAR=4.1
Pod undocumented
Pod undocumented
18 0 ▶field @_fetch_errors :reader;
Subroutine fetch_errors: not called CC=1 SCAR=2.6
Pod undocumented
19 1 field @_prereq_dists;
20  
21 # Merge CPAN prerequisites to PKGBUILD dependencies.
22 #
23 # References:
24 # https://blogs.perl.org/users/neilb/2017/04/an-introduction-to-distribution-metadata.html
25 # https://blogs.perl.org/users/neilb/2017/04/dependency-phases-in-cpan-distribution-metadata.html
26 # https://blogs.perl.org/users/neilb/2017/04/specifying-the-type-of-your-cpan-dependencies.html
27 # https://blogs.perl.org/users/neilb/2017/05/specifying-dependencies-for-your-cpan-distribution.html
28 # https://neilb.org/2015/09/25/dependencies-model.html
29 # https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/cpan-packaging.md
30 # https://metacpan.org/pod/CPAN::Meta::Spec#PREREQUISITES
31 # https://wiki.archlinux.org/title/PKGBUILD#Dependencies
32 # https://metacpan.org/pod/CPAN::Meta::Spec#Version-Ranges
33 # https://man.archlinux.org/man/core/pacman/PKGBUILD.5.en#:~:text=depends%20(array
34 # https://man.archlinux.org/man/alpm-package-relation.7
35 # https://man.archlinux.org/man/alpm-comparison.7
36 0 ▶method merge_prereqs ()
Subroutine merge_prereqs: not called CC=28 SCAR=67.0
Pod undocumented
37 {
38 0 $self->_psub;
39  
40 0 my %meta = $self->meta;
41  
42 0 %_cpan_prereqs = (
43 depends => undef,
44 makedepends => undef,
45 checkdepends => undef,
46 optdepends => undef,
47 );
48  
49 0 my $prereqs = $self->_get_dists;
50 0 ▶ return 1 if looks_like_number($prereqs) && $prereqs == 1;
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($prereqs) and $prereqs == 1 0 0 0
Condition0%0/3
&looks_like_number($prereqs) and $prereqs == 1
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($prereqs) and $prereqs == 1
&looks_like_number($prereqs) $prereqs == 1
Truth table0%0/3
A and B
A= &looks_like_number($prereqs)B= $prereqs == 1
ABresult
0X0
100
111
51 0 $self->_pdump( '$prereqs', $prereqs, "\n" );
52  
53 0 foreach my $dep ( $prereqs->@* ) {
54 my ( $module, $dist, $phase, $relationship, $version, $failed ) =
55 0 $dep->@{ qw< module dist phase relationship version failed > };
56  
57 0 ▶ my $variable =
Branch: 4 branches
BranchTrueFalseTotal
$relationship eq 'recommends' || $relationship eq 'suggests' ? : 0 0 0
$phase eq 'configure' || $phase eq 'build' ? : 0 0 0
$phase eq 'runtime' ? : 0 0 0
$phase eq 'test' ? : 0 0 0
Condition0%0/6
$relationship eq "recommends" or $relationship eq "suggests"
l!l&&r!l&&!r
000
$phase eq "configure" or $phase eq "build"
l!l&&r!l&&!r
000
MC/DC0%0/2
$relationship eq "recommends" or $relationship eq "suggests"
$relationship eq "recommends" $relationship eq "suggests"
MC/DC0%0/2
$phase eq "configure" or $phase eq "build"
$phase eq "configure" $phase eq "build"
Truth table0%0/3
A or B
A= $relationship eq "recommends"B= $relationship eq "suggests"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $phase eq "configure"B= $phase eq "build"
ABresult
1X1
011
000
58 $relationship eq 'recommends' || $relationship eq 'suggests' ? 'optdepends'
59 : $phase eq 'configure' || $phase eq 'build' ? 'makedepends'
60 : $phase eq 'runtime' ? 'depends'
61 : $phase eq 'test' ? 'checkdepends'
62 : ();
63  
64 0 $self->_pdump( '$module', \$module, '' );
65 0 $self->_pdump( '$version', \$version, '' );
66  
67 # Version ranges are unsupported because no dist use it for its prereqs,
68 # and some operators are inconsistent with PKGBUILD operators, e.g. ==, !=.
69 0 my $is_version = do {
70 0 try {
71 0 version->parse($version);
72 }
73 catch ($e) {
74 0 $self->_pdbg("version range is unsupported\n");
75 0 undef;
76 }
77 };
78  
79 0 ▶ if ( !defined $is_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $is_version) { } 0 0 0
80 0 ▶ $_cpan_prereqs{$variable}{$module}{dist} = $dist if defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dist 0 0 0
81 0 ▶ $_cpan_prereqs{$variable}{$module}{failed} = true if defined $failed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed 0 0 0
82 0 $_cpan_prereqs{$variable}{$module}{version} = 0;
83 }
84 # Numeric version
85 else {
86 # Skip duplicated distros but get the lesser version.
87 0 ▶ if ( exists $_cpan_prereqs{$variable}{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_cpan_prereqs{$variable}{$module}) 0 0 0
88 0 $self->_pdbg("found dupe\n");
89  
90 0 my $ret = $self->_comp_vers( $version, $_cpan_prereqs{$variable}{$module}{version}, '>=' );
91  
92 0 ▶ return 1 if defined $ret && $ret == 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret and $ret == 1 0 0 0
Condition0%0/3
defined $ret and $ret == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ret and $ret == 1
defined $ret $ret == 1
Truth table0%0/3
A and B
A= defined $retB= $ret == 1
ABresult
0X0
100
111
93 0 ▶ next if defined $ret && $ret == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret and $ret == 0 0 0 0
Condition0%0/3
defined $ret and $ret == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ret and $ret == 0
defined $ret $ret == 0
Truth table0%0/3
A and B
A= defined $retB= $ret == 0
ABresult
0X0
100
111
94 }
95  
96 0 ▶ $_cpan_prereqs{$variable}{$module}{dist} = $dist if defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dist 0 0 0
97 0 ▶ $_cpan_prereqs{$variable}{$module}{failed} = true if defined $failed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed 0 0 0
98 0 $_cpan_prereqs{$variable}{$module}{version} = $version;
99 }
100  
101 0 $self->_pdbg("\n");
102 }
103  
104 # Optionals must not exist in other variable phases.
105 0 foreach my $opt ( keys $_cpan_prereqs{optdepends}->%* ) {
106 0 foreach my ( $var, $deps ) (%_cpan_prereqs) {
107 0 ▶ next if $var eq 'optdepends';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "optdepends" 0 0 0
108  
109 0 ▶ if ( exists $_cpan_prereqs{$var}{$opt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_cpan_prereqs{$var}{$opt}) 0 0 0
110 0 delete $_cpan_prereqs{optdepends}{$opt};
111 0 $self->_pdbg("$opt exists outside of optdepends\n\n");
112 }
113 }
114 }
115  
116 # Ensure perl package is in 'depends'.
117 0 ▶ if ( !exists $_cpan_prereqs{depends}{perl} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_cpan_prereqs{"depends"}{"perl"}) 0 0 0
118 0 ▶ if ( exists $_cpan_prereqs{makedepends}{perl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_cpan_prereqs{'makedepends'}{'perl'}) { } 0 0 0
119 0 $_cpan_prereqs{depends}{perl} = delete $_cpan_prereqs{makedepends}{perl};
120 }
121 else {
122 0 $_cpan_prereqs{depends}{perl}{version} = 0;
123 }
124 }
125  
126 # Ensure Module::Install is in makedepends if dist uses it.
127 0 ▶ if ( $meta{has_module_install} && !exists $_cpan_prereqs{makedepends}{'Module::Install'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta{"has_module_install"} and not exists $_cpan_prereqs{"makedepends"}{"Module::Install"}) 0 0 0
Condition0%0/3
$meta{"has_module_install"} and not exists $_cpan_prereqs{"makedepends"}{"Module::Install"}
!ll&&!rl&&r
000
MC/DC0%0/2
$meta{"has_module_install"} and not exists $_cpan_prereqs{"makedepends"}{"Module::Install"}
$meta{"has_module_install"} not exists $_cpan_prereqs{"makedepends"}{"Module::Install"}
Truth table0%0/3
A and not(B)
A= $meta{"has_module_install"}B= not exists $_cpan_prereqs{"makedepends"}{"Module::Install"}
ABresult
0X0
100
111
128 0 $_cpan_prereqs{makedepends}{'Module::Install'} = {
129 dist => 'Module-Install',
130 version => 0,
131 };
132 }
133  
134 0 $self->_pdump( '%_cpan_prereqs', \%_cpan_prereqs, "\n" );
135  
136 0 return 0;
137 1 }
138  
139 # Get distribution names from prerequisite modules.
140 0 ▶method _get_dists ()
Subroutine _get_dists: not called CC=18 SCAR=58.3
141 {
142 0 $self->_psub;
143  
144 0 my %meta = $self->meta;
145 0 ▶ return [] unless scalar $meta{dependency}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$meta{"dependency"}} 0 0 0
146  
147 0 my $prog = $self->prog;
148  
149 # Filter undesired modules.
150 0 my @prereqs;
151 my @ignored;
152 {
153 0 my @NOT_FOUND = (
154 qw<
155 Config
156 Errno
157 >
158 );
159  
160 0 foreach my ( $idx, $dep ) ( indexed $meta{dependency}->@* ) {
161 # Skip phases like 'develop' and x_Dist_Zilla.
162 # Those phases and 'conflicts' relationship (discouraged) are CPAN
163 # specific and not relevant to Arch.
164 next
165 if $dep->{phase} ne 'configure'
166 && $dep->{phase} ne 'build'
167 && $dep->{phase} ne 'runtime'
168 0 ▶ && $dep->{phase} ne 'test';
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"phase"} ne "configure" and $dep->{"phase"} ne "build" and $dep->{"phase"} ne "runtime" and $dep->{"phase"} ne "test" 0 0 0
Condition0%0/9
$dep->{"phase"} ne "configure" and $dep->{"phase"} ne "build" and $dep->{"phase"} ne "runtime" and $dep->{"phase"} ne "test"
!ll&&!rl&&r
000
$dep->{"phase"} ne "configure" and $dep->{"phase"} ne "build" and $dep->{"phase"} ne "runtime"
!ll&&!rl&&r
000
$dep->{"phase"} ne "configure" and $dep->{"phase"} ne "build"
!ll&&!rl&&r
000
MC/DC0%0/4
$dep->{"phase"} ne "configure" and $dep->{"phase"} ne "build" and $dep->{"phase"} ne "runtime" and $dep->{"phase"} ne "test"
$dep->{"phase"} ne "configure" $dep->{"phase"} ne "build" $dep->{"phase"} ne "runtime" $dep->{"phase"} ne "test"
Truth table0%0/5
A and B and C and D
A= $dep->{"phase"} ne "configure"B= $dep->{"phase"} ne "build"C= $dep->{"phase"} ne "runtime"D= $dep->{"phase"} ne "test"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
169  
170 next
171 if $dep->{relationship} ne 'requires'
172 && $dep->{relationship} ne 'recommends'
173 0 ▶ && $dep->{relationship} ne 'suggests';
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"relationship"} ne "requires" and $dep->{"relationship"} ne "recommends" and $dep->{"relationship"} ne "suggests" 0 0 0
Condition0%0/6
$dep->{"relationship"} ne "requires" and $dep->{"relationship"} ne "recommends" and $dep->{"relationship"} ne "suggests"
!ll&&!rl&&r
000
$dep->{"relationship"} ne "requires" and $dep->{"relationship"} ne "recommends"
!ll&&!rl&&r
000
MC/DC0%0/3
$dep->{"relationship"} ne "requires" and $dep->{"relationship"} ne "recommends" and $dep->{"relationship"} ne "suggests"
$dep->{"relationship"} ne "requires" $dep->{"relationship"} ne "recommends" $dep->{"relationship"} ne "suggests"
Truth table0%0/4
A and B and C
A= $dep->{"relationship"} ne "requires"B= $dep->{"relationship"} ne "recommends"C= $dep->{"relationship"} ne "suggests"
ABCresult
0XX0
10X0
1100
1111
174  
175 # MetaCPAN API fails to fetch these core modules for some reason, but it's OK.
176 0 ▶ next if any { $dep->{module} eq $_ } @NOT_FOUND;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $dep->{"module"} eq $_; } , @NOT_FOUND) 0 0 0
177  
178 # No need to fetch perl interpreter dist.
179 0 ▶ if ( $dep->{module} eq 'perl' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep->{"module"} eq "perl") 0 0 0
180 0 $dep->{dist} = 'perl';
181 0 push @ignored, $dep;
182  
183 0 next;
184 }
185  
186 0 push @prereqs, $dep;
187 }
188  
189 0 $self->_pdump( '@prereqs', \@prereqs, "\n" );
190 0 $self->_pdump( '@ignored', \@ignored, "\n" );
191 }
192  
193 # Fetch dists.
194 {
195 0 ▶ $self->_fetch(@prereqs) if scalar @prereqs;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @prereqs 0 0 0
196  
197 0 $self->_pdbg("\n");
198 0 $self->_pdump( '@_prereq_dists', \@_prereq_dists, "\n" );
199  
200 0 ▶ if ( scalar @_fetch_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_fetch_errors) 0 0 0
201 0 warn "$prog: failed to fetch distributions\n";
202 0 $self->_pdump( '@_fetch_errors', \@_fetch_errors, "\n" );
203  
204 0 return 1;
205 }
206 }
207  
208 # Include the dists.
209 {
210 0 foreach my ( $idx, $dep ) ( indexed @prereqs ) {
211 0 my $dist = $_prereq_dists[$idx];
212 0 my $type = reftype $dist;
213  
214 0 ▶ if ( defined $type && $type eq 'HASH' && exists $dist->{failed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "HASH" and exists $dist->{"failed"}) 0 0 0
Condition0%0/6
defined $type and $type eq "HASH" and exists $dist->{"failed"}
!ll&&!rl&&r
000
defined $type and $type eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $type and $type eq "HASH" and exists $dist->{"failed"}
defined $type $type eq "HASH" exists $dist->{"failed"}
Truth table0%0/4
A and B and C
A= defined $typeB= $type eq "HASH"C= exists $dist->{"failed"}
ABCresult
0XX0
10X0
1100
1111
215 0 $dep->{failed} = true;
216 0 next;
217 }
218  
219 0 $dep->{dist} = $dist;
220 }
221  
222 0 push @prereqs, @ignored;
223 }
224  
225 0 return \@prereqs;
226 1 }
227  
228 # Fetch all modules distributions concurrently.
229 0 ▶method _fetch (@prereqs)
Subroutine _fetch: not called CC=2 SCAR=17.9
230 {
231 0 $self->_psub;
232  
233 0 require Mojo::Promise;
234  
235 0 my $prog = $self->prog;
236 0 my $mua_mcpan = $self->mua_mcpan;
237 0 my %endpoints = $self->endpoints;
238  
239 0 my %env = $self->env;
240 0 ▶ local $ENV{MUAC_NOCACHE} = true if $env{cache_ignore};
Branch: 1 branch
BranchTrueFalseTotal
if $env{"cache_ignore"} 0 0 0
241  
242 # Fetch three dists at a time using the download_url endpoint.
243 #
244 # References:
245 # https://github.com/metacpan/metacpan-api/blob/master/docs/API-docs.md#download_urlmodule.
246 # https://metacpan.org/release/MIYAGAWA/App-cpanminus-1.7049/source/lib/App/cpanminus/fatscript.pm#L926
247 # https://github.com/metacpan/MetaCPAN-Client/blob/396ba371317f114db04967c90be64bb46fd7fd2c/lib/MetaCPAN/Client.pm#L217
248 return Mojo::Promise->map(
249 { concurrency => 3 },
250 0 ▶ sub ($prereq) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
251 0 my $module = $prereq->{module};
252 0 my $version = $prereq->{version};
253 0 my $url = $endpoints{download} . $module;
254 0 my $query;
255  
256 0 ▶ $query = 'version=' . $version if $version; # Ignore 0 versions.
Branch: 1 branch
BranchTrueFalseTotal
if $version 0 0 0
257 0 ▶ $url .= '?' . $query if defined $query;
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 0 0 0
258  
259 0 $mua_mcpan->get_p($url);
260 },
261 @prereqs,
262 )
263 ->then(
264 0 ▶ sub (@promises) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
265 0 foreach my $tx (@promises) {
266 0 my $res = $tx->[0]->result;
267  
268 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
269 0 my $json = $res->json;
270 0 push @_prereq_dists, $json->{distribution};
271 }
272 # Do not exit but flag if MetaCPAN API fails to fetch a module,
273 # to avoid PKGBUILD not being generated when upstream metadata
274 # has wrong module name.
275 else {
276 0 my $req = $tx->[0]->req;
277 0 my $url = $req->url;
278 0 my $mod = $url->path->parts->[-1];
279 0 my $ver = $url->query->param('version');
280 0 my $msg = $res->message;
281  
282 0 my $err = "$prog: failed to fetch $mod module";
283 0 ▶ $err .= " (query version: $ver)" if defined $ver;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ver 0 0 0
284  
285 0 warn "Failed to fetch '$url': $msg";
286 0 warn "$err\n";
287  
288 0 push @_prereq_dists, {
289 failed => true,
290 version => $ver,
291 };
292 }
293 }
294 }
295 )
296 ->catch(
297 0 ▶ sub ($e) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 warn $e;
299 0 push @_fetch_errors, $e;
300 }
301 0 )->wait;
302 }
303  
304 =encoding UTF-8
305  
306 =for highlighter language=perl
307  
308 =head1 NAME
309  
310 App::cpan2arch::MergePrereqs - merge CPAN prerequisites to PKGBUILD dependencies
311  
312 =head1 SYNOPSIS
313  
314 use App::cpan2arch;
315  
316 my $cpan2arch = App::cpan2arch->new;
317  
318 ...
319  
320 =head1 DESCRIPTION
321  
322 This role handles the translation of dependencies between
323 L<CPAN|https://metacpan.org/pod/CPAN::Meta::Spec#PREREQUISITES> and
324 L<PKGBUILD|https://man.archlinux.org/man/alpm-package-relation.7> for the
325 module/distribution.
326  
327 =head1 METHODS
328  
329 =head2 merge_prereqs
330  
331 $cpan2arch->merge_prereqs;
332  
333 Takes no arguments and returns C<0> on success.
334  
335 =head1 BUGS
336  
337 Report bugs at L<https://github.com/ryoskzypu/App-cpan2arch/issues>.
338  
339 =head1 AUTHOR
340  
341 ryoskzypu <ryoskzypu@proton.me>
342  
343 =head1 SEE ALSO
344  
345 =over 4
346  
347 =item *
348  
349 L<https://blogs.perl.org/users/neilb/2017/04/dependency-phases-in-cpan-distribution-metadata.html>
350  
351 =item *
352  
353 L<https://blogs.perl.org/users/neilb/2017/04/specifying-the-type-of-your-cpan-dependencies.html>
354  
355 =item *
356  
357 L<https://blogs.perl.org/users/neilb/2017/05/specifying-dependencies-for-your-cpan-distribution.html>
358  
359 =back
360  
361 =head1 COPYRIGHT
362  
363 Copyright © 2026 ryoskzypu
364  
365 MIT-0 License. See LICENSE for details.
366  
367 =cut
« cpan2arch/WritePkgbuild.pm ↑ Summary cpan2arch/GetMetadata.pm »