bin/tidyall

filter: Statementstmt 80.6% 75 / 93 Branchbran 50.0% 19 / 38 Conditioncond 31.8% 7 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 63.4% 113 / 178 CC 8 SCAR 13.2
CC
8
Cov
66%
handle_pipe
8.2
version
2.0
BEGIN
1.0
CRAP 3.8
« blib/lib/Code/TidyAll/Plugin/JSHint.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/PerlTidy.pm »
1 #!perl
2 8 ▶use Capture::Tiny qw(capture_merged);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use Code::TidyAll;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Path::Tiny qw(cwd path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = '0.83';
12  
13 8 my $usage = <<'EOF';
14 Usage: tidyall [options] [file] ...
15 See https://metacpan.org/module/tidyall for full documentation.
16  
17 Options:
18 -a, --all Process all files in project
19 -i, --ignore Ignore matching files (zglob syntax)
20 -g, --git Process all added/modified files according to git
21 -l, --list List each file along with the plugins it matches
22 -m, --mode Mode (e.g. "editor", "commit") - affects which plugins run
23 -p <path>, --pipe <path> Read from STDIN, output to STDOUT/STDERR
24 -r, --recursive Descend recursively into directories listed on command line
25 -j, --jobs Number of parallel jobs to run - default is 1
26 -s, --svn Process all added/modified files according to svn
27 -q, --quiet Suppress output except for errors
28 -v, --verbose Show extra output
29 -I I<path1,path2,...> Add one or more paths to @INC
30 --backup-ttl <duration> Amount of time before backup files can be purged
31 --check-only Just check each file, don't modify
32 --plugins <name> Explicitly run only the given plugins
33 --conf-file <path> Relative or absolute path to conf file
34 --conf-name <name> Conf file name to search for
35 --data-dir <path> Contains metadata, defaults to root/.tidyall.d
36 --iterations <count> Number of times to repeat each transform - default is 1
37 --no-backups Don't back up files before processing
38 --no-cache Don't cache last processed times
39 --no-cleanup Don't clean up the temporary files
40 --output-suffix <suffix> Suffix to add to tidied file
41 --refresh-cache Erase any existing cache info before processing each file
42 --root-dir Specify root directory explicitly
43 --tidyall-class <class> Subclass to use instead of Code::TidyAll
44 --version Show version
45 -h, --help Print help message
46 EOF
47  
48 sub version {
49 1 ▶ my $version = $Code::TidyAll::VERSION || 'unknown';
Subroutine version: called CC=2 SCAR=7.1
Condition50%1/2
$Code::TidyAll::VERSION || 'unknown'
l!l
10
MC/DC0%0/1
$Code::TidyAll::VERSION || 'unknown'
$Code::TidyAll::VERSION
Truth table50%1/2
A
A= $Code::TidyAll::VERSION
Aresult
11
00
50 1 print "tidyall $version on perl $] built for $Config{archname}\n";
51 1 exit;
52 }
53  
54 sub usage {
55 1 ▶ print $usage;
Subroutine usage: called CC=1 SCAR=0.0
56 1 exit;
57 }
58  
59 my (
60 8 %params,
61 $all_files,
62 $git_files,
63 $pipe,
64 $svn_files,
65 $conf_file,
66 $conf_name,
67 $iterations,
68 $inc_dirs,
69 $version,
70 $help,
71 );
72  
73 8 my @conf_names = Code::TidyAll->default_conf_names;
74  
75 GetOptions(
76 'a|all' => \$all_files,
77 'i|ignore=s@' => \$params{ignore},
78 'g|git' => \$git_files,
79 'l|list' => \$params{list_only},
80 'm|mode=s' => \$params{mode},
81 'p|pipe=s' => \$pipe,
82 'r|recursive' => \$params{recursive},
83 'j|jobs=i' => \$params{jobs},
84 's|svn' => \$svn_files,
85 'q|quiet' => \$params{quiet},
86 'v|verbose' => \$params{verbose},
87 'I=s' => \$inc_dirs,
88 'backup-ttl=i' => \$params{backup_ttl},
89 'check-only' => \$params{check_only},
90 'plugins=s@' => \$params{selected_plugins},
91 'conf-file=s' => \$conf_file,
92 'conf-name=s' => \$conf_name,
93 'data-dir=s' => \$params{data_dir},
94 'iterations=i' => \$iterations,
95 'no-backups' => \$params{no_backups},
96 'no-cache' => \$params{no_cache},
97 'no-cleanup' => \$params{no_cleanup},
98 'output-suffix=s' => \$params{output_suffix},
99 'refresh-cache' => \$params{refresh_cache},
100 'root-dir=s' => \$params{root_dir},
101 'tidyall-class=s' => \$params{tidyall_class},
102 8 ▶ 'version' => \$version,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("a|all", \$all_files, "i|ignore=s\@", \$params{"ignore"}, "g|git", \$git_files, "l|list", \$params{"list_only"}, "m|mode=s", \$params{"mode"}, "p|pipe=s", \$pipe, "r|recursive", \$params{"recursive"}, "j|jobs=i", \$params{"jobs"}, "s|svn", \$svn_files, "q|quiet", \$params{"quiet"}, "v|verbose", \$params{"verbose"}, "I=s", \$inc_dirs, "backup-ttl=i", \$params{"backup_ttl"}, "check-only", \$params{"check_only"}, "plugins=s\@", \$params{"selected_plugins"}, "conf-file=s", \$conf_file, "conf-name=s", \$conf_name, "data-dir=s", \$params{"data_dir"}, "iterations=i", \$iterations, "no-backups", \$params{"no_backups"}, "no-cache", \$params{"no_cache"}, "no-cleanup", \$params{"no_cleanup"}, "output-suffix=s", \$params{"output_suffix"}, "refresh-cache", \$params{"refresh_cache"}, "root-dir=s", \$params{"root_dir"}, "tidyall-class=s", \$params{"tidyall_class"}, "version", \$version, "h|help", \$help) or usage() 0 8 8
103 'h|help' => \$help,
104 ) or usage();
105 8 ▶version() if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 1 7 8
106 7 ▶usage() if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 6 7
107 6 ▶@conf_names = ($conf_name) if defined($conf_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf_name 0 6 6
108  
109 6 ▶unshift( @INC, split( /\s*,\s*/, $inc_dirs ) ) if defined($inc_dirs);
Branch: 1 branch
BranchTrueFalseTotal
if defined $inc_dirs 0 6 6
110  
111 6 %params = map { $_ => $params{$_} } grep { defined( $params{$_} ) } keys %params;
112 6 for my $key (qw( data_dir root_dir )) {
113 12 ▶ $params{$key} = path( $params{$key} ) if exists $params{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 0 12 12
114 }
115 6 ▶$params{iterations} = $iterations if $iterations;
Branch: 1 branch
BranchTrueFalseTotal
if $iterations 0 6 6
116  
117 0 ($conf_file) = ( grep { $_->is_file } map { $params{root_dir}->child($_) } @conf_names )
118 6 ▶ if $params{root_dir} && !$conf_file;
Branch: 1 branch
BranchTrueFalseTotal
if $params{"root_dir"} and not $conf_file 0 6 6
Condition33%1/3
$params{"root_dir"} and not $conf_file
!ll&&!rl&&r
600
MC/DC0%0/2
$params{"root_dir"} and not $conf_file
$params{"root_dir"} not $conf_file
Truth table33%1/3
A and not(B)
A= $params{"root_dir"}B= not $conf_file
ABresult
0X0
100
111
119  
120 6 ▶my $tidyall_class = $params{tidyall_class} || 'Code::TidyAll';
Condition50%1/2
$params{'tidyall_class'} || 'Code::TidyAll'
l!l
06
MC/DC0%0/1
$params{'tidyall_class'} || 'Code::TidyAll'
$params{'tidyall_class'}
Truth table50%1/2
A
A= $params{'tidyall_class'}
Aresult
11
00
121  
122 6 use_module($tidyall_class);
123  
124 6 my ( $ct, @paths );
125  
126 6 ▶if ($pipe) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pipe) { } 4 2 6
elsif ($all_files or $svn_files or $git_files) { } 0 2 2
elsif (@paths = map({path($_);} @ARGV)) { } 2 0 2
127 4 my $status = handle_pipe( path($pipe) );
128 4 exit($status);
129 }
130 ▶elsif ( ( $all_files || $svn_files || $git_files ) ) {
Condition33%2/6
$all_files or $svn_files or $git_files
l!l&&r!l&&!r
002
$all_files or $svn_files
l!l&&r!l&&!r
002
MC/DC0%0/3
$all_files or $svn_files or $git_files
$all_files $svn_files $git_files
Truth table0%0/4
A or B or C
A= $all_filesB= $svn_filesC= $git_files
ABCresult
1XX1
01X1
0011
0000
131 0 ▶ die 'cannot use filename(s) with -a/--all, -s/--svn, or -g/--git'
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
132 if @ARGV;
133 0 ▶ $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, cwd() );
Condition0%0/3
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, cwd())
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, cwd())
$conf_file $tidyall_class->find_conf_file(\@conf_names, cwd())
Truth table0%0/3
A ||= B
A= $conf_fileB= $tidyall_class->find_conf_file(\@conf_names, cwd())
ABresult
1X1
011
000
134 0 $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
135  
136 0 ▶ if ($all_files) {
Branch: 3 branches
BranchTrueFalseTotal
if ($all_files) { } 0 0 0
elsif ($svn_files) { } 0 0 0
elsif ($git_files) { } 0 0 0
137 0 @paths = $ct->find_matched_files;
138 }
139 elsif ($svn_files) {
140 0 require Code::TidyAll::SVN::Util;
141 0 @paths = Code::TidyAll::SVN::Util::svn_uncommitted_files( $ct->root_dir );
142 }
143 elsif ($git_files) {
144 0 require Code::TidyAll::Git::Util;
145 0 @paths = Code::TidyAll::Git::Util::git_modified_files( $ct->root_dir );
146 }
147 }
148 2 elsif ( @paths = map { path($_) } @ARGV ) {
149 2 ▶ $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, $paths[0]->parent );
Condition33%1/3
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
l!l&&r!l&&!r
020
MC/DC0%0/2
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
$conf_file $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
Truth table33%1/3
A ||= B
A= $conf_fileB= $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
ABresult
1X1
011
000
150 2 $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
151 }
152 else {
153 0 print "must pass -a/--all, -s/--svn, -g/--git, -p/--pipe, or filename(s)\n";
154 0 usage();
155 }
156  
157 2 my @results = $ct->process_paths(@paths);
158 2 ▶my $status = ( grep { $_->error } @results ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->error;} @results) ? : 0 2 2
159 2 exit($status);
160  
161 sub handle_pipe {
162 4 ▶ my ($pipe_filename) = @_;
Subroutine handle_pipe: called CC=7 SCAR=21.0
163  
164 4 $params{$_} = 1 for ( 'no_backups', 'no_cache', 'quiet' );
165 4 $params{$_} = 0 for ('verbose');
166  
167 4 ▶ $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, $pipe_filename->parent );
Condition33%1/3
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, $pipe_filename->parent)
l!l&&r!l&&!r
040
MC/DC0%0/2
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, $pipe_filename->parent)
$conf_file $tidyall_class->find_conf_file(\@conf_names, $pipe_filename->parent)
Truth table33%1/3
A ||= B
A= $conf_fileB= $tidyall_class->find_conf_file(\@conf_names, $pipe_filename->parent)
ABresult
1X1
011
000
168 4 my $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
169 4 my $source = do { local $/; <STDIN> };
170  
171 # Merge stdout and stderr and output all to stderr, so that stdout is
172 # dedicated to the tidied content
173 #
174 4 my $result;
175 my $output = capture_merged {
176 4 ▶ $result = $ct->process_source( $source, $ct->_small_path( $pipe_filename->absolute ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
177 4 };
178 4 print STDERR $output;
179  
180 4 ▶ if ( my $error = $result->error ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $error = $result->error) { } 2 2 4
elsif ($result->state eq 'no_match') { } 0 2 2
elsif ($result->state eq 'checked') { } 0 2 2
181 2 print $source; # Error already printed above
182 2 return 1;
183 }
184 elsif ( $result->state eq 'no_match' ) {
185 0 print $source;
186 0 print STDERR "No plugins apply for '$pipe' in config";
187 0 return 1;
188 }
189 elsif ( $result->state eq 'checked' ) {
190 0 print $source;
191 0 return 0;
192 }
193 else {
194 2 print $result->new_contents;
195 2 return 0;
196 }
197 }
198  
199 1;
200  
201 __END__
202  
203 =head1 NAME
204  
205 tidyall - Your all-in-one code tidier and validator
206  
207 =head1 SYNOPSIS
208  
209 # Create a tidyall.ini or .tidyallrc at the top of your project
210 #
211 [PerlTidy]
212 select = **/*.{pl,pm,t}
213 argv = -noll -it=2
214  
215 [PerlCritic]
216 select = lib/**/*.pm
217 ignore = lib/UtterHack.pm
218 argv = -severity 3
219  
220 # Process all files in the current project,
221 # look upwards from cwd for conf file
222 #
223 % tidyall -a
224  
225 # Process one or more specific files,
226 # look upwards from the first file for conf file
227 #
228 % tidyall file [file...]
229  
230 # Process a directory recursively
231 #
232 % tidyall -r dir
233  
234 =head1 DESCRIPTION
235  
236 There are a lot of great code tidiers and validators out there. C<tidyall>
237 makes them available from a single unified interface.
238  
239 You can run C<tidyall> on a single file or on an entire project hierarchy, and
240 configure which tidiers/validators are applied to which files. C<tidyall> will
241 back up files beforehand, and for efficiency will only consider files that have
242 changed since they were last processed.
243  
244 =head2 What's a tidier? What's a validator?
245  
246 A I<tidier> transforms a file so as to improve its appearance without changing
247 its semantics. Examples include L<perltidy>, L<podtidy> and
248 L<js-beautify|https://npmjs.org/package/js-beautify>.
249  
250 A I<validator> analyzes a file for some definition of correctness. Examples
251 include L<perlcritic>, L<podchecker> and L<jshint|http://www.jshint.com/>.
252  
253 Many tidiers are also validators, e.g. C<perltidy> will throw an error on badly
254 formed Perl.
255  
256 To use a tidier or validator with C<tidyall> it must have a corresponding
257 plugin class, usually under the prefix C<Code::TidyAll::Plugin::>. This
258 distribution comes with plugins for:
259  
260 =over
261  
262 =item *
263  
264 Perl: L<perlcritic|Code::TidyAll::Plugin::PerlCritic>,
265 L<perltidy|Code::TidyAll::Plugin::PerlTidy>,
266 L<perltidy-sweet|Code::TidyAll::Plugin::PerlTidySweet>
267  
268 =item *
269  
270 Pod: L<podchecker|Code::TidyAll::Plugin::PodChecker>,
271 L<podspell|Code::TidyAll::Plugin::PodSpell>,
272 L<podtidy|Code::TidyAll::Plugin::PodTidy>
273  
274 =item *
275  
276 Mason: L<masontidy|Code::TidyAll::Plugin::MasonTidy>
277  
278 =item *
279  
280 JavaScript: L<js-beautify|Code::TidyAll::Plugin::JSBeautify>,
281 L<jshint|Code::TidyAll::Plugin::JSHint>,
282 L<jslint|Code::TidyAll::Plugin::JSLint>
283  
284 =item *
285  
286 JSON: L<JSON|Code::TidyAll::Plugin::JSON>
287  
288 =item *
289  
290 CSS: L<cssunminifier|Code::TidyAll::Plugin::CSSUnminifier>
291  
292 =item *
293  
294 PHP: L<phpcs|Code::TidyAll::Plugin::PHPCodeSniffer>
295  
296 =item *
297  
298 Misc: L<Code::TidyAll::Plugin::SortLines>
299  
300 =back
301  
302 See L<Code::TidyAll::Plugin> for information about creating your own plugin.
303  
304 =head1 USING TIDYALL
305  
306 C<tidyall> works on a project basis, where a project is just a directory
307 hierarchy of files. svn or git working directories are typical examples of
308 projects.
309  
310 The top of the project is called the I<root directory>. In the root directory
311 you'll need a config file named C<tidyall.ini> or C<.tidyallrc>; it defines how
312 various tidiers and validators will be applied to the files in your project.
313  
314 C<tidyall> will find your root directory and config file automatically
315 depending on how you call it:
316  
317 =over
318  
319 =item C<< tidyall file [file...] >>
320  
321 C<tidyall> will search upwards from the first file for the conf file.
322  
323 =item C<< tidyall -p/--pipe file >>
324  
325 C<tidyall> will search upwards from the specified file for the conf file.
326  
327 =item C<< tidyall -a/--all >> or C<< tidyall -s/--svn >> or C<< tidyall -g/--git >>
328  
329 C<tidyall> will search upwards from the current working directory for the conf
330 file.
331  
332 =item C<< tidyall -a --root-dir dir >>
333  
334 C<tidyall> will expect to find the conf file in the specified root directory.
335  
336 =back
337  
338 You can also pass --conf-name to change the name that is searched for, or
339 --conf-file to specify an explicit path.
340  
341 =head1 CONFIGURATION
342  
343 The config file (C<tidyall.ini> or C<.tidyallrc>) is in L<Config::INI> format.
344 Here's a sample:
345  
346 ignore = **/*.bak
347  
348 [PerlTidy]
349 select = **/*.{pl,pm,t}
350 argv = -noll -it=2
351  
352 [PerlCritic]
353 select = lib/**/*.pm
354 ignore = lib/UtterHack.pm lib/OneTime/*.pm
355 argv = -severity 3
356  
357 [PodTidy]
358 select = lib/**/*.{pm,pod}
359  
360 In order, the four sections declare:
361  
362 =over
363  
364 =item *
365  
366 Always ignore C<*.bak> files.
367  
368 =item *
369  
370 Apply C<PerlTidy> with settings "-noll -it=2" to all *.pl, *.pm, and *.t files.
371  
372 =item *
373  
374 Apply C<PerlCritic> with severity 3 to all Perl modules somewhere underneath
375 "lib/", except for "lib/UtterHack.pm".
376  
377 =item *
378  
379 Apply C<PodTidy> with default settings to all *.pm and *.pod files underneath
380 "lib/".
381  
382 =back
383  
384 =head2 Standard configuration elements
385  
386 =over
387  
388 =item [class] or [class description]
389  
390 The header of each section refers to a tidyall I<plugin>. The name is
391 automatically prefixed with C<Code::TidyAll::Plugin::> unless it begins with a
392 '+', e.g.
393  
394 ; Uses plugin Code::TidyAll::Plugin::PerlTidy
395 ;
396 [PerlTidy]
397  
398 ; Uses plugin My::TidyAll::Plugin
399 ;
400 [+My::TidyAll::Plugin]
401  
402 You can also include an optional description after the class. The description
403 will be ignored and only the first word will be used for the plugin. This
404 allows you to a list a plugin more than once, with different configuration each
405 time. For example, two different C<PerlCritic> configurations:
406  
407 ; Be brutal on libraries
408 ;
409 [PerlCritic strict]
410 select = lib/**/*.pm
411 argv = --brutal
412  
413 ; but gentle on scripts
414 ;
415 [PerlCritic lenient]
416 select = bin/**/*.pl
417 argv = --gentle
418  
419 Warning: If you simply list the same plugin twice with no description (or the
420 same description), one of them will be silently ignored.
421  
422 =item select
423  
424 One or more L<File::Zglob> patterns, separated by whitespace or on multiple
425 lines, indicating which files to select. At least one is required. e.g.
426  
427 ; All .t and .pl somewhere under bin and t;
428 ; plus all .pm files directly under lib/Foo and lib/Bar
429 ;
430 select = {bin,t}/**/*.p[lm]
431 select = lib/{Foo,Bar}/*.pm
432  
433 ; All .txt files anywhere in the project
434 ;
435 select = **/*.txt
436  
437 The pattern is relative to the root directory and should have no leading slash.
438 All standard glob characters (C<*>, C<?>, C<[]>, C<{}>) will work; in addition,
439 C<**> can be used to represent zero or more directories. See L<File::Zglob>
440 documentation for more details.
441  
442 =item ignore
443  
444 One or more L<File::Zglob> patterns, separated by whitespace or on multiple
445 lines, indicating which files to ignore. This is optional and overrides
446 C<select>. e.g.
447  
448 ; All .pl files anywhere under bin, except bin/awful.pl or anywhere
449 ; under bin/tmp
450 ;
451 select = bin/**/*.pl
452 ignore = bin/awful.pl bin/tmp/**/*.pl
453  
454 Ignore patterns can also specified at the beginning of the file before any
455 plugin section was started, thus making them global. These ignores will be
456 applied for all plugins.
457  
458 =item shebang
459  
460 One or more words on multiple lines, indicating which shebang lines to accept.
461 This is optional and further filters C<select>. e.g.
462  
463 ; All files with no extension anywhere under bin that include a "perl" or
464 ; "perl5" shebang line.
465 select = bin/**/*
466 ignore = bin/**/*.*
467 shebang = perl
468 shebang = perl5
469  
470 =item only_modes
471  
472 A list of modes, separated by whitespace. e.g.
473  
474 only_modes = test cli
475  
476 The plugin will I<only> run if one of these modes is passed to C<tidyall> via
477 C<-m> or C<--mode>.
478  
479 =item except_modes
480  
481 A list of modes, separated by whitespace. e.g.
482  
483 except_modes = commit editor
484  
485 The plugin will I<not> run if one of these modes is passed to C<tidyall> via
486 C<-m> or C<--mode>.
487  
488 =item argv
489  
490 Many plugins (such as L<perltidy|Code::TidyAll::Plugin::PerlTidy>,
491 L<perlcritic|Code::TidyAll::Plugin::PerlCritic> and
492 L<podtidy|Code::TidyAll::Plugin::PodTidy>) take this option, which specifies
493 arguments to pass to the underlying command-line utility.
494  
495 =item weight
496  
497 This is an integer that is used to sort plugins. By default, tidier plugins run
498 first, then validator plugins, with each group sorted alphabetically.
499  
500 =back
501  
502 =head1 PLUGIN ORDER AND ATOMICITY
503  
504 If multiple plugins match a file, tidiers are applied before validators so that
505 validators are checking the final result. Within those two groups, the plugins
506 are applied in alphabetical order by plugin name/description.
507  
508 You can also explicitly set the weight of each plugin. By default, tidiers have
509 a weight of 50 and validators have a weight of 60. You can set the weight to
510 any integer to influence when the plugin runs.
511  
512 The application of multiple plugins is all-or-nothing. If an error occurs
513 during the application of any plugin, the file is not modified at all.
514  
515 =head1 COMMAND-LINE OPTIONS
516  
517 =over
518  
519 =item -a, --all
520  
521 Process all files. Does a recursive search for all files in the project
522 hierarchy, starting at the root, and processes any file that matches at least
523 one plugin in the configuration.
524  
525 =item -i, --ignore
526  
527 Ignore matching files. This uses zglob syntax. You can pass this option more
528 than once.
529  
530 =item -g, --git
531  
532 Process all added or modified files in the current git working directory.
533  
534 =item -l, --list
535  
536 List each file along with the list of plugins it matches (files without any
537 matches are skipped). Does not actually process any files and does not care
538 whether files are cached. Generally used with -a, -g, or -s. e.g.
539  
540 % tidyall -a -l
541 lib/CHI.pm (PerlCritic, PerlTidy, PodTidy)
542 lib/CHI/Benchmarks.pod (PodTidy)
543 lib/CHI/CacheObject.pm (PerlCritic, PerlTidy, PodTidy)
544  
545 =item -m, --mode
546  
547 Optional mode that can affect which plugins run. Defaults to C<cli>. See
548 L</MODES>.
549  
550 =item -p path, --pipe path
551  
552 Read content from STDIN and write the resulting content to STDOUT. If
553 successful, tidyall exits with status 0. If an error occurs, tidyall outputs
554 the error message to STDERR, I<mirrors the input content> to STDOUT with no
555 changes, and exits with status 1. The mirroring means that you can safely pipe
556 to your destination regardless of whether an error occurs.
557  
558 When specifying this option you must specify exactly one filename, relative or
559 absolute, which will be used to determine which plugins to apply and also where
560 the root directory and configuration file are. The file will not actually be
561 read and does need even need to exist.
562  
563 This option implies --no-backups and --no-cache (since there's no actual file)
564 and --quiet (since we don't want to mix extraneous output with the tidied
565 result).
566  
567 # Read from STDIN and write to STDOUT, with appropriate plugins
568 # for some/path.pl (which need not exist)
569 #
570 % tidyall --pipe some/path.pl
571  
572 =item -r, --recursive
573  
574 Recursively enter any directories listed on the command-line and process all
575 the files within. By default, directories encountered on the command-line will
576 generate a warning.
577  
578 =item -j, --jobs
579  
580 Specify how many jobs should run in parallel. By default, we only run 1, but if
581 you have multiple cores this should cause tidyall to run faster, especially on
582 larger code bases.
583  
584 =item -s, --svn
585  
586 Process all added or modified files in the current svn working directory.
587  
588 =item -q, --quiet
589  
590 Suppress output except for errors.
591  
592 =item -v, --verbose
593  
594 Show extra output.
595  
596 =item -I I<path1,path2,...>
597  
598 Add one or more library paths to @INC, like Perl's -I. Useful if
599 --tidyall-class or plugins are in an alternate lib directory.
600  
601 =item --backup-ttl I<duration>
602  
603 Amount of time before backup files can be purged. Can be a number of seconds or
604 any string recognized by L<Time::Duration::Parse>, e.g. "4h" or "1day".
605 Defaults to "1h".
606  
607 =item --check-only
608  
609 Instead of actually tidying files, check if each file is tidied (i.e. if its
610 tidied version is equal to its current version) and consider it an error if
611 not. This is used by L<Test::Code::TidyAll> and the
612 L<svn|Code::TidyAll::SVN::Precommit> and L<git|Code::TidyAll::Git::Precommit>
613 pre-commit hooks, for example, to enforce that you've tidied your files.
614  
615 =item --plugins I<name>
616  
617 Only run the specified plugins. The name should match the name given in the
618 config file exactly, including a leading "+" if one exists.
619  
620 This overrides the C<--mode> option.
621  
622 Note that plugins will still only run on files which match their C<select> and
623 C<ignore> configuration.
624  
625 =item --conf-file I<path>
626  
627 Specify relative or absolute path to conf file, instead of searching for it in
628 the usual way.
629  
630 =item --conf-name I<name>
631  
632 Specify a conf file name to search for instead of the defaults (C<tidyall.ini>
633 / C<.tidyallrc>).
634  
635 =item --data-dir I<path>
636  
637 Contains data like backups and cache. Defaults to root_dir/.tidyall.d
638  
639 =item --iterations I<count>
640  
641 Run each tidier transform I<count> times. Default is 1.
642  
643 In some cases (hopefully rare) the output from a tidier can be different if it
644 is applied multiple times. You may want to perform multiple iterations to make
645 sure the content "settles" into its final tidied form -- especially if the
646 tidiness is being enforced with a version-control hook or a test. Of course,
647 performance will suffer a little. You should rarely need to set this higher
648 than 2.
649  
650 This only affects tidiers, not validators; e.g.
651 L<perlcritic|Code::TidyAll::Plugin::PerlCritic> and
652 L<jshint|Code::TidyAll::Plugin::JSHint> would still only be run once.
653  
654 =item --no-backups
655  
656 Don't backup files before processing.
657  
658 =item --no-cache
659  
660 Don't cache last processed times; process all files every time. See also
661 C<--refresh-cache>.
662  
663 =item --no-cleanup
664  
665 Don't clean up temporary files.
666  
667 =item --output-suffix I<suffix>
668  
669 Suffix to add to a filename before outputting the modified version, e.g.
670 C<.tdy>. Default is none, which means overwrite the file.
671  
672 =item --refresh-cache
673  
674 Erase any existing cache info before processing each file, then write new cache
675 info. See also C<--no-cache>.
676  
677 =item --root-dir
678  
679 Specify root directory explicitly. Usually this is inferred from the specified
680 files or the current working directory.
681  
682 =item --tidyall-class I<class>
683  
684 Subclass to use instead of C<Code::TidyAll>.
685  
686 =item --version
687  
688 Show the version of L<Code::TidyAll> that this script invokes.
689  
690 =item -h, --help
691  
692 Print help message
693  
694 =back
695  
696 =head2 Specifying options in configuration
697  
698 Almost any command-line option can be specified at the top of the config file,
699 above the plugin sections. Replace dashes with underscores. e.g.
700  
701 backup_ttl = 4h
702 iterations = 2
703 tidyall_class = My::Code::TidyAll
704  
705 [PerlTidy]
706 select = **/*.{pl,pm,t}
707 argv = -noll -it=2
708  
709 ...
710  
711 If an option is passed in both places, the command-line takes precedence.
712  
713 =head3 inc
714  
715 You can specify C<inc> as a global configuration option outside of any plugin's
716 section. You can specify this more than once to include multiple directories.
717 Any directories you list here will be I<prepended> to C<@INC> before loading
718 plugins or a C<tidyall_class>
719  
720 =head1 EXIT STATUS
721  
722 C<tidyall> will exit with status 1 if any errors occurred while processing
723 files, and 0 otherwise.
724  
725 =head1 MODES
726  
727 You can use tidyall in a number of different contexts, and you may not want to
728 run all plugins in all of them.
729  
730 You can pass a mode to tidyall via C<-m> or C<--mode>, and then specify that
731 certain plugins should only be run in certain modes (via L</only_modes>) or
732 should be run in all but certain modes (via L</except_modes>).
733  
734 Examples of modes:
735  
736 =over
737  
738 =item *
739  
740 C<cli> - when invoking tidyall explicitly from the command-line with no mode
741 specified
742  
743 =item *
744  
745 C<editor> - when invoking from an editor
746  
747 =item *
748  
749 C<commit> - when using a commit hook like L<Code::TidyAll::SVN::Precommit> or
750 L<Code::TidyAll::Git::Precommit>
751  
752 =item *
753  
754 C<test> - when using L<Test::Code::TidyAll>
755  
756 =back
757  
758 Now since L<perlcritic|Code::TidyAll::Plugin::PerlCritic> is a bit
759 time-consuming, you might only want to run it during tests and explicit
760 command-line invocation:
761  
762 [PerlCritic]
763 select = lib/**/*.pm
764 only_modes = test cli
765 ...
766  
767 Or you could specify that it be run in all modes I<except> the editor:
768  
769 [PerlCritic]
770 select = lib/**/*.pm
771 except_modes = editor
772 ...
773  
774 If you specify neither C<only_modes> nor C<except_modes> for a plugin, then it
775 will always run.
776  
777 =head1 LAST-PROCESSED CACHE
778  
779 C<tidyall> keeps track of each file's signature after it was last processed. On
780 subsequent runs, it will only process a file if its signature has changed. The
781 cache is kept in files under the data dir.
782  
783 You can force a refresh of the cache with C<--refresh-cache>, or turn off the
784 behavior entirely with C<--no-cache>.
785  
786 =head1 BACKUPS
787  
788 C<tidyall> will backup each file before modifying it. The timestamped backups
789 are kept in a separate directory hierarchy under the data dir.
790  
791 Old backup files will be purged automatically as part of occasional C<tidyall>
792 runs. The duration specified in C<--backup-ttl> indicates both the minimum
793 amount of time backups should be kept, and the frequency that purges should be
794 run. It may be specified as "30m" or "4 hours" or any string acceptable to
795 L<Time::Duration::Parse>. It defaults to "1h" (1 hour).
796  
797 You can turn off backups with C<--no-backups>.
798  
799 =head1 "MISSING" PREREQS
800  
801 The C<Code::TidyAll> distribution intentionally does not depend on the prereqs
802 needed for each plugin. This means that if you want to use the
803 L<perltidy|Code::TidyAll::Plugin::PerlTidy>, you must install the L<Perl::Tidy>
804 module manually.
805  
806 =head1 RELATED TOOLS
807  
808 =over
809  
810 =item *
811  
812 L<etc/editors/tidyall.el|https://raw.github.com/autarch-code/perl-code-tidyall/master/etc/editors/tidyall.el>
813 and
814 L<etc/editors/tidyall.vim|https://raw.github.com/autarch-code/perl-code-tidyall/master/etc/editors/tidyall.vim>
815 in this distribution contains Emacs and Vim commands for running C<tidyall> on
816 the current buffer. You can assign this to the keystroke of your choice (e.g.
817 ctrl-t or ,t).
818  
819 =item *
820  
821 L<Code::TidyAll::SVN::Precommit> implements a subversion pre-commit hook that
822 checks if all files are tidied and valid according to C<tidyall>, and rejects
823 the commit if not.
824  
825 =item *
826  
827 L<Code::TidyAll::Git::Precommit> and L<Code::TidyAll::Git::Prereceive>
828 implement git pre-commit and pre-receive hooks, respectively, that check if all
829 files are tidied and valid according to C<tidyall>.
830  
831 =item *
832  
833 L<Test::Code::TidyAll> is a testing library to check that all the files in your
834 project are in a tidied and valid state.
835  
836 =back
837  
838 =head1 KNOWN BUGS
839  
840 =over
841  
842 =item *
843  
844 Does not yet work on Windows
845  
846 =back
847  
848 =head1 AUTHOR
849  
850 Jonathan Swartz
851  
852 =head1 ACKNOWLEDGMENTS
853  
854 Thanks to Jeff Thalhammer for helping me refine this API. Thanks to Jeff for
855 perlcritic, Steve Hancock for perltidy, and all the other authors of great open
856 source tidiers and validators.
857  
858 =cut
« blib/lib/Code/TidyAll/Plugin/JSHint.pm ↑ Summary blib/lib/Code/TidyAll/Plugin/PerlTidy.pm »