Spoon/Installer.pm

filter: Statementstmt 52.5% 83 / 158 Branchbran 32.8% 25 / 76 Conditioncond 23.5% 4 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 63.6% 14 / 22 Podpod 0.0% 0 / 16 totaltotal 42.0% 126 / 300 CC 46 SCAR 32.7
CC
46
Cov
45%
compress_lib
90.0
compress_files
56.0
get_file_content
30.0
CRAP 26.3
↑ Summary Spoon/Trace.pm »
1 package Spoon::Installer;
2 3 ▶use Spiffy -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Spoon::Base -mixin => qw(hub);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 const extract_to => '.';
7 3 field quiet => 0;
8  
9 0 ▶sub compress_from {
Subroutine compress_from: not called CC=1 SCAR=6.9
Pod undocumented
10 0 $self->extract_to;
11 }
12  
13 2 ▶sub extract_files {
Subroutine extract_files: called CC=10 SCAR=29.0
Pod undocumented
14 2 my @files = $self->get_packed_files;
15 2 ▶ while (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 8 2 10
16 8 my ($file_name, $file_contents) = splice(@files, 0, 2);
17 8 my $locked = $file_name =~ s/^!//;
18 8 my $file_path = join '/', $self->extract_to, $file_name;
19 8 my $file = io->file($file_path)->assert;
20 8 ▶ if ($locked and -f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locked and -f $file_path) 0 8 8
Condition33%1/3
$locked and -f $file_path
!ll&&!rl&&r
800
MC/DC0%0/2
$locked and -f $file_path
$locked -f $file_path
Truth table33%1/3
A and B
A= $lockedB= -f $file_path
ABresult
0X0
100
111
21 0 ▶ warn " Skipping $file (already exists)\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
22 0 next;
23 }
24 8 my $content = $self->set_file_content($file_path, $file_contents);
25 8 ▶ if ($file->exists and $file->all eq $content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists and $file->all eq $content) 0 8 8
Condition33%1/3
$file->exists and $file->all eq $content
!ll&&!rl&&r
800
MC/DC0%0/2
$file->exists and $file->all eq $content
$file->exists $file->all eq $content
Truth table33%1/3
A and B
A= $file->existsB= $file->all eq $content
ABresult
0X0
100
111
26 0 ▶ warn " Skipping $file (unchanged)\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
27 0 next;
28 }
29 8 ▶ warn " - $file\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 8 8
30 8 ▶ $file->binary if $self->file_is_binary($file_path);
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_binary($file_path) 0 8 8
31 8 $file->assert->print($content);
32 }
33 }
34  
35 8 ▶sub set_file_content {
Subroutine set_file_content: called CC=4 SCAR=14.0
Pod undocumented
36 8 my $path = shift;
37 8 my $content = shift;
38 8 ▶ $content = $self->base64_decode($content)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_binary($path) 0 8 8
39 if $self->file_is_binary($path);
40 8 ▶ $content = $self->fix_hashbang($content)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_executable($path) 0 8 8
41 if $self->file_is_executable($path);
42 8 ▶ $content = $self->wrap_html($content, $path)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_html($path) 5 3 8
43 if $self->file_is_html($path);
44 8 return $content;
45 }
46  
47 16 ▶sub file_is_binary {
Subroutine file_is_binary: called CC=1 SCAR=0.0
Pod undocumented
48 16 my $path = shift;
49 16 $path =~ /\.(gif|jpg|png)$/;
50 }
51  
52 8 ▶sub file_is_executable {
Subroutine file_is_executable: called CC=1 SCAR=0.0
Pod undocumented
53 8 my $path = shift;
54 8 $path =~ /\.(pl|cgi)$/;
55 }
56  
57 8 ▶sub file_is_html {
Subroutine file_is_html: called CC=1 SCAR=0.0
Pod undocumented
58 8 my $path = shift;
59 8 $path =~ /\.html$/;
60 }
61  
62 0 ▶sub fix_hashbang {
Subroutine fix_hashbang: not called CC=1 SCAR=6.9
Pod undocumented
63 0 require Config;
64 0 my $content = shift;
65 0 $content =~ s/^#!.*\n/$Config::Config{startperl} -w\n/;
66 0 return $content;
67 }
68  
69 5 ▶sub wrap_html {
Subroutine wrap_html: called CC=3 SCAR=11.0
Pod undocumented
70 5 my ($content, $path) = @_;
71 5 $path =~ s/^.*\/(.*)$/$1/;
72 5 $path =~ s/\.html$//;
73 5 $content = $self->strip_html($content);
74 5 ▶ $content = "<!-- BEGIN $path -->\n$content"
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /^\s/ 3 2 5
75 unless $content =~ /^\s/;
76 5 ▶ $content = "$content<!-- END $path -->\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\s\n\z/ 3 2 5
77 unless $content =~ /\s\n\z/;
78 5 return $content;
79 }
80  
81 2 ▶sub get_packed_files {
Subroutine get_packed_files: called CC=10 SCAR=23.2
Pod undocumented
82 2 my %seen;
83 my @return;
84 2 for my $class (@{Spiffy::all_my_bases(ref $self)}) {
85 7 ▶ next if $class =~ /-/;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /-/ 2 5 7
86 5 ▶ last if $class =~ /^Spoon/;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^Spoon/ 2 3 5
87 3 ▶ my $data = $self->data($class)
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->data($class) or next 3 0 3
88 or next;
89 3 my @files = split /^__(.+)__\n/m, $data;
90 3 shift @files;
91 3 ▶ while (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 9 3 12
92 9 my ($name, $content) = splice(@files, 0, 2);
93 9 ▶ $name = $self->resolve_install_path($name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("resolve_install_path") 0 9 9
94 if $self->can('resolve_install_path');
95 9 my $name2 = $name;
96 9 $name2 =~ s/^\!//;
97 9 ▶ next if $seen{$name2}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name2}++ 1 8 9
98 8 ▶ $content ||= '';
Condition50%1/2
$content ||= ""
l!l
80
MC/DC0%0/1
$content ||= ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
99 8 ▶ push @return, $name, $content
Branch: 1 branch
BranchTrueFalseTotal
if length $content 8 0 8
100 if length $content;
101 }
102 }
103 2 return @return;
104 }
105  
106 0 ▶sub get_local_packed_files {
Subroutine get_local_packed_files: not called CC=4 SCAR=30.0
Pod undocumented
107 0 my @return;
108 0 my $class = ref $self;
109 0 ▶ my $data = $self->data($class)
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->data($class) or return 0 0 0
110 or return;
111 0 my @files = split /^__(.+)__\n/m, $data;
112 0 shift @files;
113 0 ▶ while (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 0 0 0
114 0 my ($name, $content) = splice(@files, 0, 2);
115 0 ▶ $name = $self->resolve_install_path($name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("resolve_install_path") 0 0 0
116 if $self->can('resolve_install_path');
117 0 push @return, $name, $content;
118 }
119 0 return @return;
120 }
121  
122 3 ▶sub data {
Subroutine data: called CC=2 SCAR=7.2
Pod undocumented
123 3 ▶ my $package = shift || ref($self);
Condition33%1/3
shift() || ref $self
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || ref $self
shift() ref $self
Truth table33%1/3
A || B
A= shift()B= ref $self
ABresult
1X1
011
000
124 3 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
125 3 local $/;
126 3 eval "package $package; <DATA>";
127 }
128  
129 0 ▶sub compress_files {
Subroutine compress_files: not called CC=7 SCAR=40.3
Pod undocumented
130 0 require File::Spec;
131 0 my $source_dir = shift;
132 0 my $new_pack = '';
133 0 my @files = $self->get_local_packed_files;
134 0 ▶ my $first_file = $files[0]
Branch: 1 branch
BranchTrueFalseTotal
my $first_file = $files[0] or return 0 0 0
135 or return;
136 0 my $directory = $self->compress_from;
137 0 ▶ while (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 0 0 0
138 0 my ($file_name, $file_contents) = splice(@files, 0, 2);
139 0 ▶ my $locked = $file_name =~ s/^!// ? '!' : '';
Branch: 1 branch
BranchTrueFalseTotal
$file_name =~ s/^!// ? : 0 0 0
140 0 my $source_path =
141 File::Spec->canonpath("$source_dir/$directory/$file_name");
142 0 ▶ die "$file_name does not exist as $source_path"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $source_path 0 0 0
143 unless -f $source_path;
144 0 ▶ my $content = $locked
Branch: 1 branch
BranchTrueFalseTotal
$locked ? : 0 0 0
145 ? $file_contents
146 : $self->get_file_content($source_path);
147 0 $content =~ s/\r\n/\n/g;
148 0 $content =~ s/\r/\n/g;
149 0 $new_pack .= "__$locked${file_name}__\n$content";
150 }
151 0 my $module = ref($self) . '.pm';
152 0 $module =~ s/::/\//g;
153 0 ▶ my $module_path = $INC{$module} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $module_path = $INC{$module} or die 0 0 0
154 0 my $module_text = io($module_path)->all;
155 0 my ($module_code) = split /^__\Q$first_file\E__\n/m, $module_text;
156 0 ($module_code . $new_pack) > io($module_path);
157 }
158  
159 0 ▶sub get_file_content {
Subroutine get_file_content: not called CC=5 SCAR=34.0
Pod undocumented
160 0 my $path = shift;
161 0 my $content = io($path)->all;
162 0 ▶ $content = $self->base64_encode($content)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_binary($path) 0 0 0
163 if $self->file_is_binary($path);
164 0 ▶ $content = $self->unfix_hashbang($content)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_executable($path) 0 0 0
165 if $self->file_is_executable($path);
166 0 ▶ $content = $self->strip_html($content)
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_is_html($path) 0 0 0
167 if $self->file_is_html($path);
168 0 ▶ $content .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\n\z/ 0 0 0
169 unless $content =~ /\n\z/;
170 0 return $content;
171 }
172  
173 0 ▶sub unfix_hashbang {
Subroutine unfix_hashbang: not called CC=1 SCAR=6.9
Pod undocumented
174 0 my $content = shift;
175 0 $content =~ s/^#!.*\n/#!\/usr\/bin\/perl\n/;
176 0 return $content;
177 }
178  
179 5 ▶sub strip_html {
Subroutine strip_html: called CC=1 SCAR=0.0
Pod undocumented
180 5 my $content = shift;
181 5 $content =~ s/^<!-- BEGIN .* -->\n//;
182 5 $content =~ s/(?<=\n)<!-- END .* -->\n\z//;
183 5 return $content;
184 }
185  
186 0 ▶sub compress_lib {
Subroutine compress_lib: not called CC=9 SCAR=45.0
Pod undocumented
187 0 ▶ die "Must be run from the module source code directory\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d "lib" and -f "Makefile.PL" 0 0 0
Condition0%0/3
-d "lib" and -f "Makefile.PL"
!ll&&!rl&&r
000
MC/DC0%0/2
-d "lib" and -f "Makefile.PL"
-d "lib" -f "Makefile.PL"
Truth table0%0/3
A and B
A= -d "lib"B= -f "Makefile.PL"
ABresult
0X0
100
111
188 unless -d 'lib' and -f 'Makefile.PL';
189 0 unshift @INC,'lib';
190 0 ▶ my $source_dir = shift
Branch: 1 branch
BranchTrueFalseTotal
my $source_dir = shift() or die "No source directory specified\n" 0 0 0
191 or die "No source directory specified\n";
192 0 ▶ die "Invalid source directory '$source_dir'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $source_dir 0 0 0
193 unless -d $source_dir;
194 map {
195 0 my $class_name = $_;
196 0 my $class_id = $class_name->class_id;
197 0 $self->hub->config->add_config(
198 +{ "${class_id}_class" => $class_name }
199 );
200 0 ▶ warn "Compressing $class_name\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
201 0 $self->hub->$class_id->compress_files($source_dir);
202 }
203 grep {
204 0 my $name = $_;
205 0 eval "require $name";
206 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
207 0 ▶ UNIVERSAL::can($name, 'compress_files')
Condition0%0/3
UNIVERSAL::can($name, 'compress_files') && !($name =~ /::(Installer)$/)
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($name, 'compress_files') && !($name =~ /::(Installer)$/)
UNIVERSAL::can($name, 'compress_files') !($name =~ /::(Installer)$/)
Truth table0%0/3
A && not(B)
A= UNIVERSAL::can($name, 'compress_files')B= !($name =~ /::(Installer)$/)
ABresult
0X0
100
111
208 and $name !~ /::(Installer)$/;
209 } map {
210 0 my $name = $_->name;
211 0 ▶ ($name =~ s/^lib\/(.*)\.pm$/$1/) ? do {
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s[^lib/(.*)\.pm$][$1] ? : 0 0 0
212 0 $name =~ s/\//::/g;
213 0 $name;
214 } : ();
215 } io('lib')->All_Files;
216 }
217  
218 3 __END__
↑ Summary Spoon/Trace.pm »