| 1 | package JavaScript::Prepare; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use FileHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use JavaScript::Minifier::XS qw( minify ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | our $VERSION = qv( 0.1 ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 7 | my %args = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | my $self = { | |||||||||||||||||||||||||||
| 18 | strip => 0, | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | 7 | bless $self, $class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $self->{'strip'} = 1 | ||||||||||||||||||||||||||||
| 23 | 7 | ▶ | if defined $args{'strip'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 7 | return $self; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | 3 | my @args = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | my $minified = ''; | |||||||||||||||||||||||||||
| 33 | 3 | foreach my $arg ( @args ) { | |||||||||||||||||||||||||||
| 34 | 4 | given ( $arg ) { | |||||||||||||||||||||||||||
| 35 | 4 | when ( -f $arg ) { | |||||||||||||||||||||||||||
| 36 | 3 | $minified .= $self->process_file( $arg ); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 1 | when ( -d $arg ) { | |||||||||||||||||||||||||||
| 39 | 1 | $minified .= $self->process_directory( $arg ); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 0 | default { | |||||||||||||||||||||||||||
| 42 | 0 | return ''; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | return $minified; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 19 | ▶ | sub process_string { | ||||||||||||||||||||||||||
|
Subroutine
process_string: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 19 | my $js = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $js =~ s{^ \s* console.log(.*?); \s* $}{}gmx | ||||||||||||||||||||||||||||
| 55 | 19 | ▶ | if $self->{'strip'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 19 | my $minified = minify($js); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 19 | ▶ | return "${minified}\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $minified and length $minified
MC/DC50%1/2
defined $minified and length $minified
defined $minified
length $minified
Truth table66%2/3
A and B
A= defined $minifiedB= length $minified
| |||||||||||||||||||||||||||||
| 60 | if defined $minified && length $minified; | ||||||||||||||||||||||||||||
| 61 | 1 | return ''; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | ▶ | sub process_file { | ||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | 16 | my $file = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 16 | my $content = $self->read_file( $file ); | |||||||||||||||||||||||||||
| 69 | 16 | ▶ | return '' unless $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 16 | my $control_file = $content =~ m{^# control file}; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 16 | ▶ | if ( $control_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | return $self->process_control_file( $file ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 14 | return $self->process_string( $content ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub process_control_file { | ||||||||||||||||||||||||||
|
Subroutine
process_control_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 2 | my $file = shift; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | my $dir = dirname $file; | |||||||||||||||||||||||||||
| 85 | 2 | my $content = $self->read_file( $file ); | |||||||||||||||||||||||||||
| 86 | 2 | my @lines = split m{\n}, $content; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my $minified = ''; | |||||||||||||||||||||||||||
| 89 | 2 | foreach my $line ( @lines ) { | |||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||
| 91 | 8 | $line =~ m{ | |||||||||||||||||||||||||||
| 92 | ^ | ||||||||||||||||||||||||||||
| 93 | ( \S+ )? # $1: a filename | ||||||||||||||||||||||||||||
| 94 | \s* (?: \# .* )? # optional comment | ||||||||||||||||||||||||||||
| 95 | $ | ||||||||||||||||||||||||||||
| 96 | }x; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 8 | ▶ | $minified .= $self->process_file( "$dir/$1" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | if defined $1; | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | return $minified; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 18 | ▶ | sub read_file { | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 18 | my $file = shift; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 18 | ▶ | my $handle = FileHandle->new( $file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or return; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 18 | my $content = do { | |||||||||||||||||||||||||||
| 112 | 18 | local $/; | |||||||||||||||||||||||||||
| 113 | <$handle> | ||||||||||||||||||||||||||||
| 114 | 18 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 18 | return $content; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | ▶ | sub process_directory { | ||||||||||||||||||||||||||
|
Subroutine
process_directory: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 2 | my $directory = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | my @files = $self->get_files_in_directory( $directory ); | |||||||||||||||||||||||||||
| 124 | 2 | my $minified; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | foreach my $file ( @files ) { | |||||||||||||||||||||||||||
| 127 | 4 | $minified .= $self->process_file( $file ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | return $minified; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 6 | ▶ | sub get_files_in_directory { | ||||||||||||||||||||||||||
|
Subroutine
get_files_in_directory: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 6 | my $directory = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | ▶ | opendir my $handle, $directory | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | or return; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | my @files; | |||||||||||||||||||||||||||
| 140 | my @directories; | ||||||||||||||||||||||||||||
| 141 | 6 | ▶ | while ( my $entry = readdir $handle ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 21 | ▶ | next if $entry =~ m{^\.}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 9 | my $target = "$directory/$entry"; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | ▶ | push( @files, $target ) if -f $target; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 9 | ▶ | push( @directories, $target ) if -d $target; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 6 | closedir $handle; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 6 | foreach my $dir ( @directories ) { | |||||||||||||||||||||||||||
| 152 | 3 | my @subfiles; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | foreach my $file ( $self->get_files_in_directory( $dir ) ) { | |||||||||||||||||||||||||||
| 155 | 3 | push @subfiles, $file; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | @files = ( @subfiles, @files ); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 6 | return @files; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 7 | 1; | |||||||||||||||||||||||||||