| 1 | package CPAN::Maker::Bootstrapper::Role::PerlTidy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(slurp); | ||
| 8 | use English qw(-no_match_vars); | ||
| 9 | use Role::Tiny; | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | sub cmd_perltidy { | ||
| 13 | ######################################################################## | ||
| 14 | my ($self) = @_; | ||
| 15 | |||
| 16 | my $has_perl_tidy = eval { | ||
| 17 | require Perl::Tidy; | ||
| 18 | 1; | ||
| 19 | }; | ||
| 20 | |||
| 21 | die "ERROR: perltidy command requires Perl::Tidy\n" | ||
| 22 | if !$has_perl_tidy || $EVAL_ERROR; | ||
| 23 | |||
| 24 | my ($source) = $self->get_args; | ||
| 25 | |||
| 26 | my $file_list = $self->get_file_list; | ||
| 27 | my $profile = $self->get_profile; | ||
| 28 | |||
| 29 | die "ERROR: source-file and --file-list are mutually exclusive\n" | ||
| 30 | if $source && $file_list; | ||
| 31 | |||
| 32 | die "ERROR: usage: cmb perltidy [--file-list file-list] [--profile file] [source-file]\n" | ||
| 33 | if !$source && !$file_list; | ||
| 34 | |||
| 35 | die "ERROR: $profile not found or not readable\n" | ||
| 36 | if $profile && ( !-f $profile || !-r $profile ); | ||
| 37 | |||
| 38 | my @files = $self->_perltidy_files( $source, $file_list ); | ||
| 39 | |||
| 40 | for my $file (@files) { | ||
| 41 | my $source_text = slurp($file); | ||
| 42 | my $tidied_text = q{}; | ||
| 43 | my $stderr = q{}; | ||
| 44 | my $errorfile = q{}; | ||
| 45 | |||
| 46 | my %tidy_args = ( | ||
| 47 | source => \$source_text, | ||
| 48 | destination => \$tidied_text, | ||
| 49 | stderr => \$stderr, | ||
| 50 | errorfile => \$errorfile, | ||
| 51 | ); | ||
| 52 | |||
| 53 | $tidy_args{perltidyrc} = $profile | ||
| 54 | if $profile; | ||
| 55 | |||
| 56 | my $error = Perl::Tidy::perltidy(%tidy_args); | ||
| 57 | |||
| 58 | if ($error) { | ||
| 59 | print {*STDERR} $stderr | ||
| 60 | if $stderr; | ||
| 61 | |||
| 62 | print {*STDERR} $errorfile | ||
| 63 | if $errorfile; | ||
| 64 | |||
| 65 | die "ERROR: perltidy failed for $file\n"; | ||
| 66 | } | ||
| 67 | |||
| 68 | die "ERROR: $file is not tidy - run: make tidy\n" | ||
| 69 | if $source_text ne $tidied_text; | ||
| 70 | |||
| 71 | my $sentinel = "$file.tdy"; | ||
| 72 | |||
| 73 | open my $fh, '>', $sentinel | ||
| 74 | or die "ERROR: cannot write $sentinel: $ERRNO\n"; | ||
| 75 | |||
| 76 | close $fh | ||
| 77 | or die "ERROR: cannot close $sentinel: $ERRNO\n"; | ||
| 78 | } | ||
| 79 | |||
| 80 | return $SUCCESS; | ||
| 81 | } | ||
| 82 | |||
| 83 | ######################################################################## | ||
| 84 | sub _perltidy_files { | ||
| 85 | ######################################################################## | ||
| 86 | my ( $self, $source, $file_list ) = @_; | ||
| 87 | |||
| 88 | my @files; | ||
| 89 | |||
| 90 | if ($file_list) { | ||
| 91 | die "ERROR: $file_list not found or not readable\n" | ||
| 92 | if !-f $file_list || !-r $file_list; | ||
| 93 | |||
| 94 | for my $file ( split /\n/xsm, slurp($file_list) ) { | ||
| 95 | next if !$file; | ||
| 96 | next if $file =~ /^[#]/xsm; | ||
| 97 | |||
| 98 | push @files, $file; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | @files = ($source); | ||
| 103 | } | ||
| 104 | |||
| 105 | die "ERROR: no files to tidy\n" | ||
| 106 | if !@files; | ||
| 107 | |||
| 108 | for my $file (@files) { | ||
| 109 | die "ERROR: $file not found or not readable\n" | ||
| 110 | if !-f $file || !-r $file; | ||
| 111 | } | ||
| 112 | |||
| 113 | return @files; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |