File Coverage

blib/lib/Dist/Zilla/Util.pm
Criterion Covered Total %
statement 57 61 93.4
branch 23 30 76.6
condition 3 11 27.2
subroutine 14 15 93.3
pod 2 3 66.6
total 99 120 82.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Util 6.030;
2             # ABSTRACT: random snippets of code that Dist::Zilla wants
3              
4 54     54   102520 use Dist::Zilla::Pragmas;
  54         136  
  54         411  
5              
6 54     54   443 use Carp ();
  54         134  
  54         876  
7 54     54   2051 use Encode ();
  54         45844  
  54         1012  
8              
9 54     54   1513 use namespace::autoclean;
  54         39969  
  54         428  
10              
11             {
12             package
13             Dist::Zilla::Util::PEA;
14             @Dist::Zilla::Util::PEA::ISA = ('Pod::Simple');
15              
16             sub _new {
17 14     14   4638 my ($class, @args) = @_;
18 14         7298 require Pod::Simple;
19 14         282677 my $parser = $class->new(@args);
20             $parser->code_handler(sub {
21 131     131   5736 my ($line, $line_number, $parser) = @_;
22 131 100       329 return if $parser->{abstract};
23              
24              
25 43 100       207 return $parser->{abstract} = $1
26             if $line =~ /^\s*#+\s*ABSTRACT:[ \t]*(\S.*)$/m;
27 36         79 return;
28 14         437 });
29 14         194 return $parser;
30             }
31              
32             sub _handle_element_start {
33 61     61   14117 my ($parser, $ele_name, $attr) = @_;
34              
35 61 100       217 if ($ele_name eq 'head1') {
    100          
    100          
36 19         41 $parser->{buffer} = "";
37             }
38             elsif ($ele_name eq 'Para') {
39 17         46 $parser->{buffer} = "";
40             }
41             elsif ($ele_name eq 'C') {
42 4         14 $parser->{in_C} = 1;
43             }
44              
45 61         126 return;
46             }
47              
48             sub _handle_element_end {
49 61     61   926 my ($parser, $ele_name, $attr) = @_;
50              
51 61 100       188 return if $parser->{abstract};
52 15 100 66     79 if ($ele_name eq 'head1') {
    100          
    50          
53 7         19 $parser->{in_section} = $parser->{buffer};
54             }
55             elsif ($ele_name eq 'Para' && $parser->{in_section} eq 'NAME' ) {
56 7 50       61 if ($parser->{buffer} =~ /^(?:\S+\s+)+?-+\s+(.+)$/s) {
57 7         32 $parser->{abstract} = $1;
58             }
59             }
60             elsif ($ele_name eq 'C') {
61 1         3 delete $parser->{in_C};
62             }
63              
64 15         30 return;
65             }
66              
67             sub _handle_text {
68 49     49   490 my ($parser, $text) = @_;
69              
70             # The C<...> tags are expected to be preserved. MetaCPAN renders them.
71 49 100       112 if ($parser->{in_C}) {
72 9         28 $parser->{buffer} .= "C<$text>";
73             }
74             else {
75 40         93 $parser->{buffer} .= $text;
76             }
77 49         98 return;
78             }
79             }
80              
81             #pod =method abstract_from_file
82             #pod
83             #pod This method, I<which is likely to change or go away>, tries to guess the
84             #pod abstract of a given file, assuming that it's Perl code. It looks for a POD
85             #pod C<=head1> section called "NAME" or a comment beginning with C<ABSTRACT:>.
86             #pod
87             #pod =cut
88              
89             sub abstract_from_file {
90 8     8 1 33 my ($self, $file) = @_;
91 8         78 my $e = Dist::Zilla::Util::PEA->_new;
92              
93 8         86 $e->parse_string_document($file->content);
94              
95 8         258 return $e->{abstract};
96             }
97              
98             #pod =method expand_config_package_name
99             #pod
100             #pod my $pkg_name = Dist::Zilla::Util->expand_config_package_name($string);
101             #pod
102             #pod This method, I<which is likely to change or go away>, rewrites the given string
103             #pod into a package name.
104             #pod
105             #pod Prefixes are rewritten as follows:
106             #pod
107             #pod =for :list
108             #pod * C<=> becomes nothing
109             #pod * C<@> becomes C<Dist::Zilla::PluginBundle::>
110             #pod * C<%> becomes C<Dist::Zilla::Stash::>
111             #pod * otherwise, C<Dist::Zilla::Plugin::> is prepended
112             #pod
113             #pod =cut
114              
115 54         744 use String::RewritePrefix 0.006 rewrite => {
116             -as => '_expand_config_package_name',
117             prefixes => {
118             '=' => '',
119             '@' => 'Dist::Zilla::PluginBundle::',
120             '%' => 'Dist::Zilla::Stash::',
121             '' => 'Dist::Zilla::Plugin::',
122             },
123 54     54   52307 };
  54         32284  
124              
125             sub expand_config_package_name {
126 692     692 1 1454 shift; goto &_expand_config_package_name
  692         3867  
127             }
128              
129             sub homedir {
130 0 0 0 0 0 0 $^O eq 'MSWin32' && "$]" < 5.016 ? $ENV{HOME} || $ENV{USERPROFILE} : (glob('~'))[0];
      0        
131             }
132              
133             sub _global_config_root {
134 1     1   6 require Dist::Zilla::Path;
135 1 50       11 return Dist::Zilla::Path::path($ENV{DZIL_GLOBAL_CONFIG_ROOT}) if $ENV{DZIL_GLOBAL_CONFIG_ROOT};
136              
137 0         0 my $homedir = homedir();
138 0 0       0 Carp::croak("couldn't determine home directory") if not $homedir;
139              
140 0         0 return Dist::Zilla::Path::path($homedir)->child('.dzil');
141             }
142              
143             sub _assert_loaded_class_version_ok {
144 2     2   28 my ($self, $pkg, $version) = @_;
145              
146 2         18 require CPAN::Meta::Requirements;
147 2         30 my $req = CPAN::Meta::Requirements->from_string_hash({
148             $pkg => $version,
149             });
150              
151 2         580 my $have_version = $pkg->VERSION;
152 2 100       14 unless ($req->accepts_module($pkg => $have_version)) {
153 1   50     180 die( sprintf
154             "%s version (%s) does not match required version: %s\n",
155             $pkg,
156             $have_version // 'undef',
157             $version,
158             );
159             }
160             }
161              
162             1;
163              
164             __END__
165              
166             =pod
167              
168             =encoding UTF-8
169              
170             =head1 NAME
171              
172             Dist::Zilla::Util - random snippets of code that Dist::Zilla wants
173              
174             =head1 VERSION
175              
176             version 6.030
177              
178             =head1 PERL VERSION
179              
180             This module should work on any version of perl still receiving updates from
181             the Perl 5 Porters. This means it should work on any version of perl released
182             in the last two to three years. (That is, if the most recently released
183             version is v5.40, then this module should work on both v5.40 and v5.38.)
184              
185             Although it may work on older versions of perl, no guarantee is made that the
186             minimum required version will not be increased. The version may be increased
187             for any reason, and there is no promise that patches will be accepted to lower
188             the minimum required perl.
189              
190             =head1 METHODS
191              
192             =head2 abstract_from_file
193              
194             This method, I<which is likely to change or go away>, tries to guess the
195             abstract of a given file, assuming that it's Perl code. It looks for a POD
196             C<=head1> section called "NAME" or a comment beginning with C<ABSTRACT:>.
197              
198             =head2 expand_config_package_name
199              
200             my $pkg_name = Dist::Zilla::Util->expand_config_package_name($string);
201              
202             This method, I<which is likely to change or go away>, rewrites the given string
203             into a package name.
204              
205             Prefixes are rewritten as follows:
206              
207             =over 4
208              
209             =item *
210              
211             C<=> becomes nothing
212              
213             =item *
214              
215             C<@> becomes C<Dist::Zilla::PluginBundle::>
216              
217             =item *
218              
219             C<%> becomes C<Dist::Zilla::Stash::>
220              
221             =item *
222              
223             otherwise, C<Dist::Zilla::Plugin::> is prepended
224              
225             =back
226              
227             =head1 AUTHOR
228              
229             Ricardo SIGNES 😏 <cpan@semiotic.systems>
230              
231             =head1 COPYRIGHT AND LICENSE
232              
233             This software is copyright (c) 2023 by Ricardo SIGNES.
234              
235             This is free software; you can redistribute it and/or modify it under
236             the same terms as the Perl 5 programming language system itself.
237              
238             =cut