blib/lib/App/perlimports/CLI.pm

filter: Statementstmt 80.5% 153 / 190 Branchbran 59.0% 52 / 88 Conditioncond 40.0% 18 / 45 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 96.6% 29 / 30 Podpod 100% 1 / 1 totaltotal 67.4% 255 / 378 CC 64 SCAR 45.1
CC
64
Cov
69%
run
206.4
_build_config
10.0
_build_config_file
7.5
CRAP 91.1
↑ Summary blib/lib/App/perlimports/Include.pm »
1 package App::perlimports::CLI;
2  
3 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 our $VERSION = '0.000067';
7  
8 17 ▶use App::perlimports ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use App::perlimports::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use App::perlimports::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Capture::Tiny qw( capture_stdout );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Getopt::Long::Descriptive qw( describe_options );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use List::Util qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Log::Dispatch ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Path::Iterator::Rule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use Types::Standard qw( ArrayRef Bool HashRef InstanceOf Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 17 has _args => (
21 is => 'ro',
22 isa => HashRef,
23 lazy => 1,
24 builder => '_build_args',
25 );
26  
27 17 has _config => (
28 is => 'ro',
29 isa => InstanceOf [App::perlimports::Config::],
30 lazy => 1,
31 builder => '_build_config',
32 );
33  
34 17 has _config_file => (
35 is => 'ro',
36 isa => Str,
37 lazy => 1,
38 init_arg => 'config',
39 builder => '_build_config_file',
40 );
41  
42 # off by default
43 has _inplace_edit => (
44 is => 'ro',
45 isa => Bool,
46 lazy => 1,
47 7 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
48 7 my $self = shift;
49 return
50 7 ▶ defined $self->_opts->inplace_edit
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_opts->inplace_edit ? : 0 7 7
51 ? $self->_opts->inplace_edit
52 : 0;
53 },
54 17 );
55  
56 has _json => (
57 is => 'ro',
58 isa => Bool,
59 lazy => 1,
60 8 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
61 8 my $self = shift;
62 8 ▶ return defined $self->_opts->json
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_opts->json ? : 0 8 8
63 ? $self->_opts->json
64 : 0;
65 },
66 17 );
67  
68 has _lint => (
69 is => 'ro',
70 isa => Bool,
71 lazy => 1,
72 8 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
73 8 my $self = shift;
74 8 ▶ return defined $self->_opts->lint
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_opts->lint ? : 0 8 8
75 ? $self->_opts->lint
76 : 0;
77 },
78 17 );
79  
80 has _opts => (
81 is => 'ro',
82 isa => InstanceOf ['Getopt::Long::Descriptive::Opts'],
83 lazy => 1,
84 15 ▶ default => sub { $_[0]->_args->{opts} },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 17 );
86  
87 # off by default
88 has _quiet => (
89 is => 'ro',
90 isa => Bool,
91 lazy => 1,
92 8 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
93 8 my $self = shift;
94 8 ▶ return defined $self->_opts->quiet
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_opts->quiet ? : 0 8 8
95 ? $self->_opts->quiet
96 : 0;
97 },
98 17 );
99  
100 # off by default
101 has _read_stdin => (
102 is => 'ro',
103 isa => Bool,
104 lazy => 1,
105 9 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
106 9 my $self = shift;
107 return
108 defined $self->_opts->read_stdin ? $self->_opts->read_stdin
109 : defined $self->_config->{read_stdin}
110 ? $self->_config->{read_stdin}
111 9 ▶ : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $self->_opts->read_stdin ? : 0 9 9
defined $self->_config->{'read_stdin'} ? : 0 8 8
112 },
113 17 );
114  
115 has _usage => (
116 is => 'ro',
117 isa => Object,
118 lazy => 1,
119 4 ▶ default => sub { $_[0]->_args->{usage} },
Subroutine __ANON__: called CC=1 SCAR=0.0
120 17 );
121  
122 17 with 'App::perlimports::Role::Logger';
123  
124 15 ▶sub _build_args {
Subroutine _build_args: called CC=1 SCAR=0.0
125 15 my ( $opt, $usage ) = describe_options(
126 'perlimports %o',
127 [
128 'filename|f=s',
129 'A file you would like to run perlimports on. Alternatively, just provide a list of one or more file names without a named parameter as the last arguments to this script: perlimports file1 file2 file3'
130 ],
131 [],
132 [
133 'config-file=s',
134 'Path to a perlimports config file. If this parameter is not supplied, we will look for a file called perlimports.toml or .perlimports.toml in the current directory and then look for a perlimports.toml in XDG_CONFIG_HOME (usually something like $HOME/.config/perlimports/perlimports.toml). This behaviour can be disabled via --no-config-file'
135 ],
136 [],
137 [
138 'create-config-file=s',
139 'Create a sample config file using the supplied name and then exit.',
140 { shortcircuit => 1 }
141 ],
142 [],
143 [
144 'ignore-modules=s',
145 'Comma-separated list of modules to ignore.'
146 ],
147 [],
148 [
149 'ignore-modules-pattern=s',
150 'Regular expression that matches modules to ignore.'
151 ],
152 [],
153 [
154 'cache!',
155 '(Experimental and currently discouraged.) Cache some objects in order to speed up subsequent runs. Defaults to no cache.',
156 ],
157 [],
158 [
159 'ignore-modules-filename=s',
160 'Path to file listing modules to ignore. One per line.'
161 ],
162 [],
163 [
164 'ignore-modules-pattern-filename=s',
165 'Path to file listing regular expressions that matches modules to ignore. One per line.'
166 ],
167 [],
168 [
169 'json',
170 '(Experimental) Emit linting results as JSON rather than plain text'
171 ],
172 [],
173 [
174 'never-export-modules=s',
175 'Comma-separated list of modules which do not export symbols.'
176 ],
177 [],
178 [
179 'never-export-modules-filename=s',
180 q{Path to file listing modules which don't export symbols. One per line.}
181 ],
182 [],
183 [ 'inplace-edit|i', 'Edit the file in place.' ],
184 [],
185 [
186 'libs=s',
187 'Comma-separated list of library paths to include (eg --libs lib,t/lib,dev/lib)',
188  
189 ],
190 [],
191 [
192 'lint',
193 'Act as a linter only. Do not edit any files.',
194 ],
195 [],
196 [
197 'quiet|q',
198 'Suppress the "<file> OK" summary and console log output.',
199 ],
200 [],
201 [
202 'no-config-file',
203 'Do not look for a perlimports config file.'
204 ],
205 [],
206 [
207 'indent=i',
208 'Indent for multiline qw() lists. Default: perltidy -i value',
209 ],
210 [],
211 [
212 'pad-brackets!',
213 'Pad square brackets in import lists: [ qw( foo ) ] vs [qw( foo )]. Defaults to false',
214 ],
215 [],
216 [
217 'padding!',
218 'Pad imports: qw( foo bar ) vs qw(foo bar). Defaults to true',
219 ],
220 [],
221 [
222 'read-stdin',
223 'Read statements to process from STDIN rather than the supplied file.',
224 ],
225 [],
226 [
227 'sort!',
228 '(Experimental) Sort contiguous include statements. Defaults to false.',
229 ],
230 [],
231 [
232 'preserve-duplicates!',
233 'Preserve duplicate use statements for the same module. This is the default behaviour. You are encouraged to disable it.',
234 ],
235 [],
236 [
237 'preserve-require!',
238 'Preserve require statements rather than translating them to "use Module ();". This is the default behaviour.',
239 ],
240 [],
241 [
242 'preserve-unused!',
243 'Preserve use statements for modules which appear to be unused. This is the default behaviour. You are encouraged to disable it.',
244 ],
245 [],
246 [
247 'range-begin=i',
248 'Experimental. First line of range to tidy or lint. Mostly useful for editors.',
249 ],
250 [],
251 [
252 'range-end=i',
253 'Experimental. Last line of range to tidy or lint. Mostly useful for editors.',
254 ],
255 [],
256 [
257 'tidy-whitespace!',
258 'Reformat use statements even when changes are only whitespace. This is the default behaviour.',
259 ],
260 [],
261 [],
262 [ 'version', 'Print installed version', { shortcircuit => 1 } ],
263 [
264 'log-level|l=s', 'Print messages to STDERR',
265 ],
266 [
267 'log-filename=s', 'Log messages to file rather than STDERR',
268 ],
269 [ 'help', 'Print usage message and exit', { shortcircuit => 1 } ],
270 [
271 'verbose-help', 'Print usage message and documentation ',
272 { shortcircuit => 1 }
273 ],
274 );
275  
276 15 return { opts => $opt, usage => $usage, };
277 }
278  
279 9 ▶sub _build_config {
Subroutine _build_config: called CC=10 SCAR=23.0
280 9 my $self = shift;
281 9 my %config;
282 9 ▶ if ( !$self->_opts->no_config_file && $self->_config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_opts->no_config_file and $self->_config_file) 7 1 8
Condition100%3/3
not $self->_opts->no_config_file and $self->_config_file
!ll&&!rl&&r
117
MC/DC50%1/2
not $self->_opts->no_config_file and $self->_config_file
not $self->_opts->no_config_file $self->_config_file
Truth table66%2/3
not(A) and B
A= not $self->_opts->no_config_fileB= $self->_config_file
ABresult
0X0
100
111
283 7 %config = %{ $self->_read_config_file };
284  
285 # The Bool type provided by Types::Standard doesn't seem to like
286 # JSON::PP::Boolean
287 7 for my $key ( keys %config ) {
288 98 my $maybe_bool = $config{$key};
289 98 my $ref = ref $maybe_bool;
290 98 ▶ next unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 35 63 98
291  
292 63 ▶ if ( $ref eq 'JSON::PP::Boolean'
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "JSON::PP::Boolean" or $ref eq "Types::Serializer::Boolean") 42 21 63
Condition66%2/3
$ref eq "JSON::PP::Boolean" or $ref eq "Types::Serializer::Boolean"
l!l&&r!l&&!r
42021
MC/DC50%1/2
$ref eq "JSON::PP::Boolean" or $ref eq "Types::Serializer::Boolean"
$ref eq "JSON::PP::Boolean" $ref eq "Types::Serializer::Boolean"
Truth table66%2/3
A or B
A= $ref eq "JSON::PP::Boolean"B= $ref eq "Types::Serializer::Boolean"
ABresult
1X1
011
000
293 || $ref eq 'Types::Serializer::Boolean' ) {
294 42 ▶ $config{$key} = $$maybe_bool ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$maybe_bool ? : 14 28 42
295 }
296 }
297 }
298  
299 8 my @config_options = qw(
300 cache
301 ignore_modules_filename
302 ignore_modules_pattern
303 indent
304 log_filename
305 log_level
306 never_export_modules_filename
307 pad_brackets
308 padding
309 preserve_duplicates
310 preserve_require
311 preserve_unused
312 sort
313 tidy_whitespace
314 );
315 my @config_option_lists
316 8 = ( 'ignore_modules', 'libs', 'never_export_modules' );
317  
318 2 my %args = map { $_ => $self->_opts->$_ }
319 8 grep { defined $self->_opts->$_ } @config_options;
320  
321 8 for my $list (@config_option_lists) {
322 24 my $val = $self->_opts->$list;
323 24 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1 23 24
324 1 $args{$list} = [ split m{,}, $val ];
325 }
326 }
327 8 return App::perlimports::Config->new( %config, %args );
328 }
329  
330 8 ▶sub _build_config_file {
Subroutine _build_config_file: called CC=6 SCAR=20.2
331 8 my $self = shift;
332  
333 8 ▶ if ( $self->_opts->config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_opts->config_file) 2 6 8
334 2 ▶ if ( !-e $self->_opts->config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->_opts->config_file) 1 1 2
335 1 die $self->_opts->config_file . ' not found';
336 }
337 1 return $self->_opts->config_file;
338 }
339  
340 6 my @filenames = ( 'perlimports.toml', '.perlimports.toml', );
341  
342 6 for my $name (@filenames) {
343 6 ▶ return $name if -e $name;
Branch: 1 branch
BranchTrueFalseTotal
if -e $name 6 0 6
344 }
345  
346 0 require File::XDG;
347  
348 0 my $xdg_config = File::XDG->new( name => 'perlimports', api => 1 );
349 0 my $file = $xdg_config->config_home->child( $filenames[0] );
350 0 ▶ return -e $file ? "$file" : q{};
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 0 0 0
351 }
352  
353 7 ▶sub _read_config_file {
Subroutine _read_config_file: called CC=2 SCAR=7.0
354 7 my $self = shift;
355  
356 7 require TOML::Tiny;
357 7 my $config = TOML::Tiny::from_toml( path( $self->_config_file )->slurp );
358 7 ▶ return $config || {};
Condition50%1/2
$config || {}
l!l
70
MC/DC0%0/1
$config || {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
359 }
360  
361 ## no critic (Subroutines::ProhibitExcessComplexity)
362 15 ▶sub run {
Subroutine run: called CC=40 SCAR=53.3
Pod documented
363 15 my $self = shift;
364 15 my $opts = $self->_opts;
365  
366 15 ▶ ( print $VERSION, "\n" ) && return 0 if $opts->version;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->version 1 14 15
Condition60%3/5
$opts->version && (print($App::perlimports::CLI::VERSION, "\n") && (return 0))
!ll&&!rl&&r
1410
print $App::perlimports::CLI::VERSION, "\n" and return 0
!ll
01
MC/DC0%0/2
$opts->version && (print($App::perlimports::CLI::VERSION, "\n") && (return 0))
$opts->version print $App::perlimports::CLI::VERSION, "\n"
Truth table33%1/3
A && B
A= $opts->versionB= print $App::perlimports::CLI::VERSION, "\n"
ABresult
0X0
100
111
367 14 ▶ ( print $self->_usage->text ) && return 0 if $opts->help;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->help 2 12 14
Condition60%3/5
$opts->help && (print($self->_usage->text) && (return 0))
!ll&&!rl&&r
1220
print $self->_usage->text and return 0
!ll
02
MC/DC0%0/2
$opts->help && (print($self->_usage->text) && (return 0))
$opts->help print $self->_usage->text
Truth table33%1/3
A && B
A= $opts->helpB= print $self->_usage->text
ABresult
0X0
100
111
368  
369 12 ▶ if ( $opts->verbose_help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->verbose_help) 1 11 12
370 1 require Pod::Usage; ## no perlimports
371 1 my $fh = \*STDOUT;
372 1 Pod::Usage::pod2usage(
373 (
374 {
375 -exitval => 'NOEXIT',
376 -message => $self->_usage->text,
377 -output => $fh,
378 }
379 )
380 );
381 1 return 0;
382 }
383  
384 11 ▶ if ( $opts->create_config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->create_config_file) 2 9 11
385 2 my $exit_code = 0;
386 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
387 2 App::perlimports::Config->create_config(
388 $opts->create_config_file );
389 }
390 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
391 1 print STDERR $_, "\n";
392 1 $exit_code = 1;
393 2 };
394 2 return $exit_code;
395 }
396  
397 9 my $input;
398 my $selection;
399 9 my $tmp_file;
400  
401 9 ▶ if ( $self->_read_stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_read_stdin) 0 8 8
402 ## no critic (Variables::RequireInitializationForLocalVars)
403 0 local $/;
404 0 $input = <>;
405 0 ▶ if ( $opts->range_begin && $opts->range_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->range_begin and $opts->range_end) { } 0 0 0
Condition0%0/3
$opts->range_begin and $opts->range_end
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->range_begin and $opts->range_end
$opts->range_begin $opts->range_end
Truth table0%0/3
A and B
A= $opts->range_beginB= $opts->range_end
ABresult
0X0
100
111
406 0 $tmp_file = Path::Tiny->tempfile('perlimportsXXXXXXXX');
407 0 $tmp_file->spew($input);
408 0 my @lines = split( qr{\n}, $input );
409 0 my $end = $opts->range_end;
410 0 ▶ if ( $end > scalar @lines + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end > scalar @lines + 1) 0 0 0
411 0 $end = scalar @lines + 1;
412 }
413 0 $selection = join "\n",
414 @lines[ $opts->range_begin - 1 .. $end - 1 ];
415 }
416 else {
417 0 $selection = $input;
418 }
419 }
420  
421 8 unshift @INC, @{ $self->_config->libs };
422  
423 8 ▶ my $logger
Branch: 3 branches
BranchTrueFalseTotal
$self->_has_logger ? : 0 8 8
$self->_config->log_filename ? : 0 8 8
$self->_quiet ? : 0 8 8
424 = $self->_has_logger
425 ? $self->logger
426 : Log::Dispatch->new(
427 outputs => [
428 $self->_config->log_filename
429  
430 # --quiet only quiets the console (STDERR). A log file (configured
431 # via --log-filename or the config file) keeps its log level.
432 ? [
433 'File',
434 binmode => ':encoding(UTF-8)',
435 filename => $self->_config->log_filename,
436 min_level => $self->_config->log_level,
437 mode => '>>',
438 newline => 1,
439 ]
440 : [
441 'Screen',
442  
443 # --quiet suppresses noisy info/notice console output (e.g.
444 # the 🚀/📦 progress lines) but must still let genuine
445 # warnings and errors through -- including --lint failure
446 # diagnostics and --json output, which are logged at the
447 # error level. See GH #163.
448 min_level => $self->_quiet
449 ? 'warning'
450 : $self->_config->log_level,
451 newline => 1,
452 stderr => 1,
453 utf8 => 1,
454 ]
455 ]
456 );
457  
458 8 ▶ if ( $self->_json && !$self->_lint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_json and not $self->_lint) 0 8 8
Condition33%1/3
$self->_json and not $self->_lint
!ll&&!rl&&r
800
MC/DC0%0/2
$self->_json and not $self->_lint
$self->_json not $self->_lint
Truth table33%1/3
A and not(B)
A= $self->_jsonB= not $self->_lint
ABresult
0X0
100
111
459 0 $logger->error('--json can only be used with --lint');
460 0 return 1;
461 }
462  
463 8 ▶ if ( $self->_lint && $self->_inplace_edit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lint and $self->_inplace_edit) 0 8 8
Condition33%1/3
$self->_lint and $self->_inplace_edit
!ll&&!rl&&r
800
MC/DC0%0/2
$self->_lint and $self->_inplace_edit
$self->_lint $self->_inplace_edit
Truth table33%1/3
A and B
A= $self->_lintB= $self->_inplace_edit
ABresult
0X0
100
111
464 0 $logger->error('Cannot lint if inplace edit has been enabled');
465 0 return 1;
466 }
467  
468 8 ▶ if ( ( $opts->range_begin && !$opts->range_end )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->range_begin and not $opts->range_end or $opts->range_end and not $opts->range_begin) 0 8 8
Condition33%3/9
$opts->range_begin and not $opts->range_end or $opts->range_end and not $opts->range_begin
l!l&&r!l&&!r
008
$opts->range_begin and not $opts->range_end
!ll&&!rl&&r
800
$opts->range_end and not $opts->range_begin
!ll&&!rl&&r
800
MC/DC0%0/4
$opts->range_begin and not $opts->range_end or $opts->range_end and not $opts->range_begin
$opts->range_begin not $opts->range_end $opts->range_end not $opts->range_begin
Truth table14%1/7
A and not(B) or C and not(D)
A= $opts->range_beginB= not $opts->range_endC= $opts->range_endD= not $opts->range_begin
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
469 || ( $opts->range_end && !$opts->range_begin ) ) {
470 0 $logger->error('You must supply both range_begin and range_end');
471 0 return 1;
472 }
473  
474 8 ▶ if ( $opts->range_begin && !$self->_read_stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->range_begin and not $self->_read_stdin) 0 8 8
Condition33%1/3
$opts->range_begin and not $self->_read_stdin
!ll&&!rl&&r
800
MC/DC0%0/2
$opts->range_begin and not $self->_read_stdin
$opts->range_begin not $self->_read_stdin
Truth table33%1/3
A and not(B)
A= $opts->range_beginB= not $self->_read_stdin
ABresult
0X0
100
111
475 0 $logger->error(
476 'You must specify --read-stdin if you provide a range');
477 0 return 1;
478 }
479  
480 8 ▶ my @files = $tmp_file ? ("$tmp_file") : _filter_paths(
Branch: 2 branches
BranchTrueFalseTotal
$tmp_file ? : 0 8 8
$opts->filename ? : 5 3 8
481 $opts->filename ? $opts->filename : (),
482 @ARGV
483 );
484  
485 8 ▶ unless (@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 8 8
486 0 $logger->error(q{Mandatory parameter 'filename' missing});
487 0 $logger->error( $self->_usage->text );
488 0 return 1;
489 }
490  
491 my %doc_args = (
492 cache => $self->_config->cache,
493 8 ▶ @{ $self->_config->ignore }
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_config->ignore_pattern;} ? : 7 1 8
494 ? ( ignore_modules => $self->_config->ignore )
495 : (),
496 8 ▶ @{ $self->_config->ignore_pattern }
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_config->never_export;} ? : 0 8 8
497 ? ( ignore_modules_pattern => $self->_config->ignore_pattern )
498 : (),
499 8 ▶ @{ $self->_config->never_export }
Branch: 2 branches
BranchTrueFalseTotal
@{$self->_config->ignore;} ? : 0 8 8
$selection ? : 0 8 8
500 ? ( never_export_modules => $self->_config->never_export )
501 : (),
502 indent => $self->_config->indent,
503 json => $self->_json,
504 lint => $self->_lint,
505 logger => $logger,
506 pad_brackets => $self->_config->pad_brackets,
507 padding => $self->_config->padding,
508 preserve_duplicates => $self->_config->preserve_duplicates,
509 preserve_require => $self->_config->preserve_require,
510 preserve_unused => $self->_config->preserve_unused,
511 sort => $self->_config->sort,
512 tidy_whitespace => $self->_config->tidy_whitespace,
513 $selection ? ( selection => $selection ) : (),
514 );
515  
516 8 my $exit_code = 0;
517 FILENAME:
518 8 foreach my $filename (@files) {
519 8 ▶ if ( !path($filename)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (path($filename)->is_file) 1 7 8
520 1 $logger->error("$filename does not appear to be a file");
521 1 $logger->error( $self->_usage->text );
522 1 return 1;
523 }
524  
525 7 $logger->notice( '🚀 Starting file: ' . $filename );
526  
527 7 my $pi_doc = App::perlimports::Document->new(
528 %doc_args,
529 filename => $filename,
530 );
531  
532 # Capture STDOUT here so that 3rd party code printing to STDOUT doesn't get
533 # piped back into vim.
534 7 my ( $stdout, $tidied, $linter_success );
535  
536 7 ▶ if ( $self->_lint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lint) 0 7 7
537 ( $stdout, $linter_success ) = capture_stdout(
538 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
539 0 return $pi_doc->linter_success;
540 }
541 0 );
542 0 ▶ if ( $linter_success && !$self->_json && !$self->_quiet ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($linter_success and not $self->_json and not $self->_quiet) { } 0 0 0
elsif (not $linter_success) { } 0 0 0
Condition0%0/6
$linter_success and not $self->_json and not $self->_quiet
!ll&&!rl&&r
000
$linter_success and not $self->_json
!ll&&!rl&&r
000
MC/DC0%0/3
$linter_success and not $self->_json and not $self->_quiet
$linter_success not $self->_json not $self->_quiet
Truth table0%0/4
A and not(B) and not(C)
A= $linter_successB= not $self->_jsonC= not $self->_quiet
ABCresult
0XX0
10X0
1100
1111
543  
544 # The lint *result* goes to STDOUT so that STDERR is reserved
545 # for diagnostics and log messages. This keeps a clean run
546 # silent on STDERR, which matters for tools (e.g. precious)
547 # that treat any STDERR output as a failure. See GH #163.
548 0 print STDOUT $filename . " OK\n";
549 }
550 elsif ( !$linter_success ) {
551 0 $exit_code = 1;
552 }
553 0 next FILENAME;
554 }
555  
556 ( $stdout, $tidied ) = capture_stdout(
557 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
558 7 return $pi_doc->tidied_document;
559 }
560 7 );
561  
562 7 ▶ if ( $self->_read_stdin ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_read_stdin) { } 0 7 7
elsif ($self->_inplace_edit) { } 0 7 7
563 0 print STDOUT $tidied;
564 }
565 elsif ( $self->_inplace_edit ) {
566  
567 # append() with truncate, because spew() can change file permissions
568 0 path($filename)->append( { truncate => 1 }, $tidied );
569 }
570  
571 else {
572 7 print STDOUT $tidied;
573 }
574 }
575 7 return $exit_code;
576 }
577  
578 ## use critic
579  
580 8 ▶sub _filter_paths {
Subroutine _filter_paths: called CC=4 SCAR=16.6
581 8 my @paths = @_;
582 8 my @files;
583 8 my $rule = Path::Iterator::Rule->new->or(
584 Path::Iterator::Rule->new->perl_module,
585 Path::Iterator::Rule->new->perl_script,
586 Path::Iterator::Rule->new->perl_test,
587 );
588  
589 8 foreach my $path (@paths) {
590 8 ▶ if ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 8 8
591 0 my $iter = $rule->iter($path);
592 0 ▶ while ( defined( my $file = $iter->() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = &$iter())) 0 0 0
593 0 push @files, $file;
594 }
595 }
596 else {
597 8 push @files, $path;
598 }
599 }
600 8 return uniq @files;
601 }
602  
603 17 1;
604  
605 =pod
606  
607 =encoding UTF-8
608  
609 =head1 NAME
610  
611 App::perlimports::CLI - CLI arg parsing for C<perlimports>
612  
613 =head1 VERSION
614  
615 version 0.000067
616  
617 =head1 DESCRIPTION
618  
619 This module isn't really meant to provide a public interface.
620  
621 =head2 run()
622  
623 The method which will do the argument parsing and print out the results.
624  
625 =head1 AUTHOR
626  
627 Olaf Alders <olaf@wundercounter.com>
628  
629 =head1 COPYRIGHT AND LICENSE
630  
631 This software is copyright (c) 2020 by Olaf Alders.
632  
633 This is free software; you can redistribute it and/or modify it under
634 the same terms as the Perl 5 programming language system itself.
635  
636 =cut
637  
638 __END__
↑ Summary blib/lib/App/perlimports/Include.pm »