lib/Dist/Zilla/Plugin/JSAN/Bundle.pm

filter: Statementstmt 25.0% 25 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 7 totaltotal 20.0% 32 / 160 CC 18 SCAR 27.3
CC
18
Cov
19%
get_component_content
30.0
get_npm_root
30.0
concatenate_for_task
20.0
CRAP 15.4
↑ Summary lib/Dist/Zilla/Plugin/JSAN/GitHubDocs.pm »
1 package Dist::Zilla::Plugin::JSAN::Bundle;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Dist::Zilla::Plugin::JSAN::Bundle::VERSION = '0.06';
4 }
5  
6 # ABSTRACT: Bundle the library files into "tasks", using information from Components.JS
7  
8 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 with 'Dist::Zilla::Role::FileGatherer';
11 1 with 'Dist::Zilla::Role::FileMunger';
12  
13 1 ▶use Dist::Zilla::File::FromCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use JSON 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Capture::Tiny qw/capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 1 has 'npm_root' => (
21 isa => 'Str',
22 is => 'rw',
23 );
24  
25  
26 1 has 'roots_only_for_release' => (
27 is => 'rw',
28 default => 1
29 );
30  
31  
32 #================================================================================================================================================================================================================================================
33 0 ▶sub gather_files {
Subroutine gather_files: not called CC=1 SCAR=0.0
Pod undocumented
34 }
35  
36  
37 #================================================================================================================================================================================================================================================
38 0 ▶sub munge_files {
Subroutine munge_files: not called CC=3 SCAR=24.8
Pod undocumented
39 0 my $self = shift;
40
41 0 ▶ return unless -f 'Components.JS';
Branch: 1 branch
BranchTrueFalseTotal
unless -f "Components.JS" 0 0 0
42
43 0 my $components = file('Components.JS')->slurp;
44  
45 #removing // style comments
46 0 $components =~ s!//.*$!!gm;
47  
48 #extracting from outermost {} brackets
49 0 $components =~ m/(\{.*\})/s;
50 0 $components = $1;
51  
52 0 my $deploys = decode_json $components;
53  
54 0 foreach my $deploy (keys(%$deploys)) {
55 0 $self->concatenate_for_task($deploys, $deploy);
56 }
57 }
58  
59  
60 #================================================================================================================================================================================================================================================
61 0 ▶sub concatenate_for_task {
Subroutine concatenate_for_task: not called CC=4 SCAR=30.0
Pod undocumented
62 0 my ($self, $deploys, $task_name) = @_;
63
64 0 my @components = $self->expand_task_entry($deploys, $task_name);
65 0 ▶ die "No components in task: [$task_name]" unless @components > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @components > 0 0 0 0
66
67 0 my @dist_dirs = split /-/, $self->zilla->name;
68 0 push @dist_dirs, $task_name;
69 0 $dist_dirs[-1] .= '.js';
70
71 0 ▶ my $generate = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
72 0 my $bundle_content = '';
73
74 0 foreach my $comp (@components) {
75 0 $bundle_content .= $self->get_component_content($comp) . ";\n";
76 }
77
78 0 return $bundle_content;
79 0 };
80
81 0 $self->add_file(Dist::Zilla::File::FromCode->new({
82
83 name => file('lib', 'Task', @dist_dirs) . '',
84
85 code => $generate
86 }));
87
88 0 ▶ if (!$self->roots_only_for_release || $ENV{ DZIL_RELEASING }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->roots_only_for_release or $ENV{"DZIL_RELEASING"}) 0 0 0
Condition0%0/3
not $self->roots_only_for_release or $ENV{"DZIL_RELEASING"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->roots_only_for_release or $ENV{"DZIL_RELEASING"}
not $self->roots_only_for_release $ENV{"DZIL_RELEASING"}
Truth table0%0/3
not(A) or B
A= not $self->roots_only_for_releaseB= $ENV{"DZIL_RELEASING"}
ABresult
1X1
011
000
89
90 0 my $root_file_name = join("-", 'task', map { lc } @dist_dirs);
91
92 0 $self->add_file(Dist::Zilla::File::FromCode->new({
93
94 name => file($root_file_name) . '',
95
96 code => $generate
97 }));
98 }
99 }
100  
101  
102 #================================================================================================================================================================================================================================================
103 0 ▶sub expand_task_entry {
Subroutine expand_task_entry: not called CC=4 SCAR=30.0
Pod undocumented
104 0 my ($self, $deploys, $task_name, $seen) = @_;
105
106 0 ▶ $seen = {} if !$seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen 0 0 0
107
108 0 ▶ die "Recursive visit to task [$task_name] when expanding entries" if $seen->{ $task_name };
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$task_name} 0 0 0
109
110 0 $seen->{ $task_name } = 1;
111
112 return map {
113
114 0 ▶ /^\+(.+)/ ? $self->expand_task_entry($deploys, $1, $seen) : $_;
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)/ ? : 0 0 0
115
116 0 } @{$deploys->{ $task_name }};
117 }
118  
119  
120 #================================================================================================================================================================================================================================================
121 0 ▶sub get_component_content {
Subroutine get_component_content: not called CC=5 SCAR=34.0
Pod undocumented
122 0 my ($self, $component) = @_;
123
124 0 ▶ if ((ref $component eq 'HASH') && $component->{ text }) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $component eq 'HASH' and $component->{'text'}) { } 0 0 0
elsif ($component =~ /^jsan:(.+)/) { } 0 0 0
elsif ($component =~ /^=(.+)/) { } 0 0 0
Condition0%0/3
ref $component eq "HASH" and $component->{"text"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $component eq "HASH" and $component->{"text"}
ref $component eq "HASH" $component->{"text"}
Truth table0%0/3
A and B
A= ref $component eq "HASH"B= $component->{"text"}
ABresult
0X0
100
111
125
126 0 return $component->{ text };
127
128 } elsif ($component =~ /^jsan:(.+)/) {
129
130 0 my @file = ($self->get_npm_root, '.jsan', split /\./, $1);
131 0 $file[ -1 ] .= '.js';
132
133 0 return file(@file)->slurp;
134 } elsif ($component =~ /^=(.+)/) {
135 0 return file($1)->slurp;
136 } else {
137 0 my $file_name = $self->comp_to_filename($component);
138
139 0 my ($found) = grep { $_->name eq $file_name } (@{$self->zilla->files});
140
141 0 return $found->content;
142 }
143 }
144  
145  
146 #================================================================================================================================================================================================================================================
147 0 ▶sub get_npm_root {
Subroutine get_npm_root: not called CC=5 SCAR=34.0
Pod undocumented
148 0 my ($self) = @_;
149
150 0 ▶ return $self->npm_root if $self->npm_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->npm_root 0 0 0
151
152 0 $self->log('Trying to determine the `root` config setting of `npm`');
153
154 0 my $root = $ENV{npm_config_root};
155
156 0 ▶ if ($root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root) 0 0 0
157
158 0 $self->npm_root($root);
159
160 0 $self->log("Found: [$root]");
161
162 0 return $self->npm_root;
163 };
164
165 0 my $exit_code;
166
167 0 ▶ my ($stdout, $stderr) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 system('npm root -g');
169
170 0 $exit_code = $? >> 8;
171 0 };
172
173 0 chomp($stdout);
174
175 0 ▶ if (!$exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exit_code) 0 0 0
176 0 $self->log("Found: [$stdout]");
177
178 0 $self->npm_root($stdout);
179
180 0 return $self->npm_root;
181 };
182
183 0 $self->log('`npm config get root` failed, trying with [sudo]');
184
185 0 ▶ ($stdout, $stderr) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 system('sudo npm config get root');
187
188 0 $exit_code = $? >> 8;
189 0 };
190
191 0 chomp($stdout);
192
193 0 ▶ if (!$exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exit_code) 0 0 0
194 0 $self->log("Found: [$stdout]");
195
196 0 $self->npm_root($stdout);
197
198 0 return $self->npm_root;
199 };
200
201 0 die "Can't determine the `npm` root";
202 }
203  
204  
205 #================================================================================================================================================================================================================================================
206 0 ▶sub comp_to_filename {
Subroutine comp_to_filename: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my ($self, $comp) = @_;
208
209 0 my @dirs = split /\./, $comp;
210 0 $dirs[-1] .= '.js';
211
212 0 return file('lib', @dirs);
213 }
214  
215  
216 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
218  
219  
220 1 1;
221  
222  
223  
224 __END__
↑ Summary lib/Dist/Zilla/Plugin/JSAN/GitHubDocs.pm »