AppSpec.pm

filter: Statementstmt 24.5% 28 / 114 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.2% 9 / 14 Podpod 100% 4 / 4 totaltotal 23.0% 41 / 178 CC 21 SCAR 42.2
CC
21
Cov
18%
cmd_new
156.0
cmd_completion
56.0
cmd_validate
12.0
CRAP 68.3
↑ Summary AppSpec/Schema/Validator.pm »
1 # ABSTRACT: App Module ad utilities for appspec tool
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 package App::AppSpec;
7 3 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Encode qw/ encode_utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Basename qw/ dirname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = 'v0.6.1'; # TRIAL VERSION
13  
14 3 ▶use base 'App::Spec::Run::Cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub _read_spec {
Subroutine _read_spec: not called CC=1 SCAR=6.9
17 0 my ($self, $run) = @_;
18 0 my $parameters = $run->parameters;
19  
20 0 my $spec_file = $parameters->{spec_file};
21 0 my $spec = App::Spec->read($spec_file);
22 0 return $spec;
23 }
24  
25 0 ▶sub cmd_completion {
Subroutine cmd_completion: not called CC=7 SCAR=40.3
Pod documented
26 0 my ($self, $run) = @_;
27 0 my $options = $run->options;
28 0 my $parameters = $run->parameters;
29 0 my $name = $options->{name};
30  
31 0 ▶ my $shell = $options->{zsh} ? "zsh" : $options->{bash} ? "bash" : $options->{fish} ? "fish": '';
Branch: 3 branches
BranchTrueFalseTotal
$options->{'zsh'} ? : 0 0 0
$options->{'bash'} ? : 0 0 0
$options->{'fish'} ? : 0 0 0
32 0 ▶ die "Specify which shell" unless $shell;
Branch: 1 branch
BranchTrueFalseTotal
unless $shell 0 0 0
33  
34 0 my $spec = $self->_read_spec($run);
35 0 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
36 0 $spec->name($name);
37 }
38 0 my $completion = $spec->generate_completion(
39 shell => $shell,
40 );
41 0 ▶ if ($completion =~ m/[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($completion =~ /[^\x00-\x7F]/) 0 0 0
42 0 $completion = encode_utf8 $completion;
43 }
44 0 say $completion;
45 }
46  
47 0 ▶sub generate_pod {
Subroutine generate_pod: not called CC=2 SCAR=17.9
Pod documented
48 0 my ($self, $run) = @_;
49 0 my $parameters = $run->parameters;
50  
51 0 my $spec = $self->_read_spec($run);
52  
53 0 require App::Spec::Pod;
54 0 my $generator = App::Spec::Pod->new(
55 spec => $spec,
56 );
57 0 my $pod = $generator->generate;
58  
59 0 ▶ if ($pod =~ m/[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /[^\x00-\x7F]/) 0 0 0
60 0 $pod = encode_utf8 $pod;
61 }
62 0 say $pod;
63 }
64  
65 0 ▶sub cmd_validate {
Subroutine cmd_validate: not called CC=3 SCAR=24.8
Pod documented
66 0 my ($self, $run) = @_;
67 0 my $options = $run->options;
68 0 my $parameters = $run->parameters;
69  
70 0 my @errors;
71 0 require App::AppSpec::Schema::Validator;
72 0 my $validator = App::AppSpec::Schema::Validator->new;
73 0 my $spec_file = $parameters->{spec_file};
74 0 ▶ if (ref $spec_file eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec_file eq 'SCALAR') { } 0 0 0
75 0 my $spec = YAML::PP::Load($$spec_file);
76 0 @errors = $validator->validate_spec($spec);
77 }
78 else {
79 0 @errors = $validator->validate_spec_file($spec_file);
80 }
81 0 binmode STDOUT, ":encoding(utf-8)";
82 0 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) { } 0 0 0
83 0 print $validator->format_errors(\@errors);
84 0 say $run->colored(out => red => "Not valid!");
85 }
86 else {
87 0 say $run->colored(out => [qw/ bold green /] => "Validated ✓");
88 }
89 }
90  
91 0 ▶sub cmd_new {
Subroutine cmd_new: not called CC=12 SCAR=50.5
Pod documented
92 0 my ($self, $run) = @_;
93 0 my $options = $run->options;
94 0 my $params = $run->parameters;
95 0 my $dist_path = $params->{path};
96 0 require File::Path;
97  
98 0 my $name = $options->{name};
99 0 my $class = $options->{class};
100 0 my $overwrite = $options->{overwrite};
101 0 ▶ unless ($name =~ m/^\w[\w+-]*/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^\w[\w+-]*/) 0 0 0
102 0 die "Option name '$name': invalid app name";
103 }
104 0 ▶ unless ($class =~ m/^[a-zA-Z]\w*(::\w+)+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ /^[a-zA-Z]\w*(::\w+)+$/) 0 0 0
105 0 die "Option class '$class': invalid classname";
106 }
107 0 my $dist = $class;
108 0 $dist =~ s/::/-/g;
109 0 ▶ $dist = $dist_path // $dist;
Condition0%0/3
$dist_path // $dist
l!l&&r!l&&!r
000
MC/DC0%0/2
$dist_path // $dist
$dist_path $dist
Truth table0%0/3
A // B
A= $dist_pathB= $dist
ABresult
1X1
011
000
110 0 ▶ if (-d $dist and not $overwrite) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $dist and not $overwrite) { } 0 0 0
elsif (-d $dist) { } 0 0 0
Condition0%0/3
-d $dist and not $overwrite
!ll&&!rl&&r
000
MC/DC0%0/2
-d $dist and not $overwrite
-d $dist not $overwrite
Truth table0%0/3
A and not(B)
A= -d $distB= not $overwrite
ABresult
0X0
100
111
111 0 die "Directory $dist already exists";
112 }
113 elsif (-d $dist) {
114 0 say "Removing old $dist directory first";
115 0 File::Path::remove_tree($dist);
116 }
117 0 my $spec = <<"EOM";
118 name: $name
119 appspec: { version: '0.001' }
120 class: $class
121 title: 'app title'
122 description: 'app description'
123 options:
124 - name: some-flag
125 type: flag
126 summary: option summary
127 - spec: other-option=s --another option
128 EOM
129 0 ▶ my $subname = $options->{"with-subcommands"} ? "mycommand" : "execute";
Branch: 1 branch
BranchTrueFalseTotal
$options->{'with-subcommands'} ? : 0 0 0
130 0 my $module = <<"EOM";
131 package $class;
132 use strict;
133 use warnings;
134 use feature qw/ say /;
135 use base 'App::Spec::Run::Cmd';
136  
137 sub $subname \{
138 my (\$self, \$run) = \@_;
139 my \$options = \$run->options;
140 my \$parameters = \$run->parameters;
141  
142 say "Hello world";
143 \}
144  
145 1;
146 EOM
147 0 my $script = <<"EOM";
148 #!/usr/bin/env perl
149 use strict;
150 use warnings;
151  
152 use App::Spec;
153 use App::AppSpec;
154 use $class;
155 use File::Share qw/ dist_file /;
156  
157 my \$specfile = dist_file("$dist", "$name-spec.yaml");
158 my \$spec = App::Spec->read(\$specfile);
159 my \$run = \$spec->runner;
160 \$run->run;
161 EOM
162 0 ▶ if ($options->{"with-subcommands"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"with-subcommands"}) 0 0 0
163 0 $spec .= <<"EOM";
164 subcommands:
165 mycommand:
166 summary: "Summary for mycommand"
167 op: "mycommand"
168 description: "Description for mycommand"
169 parameters:
170 - name: "myparam"
171 summary: "Summary for myparam"
172 required: 1
173 EOM
174 }
175 0 my $module_path = $class;
176 0 $module_path =~ s#::#/#g;
177 0 $module_path = "$dist/lib/$module_path.pm";
178 0 File::Path::make_path($dist);
179 0 File::Path::make_path("$dist/share");
180 0 File::Path::make_path("$dist/bin");
181 0 File::Path::make_path(dirname $module_path);
182 0 my $specfile = "$dist/share/$name-spec.yaml";
183 0 say "Writing spec to $specfile";
184 0 ▶ open my $fh, ">", $specfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $specfile or die $! 0 0 0
185 0 print $fh $spec;
186 0 close $fh;
187  
188 0 ▶ open $fh, ">", $module_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $module_path or die $! 0 0 0
189 0 print $fh $module;
190 0 close $fh;
191  
192 0 ▶ open $fh, ">", "$dist/bin/$name" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "$dist/bin/$name" or die $! 0 0 0
193 0 print $fh $script;
194 0 close $fh;
195 }
196  
197 =pod
198  
199 =head1 NAME
200  
201 App::AppSpec - Utilities for App::Spec authors
202  
203 =head1 SYNOPSIS
204  
205 See L<appspec> documentation for the command line utility.
206  
207 =head1 DESCRIPTION
208  
209 This is the app class for the L<appspec> command line tool.
210 It contains utilities for L<App::Spec> files, like generating
211 completion or pod from it.
212  
213 =head1 METHODS
214  
215 =over 4
216  
217 =item cmd_completion, cmd_new, cmd_validate, generate_pod
218  
219 =back
220  
221 =head1 LICENSE
222  
223 This library is free software and may be distributed under the same terms
224 as perl itself.
225  
226 =cut
227  
228 3 1;
↑ Summary AppSpec/Schema/Validator.pm »