| 1 | 1 | ▶ | use 5.006; # our | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Dist::Zilla::Plugin::Munge::Whitespace; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.001001'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Strip superfluous spaces from pesky files. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Moose qw( has with around ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Dist::Zilla::Role::FileMunger 1.000; # munge_file | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | with 'Dist::Zilla::Role::FileMunger'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | has 'preserve_trailing' => ( is => 'ro', isa => 'Bool', lazy => 1, default => sub { undef } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | has 'preserve_cr' => ( is => 'ro', isa => 'Bool', lazy => 1, default => sub { undef } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | has 'filename' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | has 'match' => ( is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | has '_match_expr' => ( is => 'ro', isa => 'RegexpRef', lazy_build => 1 ); | |||||||||||||||||||||||||||
| 24 | 1 | has '_eol_kill_expr' => ( is => 'ro', isa => 'RegexpRef', lazy_build => 1 ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | around dump_config => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 27 | 0 | my ( $orig, $self, @args ) = @_; | |||||||||||||||||||||||||||
| 28 | 0 | my $config = $self->$orig(@args); | |||||||||||||||||||||||||||
| 29 | 0 | my $localconf = $config->{ +__PACKAGE__ } = {}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | for my $attr (qw( preserve_trailing preserve_cr filename match )) { | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | next unless $self->meta->find_attribute_by_name($attr)->has_value($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $localconf->{$attr} = $self->can($attr)->($self); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless __PACKAGE__ eq ref $self; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | return $config; | |||||||||||||||||||||||||||
| 40 | 1 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 43 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub mvp_multivalue_args { return qw{ filename match } } | ||||||||||||||||||||||||||
|
Subroutine
mvp_multivalue_args: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub munge_file { | ||||||||||||||||||||||||||
|
Subroutine
munge_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | return unless $file->name =~ $self->_match_expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( $file->isa('Dist::Zilla::File::FromCode') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $self->_munge_from_code($file); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | return $self->_munge_static($file); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub _build__match_expr { | ||||||||||||||||||||||||||
|
Subroutine
_build__match_expr: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | my (@matches) = @{ $self->match }; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( scalar @{ $self->filename } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | unshift @matches, sprintf q[\A(?:%s)\z], join q[|], map { quotemeta } @{ $self->filename }; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | my $combined = join q[|], @matches; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ## no critic (RegularExpressions::RequireDotMatchAnything) | ||||||||||||||||||||||||||||
| 71 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||||||||||||||||||||||||||||
| 72 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return qr/$combined/; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _build__eol_kill_expr { | ||||||||||||||||||||||||||
|
Subroutine
_build__eol_kill_expr: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ## no critic (RegularExpressions::RequireDotMatchAnything) | ||||||||||||||||||||||||||||
| 81 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||||||||||||||||||||||||||||
| 82 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $bad_bits = qr//; | |||||||||||||||||||||||||||
| 85 | 0 | my $end_line; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ( not $self->preserve_trailing ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Add horrible spaces to end | ||||||||||||||||||||||||||||
| 89 | 0 | $bad_bits = qr/[\x{20}\x{09}]+/; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $self->preserve_cr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # preserve CR keeps the CR optional as part of the EOL lookahead. | ||||||||||||||||||||||||||||
| 94 | 0 | $end_line = qr/(?=\x{0D}?\x{0A}|\z)/; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | # No-preserve CR swallows any CRs directly before the EOL lookahead. | ||||||||||||||||||||||||||||
| 98 | 0 | $end_line = qr/\x{0D}?(?=\x{0A}|\z)/; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return qr/${bad_bits}${end_line}/; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub _munge_string { | ||||||||||||||||||||||||||
|
Subroutine
_munge_string: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 105 | 0 | my ( $self, $name, $string ) = @_; | |||||||||||||||||||||||||||
| 106 | 0 | $self->log_debug( [ 'Stripping trailing whitespace from %s', $name ] ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( $self->preserve_cr and $self->preserve_trailing ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->preserve_cr and $self->preserve_trailing
MC/DC0%0/2
$self->preserve_cr and $self->preserve_trailing
$self->preserve_cr
$self->preserve_trailing
Truth table0%0/3
A and B
A= $self->preserve_crB= $self->preserve_trailing
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Noop, both EOL transformations | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | ## no critic (RegularExpressions::RequireDotMatchAnything) | ||||||||||||||||||||||||||||
| 114 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||||||||||||||||||||||||||||
| 115 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $expr = $self->_eol_kill_expr; | |||||||||||||||||||||||||||
| 118 | 0 | $string =~ s/$expr//g; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | return $string; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub _munge_from_code { | ||||||||||||||||||||||||||
|
Subroutine
_munge_from_code: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 124 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( $file->can('code_return_type') and 'text' ne $file->code_return_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file->can("code_return_type") and "text" ne $file->code_return_type
MC/DC0%0/2
$file->can("code_return_type") and "text" ne $file->code_return_type
$file->can("code_return_type")
"text" ne $file->code_return_type
Truth table0%0/3
A and B
A= $file->can("code_return_type")B= "text" ne $file->code_return_type
| |||||||||||||||||||||||||||||
| 126 | 0 | $self->log_debug( [ 'Skipping %s: does not return text', $file->name ] ); | |||||||||||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | $self->log_debug( [ 'Munging FromCode (prep): %s', $file->name ] ); | |||||||||||||||||||||||||||
| 130 | 0 | my $orig_coderef = $file->code(); | |||||||||||||||||||||||||||
| 131 | $file->code( | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | 0 | $self->log_debug( [ 'Munging FromCode (write): %s', $file->name ] ); | |||||||||||||||||||||||||||
| 134 | 0 | my $content = $file->$orig_coderef(); | |||||||||||||||||||||||||||
| 135 | 0 | return $self->_munge_string( $file->name, $content ); | |||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||
| 137 | 0 | ); | |||||||||||||||||||||||||||
| 138 | 0 | return; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub _munge_static { | ||||||||||||||||||||||||||
|
Subroutine
_munge_static: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 143 | 0 | $self->log_debug( [ 'Munging Static file: %s', $file->name ] ); | |||||||||||||||||||||||||||
| 144 | 0 | my $content = $file->content; | |||||||||||||||||||||||||||
| 145 | 0 | $file->content( $self->_munge_string( $file->name, $content ) ); | |||||||||||||||||||||||||||
| 146 | 0 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||