« blib/lib/Code/TidyAll/Plugin/PerlCritic.pm
↑ Summary
blib/lib/Code/TidyAll/Plugin/CSSUnminifier.pm »
| 1 | package Code::TidyAll::Role::RunsCommand; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use IPC::Run3 qw(run3); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use List::SomeUtils qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Specio::Library::Builtins; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Specio::Library::Numeric; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Text::ParseWords qw(shellwords); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | our $VERSION = '0.83'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has ok_exit_codes => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => t( 'ArrayRef', of => t('PositiveOrZeroInt') ), | ||||||||||||||||||||||||||||
| 20 | 6 | ▶ | default => sub { [0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # We will end up getting $self->argv from the Plugin base class. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _run_or_die { | ||||||||||||||||||||||||||||
| 26 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_run_or_die: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 9 | my @argv = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | my $output; | |||||||||||||||||||||||||||
| 30 | 9 | my @cmd = ( shellwords( $self->cmd ), shellwords( $self->argv ), @argv ); | |||||||||||||||||||||||||||
| 31 | try { | ||||||||||||||||||||||||||||
| 32 | 9 | ▶ | local $?; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 33 | 9 | run3( \@cmd, \undef, \$output, \$output ); | |||||||||||||||||||||||||||
| 34 | 9 | my $code = $? >> 8; | |||||||||||||||||||||||||||
| 35 | 9 | ▶ | if ( $self->_is_bad_exit_code($code) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 3 | my $signal = $? & 127; | |||||||||||||||||||||||||||
| 37 | 3 | my $msg = "exited with $code"; | |||||||||||||||||||||||||||
| 38 | 3 | ▶ | $msg .= " - received signal $signal" if $signal; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 3 | ▶ | $msg .= " - output was:\n$output" if defined $output and length $output; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $output and length $output
MC/DC0%0/2
defined $output and length $output
defined $output
length $output
Truth table33%1/3
A and B
A= defined $outputB= length $output
| |||||||||||||||||||||||||||||
| 40 | 3 | die "$msg\n"; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | catch { | ||||||||||||||||||||||||||||
| 44 | 3 | ▶ | die sprintf( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | "Running [%s] failed\n %s", | ||||||||||||||||||||||||||||
| 46 | ( join q{ }, @cmd ), | ||||||||||||||||||||||||||||
| 47 | $_, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | 9 | }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 6 | return $output; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub _is_bad_exit_code { | ||||||||||||||||||||||||||||
| 55 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_bad_exit_code: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 9 | my $code = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 9 | ▶ | return !( any { $code == $_ } @{ $self->ok_exit_codes } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | 1; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # ABSTRACT: A role for plugins which run external commands | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =pod | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 NAME | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Code::TidyAll::Role::RunsCommand - A role for plugins which run external commands | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 VERSION | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | version 0.83 | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | package Whatever; | ||||||||||||||||||||||||||||
| 82 | use Moo; | ||||||||||||||||||||||||||||
| 83 | with 'Code::TidyAll::Role::RunsCommand'; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | This is a a role for plugins which run external commands | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =over | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item cmd | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | The command to run. This is just the executable and should not include | ||||||||||||||||||||||||||||
| 96 | additional arguments. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 METHODS | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 _run_or_die(@argv) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | This method run the plugin's command, combining any values provided to the | ||||||||||||||||||||||||||||
| 105 | plugin's C<argv> attribute with those passed to the method. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | The plugin's C<argv> attribute is parsed with the C<shellwords> subroutine from | ||||||||||||||||||||||||||||
| 108 | L<Text::ParseWords> in order to turn the C<argv> string into a list. This | ||||||||||||||||||||||||||||
| 109 | ensures that running the command does not spawn an external shell. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | The C<@argv> passed to the command comes after the values from C<argv> | ||||||||||||||||||||||||||||
| 112 | attribute. The assumption is that this will be what passes a file or source | ||||||||||||||||||||||||||||
| 113 | string to the external command. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | If the command exits with a non-zero status, then this method throws an | ||||||||||||||||||||||||||||
| 116 | exception. The error message it throws include the command that was run (with | ||||||||||||||||||||||||||||
| 117 | arguments), the exit status, any signal received by the command, and the | ||||||||||||||||||||||||||||
| 118 | command's output. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Both C<stdout> and C<stderr> from the command are combined into a single string | ||||||||||||||||||||||||||||
| 121 | returned by the method. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 _is_bad_exit_code($code) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | This method returns true if the exit code is bad and false otherwise. By | ||||||||||||||||||||||||||||
| 126 | default all non-zero codes are bad, but some programs may be expected to exit | ||||||||||||||||||||||||||||
| 127 | non-0 when they encounter validation/tidying issues. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =over 4 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =item * | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Jonathan Swartz <swartz@pobox.com> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item * | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This software is copyright (c) 2011 - 2022 by Jonathan Swartz. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 156 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 159 | F<LICENSE> file included with this distribution. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||