App/Command/policies.pm

filter: Statementstmt 23.2% 34 / 146 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 52.3% 11 / 21 Podpod 100% 5 / 5 totaltotal 21.8% 50 / 229 CC 32 SCAR 39.8
CC
32
Cov
18%
_do_policy
110.0
_get_policy_config
110.0
execute
56.0
CRAP 53.8
↑ Summary Plugin/Test/Software/Policies.pm »
1 ## no critic (NamingConventions::Capitalization)
2 ## no critic (ControlStructures::ProhibitPostfixControls)
3 ## no critic (InputOutput::RequireCheckedSyscalls)
4 package Dist::Zilla::App::Command::policies;
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use feature qw( say );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Create project policy files
12  
13 1 our $VERSION = '0.002';
14  
15 1 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use English qw( -no_match_vars ); # Avoids regex performance
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # penalty in perl 5.18 and
18 # earlier
19 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Dist::Zilla::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Software::Policies ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub abstract { return 'Create software policy files' } ## no critic (NamingConventions::ProhibitAmbiguousNames)
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 ▶sub usage_desc { return 'dzil policies [--class] [--version] [--format] [--dir] [--filename] [<policy>]' }
Subroutine usage_desc: not called CC=1 SCAR=6.9
Pod documented
30  
31 0 ▶sub validate_args {
Subroutine validate_args: not called CC=2 SCAR=17.9
Pod documented
32 0 my ( $self, $opt, $arg ) = @_;
33  
34 0 my ( $policy, @extra ) = @{$arg};
35  
36 0 ▶ croak( ( __PACKAGE__ =~ m/::(.+)$/msx )[0] . ' accepts two arguments, ignoring ' . join q{,}, @extra )
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 0 0
37 if @extra;
38  
39 0 return;
40 }
41  
42 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
43 return (
44 0 [ 'filepath=s' => 'optional: filepath of the policy file' ],
45 [ 'class=s' => 'class of policy' ],
46 [ 'class-version=i' => 'version of policy' ],
47 [ 'format=s' => 'format, e.g. markdown' ],
48 [ 'dir=s' => 'dir for policy file' ],
49 [ 'filename=s' => 'filename of policy file' ],
50 [ 'attributes=s' => 'additional attributes to the policy, key-value pairs, e.g. n=name,a=able' ],
51 [ 'list' => 'list available policies, classes and versions' ],
52 [ 'version|V' => 'print version' ],
53 [ 'completions' => 'show completions, for shell completion' ],
54 );
55 }
56  
57 0 ▶sub execute {
Subroutine execute: not called CC=7 SCAR=40.3
Pod documented
58 0 my ( $self, $opt, $arg ) = @_;
59  
60 0 my $zilla = $self->zilla;
61 0 $zilla->log_debug( [ 'args: %s', ( join q{,}, @{$arg} ) ] );
62  
63 0 ▶ if ( $opt->{'version'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"version"}) 0 0 0
64 0 say __PACKAGE__ . q{ } . $VERSION;
65 0 return;
66 }
67  
68 0 ▶ if ( $opt->{'list'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"list"}) 0 0 0
69 0 my $policies = Software::Policies->new->list();
70 0 foreach my $policy ( keys %{$policies} ) {
71 0 print $policy . q{/};
72 0 foreach my $class ( keys %{ $policies->{$policy}->{'classes'} } ) {
73 0 print $class . q{/};
74 0 foreach my $version ( keys %{ $policies->{$policy}->{'classes'}->{$class}->{'versions'} } ) {
75 0 print q{v} . $version . "\n";
76 }
77 }
78  
79 }
80 0 return;
81 }
82  
83 0 ▶ if ( @{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$arg;}) 0 0 0
84 0 my ($policy) = @{$arg};
85 0 my %args = $self->_get_policy_config( $policy, $opt );
86  
87 $self->_do_policy( $policy, $args{class}, $args{version}, $args{format},
88 0 $args{'dir'}, $args{'filename'}, $args{attributes} );
89 0 return;
90 }
91  
92 # If you do not specify a policy name,
93 # then create files for all available policies.
94 0 $self->_do_policies( $opt, $opt->{'attributes'} );
95  
96 0 return;
97 }
98  
99 0 ▶sub _do_policy { ## no critic (Subroutines::ProhibitManyArgs)
Subroutine _do_policy: not called CC=10 SCAR=47.0
100 0 my ( $self, $policy, $class, $version, $format, $dir, $filename, $attributes ) = @_;
101 0 my $zilla = $self->zilla;
102  
103 0 ▶ my %args = (
Condition0%0/2
$attributes // {}
l!l
00
MC/DC0%0/1
$attributes // {}
$attributes
Truth table0%0/2
A
A= $attributes
Aresult
11
00
104 policy => $policy,
105 attributes => $attributes // {},
106 );
107 0 ▶ $args{class} = $class if ($class);
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
108 0 ▶ $args{version} = $version if ($version);
Branch: 1 branch
BranchTrueFalseTotal
if $version 0 0 0
109 0 ▶ $args{format} = $format if ($format);
Branch: 1 branch
BranchTrueFalseTotal
if $format 0 0 0
110  
111 0 $zilla->log_debug( [ 'Looking for matching policy: %s:%s:%s:%s', $policy, $class, $version, $format, ] );
112 0 $zilla->log_debug( [ 'dir: %s, filename: %s', $dir, $filename, ] );
113 0 $zilla->log_debug( [ 'attributes: %s', $args{attributes}, ] );
114 0 my @p = Software::Policies->new->create(%args);
115  
116 # Create the file(s).
117 0 foreach (@p) {
118 0 ▶ $zilla->log_debug( [ 'Writing to dir: %s, filename: %s', $dir // q{.}, $filename // $_->{'filename'}, ] );
Condition0%0/5
$dir // "."
l!l
00
$filename // $_->{"filename"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$dir // "."
$dir
MC/DC0%0/2
$filename // $_->{"filename"}
$filename $_->{"filename"}
Truth table0%0/2
A
A= $dir
Aresult
11
00
Truth table0%0/3
A // B
A= $filenameB= $_->{"filename"}
ABresult
1X1
011
000
119 0 ▶ my $d = $dir // q{.};
Condition0%0/2
$dir // "."
l!l
00
MC/DC0%0/1
$dir // "."
$dir
Truth table0%0/2
A
A= $dir
Aresult
11
00
120 0 path($d)->mkdir();
121 0 ▶ path($d)->child( $filename // $_->{'filename'} )->spew_utf8( $_->{'text'} );
Condition0%0/3
$filename // $_->{"filename"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$filename // $_->{"filename"}
$filename $_->{"filename"}
Truth table0%0/3
A // B
A= $filenameB= $_->{"filename"}
ABresult
1X1
011
000
122 }
123 0 return;
124 }
125  
126 # We check if we have any policy plugins defined,
127 # i.e. if we have any [Software::Policies / $policy] configs.
128 # If we have, we only create policy files for them,
129 # if not, we create policy files for all available policies,
130 # decided by Software::Policies.
131 0 ▶sub _do_policies {
Subroutine _do_policies: not called CC=5 SCAR=34.0
132 0 my ( $self, $opt ) = @_;
133 0 my $zilla = $self->zilla;
134  
135 # Plugins with a specific policy, e.g. [Software::Policies / Contributing]
136 # key=policy name, value=plugin
137 my @policies =
138 0 map { $_->plugin_name }
139 0 ▶ grep { $_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name ne 'Software::Policies' }
Condition0%0/3
$_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name ne 'Software::Policies'
!ll&&!rl&&r
000
MC/DC0%0/2
$_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name ne 'Software::Policies'
$_->isa('Dist::Zilla::Plugin::Software::Policies') $_->plugin_name ne 'Software::Policies'
Truth table0%0/3
A && B
A= $_->isa('Dist::Zilla::Plugin::Software::Policies')B= $_->plugin_name ne 'Software::Policies'
ABresult
0X0
100
111
140 0 @{ $zilla->{'plugins'} };
141 0 $zilla->log_debug( [ 'Discovered configs for policies %s', \@policies ] );
142  
143 0 ▶ if (@policies) {
Branch: 1 branch
BranchTrueFalseTotal
if (@policies) { } 0 0 0
144 0 foreach my $policy (@policies) {
145 0 $zilla->log_debug( [ 'Create file for policy %s', $policy ] );
146  
147 0 my %args = $self->_get_policy_config( $policy, $opt );
148 $self->_do_policy( $policy, $args{class}, $args{version}, $args{format},
149 0 $args{'dir'}, $args{'filename'}, $args{attributes} );
150 }
151 }
152 else {
153 0 $zilla->log_debug( ['No specific Software::Policies defined. Create all available policies'] );
154 0 foreach my $policy ( keys %{ Software::Policies->new->list() } ) {
155 0 $zilla->log_debug( [ 'Create policy %s', $policy ] );
156  
157 0 my %args = $self->_get_policy_config( $policy, $opt );
158 $self->_do_policy( $policy, $args{class}, $args{version}, $args{format},
159 0 $args{'dir'}, $args{'filename'}, $args{attributes} );
160 }
161 }
162 0 return;
163 }
164  
165 0 ▶sub _get_policy_config {
Subroutine _get_policy_config: not called CC=10 SCAR=47.0
166 0 my ( $self, $policy, $opt ) = @_;
167 0 my $zilla = $self->zilla;
168  
169 0 $zilla->log_debug( [ '_get_policy_config(%s, %s)', $policy, $opt ] );
170  
171 # 1. "Default" values, taken from dist.ini
172 0 my %args;
173 my %attributes = (
174 name => $zilla->{'name'},
175 abstract => $zilla->{'abstract'},
176 0 authors => $zilla->{'authors'},
177 license => $zilla->license,
178  
179 # main_module => $zilla->{'main_module'},
180 # version => $zilla->version,
181 );
182 0 $zilla->log_debug( ['After 1.'] );
183 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
184 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
185  
186 # 2. Config items applied to all policies.
187 # Only "[Software::Policies]"
188 0 ▶ my $plain_plugin = first { $_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name eq 'Software::Policies' }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("Dist::Zilla::Plugin::Software::Policies") and $_->plugin_name eq "Software::Policies" 0 0 0
189 0 @{ $zilla->{'plugins'} };
190 0 ▶ if ($plain_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain_plugin) 0 0 0
191 0 $zilla->log_debug( ['Discovered general setting for Software::Policies'] );
192 0 for my $key (qw( class version format dir filename )) {
193 0 ▶ $args{$key} = $plain_plugin->{$key} if $plain_plugin->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $plain_plugin->{$key} 0 0 0
194 }
195 0 my %policy_attributes = %{ $plain_plugin->{'policy_attribute'} };
196 0 @attributes{ keys %policy_attributes } = @policy_attributes{ keys %policy_attributes };
197 }
198 0 $zilla->log_debug( ['After 2.'] );
199 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
200 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
201  
202 # 3. Only one policy's config
203 # Only "[Software::Policies / $policy]", plugin_name is changed in module Plugin::S::p!
204 my $this_plugin =
205 0 ▶ first { $_->isa('Dist::Zilla::Plugin::Software::Policies') && $_->plugin_name =~ m/^ $policy $/msx } @{ $zilla->{'plugins'} };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("Dist::Zilla::Plugin::Software::Policies") and $_->plugin_name =~ /^ $policy $/msx 0 0 0
206 0 ▶ if ($this_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_plugin) 0 0 0
207 0 $zilla->log_debug( [ 'Discovered config for Software::Policies / %s: %s', $policy, $this_plugin->plugin_name ] );
208 0 for my $key (qw( class version format dir filename )) {
209 0 ▶ $args{$key} = $this_plugin->{$key} if $this_plugin->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $this_plugin->{$key} 0 0 0
210 }
211 0 my %policy_attributes = %{ $this_plugin->{'policy_attribute'} };
212 0 @attributes{ keys %policy_attributes } = @policy_attributes{ keys %policy_attributes };
213 }
214  
215 # 4. Config from the command line.
216 0 for my $key (qw( class version format dir filename )) {
217 0 ▶ $args{$key} = $opt->{$key} if $opt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$key} 0 0 0
218 }
219 0 ▶ my %attrs = map { split qr/\s*=\s*/msx, $_, 2 } ( map { split qr/,/msx } $opt->{'attributes'} // q{} );
Condition0%0/2
$opt->{"attributes"} // ""
l!l
00
MC/DC0%0/1
$opt->{"attributes"} // ""
$opt->{"attributes"}
Truth table0%0/2
A
A= $opt->{"attributes"}
Aresult
11
00
220 0 @attributes{ keys %attrs } = @attrs{ keys %attrs };
221  
222 # Set attributes into %args.
223 0 $args{attributes} = \%attributes;
224 0 $zilla->log_debug( [ 'args: %s', \%args, ] );
225 0 $zilla->log_debug( [ 'attributes: %s', \%attributes, ] );
226  
227 0 return %args;
228 }
229  
230 1 1;
231  
232 __END__
↑ Summary Plugin/Test/Software/Policies.pm »