Code-TidyAll
view release on metacpan or search on metacpan
bin/tidyall view on Meta::CPAN
#!perl
use Capture::Tiny qw(capture_merged);
use Code::TidyAll;
use Config;
use Path::Tiny qw(cwd path);
use Module::Runtime qw( use_module );
use Getopt::Long;
use strict;
use warnings;
our $VERSION = '0.85';
my $usage = <<'EOF';
Usage: tidyall [options] [file] ...
See https://metacpan.org/module/tidyall for full documentation.
Options:
-a, --all Process all files in project
-i, --ignore Ignore matching files (zglob syntax)
-g, --git Process all added/modified files according to git
-l, --list List each file along with the plugins it matches
-m, --mode Mode (e.g. "editor", "commit") - affects which plugins run
-p <path>, --pipe <path> Read from STDIN, output to STDOUT/STDERR
-r, --recursive Descend recursively into directories listed on command line
-j, --jobs Number of parallel jobs to run - default is 1
-s, --svn Process all added/modified files according to svn
-q, --quiet Suppress output except for errors
-v, --verbose Show extra output
-I I<path1,path2,...> Add one or more paths to @INC
--backup-ttl <duration> Amount of time before backup files can be purged
--check-only Just check each file, don't modify
--plugins <name> Explicitly run only the given plugins
--conf-file <path> Relative or absolute path to conf file
--conf-name <name> Conf file name to search for
--data-dir <path> Contains metadata, defaults to root/.tidyall.d
--iterations <count> Number of times to repeat each transform - default is 1
--no-backups Don't back up files before processing
--no-cache Don't cache last processed times
--no-cleanup Don't clean up the temporary files
--output-suffix <suffix> Suffix to add to tidied file
--refresh-cache Erase any existing cache info before processing each file
--root-dir Specify root directory explicitly
--tidyall-class <class> Subclass to use instead of Code::TidyAll
--version Show version
-h, --help Print help message
EOF
sub version {
my $version = $Code::TidyAll::VERSION || 'unknown';
print "tidyall $version on perl $] built for $Config{archname}\n";
exit;
}
sub usage {
print $usage;
exit;
}
my (
%params,
$all_files,
$git_files,
$pipe,
$svn_files,
$conf_file,
$conf_name,
$iterations,
$inc_dirs,
$version,
$help,
);
my @conf_names = Code::TidyAll->default_conf_names;
Getopt::Long::Configure('no_ignore_case');
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();
version() if $version;
usage() if $help;
@conf_names = ($conf_name) if defined($conf_name);
unshift( @INC, split( /\s*,\s*/, $inc_dirs ) ) if defined($inc_dirs);
%params = map { $_ => $params{$_} } grep { defined( $params{$_} ) } keys %params;
for my $key (qw( data_dir root_dir )) {
$params{$key} = path( $params{$key} ) if exists $params{$key};
}
$params{iterations} = $iterations if $iterations;
($conf_file) = ( grep { $_->is_file } map { $params{root_dir}->child($_) } @conf_names )
if $params{root_dir} && !$conf_file;
my $tidyall_class = $params{tidyall_class} || 'Code::TidyAll';
use_module($tidyall_class);
my ( $ct, @paths );
if ($pipe) {
my $status = handle_pipe( path($pipe) );
exit($status);
}
elsif ( ( $all_files || $svn_files || $git_files ) ) {
die 'cannot use filename(s) with -a/--all, -s/--svn, or -g/--git'
if @ARGV;
$conf_file ||= $tidyall_class->find_conf_file( \@conf_names, cwd() );
$ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
if ($all_files) {
@paths = $ct->find_matched_files;
}
elsif ($svn_files) {
require Code::TidyAll::SVN::Util;
@paths = Code::TidyAll::SVN::Util::svn_uncommitted_files( $ct->root_dir );
}
elsif ($git_files) {
require Code::TidyAll::Git::Util;
@paths = Code::TidyAll::Git::Util::git_modified_files( $ct->root_dir );
}
}
elsif ( @paths = map { path($_) } @ARGV ) {
$conf_file ||= $tidyall_class->find_conf_file( \@conf_names, $paths[0]->parent );
$ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
}
else {
print "must pass -a/--all, -s/--svn, -g/--git, -p/--pipe, or filename(s)\n";
usage();
}
my @results = $ct->process_paths(@paths);
my $status = ( grep { $_->error } @results ) ? 1 : 0;
exit($status);
sub handle_pipe {
my ($pipe_filename) = @_;
$params{$_} = 1 for ( 'no_backups', 'no_cache', 'quiet' );
$params{$_} = 0 for ('verbose');
$conf_file ||= $tidyall_class->find_conf_file( \@conf_names, $pipe_filename->parent );
my $ct = $tidyall_class->new_from_conf_file( $conf_file, %params );
my $source = do { local $/; <STDIN> };
# Merge stdout and stderr and output all to stderr, so that stdout is
# dedicated to the tidied content
#
my $result;
my $output = capture_merged {
bin/tidyall view on Meta::CPAN
Specify how many jobs should run in parallel. By default, we only run 1, but if
you have multiple cores this should cause tidyall to run faster, especially on
larger code bases.
=item -s, --svn
Process all added or modified files in the current svn working directory.
=item -q, --quiet
Suppress output except for errors.
=item -v, --verbose
Show extra output.
=item -I I<path1,path2,...>
Add one or more library paths to @INC, like Perl's -I. Useful if
--tidyall-class or plugins are in an alternate lib directory.
=item --backup-ttl I<duration>
Amount of time before backup files can be purged. Can be a number of seconds or
any string recognized by L<Time::Duration::Parse>, e.g. "4h" or "1day".
Defaults to "1h".
=item --check-only
Instead of actually tidying files, check if each file is tidied (i.e. if its
tidied version is equal to its current version) and consider it an error if
not. This is used by L<Test::Code::TidyAll> and the
L<svn|Code::TidyAll::SVN::Precommit> and L<git|Code::TidyAll::Git::Precommit>
pre-commit hooks, for example, to enforce that you've tidied your files.
=item --plugins I<name>
Only run the specified plugins. The name should match the name given in the
config file exactly, including a leading "+" if one exists.
This overrides the C<--mode> option.
Note that plugins will still only run on files which match their C<select> and
C<ignore> configuration.
=item --conf-file I<path>
Specify relative or absolute path to conf file, instead of searching for it in
the usual way.
=item --conf-name I<name>
Specify a conf file name to search for instead of the defaults (C<tidyall.ini>
/ C<.tidyallrc>).
=item --data-dir I<path>
Contains data like backups and cache. Defaults to root_dir/.tidyall.d
=item --iterations I<count>
Run each tidier transform I<count> times. Default is 1.
In some cases (hopefully rare) the output from a tidier can be different if it
is applied multiple times. You may want to perform multiple iterations to make
sure the content "settles" into its final tidied form -- especially if the
tidiness is being enforced with a version-control hook or a test. Of course,
performance will suffer a little. You should rarely need to set this higher
than 2.
This only affects tidiers, not validators; e.g.
L<perlcritic|Code::TidyAll::Plugin::PerlCritic> and
L<jshint|Code::TidyAll::Plugin::JSHint> would still only be run once.
=item --no-backups
Don't backup files before processing.
=item --no-cache
Don't cache last processed times; process all files every time. See also
C<--refresh-cache>.
=item --no-cleanup
Don't clean up temporary files.
=item --output-suffix I<suffix>
Suffix to add to a filename before outputting the modified version, e.g.
C<.tdy>. Default is none, which means overwrite the file.
=item --refresh-cache
Erase any existing cache info before processing each file, then write new cache
info. See also C<--no-cache>.
=item --root-dir
Specify root directory explicitly. Usually this is inferred from the specified
files or the current working directory.
=item --tidyall-class I<class>
Subclass to use instead of C<Code::TidyAll>.
=item --version
Show the version of L<Code::TidyAll> that this script invokes.
=item -h, --help
Print help message
=back
=head2 Specifying options in configuration
Almost any command-line option can be specified at the top of the config file,
above the plugin sections. Replace dashes with underscores. e.g.
backup_ttl = 4h
iterations = 2
tidyall_class = My::Code::TidyAll
[PerlTidy]
select = **/*.{pl,pm,t}
argv = -noll -it=2
...
If an option is passed in both places, the command-line takes precedence.
=head3 inc
You can specify C<inc> as a global configuration option outside of any plugin's
section. You can specify this more than once to include multiple directories.
Any directories you list here will be I<prepended> to C<@INC> before loading
plugins or a C<tidyall_class>
=head1 EXIT STATUS
C<tidyall> will exit with status 1 if any errors occurred while processing
files, and 0 otherwise.
=head1 MODES
You can use tidyall in a number of different contexts, and you may not want to
run all plugins in all of them.
You can pass a mode to tidyall via C<-m> or C<--mode>, and then specify that
certain plugins should only be run in certain modes (via L</only_modes>) or
should be run in all but certain modes (via L</except_modes>).
Examples of modes:
=over
=item *
C<cli> - when invoking tidyall explicitly from the command-line with no mode
specified
=item *
C<editor> - when invoking from an editor
=item *
C<commit> - when using a commit hook like L<Code::TidyAll::SVN::Precommit> or
L<Code::TidyAll::Git::Precommit>
=item *
C<test> - when using L<Test::Code::TidyAll>
=back
Now since L<perlcritic|Code::TidyAll::Plugin::PerlCritic> is a bit
time-consuming, you might only want to run it during tests and explicit
command-line invocation:
[PerlCritic]
( run in 0.868 second using v1.01-cache-2.11-cpan-71847e10f99 )