Critique/Command/process.pm

filter: Statementstmt 14.5% 21 / 144 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 46.1% 6 / 13 Podpod 40.0% 2 / 5 totaltotal 14.0% 29 / 207 CC 24 SCAR 49.7
CC
24
Cov
11%
execute
272.0
edit_violation
90.0
opt_spec
2.0
CRAP 143.8
« Critique/Command/collect.pm ↑ Summary Critique/Command/init.pm »
1 package App::Critique::Command::process;
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  
6 1 our $VERSION = '0.03';
7 1 our $AUTHORITY = 'cpan:STEVAN';
8  
9 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Term::ANSIColor ':constants';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use App::Critique::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use App::Critique -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub opt_spec {
17 0 ▶ my ($class) = @_;
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
18 return (
19 0 [ 'reset', 'resets the file index to 0', { default => 0 } ],
20 [ 'back', 'back up and re-process the last file', { default => 0 } ],
21 [ 'next', 'skip over processing the current file ', { default => 0 } ],
22 [],
23 $class->SUPER::opt_spec
24 );
25 }
26  
27 sub execute {
28 0 ▶ my ($self, $opt, $args) = @_;
Subroutine execute: not called CC=16 SCAR=56.1
Pod documented
29  
30 0 local $Term::ANSIColor::AUTORESET = 1;
31  
32 0 my $session = $self->cautiously_load_session( $opt, $args );
33  
34 0 info('Session file loaded.');
35  
36 0 my @tracked_files = $session->tracked_files;
37  
38 0 ▶ if ( $opt->back ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->back) 0 0 0
39 0 $session->dec_file_idx;
40 0 $tracked_files[ $session->current_file_idx ]->forget_all;
41 }
42  
43 0 ▶ if ( $opt->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->next) 0 0 0
44 0 $session->inc_file_idx;
45 }
46  
47 0 ▶ if ( $opt->reset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->reset) 0 0 0
48 0 $session->reset_file_idx;
49 0 $_->forget_all foreach @tracked_files;
50 }
51  
52 0 ▶ if ( $session->current_file_idx == scalar @tracked_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->current_file_idx == scalar @tracked_files) 0 0 0
53 0 info(HR_DARK);
54 0 info('All files have already been processed.');
55 0 info(HR_LIGHT);
56 0 info('- run `critique status` to see more information');
57 0 info('- run `critique process --reset` to review all files again');
58 0 info(HR_DARK);
59 0 return;
60 }
61  
62 0 my ($idx, $file);
63  
64 MAIN:
65 0 while (1) {
66  
67 0 info(HR_DARK);
68  
69 0 $idx = $session->current_file_idx;
70 0 $file = $tracked_files[ $idx ];
71  
72 0 my $path = $file->relative_path( $session->git_work_tree );
73  
74 0 info('Running Perl::Critic against (%s)', $path);
75 0 info(HR_LIGHT);
76  
77 0 my @violations = $self->discover_violations( $session, $file, $opt );
78  
79 # remember it the first time we use it
80 # but do not update it for each re-process
81 # which we do after each edit
82 0 ▶ $file->remember('violations' => scalar @violations)
Branch: 1 branch
BranchTrueFalseTotal
unless $file->recall("violations") 0 0 0
83 unless $file->recall('violations');
84  
85 0 ▶ if ( @violations == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@violations == 0) { } 0 0 0
86 0 info(ITALIC('No violations found, proceeding to next file.'));
87 0 next MAIN;
88 }
89 else {
90 0 my $should_review = prompt_yn(
91 BOLD(sprintf 'Found %d violations, would you like to review them?', (scalar @violations)),
92 { default => 'y' }
93 );
94  
95 0 ▶ if ( $should_review ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_review) 0 0 0
96  
97 0 ▶ my ($reviewed, $edited) = (
Condition0%0/4
$file->recall("reviewed") // 0
l!l
00
$file->recall("edited") // 0
l!l
00
MC/DC0%0/1
$file->recall("reviewed") // 0
$file->recall("reviewed")
MC/DC0%0/1
$file->recall("edited") // 0
$file->recall("edited")
Truth table0%0/2
A
A= $file->recall("reviewed")
Aresult
11
00
Truth table0%0/2
A
A= $file->recall("edited")
Aresult
11
00
98 $file->recall('reviewed') // 0,
99 $file->recall('edited') // 0,
100 );
101  
102 0 foreach my $violation ( @violations ) {
103  
104 0 $self->display_violation( $session, $file, $violation, $opt );
105 0 $reviewed++;
106  
107 0 my $should_edit = prompt_yn(
108 BOLD('Would you like to fix this violation?'),
109 { default => 'y' }
110 );
111  
112 0 my $did_commit = 0;
113  
114 0 ▶ if ( $should_edit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_edit) 0 0 0
115 0 $did_commit = $self->edit_violation( $session, $file, $violation );
116 0 ▶ $edited++ if $did_commit;
Branch: 1 branch
BranchTrueFalseTotal
if $did_commit 0 0 0
117 }
118  
119 # keep state on disc ...
120 0 $file->remember('reviewed', $reviewed);
121 0 $file->remember('edited', $edited);
122 0 $self->cautiously_store_session( $session, $opt, $args );
123  
124 0 ▶ if ( $did_commit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_commit) 0 0 0
125 0 info(HR_LIGHT);
126 0 info('File was edited, re-processing is required');
127 0 redo MAIN;
128 }
129 }
130 }
131 }
132  
133 } continue {
134  
135 0 $session->inc_file_idx;
136 0 $self->cautiously_store_session( $session, $opt, $args );
137  
138 0 ▶ if ( ($idx + 1) == scalar @tracked_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx + 1 == scalar @tracked_files) 0 0 0
139 0 info(HR_LIGHT);
140 0 info('Processing complete, run `status` to see results.');
141 0 last MAIN;
142 }
143  
144 }
145  
146 }
147  
148 sub discover_violations {
149 0 ▶ my ($self, $session, $file, $opt) = @_;
Subroutine discover_violations: not called CC=1 SCAR=6.9
Pod undocumented
150  
151 0 my @violations = $session->perl_critic->critique( $file->path->stringify );
152  
153 0 return @violations;
154 }
155  
156  
157 sub display_violation {
158 0 ▶ my ($self, $session, $file, $violation, $opt) = @_;
Subroutine display_violation: not called CC=1 SCAR=6.9
Pod undocumented
159 0 info(HR_DARK);
160 0 info(BOLD('Violation: %s'), $violation->description);
161 0 info(HR_DARK);
162 0 info('%s', $violation->explanation);
163 #if ( $opt->verbose ) {
164 # info(HR_LIGHT);
165 # info('%s', $violation->diagnostics);
166 #}
167 0 info(HR_LIGHT);
168 0 info(' policy : %s' => $violation->policy);
169 0 info(' severity : %d' => $violation->severity);
170 0 info(' location : %s @ <%d:%d>' => (
171 Path::Tiny::path( $violation->filename )->relative( $session->git_work_tree ),
172 $violation->line_number,
173 $violation->column_number
174 ));
175 0 info(HR_LIGHT);
176 0 info(ITALIC('%s'), $violation->source);
177 0 info(HR_LIGHT);
178 }
179  
180 sub edit_violation {
181 0 ▶ my ($self, $session, $file, $violation) = @_;
Subroutine edit_violation: not called CC=9 SCAR=45.0
Pod undocumented
182  
183 0 my $git = $session->git_wrapper;
184 0 my $filename = $violation->filename;
185  
186 0 my $cmd_fmt = $App::Critique::CONFIG{EDITOR};
187 0 my @cmd_args = (
188 $filename,
189 $violation->line_number,
190 $violation->column_number
191 );
192  
193 0 my $cmd = sprintf $cmd_fmt => @cmd_args;
194  
195 0 EDIT:
196 system $cmd;
197  
198 0 my $statuses = $git->status;
199 0 my @changed = $statuses->get('changed');
200 0 my $did_edit = scalar grep { my $from = $_->from; $filename =~ /$from/ } @changed;
201  
202 0 ▶ if ( $did_edit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_edit) { } 0 0 0
203 0 info(HR_DARK);
204 0 info('Changes detected, generating diff.');
205 0 info(HR_LIGHT);
206 0 info('%s', join "\n" => $git->diff);
207  
208 0 my $policy_name = $violation->policy;
209 0 $policy_name =~ s/^Perl\:\:Critic\:\:Policy\:\://;
210  
211 0 my $commit_msg = sprintf "%s - critique(%s)" => $violation->description, $policy_name;
212  
213 0 CHOOSE:
214  
215 info(HR_LIGHT);
216 0 my $commit_this_change = prompt_yn(
217 (
218 BOLD('Commit Message:').
219 "\n\n $commit_msg\n\n".
220 BOLD('Choose (y)es to use this message, or (n)o for more options')
221 ),
222 { default => 'y' }
223 );
224  
225 0 ▶ if ( $commit_this_change ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_this_change) { } 0 0 0
226 0 info(HR_DARK);
227 0 info('Adding and commiting file (%s) to git', $filename);
228 0 info(HR_LIGHT);
229 0 info('%s', join "\n" => $git->add($filename, { v => 1 }));
230 0 info('%s', join "\n" => $git->commit({ v => 1, message => $commit_msg }));
231  
232 0 my ($sha) = $git->rev_parse('HEAD');
233  
234 0 ▶ $file->remember('shas' => [ @{ $file->recall('shas') || [] }, $sha ]);
Branch: 1 branch
BranchTrueFalseTotal
$file->recall("shas") or [] 0 0 0
235 0 ▶ $file->remember('commited' => ($file->recall('commited') || 0) + 1);
Condition0%0/2
$file->recall('commited') || 0
l!l
00
MC/DC0%0/1
$file->recall('commited') || 0
$file->recall('commited')
Truth table0%0/2
A
A= $file->recall('commited')
Aresult
11
00
236  
237 0 return 1;
238 }
239 else {
240 0 info(HR_LIGHT);
241 my $what_now = prompt_str(
242 BOLD('What would you like to edit? (f)ile, (c)ommit message'),
243 0 ▶ { valid => sub { $_[0] =~ m/[fc]{1}/ } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 0 );
245  
246 0 ▶ if ( $what_now eq 'c' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($what_now eq 'c') { } 0 0 0
elsif ($what_now eq 'f') { } 0 0 0
247 0 info(HR_LIGHT);
248 0 $commit_msg = prompt_str( BOLD('Please write a commit message: ') );
249 0 goto CHOOSE;
250 }
251 elsif ( $what_now eq 'f' ) {
252 0 goto EDIT;
253 }
254 }
255 }
256 else {
257 0 info(HR_LIGHT);
258 my $what_now = prompt_str(
259 BOLD('No edits found, would like to (e)dit again, or (s)kip this file?'),
260 0 ▶ { valid => sub { $_[0] =~ m/[es]{1}/ } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 );
262  
263 0 ▶ if ( $what_now eq 'e' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($what_now eq 'e') { } 0 0 0
elsif ($what_now eq 's') { } 0 0 0
264 0 goto EDIT;
265 }
266 elsif ( $what_now eq 's' ) {
267 0 return 0;
268 }
269 }
270  
271 0 return 0;
272 }
273  
274 1 1;
275  
276 =pod
277  
278 =head1 NAME
279  
280 App::Critique::Command::process - Critique all the files.
281  
282 =head1 VERSION
283  
284 version 0.03
285  
286 =head1 DESCRIPTION
287  
288 This command will start or resume the critique session, allowing you to
289 step through the files and critique them. This current state of this
290 processing will be stored in the critique session file and so can be
291 stopped and resumed at any time.
292  
293 Note, this is an interactive command, so ...
294  
295 =head1 AUTHOR
296  
297 Stevan Little <stevan@cpan.org>
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 This software is copyright (c) 2016 by Stevan Little.
302  
303 This is free software; you can redistribute it and/or modify it under
304 the same terms as the Perl 5 programming language system itself.
305  
306 =cut
307  
308 __END__
309  
310 # ABSTRACT: Critique all the files.
311  
« Critique/Command/collect.pm ↑ Summary Critique/Command/init.pm »