Spec/Pod.pm

filter: Statementstmt 96.0% 145 / 151 Branchbran 88.6% 39 / 44 Conditioncond 90.4% 19 / 21 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 90.0% 9 / 10 Podpod 85.7% 6 / 7 totaltotal 93.0% 227 / 244 CC 43 SCAR 22.2
CC
43
Cov
94%
subcommand_pod
16.0
simple_table
8.0
options2pod
8.0
CRAP 9.2
« Spec/Completion/Fish.pm ↑ Summary Spec/Run.pm »
1 # ASTRACT: Generates Pod from App::Spec objects
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package App::Spec::Pod;
5  
6 2 our $VERSION = 'v0.15.1'; # TRIAL VERSION
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 has spec => ( is => 'ro' );
11  
12 4 ▶sub generate {
Subroutine generate: called CC=5 SCAR=16.1
Pod documented
13 4 my ($self) = @_;
14 4 my $spec = $self->spec;
15 4 my $appname = $spec->name;
16 4 my $title = $spec->title;
17 4 ▶ my $abstract = $spec->abstract // '';
Condition100%2/2
$spec->abstract // ""
l!l
31
MC/DC100%1/1
$spec->abstract // ""
$spec->abstract
Truth table100%2/2
A
A= $spec->abstract
Aresult
11
00
18 4 ▶ my $description = $spec->description // '';
Condition100%2/2
$spec->description // ""
l!l
13
MC/DC100%1/1
$spec->description // ""
$spec->description
Truth table100%2/2
A
A= $spec->description
Aresult
11
00
19 4 my $subcmds = $spec->subcommands;
20 4 my $global_options = $spec->options;
21  
22 4 $self->markup(text => \$abstract);
23 4 $self->markup(text => \$description);
24  
25 4 my @subcmd_pod = $self->subcommand_pod(
26 commands => $subcmds,
27 );
28 4 my $option_string = '';
29 4 ▶ if (@$global_options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$global_options) 4 0 4
30 4 $option_string = "=head2 GLOBAL OPTIONS\n\n" . $self->options2pod(
31 options => $global_options,
32 );
33 }
34  
35 4 my $pod = <<"EOM";
36 \=head1 NAME
37  
38 $appname - $title
39  
40 \=head1 ABSTRACT
41  
42 $abstract
43  
44 \=head1 DESCRIPTION
45  
46 $description
47  
48 $option_string
49  
50 \=head2 SUBCOMMANDS
51  
52 4 @{[ join '', @subcmd_pod ]}
53 EOM
54  
55 4 ▶ if ($pod =~ m/[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /[^\x00-\x7F]/) 1 3 4
56 1 $pod = "=encdding utf-8\n\n$pod";
57 }
58 4 return $pod;
59 }
60  
61 11 ▶sub subcommand_pod {
Subroutine subcommand_pod: called CC=16 SCAR=27.7
Pod documented
62 11 my ($self, %args) = @_;
63 11 my $spec = $self->spec;
64 11 my $appname = $spec->name;
65 11 my $commands = $args{commands};
66 11 ▶ my $previous = $args{previous} || [];
Condition100%2/2
$args{'previous'} || []
l!l
74
MC/DC100%1/1
$args{'previous'} || []
$args{'previous'}
Truth table100%2/2
A
A= $args{'previous'}
Aresult
11
00
67  
68 11 my @pod;
69 my %keys;
70 11 @keys{ keys %$commands } = ();
71 11 my @keys;
72 11 ▶ if (@$previous) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$previous) { } 7 4 11
73 7 @keys = sort keys %keys;
74 }
75 else {
76 4 for my $key (qw/ help _meta /) {
77 8 ▶ if (exists $keys{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keys{$key}) 5 3 8
78 5 push @keys, $key;
79 5 delete $keys{ $key };
80 }
81 }
82 4 unshift @keys, sort keys %keys;
83 }
84 11 for my $name (@keys) {
85 29 my $cmd_spec = $commands->{ $name };
86 29 my $name = $cmd_spec->name;
87 29 my $summary = $cmd_spec->summary;
88 29 my $description = $cmd_spec->description;
89 29 my $subcmds = $cmd_spec->subcommands;
90 29 my $parameters = $cmd_spec->parameters;
91 29 my $options = $cmd_spec->options;
92  
93 29 $self->markup(text => \$summary);
94 29 $self->markup(text => \$description);
95  
96 29 my $desc = '';
97 29 ▶ if (length $summary) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $summary) 29 0 29
98 29 $desc .= "$summary\n\n";
99 }
100 29 ▶ if (length $description) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $description) 1 28 29
101 1 $desc .= "$description\n\n";
102 }
103  
104 29 my $usage = "$appname @$previous $name";
105 29 ▶ if (keys %$subcmds) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$subcmds) 10 19 29
106 10 $usage .= " <subcommands>";
107 }
108  
109 29 my $option_string = '';
110 29 ▶ if (@$options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$options) 14 15 29
111 14 $usage .= " [options]";
112 14 $option_string = "Options:\n\n" . $self->options2pod(
113 options => $options,
114 );
115 }
116  
117 29 ▶ if (length $option_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $option_string) 14 15 29
118 14 $desc .= "$option_string\n";
119 }
120  
121 29 my $param_string = '';
122 29 ▶ if (@$parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$parameters) 9 20 29
123 9 $param_string = "Parameters:\n\n" . $self->params2pod(
124 parameters => $parameters,
125 );
126 9 for my $param (@$parameters) {
127 14 my $name = $param->name;
128 14 my $required = $param->required;
129 14 $usage .= " " . $param->to_usage_header;
130 }
131 }
132 29 ▶ if (length $param_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $param_string) 9 20 29
133 9 $desc .= $param_string;
134 }
135  
136 29 my $pod = <<"EOM";
137 \=head3 @$previous $name
138  
139 $usage
140  
141 $desc
142 EOM
143 29 ▶ if (keys %$subcmds and $name ne "help") {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$subcmds and $name ne "help") 7 22 29
Condition100%3/3
keys %$subcmds and $name ne "help"
!ll&&!rl&&r
1937
MC/DC100%2/2
keys %$subcmds and $name ne "help"
keys %$subcmds $name ne "help"
Truth table100%3/3
A and B
A= keys %$subcmdsB= $name ne "help"
ABresult
0X0
100
111
144 7 my @sub = $self->subcommand_pod(
145 previous => [@$previous, $name],
146 commands => $subcmds,
147 );
148 7 $pod .= join '', @sub;
149 }
150 29 push @pod, $pod;
151 }
152 11 return @pod;
153 }
154  
155 9 ▶sub params2pod {
Subroutine params2pod: called CC=7 SCAR=19.5
Pod documented
156 9 my ($self, %args) = @_;
157 9 my $params = $args{parameters};
158 9 my @rows;
159 9 for my $param (@$params) {
160 14 ▶ my $required = $param->required ? '*' : '';
Branch: 1 branch
BranchTrueFalseTotal
$param->required ? : 12 2 14
161 14 my $summary = $param->summary;
162 14 my $multi = '';
163 14 ▶ if ($param->mapping) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param->mapping) { } 0 14 14
elsif ($param->multiple) { } 2 12 14
164 0 $multi = '{}';
165 }
166 elsif ($param->multiple) {
167 2 $multi = '[]';
168 }
169 14 my $flags = $self->spec->_param_flags_string($param);
170 14 my @lines = split m/\n/, $summary;
171 14 ▶ push @rows, [" " . $param->name, " " . $required, $multi, ($lines[0] // '') . $flags];
Condition100%2/2
$lines[0] // ""
l!l
122
MC/DC100%1/1
$lines[0] // ""
$lines[0]
Truth table100%2/2
A
A= $lines[0]
Aresult
11
00
172 14 push @rows, [" " , " ", '', $_] for map {s/^ +//; $_ } @lines[1 .. $#lines];
173 }
174 9 my $test = $self->simple_table(\@rows);
175 9 return $test;
176 }
177  
178 27 ▶sub simple_table {
Subroutine simple_table: called CC=8 SCAR=20.8
Pod undocumented
179 27 my ($self, $rows) = @_;
180 27 my @widths;
181  
182 27 for my $row (@$rows) {
183 64 for my $i (0 .. $#$row) {
184 256 my $col = $row->[ $i ];
185 256 ▶ $widths[ $i ] ||= 0;
Condition100%2/2
$widths[$i] ||= 0
l!l
128128
MC/DC100%1/1
$widths[$i] ||= 0
$widths[$i]
Truth table100%2/2
A
A= $widths[$i]
Aresult
11
00
186 256 ▶ if ( $widths[ $i ] < length $col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($widths[$i] < length $col) 99 157 256
187 99 $widths[ $i ] = length $col;
188 }
189 }
190 }
191 27 ▶ my $format = join ' ', map { "%-" . ($_ || 0) . "s" } @widths;
Condition100%2/2
$_ || 0
l!l
8424
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
192 27 my @lines;
193 27 for my $row (@$rows) {
194 64 ▶ my $string = sprintf "$format\n", map { $_ // '' } @$row;
Condition50%1/2
$_ // ""
l!l
2560
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
195 64 push @lines, $string;
196 }
197 27 return join '', @lines;
198  
199 }
200  
201 18 ▶sub options2pod {
Subroutine options2pod: called CC=8 SCAR=20.8
Pod documented
202 18 my ($self, %args) = @_;
203 18 my $options = $args{options};
204 18 my @rows;
205 18 for my $opt (@$options) {
206 44 my $name = $opt->name;
207 44 my $aliases = $opt->aliases;
208 44 my $summary = $opt->summary;
209 44 ▶ my $required = $opt->required ? '*' : '';
Branch: 1 branch
BranchTrueFalseTotal
$opt->required ? : 0 44 44
210 44 my $multi = '';
211 44 ▶ if ($opt->mapping) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->mapping) { } 1 43 44
elsif ($opt->multiple) { } 3 40 43
212 1 $multi = '{}';
213 }
214 elsif ($opt->multiple) {
215 3 $multi = '[]';
216 }
217 my @names = map {
218 44 ▶ length $_ > 1 ? "--$_" : "-$_"
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 44 15 59
219 } ($name, @$aliases);
220 44 my $flags = $self->spec->_param_flags_string($opt);
221 44 my @lines = split m/\n/, $summary;
222 44 ▶ push @rows, [" @names", " " . $required, $multi, ($lines[0] // '') . $flags];
Condition100%2/2
$lines[0] // ""
l!l
404
MC/DC100%1/1
$lines[0] // ""
$lines[0]
Truth table100%2/2
A
A= $lines[0]
Aresult
11
00
223 44 push @rows, [" ", " " , '', $_ ] for map {s/^ +//; $_ } @lines[1 .. $#lines];
224 }
225 18 my $test = $self->simple_table(\@rows);
226 18 return $test;
227 }
228  
229 66 ▶sub markup {
Subroutine markup: called CC=4 SCAR=14.5
Pod documented
230 66 my ($self, %args) = @_;
231 66 my $text = $args{text};
232 66 ▶ return unless defined $$text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$text 28 38 66
233 38 ▶ my $markup = $self->spec->markup // '';
Condition50%1/2
$self->spec->markup // ""
l!l
380
MC/DC0%0/1
$self->spec->markup // ""
$self->spec->markup
Truth table50%1/2
A
A= $self->spec->markup
Aresult
11
00
234 38 ▶ if ($markup eq "swim") {
Branch: 1 branch
BranchTrueFalseTotal
if ($markup eq "swim") 0 38 38
235 0 $$text = $self->swim2pod($$text);
236 }
237 }
238 0 ▶sub swim2pod {
Subroutine swim2pod: not called CC=1 SCAR=6.9
Pod documented
239 0 my ($self, $text) = @_;
240 0 require Swim;
241 0 my $swim = Swim->new(text => $text);
242 0 my $pod = $swim->to_pod;
243 }
244  
245 2 1;
246  
247 __END__
« Spec/Completion/Fish.pm ↑ Summary Spec/Run.pm »