|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +=head1 NAME |
| 4 | +
|
| 5 | +run-perltidy.pl -- Run perltidy on the renderer source files. |
| 6 | +
|
| 7 | +=head1 SYNOPSIS |
| 8 | +
|
| 9 | + run-perltidy.pl [options] file1 file2 ... |
| 10 | +
|
| 11 | +=head1 DESCRIPTION |
| 12 | +
|
| 13 | +Run perltidy on the renderer source files. |
| 14 | +
|
| 15 | +=head1 OPTIONS |
| 16 | +
|
| 17 | +For this script to work the .perltidyrc file in the renderer root directory |
| 18 | +must be readable. Note that the renderer root directory is automatically |
| 19 | +detected. |
| 20 | +
|
| 21 | +This script accepts all of the options that are accepted by perltidy. See the |
| 22 | +perltidy documentation for details. |
| 23 | +
|
| 24 | +However, the C<-pro> option is not allowed. This script will use the |
| 25 | +.perltidyrc file in the renderer root directory for this option instead. |
| 26 | +
|
| 27 | +In addition the default value of C<-bext> for this script is C<'/'>, which means |
| 28 | +that backup files will be created with the C<.bak> extension, and will be |
| 29 | +deleted if there are no errors. Note that this behavior may be changed by |
| 30 | +passing a different value for the C<-bext> option. |
| 31 | +
|
| 32 | +Note that the C<-v> flag makes this script verbose, and does not output the |
| 33 | +perltidy version as it would usually do for perltidy. |
| 34 | +
|
| 35 | +Finally, if no files are passed on the command line, then perltidy will be |
| 36 | +executed on all files with the extensions C<.pl>, C<.pm>, or C<.t> in the |
| 37 | +renderer directory. If files are passed on the command line, then perltidy |
| 38 | +will only be executed on the listed files. |
| 39 | +
|
| 40 | +=cut |
| 41 | + |
| 42 | +use strict; |
| 43 | +use warnings; |
| 44 | +use feature 'say'; |
| 45 | + |
| 46 | +use Perl::Tidy; |
| 47 | +use File::Find qw(find); |
| 48 | +use Mojo::File qw(curfile); |
| 49 | + |
| 50 | +my $renderer_root = curfile->dirname->dirname; |
| 51 | + |
| 52 | +die "Version 20240903 of perltidy is required for this script.\nThe installed version is $Perl::Tidy::VERSION.\n" |
| 53 | + unless $Perl::Tidy::VERSION == 20240903; |
| 54 | +die "The .perltidyrc file in the renderer root directory is not readable.\n" |
| 55 | + unless -r "$renderer_root/.perltidyrc"; |
| 56 | + |
| 57 | +my $verbose = 0; |
| 58 | +my (@args, @files); |
| 59 | +for (@ARGV) { |
| 60 | + if ($_ eq '-v') { $verbose = 1 } |
| 61 | + elsif ($_ =~ /^-/) { push(@args, $_) } |
| 62 | + else { push(@files, $_) } |
| 63 | +} |
| 64 | + |
| 65 | +# Validate options that were passed. |
| 66 | +my %options; |
| 67 | +my $err = Perl::Tidy::perltidy(argv => \@args, dump_options => \%options); |
| 68 | +exit $err if $err; |
| 69 | +die "The -pro option is not suppored by this script.\n" if defined $options{profile}; |
| 70 | + |
| 71 | +unshift(@args, '-bext=/') unless defined $options{'backup-file-extension'}; |
| 72 | + |
| 73 | +if (@files) { |
| 74 | + for (@files) { |
| 75 | + push(@args, $_); |
| 76 | + say "Tidying file: $_" if $verbose; |
| 77 | + Perl::Tidy::perltidy(argv => \@args, perltidyrc => "$renderer_root/.perltidyrc"); |
| 78 | + pop(@args); |
| 79 | + } |
| 80 | +} else { |
| 81 | + find( |
| 82 | + { |
| 83 | + wanted => sub { |
| 84 | + my $path = $File::Find::name; |
| 85 | + my $dir = $File::Find::dir; |
| 86 | + my ($name) = $path =~ m|^$dir(?:/(.*))?$|; |
| 87 | + $name = '' unless defined $name; |
| 88 | + |
| 89 | + if (-d $path && $name =~ /^(\.git|\.github|htdocs|\.vscode|PG)$/) { |
| 90 | + $File::Find::prune = 1; |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + return unless $path =~ /\.p[lm]$/ || $path =~ /\.t$/; |
| 95 | + |
| 96 | + say "Tidying file: $path" if $verbose; |
| 97 | + |
| 98 | + push(@args, $path); |
| 99 | + Perl::Tidy::perltidy(argv => \@args, perltidyrc => "$renderer_root/.perltidyrc"); |
| 100 | + pop(@args); |
| 101 | + }, |
| 102 | + no_chdir => 1 |
| 103 | + }, |
| 104 | + $renderer_root |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +1; |
0 commit comments