blib/lib/Dist/Zilla/Plugin/LicenseFromModule.pm

filter: Statementstmt 78.5% 55 / 70 Branchbran 46.6% 14 / 30 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 60.9% 86 / 141 CC 23 SCAR 29.3
CC
23
Cov
65%
author_from
46.9
provide_license
10.0
should_override_author
3.1
CRAP 18.7
↑ Summary
1 package Dist::Zilla::Plugin::LicenseFromModule;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 our $VERSION = '0.07';
4  
5 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 with 'Dist::Zilla::Role::LicenseProvider';
7  
8 5 has 'override_author', is => 'rw', isa => 'Bool', default => 0;
9  
10 5 has source_file => (
11 is => 'ro',
12 lazy => 1,
13 isa => 'Str',
14 builder => '_default_source_file',
15 );
16  
17 4 ▶sub _default_source_file {
Subroutine _default_source_file: called CC=2 SCAR=6.9
18 4 my $self = shift;
19 4 my $pm = $self->zilla->main_module->name;
20 4 (my $pod = $pm) =~ s/\.pm$/\.pod/;
21 4 ▶ return -e $pod ? $pod : $pm;
Branch: 1 branch
BranchTrueFalseTotal
-e $pod ? : 3 1 4
22 }
23  
24 14 ▶sub _file_from_filename {
Subroutine _file_from_filename: called CC=3 SCAR=11.0
25 14 my ($self, $filename) = @_;
26 14 for my $file (@{$self->zilla->files}) {
27 57 ▶ return $file if $file->name eq $filename;
Branch: 1 branch
BranchTrueFalseTotal
if $file->name eq $filename 11 46 57
28 }
29 3 die "no file module $filename in dist";
30 }
31  
32 5 ▶use Software::LicenseUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 11 ▶sub should_override_author {
Subroutine should_override_author: called CC=3 SCAR=11.3
Pod undocumented
36 11 my $self = shift;
37  
38 11 ▶ return unless $self->override_author;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->override_author 7 4 11
39  
40 4 my $stash = $self->zilla->stash_named('%User');
41 4 ▶ return unless $stash; # no %User stash means author is taken out of copyright_holder anyway
Branch: 1 branch
BranchTrueFalseTotal
unless $stash 4 0 4
42  
43 0 return $stash->authors->[0] eq $self->zilla->authors->[0];
44 }
45  
46 14 ▶sub provide_license {
Subroutine provide_license: called CC=7 SCAR=23.0
Pod undocumented
47 14 my($self, $args) = @_;
48  
49 14 my $content = $self->_file_from_filename($self->source_file)->content;
50  
51 11 my $author = $self->author_from($content);
52 11 my $year = $self->copyright_year_from($content);
53  
54 11 ▶ if ($self->should_override_author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_override_author) 0 11 11
55 0 $self->zilla->{authors} = [ $author ]; # XXX ughhh because it's readonly
56 }
57  
58 11 my @guess = Software::LicenseUtils->guess_license_from_pod($content);
59  
60 11 ▶ if (@guess != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@guess != 1) 0 11 11
61 0 $self->log(["Failed to parse license from %s", $self->zilla->main_module->name]);
62 0 return;
63 }
64  
65 11 my $license_class = $guess[0];
66  
67 11 ▶ $self->log(["guessing from %s, License is %s\nCopyright %s %s",
Condition50%2/4
$year || '(unknown)'
l!l
110
$author || '(unknown)'
l!l
110
MC/DC0%0/1
$year || '(unknown)'
$year
MC/DC0%0/1
$author || '(unknown)'
$author
Truth table50%1/2
A
A= $year
Aresult
11
00
Truth table50%1/2
A
A= $author
Aresult
11
00
68 $self->source_file, $license_class,
69 $year || '(unknown)', $author || '(unknown)']);
70  
71 11 Module::Load::load($license_class);
72  
73 return $license_class->new({
74 holder => $author || $args->{copyright_holder},
75 year => $year || $args->{copyright_year},
76 11 ▶ });
Condition33%2/6
$author || $args->{'copyright_holder'}
l!l&&r!l&&!r
1100
$year || $args->{'copyright_year'}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$author || $args->{'copyright_holder'}
$author $args->{'copyright_holder'}
MC/DC0%0/2
$year || $args->{'copyright_year'}
$year $args->{'copyright_year'}
Truth table33%1/3
A || B
A= $authorB= $args->{'copyright_holder'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $yearB= $args->{'copyright_year'}
ABresult
1X1
011
000
77 }
78  
79 # taken from Module::Install::Metadata::author_from (as well as Minilla)
80 11 ▶sub author_from {
Subroutine author_from: called CC=11 SCAR=38.5
Pod undocumented
81 11 my($self, $content) = @_;
82  
83 11 ▶ if ($content =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ / =head \d \s+ (?:authors?)\b \s* ([^\n]*) | =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s* .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s* ([^\n]*) /imsx) 11 0 11
84 =head \d \s+ (?:authors?)\b \s*
85 ([^\n]*)
86 |
87 =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
88 .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
89 ([^\n]*)
90 /ixms) {
91 11 ▶ my $author = $1 || $2;
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
380
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
92  
93 # XXX: ugly but should work anyway...
94 11 ▶ if (eval "require Pod::Escapes; 1") { ## no critics.
Branch: 2 branches
BranchTrueFalseTotal
if (eval 'require Pod::Escapes; 1') { } 11 0 11
elsif (eval 'require Pod::Text; 1' and $Pod::Text::VERSION < 3) { } 0 0 0
95 # Pod::Escapes has a mapping table.
96 # It's in core of perl >= 5.9.3, and should be installed
97 # as one of the Pod::Simple's prereqs, which is a prereq
98 # of Pod::Text 3.x (see also below).
99 11 $author =~ s{ E<( (\d+) | ([A-Za-z]+) )> }
100 {
101 defined $2
102 ? chr($2)
103 : defined $Pod::Escapes::Name2character_number{$1}
104 ? chr($Pod::Escapes::Name2character_number{$1})
105 16 ▶ : do {
Branch: 2 branches
BranchTrueFalseTotal
defined $2 ? : 0 16 16
defined $Pod::Escapes::Name2character_number{$1} ? : 16 0 16
106 0 warn "Unknown escape: E<$1>";
107 0 "E<$1>";
108 };
109 }gex;
110 }
111 ## no critic.
112 ▶ elsif (eval "require Pod::Text; 1" && $Pod::Text::VERSION < 3) {
Condition0%0/3
eval "require Pod::Text; 1" and $Pod::Text::VERSION < 3
!ll&&!rl&&r
000
MC/DC0%0/2
eval "require Pod::Text; 1" and $Pod::Text::VERSION < 3
eval "require Pod::Text; 1" $Pod::Text::VERSION < 3
Truth table0%0/3
A and B
A= eval "require Pod::Text; 1"B= $Pod::Text::VERSION < 3
ABresult
0X0
100
111
113 # Pod::Text < 3.0 has yet another mapping table,
114 # though the table name of 2.x and 1.x are different.
115 # (1.x is in core of Perl < 5.6, 2.x is in core of
116 # Perl < 5.9.3)
117 0 ▶ my $mapping = ($Pod::Text::VERSION < 2)
Branch: 1 branch
BranchTrueFalseTotal
$Pod::Text::VERSION < 2 ? : 0 0 0
118 ? \%Pod::Text::HTML_Escapes
119 : \%Pod::Text::ESCAPES;
120 0 $author =~ s{ E<( (\d+) | ([A-Za-z]+) )> }
121 {
122 defined $2
123 ? chr($2)
124 : defined $mapping->{$1}
125 ? $mapping->{$1}
126 0 ▶ : do {
Branch: 2 branches
BranchTrueFalseTotal
defined $2 ? : 0 0 0
defined $mapping->{$1} ? : 0 0 0
127 0 warn "Unknown escape: E<$1>";
128 0 "E<$1>";
129 };
130 }gex;
131 }
132 else {
133 0 $author =~ s{E<lt>}{<}g;
134 0 $author =~ s{E<gt>}{>}g;
135 }
136 11 return $author;
137 }
138  
139 0 return;
140 }
141  
142 11 ▶sub copyright_year_from {
Subroutine copyright_year_from: called CC=2 SCAR=7.6
Pod undocumented
143 11 my($self, $content) = @_;
144  
145 11 ▶ if ($content =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ / =head \d \s+ (?:licen[cs]e|licensing|copyright|legal|authors?)\b \s* .*? copyright .*? ([\d\-]+) /imsx) 11 0 11
146 =head \d \s+ (?:licen[cs]e|licensing|copyright|legal|authors?)\b \s*
147 .*? copyright .*? ([\d\-]+)
148 /ixms) {
149 11 return $1;
150 }
151  
152 0 return;
153 }
154  
155 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 5 __PACKAGE__->meta->make_immutable;
157 5 1;
158 __END__
↑ Summary