#!/home/grinnz/projects/cpandoc-browser/perls/5.40.2/bin/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 , --pipe 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 Add one or more paths to @INC --backup-ttl Amount of time before backup files can be purged --check-only Just check each file, don't modify --plugins Explicitly run only the given plugins --conf-file Relative or absolute path to conf file --conf-name Conf file name to search for --data-dir Contains metadata, defaults to root/.tidyall.d --iterations 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 to add to tidied file --refresh-cache Erase any existing cache info before processing each file --root-dir Specify root directory explicitly --tidyall-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 $/; }; # Merge stdout and stderr and output all to stderr, so that stdout is # dedicated to the tidied content # my $result; my $output = capture_merged { $result = $ct->process_source( $source, $ct->_small_path( $pipe_filename->absolute ) ); }; print STDERR $output; if ( my $error = $result->error ) { print $source; # Error already printed above return 1; } elsif ( $result->state eq 'no_match' ) { print $source; print STDERR "No plugins apply for '$pipe' in config"; return 1; } elsif ( $result->state eq 'checked' ) { print $source; return 0; } else { print $result->new_contents; return 0; } } 1; __END__ =head1 NAME tidyall - Your all-in-one code tidier and validator =head1 SYNOPSIS # Create a tidyall.ini or .tidyallrc at the top of your project # [PerlTidy] select = **/*.{pl,pm,t} argv = -noll -it=2 [PerlCritic] select = lib/**/*.pm ignore = lib/UtterHack.pm argv = -severity 3 # Process all files in the current project, # look upwards from cwd for conf file # % tidyall -a # Process one or more specific files, # look upwards from the first file for conf file # % tidyall file [file...] # Process a directory recursively # % tidyall -r dir =head1 DESCRIPTION There are a lot of great code tidiers and validators out there. C makes them available from a single unified interface. You can run C on a single file or on an entire project hierarchy, and configure which tidiers/validators are applied to which files. C will back up files beforehand, and for efficiency will only consider files that have changed since they were last processed. =head2 What's a tidier? What's a validator? A I transforms a file so as to improve its appearance without changing its semantics. Examples include L, L and L. A I analyzes a file for some definition of correctness. Examples include L, L and L. Many tidiers are also validators, e.g. C will throw an error on badly formed Perl. To use a tidier or validator with C it must have a corresponding plugin class, usually under the prefix C. This distribution comes with plugins for: =over =item * Perl: L, L, L =item * Pod: L, L, L =item * Mason: L =item * JavaScript: L, L, L =item * JSON: L =item * CSS: L =item * PHP: L =item * Misc: L =back See L for information about creating your own plugin. =head1 USING TIDYALL C works on a project basis, where a project is just a directory hierarchy of files. svn or git working directories are typical examples of projects. The top of the project is called the I. In the root directory you'll need a config file named C or C<.tidyallrc>; it defines how various tidiers and validators will be applied to the files in your project. C will find your root directory and config file automatically depending on how you call it: =over =item C<< tidyall file [file...] >> C will search upwards from the first file for the conf file. =item C<< tidyall -p/--pipe file >> C will search upwards from the specified file for the conf file. =item C<< tidyall -a/--all >> or C<< tidyall -s/--svn >> or C<< tidyall -g/--git >> C will search upwards from the current working directory for the conf file. =item C<< tidyall -a --root-dir dir >> C will expect to find the conf file in the specified root directory. =back You can also pass --conf-name to change the name that is searched for, or --conf-file to specify an explicit path. =head1 CONFIGURATION The config file (C or C<.tidyallrc>) is in L format. Here's a sample: ignore = **/*.bak [PerlTidy] select = **/*.{pl,pm,t} argv = -noll -it=2 [PerlCritic] select = lib/**/*.pm ignore = lib/UtterHack.pm lib/OneTime/*.pm argv = -severity 3 [PodTidy] select = lib/**/*.{pm,pod} In order, the four sections declare: =over =item * Always ignore C<*.bak> files. =item * Apply C with settings "-noll -it=2" to all *.pl, *.pm, and *.t files. =item * Apply C with severity 3 to all Perl modules somewhere underneath "lib/", except for "lib/UtterHack.pm". =item * Apply C with default settings to all *.pm and *.pod files underneath "lib/". =back =head2 Standard configuration elements =over =item [class] or [class description] The header of each section refers to a tidyall I. The name is automatically prefixed with C unless it begins with a '+', e.g. ; Uses plugin Code::TidyAll::Plugin::PerlTidy ; [PerlTidy] ; Uses plugin My::TidyAll::Plugin ; [+My::TidyAll::Plugin] You can also include an optional description after the class. The description will be ignored and only the first word will be used for the plugin. This allows you to a list a plugin more than once, with different configuration each time. For example, two different C configurations: ; Be brutal on libraries ; [PerlCritic strict] select = lib/**/*.pm argv = --brutal ; but gentle on scripts ; [PerlCritic lenient] select = bin/**/*.pl argv = --gentle Warning: If you simply list the same plugin twice with no description (or the same description), one of them will be silently ignored. =item select One or more L patterns, separated by whitespace or on multiple lines, indicating which files to select. At least one is required. e.g. ; All .t and .pl somewhere under bin and t; ; plus all .pm files directly under lib/Foo and lib/Bar ; select = {bin,t}/**/*.p[lm] select = lib/{Foo,Bar}/*.pm ; All .txt files anywhere in the project ; select = **/*.txt The pattern is relative to the root directory and should have no leading slash. All standard glob characters (C<*>, C, C<[]>, C<{}>) will work; in addition, C<**> can be used to represent zero or more directories. See L documentation for more details. =item ignore One or more L patterns, separated by whitespace or on multiple lines, indicating which files to ignore. This is optional and overrides C. e.g. ; All files with no extension anywhere under bin that include a "perl" or ; "perl5" shebang line. select = bin/**/* ignore = bin/**/*.* shebang = perl shebang = perl5 =item only_modes A list of modes, separated by whitespace. e.g. only_modes = test cli The plugin will I run if one of these modes is passed to C via C<-m> or C<--mode>. =item except_modes A list of modes, separated by whitespace. e.g. except_modes = commit editor The plugin will I run if one of these modes is passed to C via C<-m> or C<--mode>. =item argv Many plugins (such as L, L and L) take this option, which specifies arguments to pass to the underlying command-line utility. =item weight This is an integer that is used to sort plugins. By default, tidier plugins run first, then validator plugins, with each group sorted alphabetically. =back =head1 PLUGIN ORDER AND ATOMICITY If multiple plugins match a file, tidiers are applied before validators so that validators are checking the final result. Within those two groups, the plugins are applied in alphabetical order by plugin name/description. You can also explicitly set the weight of each plugin. By default, tidiers have a weight of 50 and validators have a weight of 60. You can set the weight to any integer to influence when the plugin runs. The application of multiple plugins is all-or-nothing. If an error occurs during the application of any plugin, the file is not modified at all. =head1 COMMAND-LINE OPTIONS =over =item -a, --all Process all files. Does a recursive search for all files in the project hierarchy, starting at the root, and processes any file that matches at least one plugin in the configuration. =item -i, --ignore Ignore matching files. This uses zglob syntax. You can pass this option more than once. =item -g, --git Process all added or modified files in the current git working directory. =item -l, --list List each file along with the list of plugins it matches (files without any matches are skipped). Does not actually process any files and does not care whether files are cached. Generally used with -a, -g, or -s. e.g. % tidyall -a -l lib/CHI.pm (PerlCritic, PerlTidy, PodTidy) lib/CHI/Benchmarks.pod (PodTidy) lib/CHI/CacheObject.pm (PerlCritic, PerlTidy, PodTidy) =item -m, --mode Optional mode that can affect which plugins run. Defaults to C. See L. =item -p path, --pipe path Read content from STDIN and write the resulting content to STDOUT. If successful, tidyall exits with status 0. If an error occurs, tidyall outputs the error message to STDERR, I to STDOUT with no changes, and exits with status 1. The mirroring means that you can safely pipe to your destination regardless of whether an error occurs. When specifying this option you must specify exactly one filename, relative or absolute, which will be used to determine which plugins to apply and also where the root directory and configuration file are. The file will not actually be read and does need even need to exist. This option implies --no-backups and --no-cache (since there's no actual file) and --quiet (since we don't want to mix extraneous output with the tidied result). # Read from STDIN and write to STDOUT, with appropriate plugins # for some/path.pl (which need not exist) # % tidyall --pipe some/path.pl =item -r, --recursive Recursively enter any directories listed on the command-line and process all the files within. By default, directories encountered on the command-line will generate a warning. =item -j, --jobs 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 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 Amount of time before backup files can be purged. Can be a number of seconds or any string recognized by L, 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 and the L and L pre-commit hooks, for example, to enforce that you've tidied your files. =item --plugins I 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