| 1 | #!perl | ||||||||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use Capture::Tiny qw(capture_merged); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use Code::TidyAll; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use Config; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Path::Tiny qw(cwd path); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 12 | our $VERSION = '0.85'; | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 12 | 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 | 1 | ▶ | sub version { | ||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | my $version = $Code::TidyAll::VERSION || 'unknown'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Code::TidyAll::VERSION || 'unknown'
MC/DC0%0/1
$Code::TidyAll::VERSION || 'unknown'
$Code::TidyAll::VERSION
Truth table50%1/2
A
A= $Code::TidyAll::VERSION
| |||||||||||||||||||||||||||||||||||
| 50 | 1 | print "tidyall $version on perl $] built for $Config{archname}\n"; | |||||||||||||||||||||||||||||||||
| 51 | 1 | exit; | |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 55 | 1 | print $usage; | |||||||||||||||||||||||||||||||||
| 56 | 1 | exit; | |||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | my ( | ||||||||||||||||||||||||||||||||||
| 60 | 12 | %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 | 12 | my @conf_names = Code::TidyAll->default_conf_names; | |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 12 | Getopt::Long::Configure('no_ignore_case'); | |||||||||||||||||||||||||||||||||
| 76 | GetOptions( | ||||||||||||||||||||||||||||||||||
| 77 | 'a|all' => \$all_files, | ||||||||||||||||||||||||||||||||||
| 78 | 'i|ignore=s@' => \$params{ignore}, | ||||||||||||||||||||||||||||||||||
| 79 | 'g|git' => \$git_files, | ||||||||||||||||||||||||||||||||||
| 80 | 'l|list' => \$params{list_only}, | ||||||||||||||||||||||||||||||||||
| 81 | 'm|mode=s' => \$params{mode}, | ||||||||||||||||||||||||||||||||||
| 82 | 'p|pipe=s' => \$pipe, | ||||||||||||||||||||||||||||||||||
| 83 | 'r|recursive' => \$params{recursive}, | ||||||||||||||||||||||||||||||||||
| 84 | 'j|jobs=i' => \$params{jobs}, | ||||||||||||||||||||||||||||||||||
| 85 | 's|svn' => \$svn_files, | ||||||||||||||||||||||||||||||||||
| 86 | 'q|quiet' => \$params{quiet}, | ||||||||||||||||||||||||||||||||||
| 87 | 'v|verbose' => \$params{verbose}, | ||||||||||||||||||||||||||||||||||
| 88 | 'I=s' => \$inc_dirs, | ||||||||||||||||||||||||||||||||||
| 89 | 'backup-ttl=i' => \$params{backup_ttl}, | ||||||||||||||||||||||||||||||||||
| 90 | 'check-only' => \$params{check_only}, | ||||||||||||||||||||||||||||||||||
| 91 | 'plugins=s@' => \$params{selected_plugins}, | ||||||||||||||||||||||||||||||||||
| 92 | 'conf-file=s' => \$conf_file, | ||||||||||||||||||||||||||||||||||
| 93 | 'conf-name=s' => \$conf_name, | ||||||||||||||||||||||||||||||||||
| 94 | 'data-dir=s' => \$params{data_dir}, | ||||||||||||||||||||||||||||||||||
| 95 | 'iterations=i' => \$iterations, | ||||||||||||||||||||||||||||||||||
| 96 | 'no-backups' => \$params{no_backups}, | ||||||||||||||||||||||||||||||||||
| 97 | 'no-cache' => \$params{no_cache}, | ||||||||||||||||||||||||||||||||||
| 98 | 'no-cleanup' => \$params{no_cleanup}, | ||||||||||||||||||||||||||||||||||
| 99 | 'output-suffix=s' => \$params{output_suffix}, | ||||||||||||||||||||||||||||||||||
| 100 | 'refresh-cache' => \$params{refresh_cache}, | ||||||||||||||||||||||||||||||||||
| 101 | 'root-dir=s' => \$params{root_dir}, | ||||||||||||||||||||||||||||||||||
| 102 | 'tidyall-class=s' => \$params{tidyall_class}, | ||||||||||||||||||||||||||||||||||
| 103 | 12 | ▶ | 'version' => \$version, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 'h|help' => \$help, | ||||||||||||||||||||||||||||||||||
| 105 | ) or usage(); | ||||||||||||||||||||||||||||||||||
| 106 | 12 | ▶ | version() if $version; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 11 | ▶ | usage() if $help; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 108 | 10 | ▶ | @conf_names = ($conf_name) if defined($conf_name); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 10 | ▶ | unshift( @INC, split( /\s*,\s*/, $inc_dirs ) ) if defined($inc_dirs); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 10 | %params = map { $_ => $params{$_} } grep { defined( $params{$_} ) } keys %params; | |||||||||||||||||||||||||||||||||
| 113 | 10 | for my $key (qw( data_dir root_dir )) { | |||||||||||||||||||||||||||||||||
| 114 | 20 | ▶ | $params{$key} = path( $params{$key} ) if exists $params{$key}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | 10 | ▶ | $params{iterations} = $iterations if $iterations; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 0 | ($conf_file) = ( grep { $_->is_file } map { $params{root_dir}->child($_) } @conf_names ) | |||||||||||||||||||||||||||||||||
| 119 | 10 | ▶ | if $params{root_dir} && !$conf_file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$params{"root_dir"} and not $conf_file
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
| |||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 10 | ▶ | my $tidyall_class = $params{tidyall_class} || 'Code::TidyAll'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'tidyall_class'} || 'Code::TidyAll'
MC/DC0%0/1
$params{'tidyall_class'} || 'Code::TidyAll'
$params{'tidyall_class'}
Truth table50%1/2
A
A= $params{'tidyall_class'}
| |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 10 | use_module($tidyall_class); | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 10 | my ( $ct, @paths ); | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 10 | ▶ | if ($pipe) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 128 | 4 | my $status = handle_pipe( path($pipe) ); | |||||||||||||||||||||||||||||||||
| 129 | 4 | exit($status); | |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | ▶ | elsif ( ( $all_files || $svn_files || $git_files ) ) { | |||||||||||||||||||||||||||||||||
|
Condition33%2/6
$all_files or $svn_files or $git_files
$all_files or $svn_files
MC/DC0%0/3
$all_files or $svn_files or $git_files
$all_files
$svn_files
$git_files
Truth table25%1/4
A or B or C
A= $all_filesB= $svn_filesC= $git_files
| |||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | die 'cannot use filename(s) with -a/--all, -s/--svn, or -g/--git' | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | if @ARGV; | ||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, cwd() ); | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, cwd())
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())
| |||||||||||||||||||||||||||||||||||
| 135 | 0 | $ct = $tidyall_class->new_from_conf_file( $conf_file, %params ); | |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($all_files) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 138 | 0 | @paths = $ct->find_matched_files; | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | elsif ($svn_files) { | ||||||||||||||||||||||||||||||||||
| 141 | 0 | require Code::TidyAll::SVN::Util; | |||||||||||||||||||||||||||||||||
| 142 | 0 | @paths = Code::TidyAll::SVN::Util::svn_uncommitted_files( $ct->root_dir ); | |||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||
| 144 | elsif ($git_files) { | ||||||||||||||||||||||||||||||||||
| 145 | 0 | require Code::TidyAll::Git::Util; | |||||||||||||||||||||||||||||||||
| 146 | 0 | @paths = Code::TidyAll::Git::Util::git_modified_files( $ct->root_dir ); | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | 6 | elsif ( @paths = map { path($_) } @ARGV ) { | |||||||||||||||||||||||||||||||||
| 150 | 6 | ▶ | $conf_file ||= $tidyall_class->find_conf_file( \@conf_names, $paths[0]->parent ); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$conf_file ||= $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
MC/DC50%1/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 table66%2/3
A ||= B
A= $conf_fileB= $tidyall_class->find_conf_file(\@conf_names, $paths[0]->parent)
| |||||||||||||||||||||||||||||||||||
| 151 | 4 | $ct = $tidyall_class->new_from_conf_file( $conf_file, %params ); | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||||||||||||||||
| 154 | 0 | print "must pass -a/--all, -s/--svn, -g/--git, -p/--pipe, or filename(s)\n"; | |||||||||||||||||||||||||||||||||
| 155 | 0 | usage(); | |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 4 | my @results = $ct->process_paths(@paths); | |||||||||||||||||||||||||||||||||
| 159 | 4 | ▶ | my $status = ( grep { $_->error } @results ) ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | 4 | exit($status); | |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | 4 | ▶ | sub handle_pipe { | ||||||||||||||||||||||||||||||||
|
Subroutine
handle_pipe: called CC=7 SCAR=21.0
| |||||||||||||||||||||||||||||||||||
| 163 | 4 | my ($pipe_filename) = @_; | |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | 4 | $params{$_} = 1 for ( 'no_backups', 'no_cache', 'quiet' ); | |||||||||||||||||||||||||||||||||
| 166 | 4 | $params{$_} = 0 for ('verbose'); | |||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | 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)
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)
| |||||||||||||||||||||||||||||||||||
| 169 | 4 | my $ct = $tidyall_class->new_from_conf_file( $conf_file, %params ); | |||||||||||||||||||||||||||||||||
| 170 | 4 | my $source = do { local $/; <STDIN> }; | |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | # Merge stdout and stderr and output all to stderr, so that stdout is | ||||||||||||||||||||||||||||||||||
| 173 | # dedicated to the tidied content | ||||||||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||||||||
| 175 | 4 | my $result; | |||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | my $output = capture_merged { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 177 | 4 | $result = $ct->process_source( $source, $ct->_small_path( $pipe_filename->absolute ) ); | |||||||||||||||||||||||||||||||||
| 178 | 4 | }; | |||||||||||||||||||||||||||||||||
| 179 | 4 | print STDERR $output; | |||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | 4 | ▶ | if ( my $error = $result->error ) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 182 | 2 | print $source; # Error already printed above | |||||||||||||||||||||||||||||||||
| 183 | 2 | return 1; | |||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||
| 185 | elsif ( $result->state eq 'no_match' ) { | ||||||||||||||||||||||||||||||||||
| 186 | 0 | print $source; | |||||||||||||||||||||||||||||||||
| 187 | 0 | print STDERR "No plugins apply for '$pipe' in config"; | |||||||||||||||||||||||||||||||||
| 188 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | elsif ( $result->state eq 'checked' ) { | ||||||||||||||||||||||||||||||||||
| 191 | 0 | print $source; | |||||||||||||||||||||||||||||||||
| 192 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||||||||
| 195 | 2 | print $result->new_contents; | |||||||||||||||||||||||||||||||||
| 196 | 2 | return 0; | |||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | 1; | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||||||||||||||