PMUtils.pm

filter: Statementstmt 33.6% 37 / 110 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 4 / 12 Podpod 100% 6 / 6 totaltotal 29.3% 47 / 160 CC 22 SCAR 40.9
CC
22
Cov
26%
pmunlink
110.0
pmpath
90.0
pmabstract
12.0
CRAP 60.0
« pmgrep.pm ↑ Summary ModuleUtils.pm »
1 package App::PMUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 1 our $DATE = '2024-08-30'; # DATE
10 1 our $DIST = 'App-PMUtils'; # DIST
11 1 our $VERSION = '0.745'; # VERSION
12  
13 1 our %SPEC;
14  
15 our $arg_module_multiple = {
16  
17 # XXX Perinci::Sub::GetArgs::Argv can't yet handle case for greedy=1 and when 'array' is not specified explicitly
18 #schema => ['perl::modnames*', {
19 # min_len=>1,
20 # 'x.perl.default_value_rules' => ["Perl::these_mods"],
21 #}],
22  
23 schema => ['array*', {
24 of=>['perl::modname*'],
25 min_len=>1,
26 'x.perl.default_value_rules' => ["Perl::these_mods"],
27 "x.perl.coerce_rules" => ["From_str_or_array::expand_perl_modname_wildcard"],
28 }],
29  
30 pos => 0,
31 slurpy => 1,
32 0 ▶ element_completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 require Complete::Module;
34 0 my %args = @_;
35 0 Complete::Module::complete_module(word=>$args{word});
36 },
37 1 };
38  
39 our $arg_module_single = {
40 schema => ['perl::modname*', {
41 'x.perl.default_value_rules' => ["Perl::this_mod"],
42 }],
43 #req => 1,
44 pos => 0,
45 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 require Complete::Module;
47 0 my %args = @_;
48 0 Complete::Module::complete_module(word=>$args{word});
49 },
50 1 };
51  
52 1 our %argopts_pmpath_all = (
53 all => {
54 summary => 'Get all found files for each module instead of the first one',
55 schema => 'bool',
56 cmdline_aliases => {a=>{}},
57 },
58 );
59  
60 1 our %argopts_pmpath_types = (
61 pm => {
62 schema => ['int*', min=>0],
63 default => 1,
64 },
65 pmc => {
66 schema => ['int*', min=>0],
67 default => 0,
68 },
69 pod => {
70 schema => ['int*', min=>0],
71 default => 0,
72 },
73 );
74  
75 $SPEC{pmpath} = {
76 1 v => 1.1,
77 summary => 'Get path to locally installed Perl module',
78 args => {
79 module => $App::PMUtils::arg_module_multiple,
80 %argopts_pmpath_all,
81 %argopts_pmpath_types,
82 abs => {
83 summary => 'Absolutify each path',
84 schema => 'bool',
85 cmdline_aliases => {P=>{}},
86 },
87 prefix => {
88 schema => ['int*', min=>0],
89 default => 0,
90 },
91 dir => {
92 summary => 'Show directory instead of path',
93 description => <<'_',
94  
95 Also, will return `.` if not found, so you can conveniently do this on a Unix
96 shell:
97  
98 % cd `pmpath -Pd Moose`
99  
100 and it won't change directory if the module doesn't exist.
101  
102 _
103 schema => ['bool', is=>1],
104 cmdline_aliases => {d=>{}},
105 },
106 },
107 };
108 0 ▶sub pmpath {
Subroutine pmpath: not called CC=9 SCAR=45.0
Pod documented
109 0 require Module::Path::More;
110 0 my %args = @_;
111  
112 0 my $mods = $args{module};
113 0 my $res = [];
114 0 my $found;
115  
116 0 for my $mod (@{$mods}) {
117 my $mpath = Module::Path::More::module_path(
118 module => $mod,
119 find_pm => $args{pm},
120 find_pmc => $args{pmc},
121 find_pod => $args{pod},
122 find_prefix => $args{prefix},
123 abs => $args{abs},
124 all => $args{all},
125 0 );
126 0 ▶ $found++ if $mpath;
Branch: 1 branch
BranchTrueFalseTotal
if $mpath 0 0 0
127 0 ▶ for (ref($mpath) eq 'ARRAY' ? @$mpath : ($mpath)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $mpath eq 'ARRAY' ? : 0 0 0
128 0 ▶ if ($args{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dir"}) 0 0 0
129 0 require File::Spec;
130 0 my ($vol, $dir, $file) = File::Spec->splitpath($_);
131 0 $_ = $dir;
132 }
133 0 ▶ push @$res, @$mods > 1 ? {module=>$mod, path=>$_} : $_;
Branch: 1 branch
BranchTrueFalseTotal
@$mods > 1 ? : 0 0 0
134 }
135 }
136  
137 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 0 0 0
138 0 [200, "OK", $res];
139 } else {
140 0 ▶ if ($args{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'dir'}) { } 0 0 0
141 0 [200, "OK (not found)", "."];
142 } else {
143 0 [404, "No such module"];
144 }
145 }
146 }
147  
148 1 $SPEC{pmdir} = do {
149 1 my $meta = { %{ $SPEC{pmpath} } }; # shallow copy
150 1 $meta->{summary} = "Get directory of locally installed Perl module/prefix";
151 1 $meta->{description} = <<'_';
152  
153 This is basically a shortcut for:
154  
155 % pmpath -Pd MODULE_OR_PREFIX_NAME
156  
157 Sometimes I forgot that <prog:pmpath> has a `-d` option, and often intuitively
158 look for a <prog:pmdir> command.
159  
160 _
161 1 $meta->{args} = { %{ $SPEC{pmpath}{args} } }; # shalow copy
162 1 delete $meta->{args}{all};
163 1 delete $meta->{args}{dir};
164 1 delete $meta->{args}{prefix};
165 1 $meta;
166 };
167 0 ▶sub pmdir {
Subroutine pmdir: not called CC=1 SCAR=6.9
Pod documented
168 0 pmpath(@_, prefix=>1, dir=>1);
169 }
170  
171 $SPEC{rel2mod} = {
172 1 v => 1.1,
173 summary => 'Convert release name (e.g. Foo-Bar-1.23.tar.gz) to '.
174 'module name (Foo::Bar)',
175 args => {
176 releases => {
177 #'x.name.is_plural' => 1,
178 schema => ['array*', of=>'str*'],
179 req => 1,
180 pos => 0,
181 greedy => 1,
182 cmdline_src => 'stdin_or_args',
183 },
184 },
185 result_naked => 1,
186 };
187 0 ▶sub rel2mod {
Subroutine rel2mod: not called CC=2 SCAR=17.9
Pod documented
188 0 my %args = @_;
189  
190 #use DD; dd \%args;
191  
192 0 my @res;
193 0 for (@{ $args{releases} }) {
194 0 s!.+/!!; # remove directory path
195 0 s/(.+)-v?\d.+/$1/;
196 0 s/-/::/g;
197 0 push @res, $_;
198 }
199  
200 0 \@res;
201 }
202  
203 $SPEC{pmunlink} = {
204 1 v => 1.1,
205 summary => 'Unlink (remove) locally installed Perl module',
206 args => {
207 module => $App::PMUtils::arg_module_multiple,
208 %argopts_pmpath_all,
209 %argopts_pmpath_types,
210 },
211 features => {
212 dry_run => 1,
213 },
214 };
215 0 ▶sub pmunlink {
Subroutine pmunlink: not called CC=10 SCAR=47.0
Pod documented
216 0 my %args = @_;
217  
218 0 my $res = pmpath(%args);
219 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
220  
221 0 ▶ return [304, "No module files to delete"] unless @{ $res->[2] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res->[2];} 0 0 0
222  
223 0 my $num_success = 0;
224 0 my $num_fail = 0;
225 0 for my $item (@{ $res->[2] }) {
226 0 ▶ my $path = ref $item eq 'HASH' ? $item->{path} : $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'HASH' ? : 0 0 0
227 0 my $success;
228  
229 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
230 0 log_trace "[DRY_RUN] Unlinking $path ...";
231 0 $num_success++;
232 0 next;
233 }
234 0 log_trace "Unlinking $path ...";
235 0 ▶ if (unlink $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $path) { } 0 0 0
236 0 $num_success++;
237 } else {
238 0 warn "Can't unlink $path: $!\n";
239 0 $num_fail++;
240 }
241 }
242  
243 0 ▶ [$num_success ? 200:500,
Branch: 3 branches
BranchTrueFalseTotal
$num_success ? : 0 0 0
$num_success ? : 0 0 0
$num_fail ? : 0 0 0
244 $num_success ? "OK" : "All files failed",
245 undef,
246 {'cmdline.exit_code' => $num_fail ? 1:0}];
247 }
248  
249 $SPEC{pmabstract} = {
250 1 v => 1.1,
251 summary => 'Extract the abstract of locally installed Perl module(s)',
252 args => {
253 module => $App::PMUtils::arg_module_multiple,
254 },
255 };
256 0 ▶sub pmabstract {
Subroutine pmabstract: not called CC=3 SCAR=24.8
Pod documented
257 0 require Module::Abstract;
258  
259 0 my %args = @_;
260 0 my @rows;
261 0 for my $mod (@{ $args{module} }) {
262 0 push @rows, {
263 module => $mod,
264 abstract => Module::Abstract::module_abstract($mod),
265 };
266 }
267  
268 0 ▶ if (@rows > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows > 1) { } 0 0 0
269 0 return [200, "OK", \@rows, {'table.fields'=>[qw/module abstract/]}];
270 } else {
271 0 return [200, "OK", $rows[0]{abstract}];
272 }
273 }
274  
275 $SPEC{update_this_mod} = {
276 1 v => 1.1,
277 summary => 'Update "this" Perl module',
278 description => <<'_',
279  
280 Will use <pm:App::ThisDist>'s `this_mod()` to find out what the current Perl
281 module is, then run "cpanm -n" against the module. It's a convenient shortcut
282 for:
283  
284 % this-mod | cpanm -n
285  
286 _
287 args => {
288 # XXX cpanm options
289 },
290 deps => {
291 prog => 'cpanm',
292 },
293 };
294 0 ▶sub update_this_mod {
Subroutine update_this_mod: not called CC=2 SCAR=17.9
Pod documented
295 0 require App::ThisDist;
296 0 require IPC::System::Options;
297  
298 0 my %args = @_;
299  
300 0 my $mod = App::ThisDist::this_mod();
301 0 ▶ return [412, "Can't determine the current module"] unless defined $mod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mod 0 0 0
302 0 IPC::System::Options::system({log=>1, die=>1}, "cpanm", "-n", $mod);
303 0 [200];
304 }
305  
306 1 1;
307 # ABSTRACT: Command-line utilities related to Perl modules
308  
309 __END__
« pmgrep.pm ↑ Summary ModuleUtils.pm »