|
| 1 | +package Code::TidyAll::Plugin::PerlTidySweet; |
| 2 | + |
| 3 | +use Capture::Tiny qw(capture_merged); |
| 4 | +use Perl::Tidy::Sweetened 1.00; |
| 5 | +use Moo; |
| 6 | +extends 'Code::TidyAll::Plugin'; |
| 7 | + |
| 8 | +our $VERSION = '0.28'; |
| 9 | + |
| 10 | +sub transform_source { |
| 11 | + my ( $self, $source ) = @_; |
| 12 | + |
| 13 | + # perltidy reports errors in two different ways. |
| 14 | + # Argument/profile errors are output and an error_flag is returned. |
| 15 | + # Syntax errors are sent to errorfile or stderr, depending on the |
| 16 | + # the setting of -se/-nse (aka --standard-error-output). These flags |
| 17 | + # might be hidden in other bundles, e.g. -pbp. Be defensive and |
| 18 | + # check both. |
| 19 | + my ( $output, $error_flag, $errorfile, $stderr, $destination ); |
| 20 | + $output = capture_merged { |
| 21 | + $error_flag = Perl::Tidy::Sweetened::perltidy( |
| 22 | + argv => $self->argv, |
| 23 | + source => \$source, |
| 24 | + destination => \$destination, |
| 25 | + stderr => \$stderr, |
| 26 | + errorfile => \$errorfile |
| 27 | + ); |
| 28 | + }; |
| 29 | + die $stderr if $stderr; |
| 30 | + die $errorfile if $errorfile; |
| 31 | + die $output if $error_flag; |
| 32 | + print STDERR $output if defined($output); |
| 33 | + return $destination; |
| 34 | +} |
| 35 | + |
| 36 | +1; |
| 37 | + |
| 38 | +# ABSTRACT: Use perltidy-sweet with tidyall |
| 39 | + |
| 40 | +__END__ |
| 41 | +
|
| 42 | +=pod |
| 43 | +
|
| 44 | +=head1 SYNOPSIS |
| 45 | +
|
| 46 | + # In configuration: |
| 47 | +
|
| 48 | + ; Configure in-line |
| 49 | + ; |
| 50 | + [PerlTidySweet] |
| 51 | + select = lib/**/*.pm |
| 52 | + argv = --noll |
| 53 | +
|
| 54 | + ; or refer to a .perltidyrc in the same directory |
| 55 | + ; |
| 56 | + [PerlTidySweet] |
| 57 | + select = lib/**/*.pm |
| 58 | + argv = --profile=$ROOT/.perltidyrc |
| 59 | +
|
| 60 | +=head1 DESCRIPTION |
| 61 | +
|
| 62 | +Runs L<perltidy-sweet>, a Perl tidier based on Perl::Tidy that also supports |
| 63 | +new syntactic sugar as provided by L<Method::Signature::Simple>, |
| 64 | +L<MooseX::Method::Signatures> and the p5-mop. |
| 65 | +
|
| 66 | +=head1 INSTALLATION |
| 67 | +
|
| 68 | +Install perltidy-sweet from CPAN. |
| 69 | +
|
| 70 | + cpanm Perl::Tidy::Sweet |
| 71 | +
|
| 72 | +=head1 CONFIGURATION |
| 73 | +
|
| 74 | +=over |
| 75 | +
|
| 76 | +=item argv |
| 77 | +
|
| 78 | +Arguments to pass to perltidy-sweet |
| 79 | +
|
| 80 | +=back |
| 81 | +
|
| 82 | +=cut |
0 commit comments