Simple/Helpers.pm

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