Plugin/Web/Bundle.pm

filter: Statementstmt 87.5% 98 / 112 Branchbran 55.0% 22 / 40 Conditioncond 76.9% 10 / 13 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 8 totaltotal 76.1% 153 / 201 CC 30 SCAR 18.1
CC
30
Cov
79%
get_file_content
15.5
__ANON__
9.1
get_entry_content
8.3
CRAP 6.1
« Plugin/Web/PkgVersion.pm ↑ Summary App/Command/add_header.pm »
1 package Dist::Zilla::Plugin::Web::Bundle;
2 2 $Dist::Zilla::Plugin::Web::Bundle::VERSION = '0.0.10';
3 # ABSTRACT: Bundle the library files into "tasks", using information from components.json
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 with 'Dist::Zilla::Role::FileGatherer';
8 2 with 'Dist::Zilla::Role::FileMunger';
9  
10 2 ▶use Dist::Zilla::File::Generated;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use JSON -support_by_pp, -no_export;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IPC::Run qw( run );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Capture::Tiny qw/capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 has 'lazy' => (
20 isa => 'Bool',
21 is => 'rw',
22
23 default => 0
24 );
25  
26  
27  
28 2 has 'filename' => (
29 isa => 'Str',
30 is => 'rw',
31
32 default => 'components.json'
33 );
34  
35  
36 2 has 'lib_dir' => (
37 isa => 'Str',
38 is => 'rw',
39
40 default => 'lib'
41 );
42  
43  
44 has 'bundle_files' => (
45 is => 'rw',
46
47 1 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
48 2 );
49  
50  
51 2 has 'npm_root' => (
52 is => 'ro',
53
54 lazy => 1,
55
56 builder => '_get_npm_root',
57 );
58  
59  
60 2 has 'components' => (
61 is => 'ro',
62 lazy => 1,
63 builder => '_build_components_info',
64 );
65  
66  
67 #================================================================================================================================================================================================================================================
68 1 ▶sub _build_components_info {
Subroutine _build_components_info: called CC=1 SCAR=0.0
69 1 my ($self) = @_;
70  
71 1 my $content = file($self->filename)->slurp;
72  
73 #removing // style comments
74 1 $content =~ s!//.*$!!gm;
75  
76 #extracting from outermost {} brackets
77 1 $content =~ m/(\{.*\})/s;
78 1 $content = $1;
79
80 1 my $json = JSON->new->relaxed->allow_singlequote->allow_barekey;
81  
82 1 return $json->decode($content);
83 }
84  
85  
86  
87 #================================================================================================================================================================================================================================================
88 1 ▶sub gather_files {
Subroutine gather_files: called CC=1 SCAR=0.0
Pod undocumented
89 }
90  
91  
92 #================================================================================================================================================================================================================================================
93 # need to build bundles in the "munge" phase - to allow other munge plugins to modify the sources
94 1 ▶sub munge_files {
Subroutine munge_files: called CC=1 SCAR=0.0
Pod undocumented
95 1 my ($self) = @_;
96
97 1 $self->process_components();
98 }
99  
100  
101 #================================================================================================================================================================================================================================================
102 1 ▶sub process_components {
Subroutine process_components: called CC=5 SCAR=16.3
Pod undocumented
103 1 my $self = shift;
104
105 1 ▶ return unless -f $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->filename 0 1 1
106
107 1 my $components = $self->components;
108
109 1 foreach my $component (keys(%$components)) {
110 7 $self->process_component($components, $component);
111 }
112
113 # unless the lazy flag is set (false by default) - generate the content of all bundles right away
114 # otherwise leave them, allowing to other mungers to modify the individual source files before bundling
115 1 ▶ unless ($self->lazy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lazy) 1 0 1
116 1 $_->content foreach (values(%{$self->bundle_files})) ;
117 }
118 }
119  
120  
121 #================================================================================================================================================================================================================================================
122 7 ▶sub process_component {
Subroutine process_component: called CC=5 SCAR=16.4
Pod undocumented
123 7 my ($self, $components, $component) = @_;
124
125 7 my $componentInfo = $components->{ $component };
126 7 ▶ $componentInfo = { contains => $componentInfo } if ref $componentInfo eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $componentInfo eq "ARRAY" 1 6 7
127
128 7 my $saveAs = $componentInfo->{ saveAs };
129
130 $self->bundle_files->{ $component } = Dist::Zilla::File::Generated->new({
131
132 name => $saveAs || "foo.js",
133
134 7 ▶ code => sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
135 7 my $bundle_content = '';
136 7 my $is_js = 1;
137
138 7 foreach my $entry (@{$componentInfo->{ contains }}) {
139 16 ▶ $is_js = 0 if $entry =~ /\.css$/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /\.css$/ 0 16 16
140
141 16 ▶ $bundle_content .= $self->get_entry_content($entry, $component) . ($is_js ? ";\n" : '');
Branch: 1 branch
BranchTrueFalseTotal
$is_js ? : 16 0 16
142 }
143
144 7 ▶ my $minify = $componentInfo->{ minify } || '';
Condition100%2/2
$componentInfo->{'minify'} || ''
l!l
16
MC/DC100%1/1
$componentInfo->{'minify'} || ''
$componentInfo->{'minify'}
Truth table100%2/2
A
A= $componentInfo->{'minify'}
Aresult
11
00
145
146 7 ▶ if ($minify eq 'yui') {
Branch: 1 branch
BranchTrueFalseTotal
if ($minify eq "yui") 1 6 7
147 1 my $yui = dir( File::ShareDir::dist_dir('Dist-Zilla-Plugin-Web'), 'minifiers' )->file('yuicompressor-2.4.6.jar') . '';
148 1 ▶ my $type = $is_js ? 'js' : 'css';
Branch: 1 branch
BranchTrueFalseTotal
$is_js ? : 1 0 1
149
150 1 my ($child_out, $child_err);
151  
152 1 my $success = run [ "java", "-jar", "$yui", "--type", "$type" ], '<', \$bundle_content, '>', \$child_out, '2>', \$child_err;
153
154 0 ▶ die "Error during minification with YUI: $child_err" unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
155
156 0 $bundle_content = $child_out;
157 }
158
159 6 return $bundle_content;
160 }
161 7 ▶ });
Condition100%2/2
$saveAs || 'foo.js'
l!l
34
MC/DC100%1/1
$saveAs || 'foo.js'
$saveAs
Truth table100%2/2
A
A= $saveAs
Aresult
11
00
162
163 # only store the bundles that has "saveAs"
164 7 ▶ if ($saveAs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saveAs) 3 4 7
165 3 my $already_has_file = $self->get_dzil_file($saveAs);
166
167 3 ▶ $self->zilla->prune_file($already_has_file) if $already_has_file;
Branch: 1 branch
BranchTrueFalseTotal
if $already_has_file 0 3 3
168
169 3 $self->add_file($self->bundle_files->{ $component });
170 }
171 }
172  
173  
174 #================================================================================================================================================================================================================================================
175 16 ▶sub get_entry_content {
Subroutine get_entry_content: called CC=8 SCAR=21.1
Pod undocumented
176 16 my ($self, $entry, $component) = @_;
177
178 16 ▶ if ((ref $entry eq 'HASH') && $entry->{ text }) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $entry eq 'HASH' and $entry->{'text'}) { } 1 15 16
elsif ($entry =~ /^\+(.+)/) { } 5 10 15
elsif (not $entry =~ m[/] || $entry =~ /\.js$/ || $entry =~ /\.css$/) { } 7 3 10
Condition66%2/3
ref $entry eq "HASH" and $entry->{"text"}
!ll&&!rl&&r
1501
MC/DC50%1/2
ref $entry eq "HASH" and $entry->{"text"}
ref $entry eq "HASH" $entry->{"text"}
Truth table66%2/3
A and B
A= ref $entry eq "HASH"B= $entry->{"text"}
ABresult
0X0
100
111
179
180 1 return $entry->{ text };
181
182 } elsif ($entry =~ /^\+(.+)/) {
183
184 5 my $bundleFile = $self->bundle_files->{ $1 };
185
186 5 ▶ die "Reference to non-existend bundle [$1] from [$component]" if !$bundleFile;
Branch: 1 branch
BranchTrueFalseTotal
unless $bundleFile 0 5 5
187
188 5 return $bundleFile->content;
189
190 ▶ } elsif ($entry !~ /\// && $entry !~ /\.js$/ && $entry !~ /\.css$/) {
Condition66%4/6
$entry =~ m[/] || $entry =~ /\.js$/ || $entry =~ /\.css$/
l!l&&r!l&&!r
307
$entry =~ m[/] or $entry =~ /\.js$/
l!l&&r!l&&!r
307
MC/DC33%1/3
$entry =~ m[/] || $entry =~ /\.js$/ || $entry =~ /\.css$/
$entry =~ m[/] $entry =~ /\.js$/ $entry =~ /\.css$/
Truth table50%2/4
A or B || C
A= $entry =~ m[/]B= $entry =~ /\.js$/C= $entry =~ /\.css$/
ABCresult
1XX1
01X1
0011
0000
191
192 7 return $self->get_file_content($self->entry_to_filename($entry), $component);
193
194 } else {
195 3 return $self->get_file_content($entry, $component);
196 }
197 }
198  
199  
200 #================================================================================================================================================================================================================================================
201 13 ▶sub get_dzil_file {
Subroutine get_dzil_file: called CC=3 SCAR=11.0
Pod undocumented
202 13 my ($self, $file_name) = @_;
203
204 13 my $found;
205
206 13 for my $file (@{$self->zilla->files}) {
207
208 146 ▶ if ($file->name eq $file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->name eq $file_name) 10 136 146
209 10 $found = $file;
210
211 10 last;
212 }
213 }
214
215 13 return $found;
216 }
217  
218  
219 #================================================================================================================================================================================================================================================
220 10 ▶sub get_file_content {
Subroutine get_file_content: called CC=5 SCAR=27.4
Pod undocumented
221 10 my ($self, $file_name, $component) = @_;
222
223 10 my $found = $self->get_dzil_file($file_name);
224
225 # return content of gathered file if found
226 10 ▶ return $found->content if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 10 0 10
227
228 # return content of file in the distribution if presenteed
229 0 ▶ return file($file_name)->slurp . '' if -e $file_name;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file_name 0 0 0
230
231 # when file name starts with "node_modules" also look in global modules (as last resort)
232 0 ▶ if ($file_name =~ m!^node_modules/(.*)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name =~ m[^node_modules/(.*)]) 0 0 0
233 0 my $npm_file_name = dir($self->npm_root)->file($1);
234
235 0 ▶ return file($npm_file_name)->slurp . '' if -e $npm_file_name;
Branch: 1 branch
BranchTrueFalseTotal
if -e $npm_file_name 0 0 0
236 }
237
238 # cry out
239 0 die "Can't find file [$file_name] in [$component]";
240 }
241  
242  
243 #================================================================================================================================================================================================================================================
244 0 ▶sub _get_npm_root {
Subroutine _get_npm_root: not called CC=2 SCAR=17.9
245
246 0 my $child_exit_status;
247
248 0 ▶ my ($npm_root, $stderr) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 system('npm root -g');
250
251 0 $child_exit_status = $? >> 8;
252 0 };
253
254 0 ▶ die "Error when getting npm root: $child_exit_status" if $child_exit_status;
Branch: 1 branch
BranchTrueFalseTotal
if $child_exit_status 0 0 0
255
256 0 chomp($npm_root);
257
258 0 return $npm_root;
259 }
260  
261  
262  
263  
264 #================================================================================================================================================================================================================================================
265 7 ▶sub entry_to_filename {
Subroutine entry_to_filename: called CC=1 SCAR=0.0
Pod undocumented
266 7 my ($self, $entry) = @_;
267
268 7 my @dirs = split /\./, $entry;
269 7 $dirs[-1] .= '.js';
270
271 7 return file($self->lib_dir, @dirs);
272 }
273  
274  
275 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
276 2 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
277  
278  
279 2 1;
280  
281 __END__
« Plugin/Web/PkgVersion.pm ↑ Summary App/Command/add_header.pm »