blib/lib/Dist/Zilla/Util/AuthorDeps.pm

filter: Statementstmt 82.6% 76 / 92 Branchbran 63.8% 23 / 36 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 70.7% 111 / 157 CC 26 SCAR 30.8
CC
26
Cov
73%
extract_author_deps
28.5
_format_author_deps
20.0
format_author_deps
2.0
CRAP 21.9
« blib/lib/Dist/Zilla/App/Command/listdeps.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PkgVersion.pm »
1 package Dist::Zilla::Util::AuthorDeps 6.040;
2 # ABSTRACT: Utils for listing your distribution's author dependencies
3  
4 2 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Dist::Zilla::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::Util 1.45 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =func extract_author_deps
13 #pod
14 #pod my $prereqs = extract_author_deps($dist_root, $missing_only);
15 #pod
16 #pod This returns a reference to an array in the form:
17 #pod
18 #pod [
19 #pod { $module1 => $ver1 },
20 #pod { $module2 => $ver2 },
21 #pod ...
22 #pod ]
23 #pod
24 #pod Each entry is one of the likely author dependencies for the distribution at the
25 #pod root path C<$dist_root>. If C<$missing_only> is true, then prereqs that appear
26 #pod to be available under the running perl will not be included.
27 #pod
28 #pod I<This function is not really meant to be reliable.> It was undocumented and
29 #pod subject to change at any time, but some downstream libraries chose to use it
30 #pod anyway. I may provide a replacement, at some point, at which point this method
31 #pod will be deprecated and begin issuing a warning. I have documented this method
32 #pod only to provide this warning!
33 #pod
34 #pod =cut
35  
36 2 ▶sub extract_author_deps {
Subroutine extract_author_deps: called CC=23 SCAR=33.5
Pod documented
37 2 my ($root, $missing) = @_;
38  
39 2 my $ini = path($root, 'dist.ini');
40  
41 2 ▶ die "dzil authordeps only works on dist.ini files, and you don't have one\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $ini 0 2 2
42 unless -e $ini;
43  
44 2 my $fh = $ini->openr_utf8;
45  
46 2 require Config::INI::Reader;
47 2 my $config = Config::INI::Reader->read_handle($fh);
48  
49 2 require CPAN::Meta::Requirements;
50 2 my $reqs = CPAN::Meta::Requirements->new;
51  
52 2 ▶ if (defined (my $license = $config->{_}->{license})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $license = $config->{"_"}{"license"})) 2 0 2
53 2 $license = 'Software::License::'.$license;
54 2 $reqs->add_minimum($license => 0);
55 }
56  
57 2 for my $section ( sort keys %$config ) {
58 14 ▶ if (q[_] eq $section) {
Branch: 1 branch
BranchTrueFalseTotal
if ("_" eq $section) 2 12 14
59 2 my $version = $config->{_}{':version'};
60 2 ▶ $reqs->add_minimum('Dist::Zilla' => $version) if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 2 0 2
61 2 next;
62 }
63  
64 12 my $pack = $section;
65 12 $pack =~ s{\s*/.*$}{}; # trim optional space and slash-delimited suffix
66  
67 12 my $version = 0;
68 12 ▶ $version = $config->{$section}->{':version'} if exists $config->{$section}->{':version'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$section}{":version"} 6 6 12
69  
70 12 my $realname = Dist::Zilla::Util->expand_config_package_name($pack);
71 12 $reqs->add_minimum($realname => $version);
72 }
73  
74 2 seek $fh, 0, 0;
75  
76 2 my $in_filter = 0;
77 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 58 2 60
78 58 ▶ next unless $in_filter or /^\[\s*\@Filter/;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_filter or /^\[\s*\@Filter/u 58 0 58
Condition33%1/3
$in_filter or /^\[\s*\@Filter/u
l!l&&r!l&&!r
0058
MC/DC0%0/2
$in_filter or /^\[\s*\@Filter/u
$in_filter /^\[\s*\@Filter/u
Truth table33%1/3
A or B
A= $in_filterB= /^\[\s*\@Filter/u
ABresult
1X1
011
000
79 0 ▶ $in_filter = 0, next if /^\[/ and ! /^\[\s*\@Filter/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\[/u and not /^\[\s*\@Filter/u 0 0 0
Condition0%0/3
/^\[/u and not /^\[\s*\@Filter/u
!ll&&!rl&&r
000
MC/DC0%0/2
/^\[/u and not /^\[\s*\@Filter/u
/^\[/u not /^\[\s*\@Filter/u
Truth table0%0/3
A and not(B)
A= /^\[/uB= not /^\[\s*\@Filter/u
ABresult
0X0
100
111
80 0 $in_filter = 1;
81  
82 0 ▶ next unless /\A-bundle\s*=\s*([^;\s]+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A-bundle\s*=\s*([^;\s]+)/u 0 0 0
83 0 my $pname = $1;
84 0 chomp($pname);
85 0 $reqs->add_minimum(Dist::Zilla::Util->expand_config_package_name($1) => 0)
86 }
87  
88 2 seek $fh, 0, 0;
89  
90 2 my @packages;
91 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 58 2 60
92 58 chomp;
93 58 ▶ next unless /\A\s*;\s*authordep\s*(\S+)\s*(?:=\s*([^;]+))?\s*/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A\s*;\s*authordep\s*(\S+)\s*(?:=\s*([^;]+))?\s*/u 52 6 58
94 6 my $module = $1;
95 6 ▶ my $ver = $2 // "0";
Condition50%1/2
$2 // 0
l!l
60
MC/DC0%0/1
$2 // 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
96 6 $ver =~ s/\s+$//;
97 # Any "; authordep " is inserted at the beginning of the list
98 # in the file order so the user can control the order of at least a part of
99 # the plugin list
100 6 push @packages, $module;
101 # And added to the requirements so we can use it later
102 6 $reqs->add_string_requirement($module => $ver);
103 }
104  
105 2 my $vermap = $reqs->as_string_hash;
106 # Add the other requirements
107 2 push @packages, sort keys %$vermap;
108  
109 # Move inc:: first in list as they may impact the loading of other
110 # plugins (in particular local ones).
111 # Also order inc:: so that those that want to hack @INC with inc:: plugins
112 # can have a consistent playground.
113 # We don't sort the others packages to preserve the same (random) ordering
114 # for the common case (no inc::, no '; authordep') as in previous dzil
115 # releases.
116 2 @packages = ((sort grep /^inc::/, @packages), (grep !/^inc::/, @packages));
117 2 @packages = List::Util::uniq(@packages);
118  
119 2 ▶ if ($missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing) 1 1 2
120 1 require Module::Runtime;
121  
122 1 my @new_packages;
123 1 PACKAGE: for my $package (@packages) {
124 11 ▶ if ($package eq 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "perl") 1 10 11
125 # This is weird, perl can never really be a prereq to fulfill but...
126 # it was like this. -- rjbs, 2024-06-02
127 1 ▶ if ($vermap->{perl} && ! eval "use $vermap->{perl}; 1") {
Subroutine BEGIN: called CC=1 SCAR=0.5
Branch: 1 branch
BranchTrueFalseTotal
if ($vermap->{"perl"} and not eval qq[use $vermap->{"perl"}; 1]) 0 1 1
Condition33%1/3
$vermap->{"perl"} and not eval qq[use $vermap->{"perl"}; 1]
!ll&&!rl&&r
010
MC/DC0%0/2
$vermap->{"perl"} and not eval qq[use $vermap->{"perl"}; 1]
$vermap->{"perl"} not eval qq[use $vermap->{"perl"}; 1]
Truth table33%1/3
A and not(B)
A= $vermap->{"perl"}B= not eval qq[use $vermap->{"perl"}; 1]
ABresult
0X0
100
111
128 0 push @new_packages, 'perl';
129 }
130  
131 1 next PACKAGE;
132 }
133  
134 10 my $ok = eval {
135 10 local @INC = (@INC, "$root");
136  
137 # This will die if module is missing
138 10 Module::Runtime::require_module($package);
139 9 my $v = $vermap->{$package};
140  
141 # This will die if VERSION is too low
142 9 ▶ !$v || $package->VERSION($v);
Branch: 1 branch
BranchTrueFalseTotal
$v and $package->VERSION($v) 5 4 9
143  
144 # Success!
145 9 1;
146 };
147  
148 10 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 9 10
149 1 push @new_packages, $package;
150 }
151 }
152  
153 1 @packages = @new_packages;
154 }
155  
156 # Now that we have a sorted list of packages, use that to build an array of
157 # hashrefs for display.
158 2 [ map { { $_ => $vermap->{$_} } } @packages ]
159 }
160  
161 #pod =func format_author_deps
162 #pod
163 #pod my $string = format_author_deps($prereqs, $include_versions);
164 #pod
165 #pod Given a reference to an array in the format returned by C<extract_author_deps>,
166 #pod this returns a string in the form:
167 #pod
168 #pod Module::One
169 #pod Module::Two
170 #pod Module::Three
171 #pod
172 #pod or, if C<$include_versions> is true:
173 #pod
174 #pod Module::One = 1.00
175 #pod Module::Two = 1.23
176 #pod Module::Three = 8.910213
177 #pod
178 #pod I<This function is not really meant to be reliable.> It was undocumented and
179 #pod subject to change at any time, but some downstream libraries chose to use it
180 #pod anyway. I may provide a replacement, at some point, at which point this method
181 #pod will be deprecated and begin issuing a warning. I have documented this method
182 #pod only to provide this warning!
183 #pod
184 #pod =cut
185  
186 0 ▶sub format_author_deps {
Subroutine format_author_deps: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($prereqs, $versions) = @_;
188 0 return _format_author_deps($prereqs, $versions);
189 }
190  
191 0 ▶sub _format_author_deps {
Subroutine _format_author_deps: not called CC=4 SCAR=30.0
192 0 my ($prereqs, $versions, $cpanm_versions) = @_;
193  
194 0 my $formatted = '';
195 0 for my $rec (@$prereqs) {
196 0 my ($mod, $ver) = %$rec;
197 0 ▶ $formatted .= $cpanm_versions ? "$mod~$ver\n"
Branch: 2 branches
BranchTrueFalseTotal
$cpanm_versions ? : 0 0 0
$versions ? : 0 0 0
198 : $versions ? "$mod = $ver\n"
199 : "$mod\n";
200 }
201  
202 0 chomp $formatted;
203  
204 0 return $formatted;
205 }
206  
207  
208 2 1;
209  
210 __END__
« blib/lib/Dist/Zilla/App/Command/listdeps.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PkgVersion.pm »