diff --git a/misc/devel/tidy.pl b/misc/devel/tidy.pl index 43a34d3944..d2c8c9984d 100755 --- a/misc/devel/tidy.pl +++ b/misc/devel/tidy.pl @@ -8,18 +8,19 @@ use File::Slurp qw( read_file write_file ); use IPC::Cmd qw( run ); use Parallel::ForkManager; -my ( $perl_files, $js_files, $tt_files, $nproc, $verbose, $help ); +my ( $perl_files, $js_files, $tt_files, $nproc, $no_write, $silent, $help ); our $perltidyrc = '.perltidyrc'; GetOptions( - 'perl' => \$perl_files, - 'js' => \$js_files, - 'tt' => \$tt_files, - 'perltidyrc' => \$perltidyrc, - 'nproc:s' => \$nproc, - 'verbose|v' => \$verbose, - 'help|?' => \$help, + 'perl' => \$perl_files, + 'js' => \$js_files, + 'tt' => \$tt_files, + 'perltidyrc:s' => \$perltidyrc, + 'no-write' => \$no_write, + 'nproc:s' => \$nproc, + 'silent' => \$silent, + 'help|?' => \$help, ) or pod2usage(2); pod2usage(1) if $help; @@ -28,6 +29,11 @@ $nproc ||= qx{nproc}; my @files = @ARGV; +pod2usage("--no-write can only be passed with a single file") if $no_write && @files > 1; + +pod2usage("--perl, --js and --tt can only be passed without any other files in parameter") + if @files && ( $perl_files || $js_files || $tt_files ); + push @files, get_perl_files() if $perl_files; push @files, get_js_files() if $js_files; push @files, get_tt_files() if $tt_files; @@ -65,13 +71,13 @@ for my $index ( 0 .. $#files ) { my $file = $files[$index]; $pm->start and next; - say sprintf "Tidying file %s/%s (%s)", $index + 1, $nb_files, $file; + l( sprintf "Tidying file %s/%s (%s)", $index + 1, $nb_files, $file ); my $error; my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = tidy($file); unless ($success) { $error = join( '', @$stderr_buf ); chomp $error; - say $error; + warn $error; } $pm->finish( 0, { error => $error, file => $file } ); @@ -79,8 +85,8 @@ for my $index ( 0 .. $#files ) { $pm->wait_all_children; if (@errors) { - say "\nSome files cannot be tidied:"; - say sprintf( "\t* %s\n%s", $_->{file}, $_->{error} ) for @errors; + l("\nSome files cannot be tidied:"); + l( sprintf( "\t* %s\n%s", $_->{file}, $_->{error} ) ) for @errors; } sub tidy { @@ -124,28 +130,43 @@ sub get_tt_files { sub tidy_perl { my ($file) = @_; - run( - command => sprintf q{perltidy --backup-and-modify-in-place --nostandard-output -pro=%s %s}, $perltidyrc, - $file - ); + my $cmd = + $no_write + ? sprintf q{perltidy --standard-output -pro=%s %s}, $perltidyrc, $file + : sprintf q{perltidy --backup-and-modify-in-place --nostandard-output -pro=%s %s}, $perltidyrc, $file; + + print qx{$cmd}; } sub tidy_js { my ($file) = @_; - run( command => sprintf q{yarn --silent run prettier --write %s}, $file ); + my $cmd = sprintf q{yarn --silent run prettier %s%s}, ( $no_write ? '' : '--write ' ), $file; + print qx{$cmd}; } sub tidy_vue { my ($file) = @_; - run( command => sprintf q{yarn --silent run prettier --write %s}, $file ); + my $cmd = sprintf q{yarn --silent run prettier %s%s}, ( $no_write ? '' : '--write ' ), $file; + print qx{$cmd}; } sub tidy_tt { - my ($file) = @_; + my ($original_file) = @_; my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ); - for ( 1 .. 2 ) { + + my ( $file_fh, $file ); # Keep this scope for $file_fh, or the file will be deleted after the following block + if ($no_write) { + $file_fh = File::Temp->new( CLEANUP => 1, SUFFIX => '.tt', DIR => '.' ); + $file = $file_fh->filename; + write_file( $file, read_file($original_file) ); + } else { + $file = $original_file; + } + + for my $pass ( 1 .. 2 ) { ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = - run( command => sprintf( q{yarn run prettier --write %s}, $file ) ); + run( command => sprintf( q{yarn --silent run prettier --write %s}, $file ) ); + if ($success) { # Revert the substitutions done by the prettier plugin @@ -155,7 +176,11 @@ sub tidy_tt { $content =~ s###g; $content =~ s#\n*( *)\n*#\n$1\n#g; - write_file( $file, $content ); + if ( $no_write && $pass == 2 ) { + print $content; + } else { + write_file( $file, $content ); + } } } return ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ); @@ -174,6 +199,10 @@ sub get_filetype { die sprintf 'Cannot guess filetype for %s', $file; } +sub l { + say shift unless $silent; +} + =head1 NAME tidy.pl - Tidy Perl, Javascript, Vue and Template::Toolkit files. @@ -187,8 +216,9 @@ tidy.pl [options] [files] --js Tidy the JavaScript files (.js, .ts, .vue) --tt Tidy the Template::Tolkit files (.inc, .tt) --perltidyrc .perltidyrc files to use for perltidy (default: .perltidyrc) + --no-write Do not modify the file, output the tidy version to STDOUT --nproc Number of processes to use (default to all available) - --verbose Verbose mode (not implemented yet) + --silent Silent mode --help Show this help message =head1 DESCRIPTION @@ -217,6 +247,14 @@ Tidy only some specific files: ./tidy.pl list of files +Output the tidy version of a file: + + ./tidy.pl --no-write /path/to/file + +Output the tidy version of a file without other information: + + ./tidy.pl --silent --no-write /path/to/file + =head1 AUTHOR Jonathan Druart