Bug 38664: The tidy script - Implement --no-write and --silent

To ease comparisons, from the QA script

Bug 38664: Fix perltidyrc option

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Jonathan Druart 2025-02-03 13:58:58 +01:00 committed by Katrin Fischer
parent d8aae1ab89
commit 616102740a
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
1 changed files with 61 additions and 23 deletions

View File

@ -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#<!--</body>-->#</body>#g;
$content =~ s#\n*( *)<script>\n*#\n$1<script>\n#g;
$content =~ s#\n*( *)</script>\n*#\n$1</script>\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 <jonathan.druart@bugs.koha-community.org>