Simple/Helpers.pm

filter: Statementstmt 19.8% 21 / 106 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 30.0% 6 / 20 Podpod n/a totaltotal 17.0% 27 / 158 CC 16 SCAR 26.5
CC
16
Cov
16%
_generate_spec_yaml
33.7
_tidy_source
21.1
_resolve_spec
20.0
CRAP 14.1
« Simple/TempFile.pm ↑ Summary Simple/Modulino.pm »
1 package CLI::Simple::Helpers;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 our @EXPORT_OK = qw(
9 _class_to_dist
10 _class_to_filename
11 _cmd_to_role
12 _extract_pod
13 _find_module_path
14 _generate_spec_yaml
15 _is_class_name
16 _main_module_stub
17 _pm_path
18 _resolve_spec
19 _role_stub
20 _spec_filename
21 _sub_to_role
22 _tarball_filename
23 );
24  
25 17 our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
26  
27 17 our $VERSION = '2.6.0';
28  
29 ########################################################################
30 0 ▶sub _class_to_dist {
Subroutine _class_to_dist: not called CC=1 SCAR=6.9
31 ########################################################################
32 0 my ($class) = @_;
33  
34 0 ( my $dist = $class ) =~ s/::/-/gxsm;
35  
36 0 return $dist;
37 }
38  
39 ########################################################################
40 0 ▶sub _class_to_filename {
Subroutine _class_to_filename: not called CC=1 SCAR=6.9
41 ########################################################################
42 0 my ($class) = @_;
43  
44 0 return lc _class_to_dist($class);
45 }
46  
47 ########################################################################
48 0 ▶sub _spec_filename {
Subroutine _spec_filename: not called CC=1 SCAR=6.9
49 ########################################################################
50 0 my ($class) = @_;
51  
52 0 return _class_to_filename($class) . '.yml';
53 }
54  
55 ########################################################################
56 0 ▶sub _tarball_filename {
Subroutine _tarball_filename: not called CC=1 SCAR=6.9
57 ########################################################################
58 0 my ($class) = @_;
59  
60 0 return _class_to_filename($class) . '-roles.tar.gz';
61 }
62  
63 ########################################################################
64 0 ▶sub _pm_path {
Subroutine _pm_path: not called CC=1 SCAR=6.9
65 ########################################################################
66 0 my ($package) = @_;
67  
68 0 ( my $path = "lib/$package.pm" ) =~ s|::|/|gxsm;
69  
70 0 return $path;
71 }
72  
73 ########################################################################
74 0 ▶sub _extract_pod {
Subroutine _extract_pod: not called CC=1 SCAR=6.9
75 ########################################################################
76 0 my ($content) = @_;
77  
78 0 my @pods = $content =~ /(^=(?:pod|head\d|over|item|begin|for|encoding).+?^=cut)/gxsm;
79  
80 0 return join q{}, @pods;
81 }
82  
83 ########################################################################
84 19 ▶sub _is_class_name {
Subroutine _is_class_name: called CC=1 SCAR=0.0
85 ########################################################################
86 19 my ($value) = @_;
87  
88 19 return $value =~ /::/xsm;
89 }
90  
91 ########################################################################
92 0 ▶sub _cmd_to_role {
Subroutine _cmd_to_role: not called CC=1 SCAR=6.9
93 ########################################################################
94 0 my ( $class, $cmd ) = @_;
95  
96 0 my $suffix = join q{::}, map { ucfirst lc $_ } split /[-_]/xsm, $cmd;
97  
98 0 return "${class}::Role::${suffix}";
99 }
100  
101 ########################################################################
102 0 ▶sub _sub_to_role {
Subroutine _sub_to_role: not called CC=1 SCAR=6.9
103 ########################################################################
104 0 my ( $class, $sub ) = @_;
105  
106 0 ( my $name = $sub ) =~ s/\Acmd_//xsm;
107  
108 0 return _cmd_to_role( $class, $name );
109 }
110  
111 ########################################################################
112 0 ▶sub _resolve_spec {
Subroutine _resolve_spec: not called CC=4 SCAR=30.0
113 ########################################################################
114 0 my ( $class, $commands, $option_specs ) = @_;
115  
116 # 1. explicit spec file from command line
117 0 my $spec_file = $ARGV[1];
118  
119 0 ▶ if ($spec_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec_file) 0 0 0
120 0 ▶ die "ERROR: spec file not found: $spec_file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $spec_file 0 0 0
121 if !-e $spec_file;
122  
123 0 require YAML::Tiny;
124 0 return YAML::Tiny::LoadFile($spec_file);
125 }
126  
127 # 2. conventional spec file in cwd
128 0 my $default_spec = _spec_filename($class);
129  
130 0 ▶ if ( -e $default_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $default_spec) 0 0 0
131 0 require YAML::Tiny;
132 0 return YAML::Tiny::LoadFile($default_spec);
133 }
134  
135 # 3. introspect from running module
136 return {
137 options => $option_specs,
138 0 commands => { map { $_ => $commands->{$_} } grep { !/\A-/xsm } keys %{$commands} },
139 };
140 }
141  
142 ########################################################################
143 0 ▶sub _generate_spec_yaml {
Subroutine _generate_spec_yaml: not called CC=6 SCAR=35.2
144 ########################################################################
145 0 my ( $class, $commands, $option_specs, $use_roles ) = @_;
146  
147 0 require YAML::Tiny;
148  
149 0 my %spec_commands;
150  
151 0 for my $cmd ( sort grep { !/\A-/xsm } keys %{$commands} ) {
152 0 my $val = $commands->{$cmd};
153  
154 # resolve coderef to actual sub name regardless of mode
155 0 my $sub_name;
156 0 ▶ if ( ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 0 0
157 0 require B;
158 0 my $cv = B::svref_2object($val);
159 0 $sub_name = $cv->GV->NAME;
160 }
161 else {
162 0 $sub_name = $val;
163 }
164  
165 0 ( my $expected = "cmd_$cmd" ) =~ s/-/_/gxsm;
166  
167 0 ▶ if ( $use_roles && $sub_name eq $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_roles and $sub_name eq $expected) { } 0 0 0
Condition0%0/3
$use_roles and $sub_name eq $expected
!ll&&!rl&&r
000
MC/DC0%0/2
$use_roles and $sub_name eq $expected
$use_roles $sub_name eq $expected
Truth table0%0/3
A and B
A= $use_rolesB= $sub_name eq $expected
ABresult
0X0
100
111
168 # derive role from the actual sub name, not the command key
169 # cmd_extract -> extract -> Pod::Extract::Role::Extract
170 0 ( my $name = $sub_name ) =~ s/\Acmd_//xsm;
171 0 $spec_commands{$cmd} = _cmd_to_role( $class, $name );
172 }
173 else {
174 0 $spec_commands{$cmd} = $sub_name;
175 }
176 }
177  
178 # extra_options live in the class stash
179 17 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
180 0 my $stash = \%{ $class . '::' };
181 0 ▶ my @extra_options = $stash->{EXTRA_OPTIONS} ? @{ ${ $stash->{EXTRA_OPTIONS} } } : ();
Branch: 1 branch
BranchTrueFalseTotal
$stash->{'EXTRA_OPTIONS'} ? : 0 0 0
182  
183 0 return YAML::Tiny::Dump(
184 { options => $option_specs,
185 extra_options => \@extra_options,
186 commands => \%spec_commands,
187 }
188 );
189 }
190  
191 ########################################################################
192 0 ▶sub _main_module_stub {
Subroutine _main_module_stub: not called CC=1 SCAR=6.9
193 ########################################################################
194 0 my ($class) = @_;
195  
196 0 my $stub = <<'END_MODULE';
197 package $class;
198  
199 use strict;
200 use warnings;
201  
202 use CLI::Simple qw(:roles);
203 use parent qw(CLI::Simple);
204  
205 our $VERSION = '%s';
206  
207 caller or exit __PACKAGE__->main;
208  
209 1;
210 END_MODULE
211 0 $stub = sprintf $stub, q{@} . 'PACKAGE' . q{@}; # to avoid replacement of PACKAGE_VERSION
212  
213 0 return _tidy_source($stub);
214 }
215  
216 ########################################################################
217 0 ▶sub _role_stub {
Subroutine _role_stub: not called CC=1 SCAR=6.9
218 ########################################################################
219 0 my ( $role, $method, $class ) = @_;
220  
221 0 my $stub = <<"END_ROLE";
222 package $role;
223  
224 use strict;
225 use warnings;
226  
227 use English qw(-no_match_vars);
228 use CLI::Simple::Constants qw(:booleans);
229 #use CLI::Simple::Utils qw(choose slurp slurp_json);
230 #use Cwd qw(abs_path getcwd);
231 #use File::Basename qw(basename dirname);
232  
233 use Role::Tiny;
234  
235 ########################################################################
236 sub $method {
237 ########################################################################
238 my (\$self) = \@_;
239  
240 # TODO: migrated from $class - move implementation here
241 die "ERROR: not yet implemented\\n";
242  
243 return \$SUCCESS;
244 }
245  
246 1;
247 END_ROLE
248  
249 0 return _tidy_source($stub);
250 }
251  
252 ########################################################################
253 0 ▶sub _find_module_path {
Subroutine _find_module_path: not called CC=4 SCAR=30.0
254 ########################################################################
255 0 my ($class) = @_;
256  
257 0 ( my $rel = $class ) =~ s|::|/|gxsm;
258 0 $rel .= '.pm';
259  
260 # check %INC first - module is already loaded
261 0 ▶ return $INC{$rel} if $INC{$rel};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$rel} 0 0 0
262  
263 # fall back to @INC search
264 0 for my $dir (@INC) {
265 0 my $path = "$dir/$rel";
266 0 ▶ return $path if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 0 0
267 }
268  
269 0 return;
270 }
271  
272 ########################################################################
273 0 ▶sub _tidy_source {
Subroutine _tidy_source: not called CC=5 SCAR=30.5
274 ########################################################################
275 0 my ($source) = @_;
276  
277 0 ▶ return $source if !eval { require Perl::Tidy; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Perl::Tidy; 1 } } 0 0 0
278  
279 0 ▶ my $perltidyrc = glob('~/.perltidyrc');
Subroutine BEGIN: called CC=1 SCAR=0.2
280  
281 0 ▶ return $source if !$perltidyrc || !-e $perltidyrc;
Branch: 1 branch
BranchTrueFalseTotal
unless $perltidyrc and -e $perltidyrc 0 0 0
Condition0%0/3
$perltidyrc and -e $perltidyrc
!ll&&!rl&&r
000
MC/DC0%0/2
$perltidyrc and -e $perltidyrc
$perltidyrc -e $perltidyrc
Truth table0%0/3
A and B
A= $perltidyrcB= -e $perltidyrc
ABresult
0X0
100
111
282  
283 0 my $tidied = q{};
284 0 my $errors = q{};
285  
286 0 Perl::Tidy::perltidy(
287 source => \$source,
288 destination => \$tidied,
289 stderr => \$errors,
290 perltidyrc => $perltidyrc,
291 argv => [],
292 );
293  
294 0 ▶ return length($tidied) ? $tidied : $source;
Branch: 1 branch
BranchTrueFalseTotal
length $tidied ? : 0 0 0
295 }
296  
297 17 1;
298  
299 __END__
« Simple/TempFile.pm ↑ Summary Simple/Modulino.pm »