| 1 | package Code::TidyAll::Plugin::PerlTidySweet; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Capture::Tiny qw(capture_merged); | ||
| 7 | use Perl::Tidy::Sweetened 1.00; | ||
| 8 | |||
| 9 | use Moo; | ||
| 10 | |||
| 11 | extends 'Code::TidyAll::Plugin'; | ||
| 12 | |||
| 13 | our $VERSION = '0.85'; | ||
| 14 | |||
| 15 | sub transform_source { | ||
| 16 | my ( $self, $source ) = @_; | ||
| 17 | |||
| 18 | # perltidy reports errors in two different ways. | ||
| 19 | # Argument/profile errors are output and an error_flag is returned. | ||
| 20 | # Syntax errors are sent to errorfile or stderr, depending on the | ||
| 21 | # the setting of -se/-nse (aka --standard-error-output). These flags | ||
| 22 | # might be hidden in other bundles, e.g. -pbp. Be defensive and | ||
| 23 | # check both. | ||
| 24 | my ( $output, $error_flag, $errorfile, $stderr, $destination ); | ||
| 25 | $output = capture_merged { | ||
| 26 | $error_flag = Perl::Tidy::Sweetened::perltidy( | ||
| 27 | argv => $self->argv, | ||
| 28 | source => \$source, | ||
| 29 | destination => \$destination, | ||
| 30 | stderr => \$stderr, | ||
| 31 | errorfile => \$errorfile | ||
| 32 | ); | ||
| 33 | }; | ||
| 34 | die $stderr if $stderr; | ||
| 35 | die $errorfile if $errorfile; | ||
| 36 | die $output if $error_flag; | ||
| 37 | print STDERR $output if defined($output); | ||
| 38 | return $destination; | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | # ABSTRACT: Use perltidy-sweet with tidyall | ||
| 44 | |||
| 45 | __END__ |