App/Utils.pm

filter: Statementstmt 97.4% 150 / 154 Branchbran 85.4% 53 / 62 Conditioncond 55.0% 11 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 84.2% 16 / 19 Podpod 100% 6 / 6 totaltotal 87.5% 239 / 273 CC 44 SCAR 20.5
CC
44
Cov
91%
_pod_node_to_text
13.0
split_string
11.4
parse_pod
11.2
CRAP 7.8
« App/Plugin/Depends/Meta/Class.pm ↑ Summary App/ParsedArgv.pm »
1 package MooseX::App::Utils;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 our $SCREEN_WIDTH = 78;
11 17 our $INDENT = 4;
12  
13 17 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 subtype 'MooseX::App::Types::List'
16 => as 'ArrayRef';
17  
18 coerce 'MooseX::App::Types::List'
19 => from 'Str'
20 0 ▶ => via { [$_] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21  
22 17 subtype 'MooseX::App::Types::CmdTypes'
23 => as enum([qw(proto option parameter)]);
24  
25 17 subtype 'MooseX::App::Types::MessageString'
26 => as 'Str';
27  
28 coerce 'MooseX::App::Types::MessageString'
29 => from 'ArrayRef'
30 0 ▶ => via { sprintf(@{$_}) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
31  
32 subtype 'MooseX::App::Types::Env'
33 => as 'Str'
34 13 ▶ => where { m/^[A-Z0-9_]+$/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 subtype 'MooseX::App::Types::Identifier'
37 => as 'Str'
38 273 ▶ => where {
Subroutine __ANON__: called CC=3 SCAR=12.0
39 273 ▶ $_ eq '?'
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "?" or /^[A-Za-z0-9][A-Za-z0-9_-]*$/ and /[^-_]$/ 68 205 273
Condition50%3/6
$_ eq '?' || /^[A-Za-z0-9][A-Za-z0-9_-]*$/ && /[^-_]$/
l!l&&r!l&&!r
682050
/^[A-Za-z0-9][A-Za-z0-9_-]*$/ and /[^-_]$/
!ll&&!rl&&r
00205
MC/DC0%0/3
$_ eq '?' || /^[A-Za-z0-9][A-Za-z0-9_-]*$/ && /[^-_]$/
$_ eq '?' /^[A-Za-z0-9][A-Za-z0-9_-]*$/ /[^-_]$/
Truth table50%2/4
A || B and C
A= $_ eq '?'B= /^[A-Za-z0-9][A-Za-z0-9_-]*$/C= /[^-_]$/
ABCresult
1XX1
0111
00X0
0100
40 17 || (m/^[A-Za-z0-9][A-Za-z0-9_-]*$/ && m/[^-_]$/) };
41  
42 17 subtype 'MooseX::App::Types::IdentifierList'
43 => as 'ArrayRef[MooseX::App::Types::Identifier]';
44  
45 coerce 'MooseX::App::Types::IdentifierList'
46 => from 'MooseX::App::Types::Identifier'
47 0 ▶ => via { [$_] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
48  
49 17 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # Default package name to command name translation function
52 30 ▶sub class_to_command {
Subroutine class_to_command: called CC=3 SCAR=11.0
Pod documented
53 30 my ($class) = @_;
54  
55 return
56 30 ▶ unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 30 30
57  
58 30 my @commands;
59 30 foreach my $part (split /\s+/,$class) {
60 32 my @parts = split( /_+|\b|(?<![A-Z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/, $part );
61 32 push (@commands,join('_',@parts));
62 }
63 30 return lc(join(" ",@commands));
64 }
65  
66 # Format output text for fixed screen width
67 54 ▶sub format_text {
Subroutine format_text: called CC=2 SCAR=6.9
Pod documented
68 54 my ($text) = @_;
69  
70 54 my @lines;
71 54 foreach my $line (split(/\n/,$text)) {
72 162 push(@lines,split_string($SCREEN_WIDTH-$INDENT,$line));
73 }
74  
75 return join(
76 "\n",
77 54 map { (' ' x $INDENT).$_ }
78 @lines
79 );
80 }
81  
82 # Format bullet list for fixed screen width
83 79 ▶sub format_list {
Subroutine format_list: called CC=4 SCAR=13.9
Pod documented
84 79 my (@list) = @_;
85  
86 79 my $max_length = max(map { length($_->[0]) } @list);
87 79 my $description_length = $SCREEN_WIDTH - $max_length - 7;
88 79 my $prefix_length = $max_length + $INDENT + 2;
89 79 my @return;
90  
91 # Loop all items
92 79 foreach my $command (@list) {
93 258 ▶ my $description = $command->[1] // '';
Condition100%2/2
$command->[1] // ""
l!l
2562
MC/DC100%1/1
$command->[1] // ""
$command->[1]
Truth table100%2/2
A
A= $command->[1]
Aresult
11
00
94 258 my @lines = split_string($description_length,$description);
95 258 push (@return,(' 'x$INDENT).sprintf('%-*s %s',$max_length,$command->[0],shift(@lines)));
96 258 ▶ while (my $line = shift (@lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 16 258 274
97 16 push(@return,' 'x $prefix_length.$line);
98 }
99 }
100 79 return join("\n",@return);
101 }
102  
103 # Simple splitting of long sentences on whitespaces or punctuation
104 420 ▶sub split_string {
Subroutine split_string: called CC=11 SCAR=24.3
Pod documented
105 420 my ($maxlength, $string) = @_;
106  
107 return
108 420 ▶ unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 420 420
109  
110 420 ▶ return $string
Branch: 1 branch
BranchTrueFalseTotal
if length $string <= $maxlength 392 28 420
111 if length $string <= $maxlength;
112  
113 28 my (@lines,$line);
114 28 $line = '';
115 28 foreach my $word (split(m/(\p{IsPunct}|\p{IsSpace})/,$string)) {
116 864 ▶ if (length($line.$word) <= $maxlength) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line . $word <= $maxlength) { } 835 29 864
117 835 $line .= $word;
118 } else {
119 29 ▶ push(@lines,$line)
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 22 7 29
120 if ($line ne '');
121 29 $line = '';
122  
123 29 ▶ if (length($word) > $maxlength) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word > $maxlength) { } 7 22 29
124 7 my (@parts) = grep { $_ ne '' } split(/(.{$maxlength})/,$word);
125 7 my $lastline = pop(@parts);
126 7 push(@lines,@parts);
127 7 ▶ if (defined $lastline && $lastline ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastline and $lastline ne "") 7 0 7
Condition33%1/3
defined $lastline and $lastline ne ""
!ll&&!rl&&r
007
MC/DC0%0/2
defined $lastline and $lastline ne ""
defined $lastline $lastline ne ""
Truth table33%1/3
A and B
A= defined $lastlineB= $lastline ne ""
ABresult
0X0
100
111
128 7 $line = $lastline;
129 }
130 } else {
131 22 $line = $word;
132 }
133 }
134 }
135 28 ▶ push(@lines,$line)
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 28 0 28
136 if ($line ne '');
137  
138 28 ▶ @lines = map { m/^\s*(.+?)\s*$/ ? $1 : $_ } @lines;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*(.+?)\s*$/ ? : 57 0 57
139  
140 28 return @lines;
141 }
142  
143 # Try to get filename for a given package name
144 24 ▶sub package_to_filename {
Subroutine package_to_filename: called CC=4 SCAR=13.9
Pod documented
145 24 my ($package) = @_;
146  
147 # Package to filename
148 24 my $package_filename = $package;
149 24 $package_filename =~ s/::/\//g;
150 24 $package_filename .= '.pm';
151  
152  
153 24 my $package_filepath;
154 24 ▶ if (defined $INC{$package_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $INC{$package_filename}) 24 0 24
155 24 $package_filepath = $INC{$package_filename};
156 24 $package_filepath =~ s/\/{2,}/\//g;
157 }
158  
159 # No filename available
160 return
161 24 ▶ unless defined $package_filepath
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package_filepath and -e $package_filepath 4 20 24
Condition66%2/3
defined $package_filepath and -e $package_filepath
!ll&&!rl&&r
0420
MC/DC50%1/2
defined $package_filepath and -e $package_filepath
defined $package_filepath -e $package_filepath
Truth table66%2/3
A and B
A= defined $package_filepathB= -e $package_filepath
ABresult
0X0
100
111
162 && -e $package_filepath;
163  
164 20 return $package_filepath;
165 }
166  
167 # Parse pod
168 24 ▶sub parse_pod {
Subroutine parse_pod: called CC=11 SCAR=24.1
Pod documented
169 24 my ($package) = @_;
170  
171 24 my $package_filepath = package_to_filename($package);
172 return
173 24 ▶ unless $package_filepath;
Branch: 1 branch
BranchTrueFalseTotal
unless $package_filepath 4 20 24
174  
175 # Parse pod
176 20 my $document = Pod::Elemental->read_file($package_filepath);
177  
178 20 Pod::Elemental::Transformer::Pod5->new->transform_node($document);
179  
180 20 my $nester_head = Pod::Elemental::Transformer::Nester->new({
181 top_selector => Pod::Elemental::Selectors::s_command('head1'),
182 content_selectors => [
183 Pod::Elemental::Selectors::s_command([ qw(head2 head3 head4 over back item) ]),
184 Pod::Elemental::Selectors::s_flat()
185 ],
186 });
187 20 $document = $nester_head->transform_node($document);
188  
189 # Process pod
190 20 my %pod;
191 20 foreach my $element (@{$document->children}) {
192 # Distzilla ABSTRACT tag
193 35 ▶ if ($element->isa('Pod::Elemental::Element::Pod5::Nonpod')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->isa('Pod::Elemental::Element::Pod5::Nonpod')) { } 20 15 35
elsif ($element->isa('Pod::Elemental::Element::Nested') and $element->command eq 'head1') { } 13 4 17
194 20 ▶ if ($element->content =~ m/^\s*#+\s*ABSTRACT:\s*(.+)$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->content =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m) 1 19 20
195 1 ▶ $pod{ABSTRACT} ||= $1;
Condition33%1/3
$pod{"ABSTRACT"} ||= $1
l!l&&r!l&&!r
010
MC/DC0%0/2
$pod{"ABSTRACT"} ||= $1
$pod{"ABSTRACT"} $1
Truth table33%1/3
A ||= B
A= $pod{"ABSTRACT"}B= $1
ABresult
1X1
011
000
196 }
197 # Pod head1 sections
198 ▶ } elsif ($element->isa('Pod::Elemental::Element::Nested')
Condition66%2/3
$element->isa("Pod::Elemental::Element::Nested") and $element->command eq "head1"
!ll&&!rl&&r
2013
MC/DC50%1/2
$element->isa("Pod::Elemental::Element::Nested") and $element->command eq "head1"
$element->isa("Pod::Elemental::Element::Nested") $element->command eq "head1"
Truth table66%2/3
A and B
A= $element->isa("Pod::Elemental::Element::Nested")B= $element->command eq "head1"
ABresult
0X0
100
111
199 && $element->command eq 'head1') {
200  
201 13 ▶ if ($element->content eq 'NAME') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->content eq 'NAME') { } 2 11 13
202 2 my $content = _pod_node_to_text($element->children);
203 2 ▶ next unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 2 2
204 2 $content =~ s/^$package(\s-)?\s//;
205 2 chomp($content);
206 2 $pod{NAME} = $content;
207 } else {
208 11 my $content = _pod_node_to_text($element->children);
209 11 ▶ next unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 11 11
210 11 chomp($content);
211 11 $pod{uc($element->content)} = $content;
212 }
213 }
214 }
215  
216 20 return %pod;
217 }
218  
219 # Transform POD to simple markup
220 61 ▶sub _pod_node_to_text {
Subroutine _pod_node_to_text: called CC=13 SCAR=25.7
221 61 my ($node,$indent) = @_;
222  
223 61 ▶ unless (defined $indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $indent) 13 48 61
224 13 my $indent_init = 0;
225 13 $indent = \$indent_init;
226 }
227  
228 61 my (@lines);
229 61 ▶ if (ref $node eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq 'ARRAY') { } 13 48 61
230 13 foreach my $element (@$node) {
231 48 push (@lines,_pod_node_to_text($element,$indent));
232 }
233  
234 } else {
235 48 my $class = ref($node);
236 48 ▶ if ($class eq 'Pod::Elemental::Element::Pod5::Ordinary') {
Branch: 3 branches
BranchTrueFalseTotal
if ($class eq 'Pod::Elemental::Element::Pod5::Ordinary') { } 23 25 48
elsif ($class eq 'Pod::Elemental::Element::Pod5::Verbatim') { } 2 23 25
elsif ($class eq 'Pod::Elemental::Element::Pod5::Command') { } 20 3 23
237 23 my $content = $node->content;
238 return
239 23 ▶ if $content =~ m/^=cut/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /^=cut/ 2 21 23
240 21 $content =~ s/\n/ /g;
241 21 $content =~ s/\s+/ /g;
242 21 push (@lines,$content."\n");
243 }
244 elsif ($class eq 'Pod::Elemental::Element::Pod5::Verbatim') {
245 2 push (@lines,$node->content."\n");
246 }
247 elsif ($class eq 'Pod::Elemental::Element::Pod5::Command') {
248 20 my $command = $node->command;
249 20 ▶ if ($command eq 'over') {
Branch: 4 branches
BranchTrueFalseTotal
if ($command eq 'over') { } 4 16 20
elsif ($command eq 'item') { } 8 8 16
elsif ($command eq 'back') { } 4 4 8
elsif ($command =~ qr/head\d/) { } 4 0 4
250 4 ${$indent}++;
251 }
252 elsif ($command eq 'item') {
253 8 push (@lines,(' ' x ($$indent-1)) . $node->content);
254 }
255 elsif ($command eq 'back') {
256 4 push (@lines,"\n");
257 4 ${$indent}--;
258 }
259 elsif ($command =~ qr/head\d/) {
260 4 push (@lines,"\n",$node->content,"\n");
261 }
262 }
263 }
264  
265 return
266 59 ▶ unless scalar @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 7 52 59
267  
268 # Convert text markup
269 52 my $return = join ("\n", grep { defined $_ } @lines);
270 52 $return =~ s/\n\n\n+/\n\n/g; # Max one empty line
271 52 $return =~ s/I<([^>]+)>/_$1_/g;
272 52 $return =~ s/B<([^>]+)>/*$1*/g;
273 52 $return =~ s/[LCBI]<([^>]+)>/$1/g;
274 52 $return =~ s/[LCBI]<([^>]+)>/$1/g;
275 52 return $return;
276 }
277  
278  
279 17 1;
280  
281 =pod
282  
283 =head1 NAME
284  
285 MooseX::App::Utils - Utility functions
286  
287 =head1 DESCRIPTION
288  
289 This package holds various utility functions used by MooseX-App internally.
290 Unless you develop plugins you will not need to interact with this class.
291  
292 =head1 FUNCTIONS
293  
294 =head2 class_to_command
295  
296 =head2 package_to_filename
297  
298 Tries to determine the filename containing the given package name.
299  
300 =head2 format_list
301  
302 =head2 format_text
303  
304 =head2 parse_pod
305  
306 =head2 split_string
307  
308  
309  
310 =head1 GLOBALS
311  
312 =head2 $MooseX::App::Utils::SCREEN_WIDTH
313  
314 Screen width for printing help and error messages
315  
316 =head2 $MooseX::App::Utils::INDENT
317  
318 Indent for printing help and error messages
319  
320 =cut
« App/Plugin/Depends/Meta/Class.pm ↑ Summary App/ParsedArgv.pm »