Iller/DocType/Dist.pm

filter: Statementstmt 99.3% 145 / 146 Branchbran 76.0% 38 / 50 Conditioncond 80.0% 12 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 29 / 29 Podpod 0.0% 0 / 6 totaltotal 89.8% 229 / 255 CC 44 SCAR 18.7
CC
44
Cov
92%
to_string
16.6
parse_header
7.0
parse_prereqs
6.0
CRAP 6.5
« Iller/Role/HasPlugins.pm ↑ Summary Iller.pm »
1 3 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
5 package Dist::Iller::DocType::Dist;
6  
7 3 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8 # ABSTRACT: Turn the Dist::Iller config into a dist.ini file
9 3 our $VERSION = '0.1411';
10  
11 3 ▶use Dist::Iller::Elk;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 with qw/
13 Dist::Iller::DocType
14 Dist::Iller::Role::HasPrereqs
15 Dist::Iller::Role::HasPlugins
16 /;
17  
18 3 ▶use Types::Standard qw/HashRef ArrayRef Str Int Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use PerlX::Maybe qw/maybe provided/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 has name => (
24 is => 'rw',
25 isa => Str,
26 predicate => 1,
27 init_arg => undef,
28 documentation => q{Since 0.1409, consider using 'distribution_name' in doctype:global instead.},
29 );
30 has author => (
31 is => 'rw',
32 2 ▶ isa => (ArrayRef[Str])->plus_coercions(Str, sub { [$_] }),
Subroutine __ANON__: called CC=1 SCAR=0.0
33 init_arg => undef,
34 traits => ['Array'],
35 3 ▶ default => sub { [ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 3 coerce => 1,
37 handles => {
38 all_authors => 'elements',
39 map_authors => 'map',
40 add_author => 'push',
41 has_author => 'count',
42 },
43 );
44 3 has license => (
45 is => 'rw',
46 isa => Str,
47 predicate => 1,
48 init_arg => undef,
49 );
50 3 has copyright_holder => (
51 is => 'rw',
52 isa => Str,
53 predicate => 1,
54 init_arg => undef,
55 );
56 has copyright_year => (
57 is => 'rw',
58 isa => Int,
59 predicate => 1,
60 init_arg => undef,
61 3 ▶ default => sub { DateTime->now->year }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 3 );
63 3 has add_prereqs_as_authordeps => (
64 is => 'rw',
65 isa => Bool,
66 default => 0,
67 );
68  
69  
70 6 ▶sub filename { 'dist.ini' }
Subroutine filename: called CC=1 SCAR=0.0
Pod undocumented
71  
72 9 ▶sub phase { 'before' }
Subroutine phase: called CC=1 SCAR=0.0
Pod undocumented
73  
74 6 ▶sub comment_start { ';' }
Subroutine comment_start: called CC=1 SCAR=0.0
Pod undocumented
75  
76 4 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
77 4 my $self = shift;
78 4 my $yaml = shift;
79 4 ▶ if(exists $yaml->{'add_prereqs_as_authordeps'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $yaml->{"add_prereqs_as_authordeps"}) 1 3 4
80 1 $self->add_prereqs_as_authordeps(delete $yaml->{'add_prereqs_as_authordeps'});
81 }
82 4 $self->parse_header($yaml->{'header'});
83 4 $self->parse_default_prereq_versions($yaml->{'default_prereq_versions'});
84 4 $self->parse_prereqs($yaml->{'prereqs'});
85 4 $self->parse_plugins($yaml->{'plugins'});
86 }
87  
88 12 ▶around qw/parse_header parse_prereqs parse_default_prereq_versions/ => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
89 12 my $next = shift;
90 12 my $self = shift;
91 12 my $yaml = shift;
92  
93 12 ▶ return if !defined $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yaml 2 10 12
94 10 $self->$next($yaml);
95 3 };
96  
97 4 ▶sub parse_header {
Subroutine parse_header: called CC=7 SCAR=19.5
98 4 my $self = shift;
99 4 my $yaml = shift;
100  
101 4 foreach my $setting (qw/name author license copyright_holder copyright_year/) {
102 20 my $value = $yaml->{ $setting };
103 20 my $predicate = "has_$setting";
104  
105 20 ▶ if(!$self->$predicate && $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->$predicate and $value) 3 17 20
Condition100%3/3
not $self->$predicate and $value
!ll&&!rl&&r
6113
MC/DC100%2/2
not $self->$predicate and $value
not $self->$predicate $value
Truth table100%3/3
not(A) and B
A= not $self->$predicateB= $value
ABresult
0X0
100
111
106 3 $self->$setting($value);
107 }
108 }
109 4 ▶ if (!$self->has_name && $self->global && $self->global->has_distribution_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_name and $self->global and $self->global->has_distribution_name) 1 3 4
Condition83%5/6
not $self->has_name and $self->global and $self->global->has_distribution_name
!ll&&!rl&&r
301
not $self->has_name and $self->global
!ll&&!rl&&r
121
MC/DC66%2/3
not $self->has_name and $self->global and $self->global->has_distribution_name
not $self->has_name $self->global $self->global->has_distribution_name
Truth table75%3/4
not(A) and B and C
A= not $self->has_nameB= $self->globalC= $self->global->has_distribution_name
ABCresult
0XX0
10X0
1100
1111
110 1 $self->name($self->global->distribution_name);
111 }
112 }
113  
114 2 ▶sub parse_default_prereq_versions {
Subroutine parse_default_prereq_versions: called CC=5 SCAR=16.6
115 2 my $self = shift;
116 2 my $yaml = shift;
117  
118 # prereqs added from this point forward checks defaults
119 2 foreach my $default (@{ $yaml }) {
120 6 $self->set_default_prereq_version((keys %$default)[0], (values %$default)[0]);
121 }
122 # check prereqs already added
123 2 foreach my $prereq ($self->all_prereqs) {
124 2 my $default_version = $self->get_default_prereq_version($prereq->module);
125 2 ▶ if($default_version && !$prereq->version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_version and not $prereq->version) 0 2 2
Condition66%2/3
$default_version and not $prereq->version
!ll&&!rl&&r
110
MC/DC0%0/2
$default_version and not $prereq->version
$default_version not $prereq->version
Truth table66%2/3
A and not(B)
A= $default_versionB= not $prereq->version
ABresult
0X0
100
111
126 0 $prereq->version($default_version);
127 }
128 }
129 }
130  
131 4 ▶sub parse_prereqs {
Subroutine parse_prereqs: called CC=6 SCAR=17.9
132 4 my $self = shift;
133 4 my $yaml = shift;
134  
135 4 foreach my $phase (qw/build configure develop runtime test/) {
136 20 foreach my $relation (qw/requires recommends suggests conflicts/) {
137  
138 MODULE:
139 80 foreach my $module (@{ $yaml->{ $phase }{ $relation } }) {
140 5 ▶ my $module_name = ref $module eq 'HASH' ? (keys %$module)[0] : $module;
Branch: 1 branch
BranchTrueFalseTotal
ref $module eq 'HASH' ? : 2 3 5
141 5 ▶ my $version = ref $module eq 'HASH' ? (values %$module)[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $module eq 'HASH' ? : 2 3 5
142  
143 5 $self->add_prereq(Dist::Iller::Prereq->new(
144 module => $module_name,
145 phase => $phase,
146 relation => $relation,
147 version => $version,
148 ));
149 }
150 }
151 }
152 }
153  
154 # to_hash does not translate prereqs into [Prereqs / *Phase*Requires] plugins
155 1 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod undocumented
156 1 my $self = shift;
157  
158 1 my $header = {
159 provided $self->has_author, author => $self->author,
160 maybe name => $self->name,
161 maybe license => $self->license,
162 maybe copyright_holder => $self->copyright_holder,
163 maybe copyright_year => $self->copyright_year,
164  
165 };
166 my $hash = {
167 header => $header,
168 prereqs => $self->prereqs_to_hash,
169 1 default_prereq_versions => [ map { +{ $_->[0] => $_->[1] } } $self->all_default_prereq_versions ],
170 plugins => $self->plugins_to_hash,
171 };
172  
173 1 return $hash;
174 }
175  
176 4 ▶sub packages_for_plugin {
Subroutine packages_for_plugin: called CC=1 SCAR=0.0
Pod undocumented
177 73 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
178 73 my $plugin = shift;
179  
180 73 ▶ my $name = $plugin->has_base ? $plugin->base : $plugin->plugin_name;
Branch: 1 branch
BranchTrueFalseTotal
$plugin->has_base ? : 7 66 73
181 73 $name =~ m{^(.)};
182 73 my $first = $1;
183  
184 73 my $clean_name = $name;
185 73 $clean_name =~ s{^[-%=@]}{};
186  
187 73 my $packages = [];
188 73 ▶ push @{ $packages } => $first eq '%' ? { version => $plugin->version, package => sprintf 'Dist::Zilla::Stash::%s', $clean_name }
Branch: 3 branches
BranchTrueFalseTotal
$first eq '%' ? : 0 73 73
$first eq '@' ? : 0 73 73
$first eq '=' ? : 0 73 73
189 : $first eq '@' ? { version => $plugin->version, package => sprintf 'Dist::Zilla::PluginBundle::%s', $clean_name }
190 : $first eq '=' ? { version => $plugin->version, package => sprintf $clean_name }
191 : { version => $plugin->version, package => sprintf 'Dist::Zilla::Plugin::%s', $clean_name }
192 ;
193 73 return $packages;
194 4 };
195 }
196  
197 8 ▶sub add_plugins_as_prereqs {
Subroutine add_plugins_as_prereqs: called CC=5 SCAR=16.1
Pod undocumented
198 8 my $self = shift;
199 8 my $packages_for_plugin = shift;
200 8 my @plugins = @_;
201  
202 8 for my $plugin (@plugins) {
203 135 ▶ if($plugin->has_prereqs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->has_prereqs) 4 131 135
204 4 $self->add_prereq($_) for $plugin->all_prereqs;
205 }
206 135 my $packages = $packages_for_plugin->($plugin);
207  
208 135 for my $package (@{ $packages }) {
209 $self->add_prereq(Dist::Iller::Prereq->new(
210 module => $package->{'package'},
211 phase => 'develop',
212 relation => 'requires',
213 137 version => $package->{'version'},
214 ));
215 }
216 }
217 8 $self->add_prereq(Dist::Iller::Prereq->new(
218 module => 'Dist::Zilla::Plugin::Prereqs',
219 phase => 'develop',
220 relation => 'requires',
221 version => '0',
222 ));
223 }
224  
225 2 ▶sub to_string {
Subroutine to_string: called CC=16 SCAR=28.1
226 2 my $self = shift;
227  
228 2 for my $phase (qw/build configure develop runtime test/) {
229 RELATION:
230 10 for my $relation (qw/requires recommends suggests conflicts/) {
231  
232 40 my $plugin_name = sprintf '%s%s', ucfirst $phase, ucfirst $relation;
233  
234 # in case to_string is called twice, don't add this again
235 40 ▶ next RELATION if $self->find_plugin(sub { $_->plugin_name eq $plugin_name });
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $self->find_plugin(sub { $_->plugin_name eq $plugin_name; } ) 0 40 40
236  
237 40 ▶ my @prereqs = $self->filter_prereqs(sub { $_->phase eq $phase && $_->relation eq $relation });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->phase eq $phase and $_->relation eq $relation 268 1072 1340
238 40 ▶ next RELATION if !scalar @prereqs;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @prereqs 33 7 40
239  
240 $self->add_plugin({
241 plugin_name => $plugin_name,
242 base => 'Prereqs',
243 7 parameters => { map { $_->module => $_->version } @prereqs },
244 });
245 }
246 }
247  
248 2 my @strings = ();
249 2 ▶ push @strings => sprintf 'name = %s', $self->name if $self->name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->name 1 1 2
250  
251 2 ▶ if($self->has_author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_author) 2 0 2
252 2 ▶ push @strings => $self->map_authors(sub { qq{author = $_} });
Subroutine __ANON__: called CC=1 SCAR=0.0
253 }
254 2 ▶ push @strings => sprintf 'license = %s', $self->license if $self->has_license;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_license 0 2 2
255 2 ▶ push @strings => sprintf 'copyright_holder = %s', $self->copyright_holder if $self->has_copyright_holder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_copyright_holder 0 2 2
256 2 ▶ push @strings => sprintf 'copyright_year = %s', $self->copyright_year if $self->has_copyright_year;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_copyright_year 2 0 2
257 2 ▶ push @strings => '' if scalar @strings;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @strings 2 0 2
258  
259 2 foreach my $plugin ($self->all_plugins) {
260 40 push @strings => $plugin->to_string, '';
261 }
262  
263 {
264 2 my $has_author_deps = 0;
265 2 my $previous_module = '';
266  
267 2 ▶ my @phases = ('develop', $self->add_prereqs_as_authordeps ? (qw/runtime test/) : ());
Branch: 1 branch
BranchTrueFalseTotal
$self->add_prereqs_as_authordeps ? : 1 1 2
268 67 ▶ my @filtered_prereqs = $self->filter_prereqs(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
269 67 my $prereq = $_;
270 67 ▶ $prereq->relation eq 'requires' && $prereq->module ne 'perl' && (any { $prereq->phase eq $_ } @phases);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$prereq->relation eq "requires" and $prereq->module ne "perl" and &any(sub { $prereq->phase eq $_; } , @phases) 63 4 67
Condition66%2/3
$prereq->relation eq "requires" and $prereq->module ne "perl"
!ll&&!rl&&r
4063
MC/DC50%1/2
$prereq->relation eq "requires" and $prereq->module ne "perl"
$prereq->relation eq "requires" $prereq->module ne "perl"
Truth table66%2/3
A and B
A= $prereq->relation eq "requires"B= $prereq->module ne "perl"
ABresult
0X0
100
111
271 2 });
272  
273 AUTHORDEP:
274 2 foreach my $authordep (sort { $a->module cmp $b->module } @filtered_prereqs) {
275 61 ▶ next AUTHORDEP if $authordep->module eq $previous_module;
Branch: 1 branch
BranchTrueFalseTotal
if $authordep->module eq $previous_module 0 61 61
276 61 push @strings => sprintf '; authordep %s = %s', $authordep->module, $authordep->version;
277 61 $has_author_deps = 1;
278 61 $previous_module = $authordep->module;
279 }
280 2 ▶ push @strings => '' if $has_author_deps;
Branch: 1 branch
BranchTrueFalseTotal
if $has_author_deps 2 0 2
281 }
282  
283 2 return join "\n" => @strings;
284  
285 }
286  
287 3 __PACKAGE__->meta->make_immutable;
288  
289 3 1;
290  
291 __END__
« Iller/Role/HasPlugins.pm ↑ Summary Iller.pm »