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.037;
2             # ABSTRACT: random snippets of code that Dist::Zilla wants
3              
4 54     54   136257 use Dist::Zilla::Pragmas;
  54         136  
  54         468  
5              
6 54     54   616 use Carp ();
  54         118  
  54         1039  
7 54     54   2307 use Encode ();
  54         63643  
  54         1231  
8              
9 54     54   1454 use namespace::autoclean;
  54         46113  
  54         448  
10              
11             {
12             package
13             Dist::Zilla::Util::PEA;
14             @Dist::Zilla::Util::PEA::ISA = ('Pod::Simple');
15              
16             sub _new {
17 14     14   465720 my ($class, @args) = @_;
18 14         6772 require Pod::Simple;
19 14         349828 my $parser = $class->new(@args);
20             $parser->code_handler(sub {
21 131     131   8411 my ($line, $line_number, $parser) = @_;
22 131 100       298 return if $parser->{abstract};
23              
24              
25 43 100       160 return $parser->{abstract} = $1
26             if $line =~ /^\s*#+\s*ABSTRACT:[ \t]*(\S.*)$/m;
27 36         72 return;
28 14         557 });
29 14         79 return $parser;
30             }
31              
32             sub _handle_element_start {
33 61     61   13951 my ($parser, $ele_name, $attr) = @_;
34              
35 61 100       228 if ($ele_name eq 'head1') {
    100          
    100          
36 19         42 $parser->{buffer} = "";
37             }
38             elsif ($ele_name eq 'Para') {
39 17         32 $parser->{buffer} = "";
40             }
41             elsif ($ele_name eq 'C') {
42 4         11 $parser->{in_C} = 1;
43             }
44              
45 61         112 return;
46             }
47              
48             sub _handle_element_end {
49 61     61   1004 my ($parser, $ele_name, $attr) = @_;
50              
51 61 100       167 return if $parser->{abstract};
52 15 100 66     61 if ($ele_name eq 'head1') {
    100          
    50          
53 7         13 $parser->{in_section} = $parser->{buffer};
54             }
55             elsif ($ele_name eq 'Para' && $parser->{in_section} eq 'NAME' ) {
56 7 50       49 if ($parser->{buffer} =~ /^(?:\S+\s+)+?-+\s+(.+)$/s) {
57 7         20 $parser->{abstract} = $1;
58             }
59             }
60             elsif ($ele_name eq 'C') {
61 1         2 delete $parser->{in_C};
62             }
63              
64 15         22 return;
65             }
66              
67             sub _handle_text {
68 49     49   402 my ($parser, $text) = @_;
69              
70             # The C<...> tags are expected to be preserved. MetaCPAN renders them.
71 49 100       82 if ($parser->{in_C}) {
72 9         29 $parser->{buffer} .= "C<$text>";
73             }
74             else {
75 40         84 $parser->{buffer} .= $text;
76             }
77 49         74 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 34 my ($self, $file) = @_;
91 8         93 my $e = Dist::Zilla::Util::PEA->_new;
92              
93 8         75 $e->parse_string_document($file->content);
94              
95 8         319 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         751 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   62505 };
  54         32652  
124              
125             sub expand_config_package_name {
126 692     692 1 1463 shift; goto &_expand_config_package_name
  692         3995  
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   5 require Dist::Zilla::Path;
135 1 50       7 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   22 my ($self, $pkg, $version) = @_;
145              
146 2         13 require CPAN::Meta::Requirements;
147 2         22 my $req = CPAN::Meta::Requirements->from_string_hash({
148             $pkg => $version,
149             });
150              
151 2         436 my $have_version = $pkg->VERSION;
152 2 100       10 unless ($req->accepts_module($pkg => $have_version)) {
153 1   50     85 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.037
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
182             released in the last two to three years. (That is, if the most recently
183             released version is v5.40, then this module should work on both v5.40 and
184             v5.38.)
185              
186             Although it may work on older versions of perl, no guarantee is made that the
187             minimum required version will not be increased. The version may be increased
188             for any reason, and there is no promise that patches will be accepted to
189             lower the minimum required perl.
190              
191             =head1 METHODS
192              
193             =head2 abstract_from_file
194              
195             This method, I<which is likely to change or go away>, tries to guess the
196             abstract of a given file, assuming that it's Perl code. It looks for a POD
197             C<=head1> section called "NAME" or a comment beginning with C<ABSTRACT:>.
198              
199             =head2 expand_config_package_name
200              
201             my $pkg_name = Dist::Zilla::Util->expand_config_package_name($string);
202              
203             This method, I<which is likely to change or go away>, rewrites the given string
204             into a package name.
205              
206             Prefixes are rewritten as follows:
207              
208             =over 4
209              
210             =item *
211              
212             C<=> becomes nothing
213              
214             =item *
215              
216             C<@> becomes C<Dist::Zilla::PluginBundle::>
217              
218             =item *
219              
220             C<%> becomes C<Dist::Zilla::Stash::>
221              
222             =item *
223              
224             otherwise, C<Dist::Zilla::Plugin::> is prepended
225              
226             =back
227              
228             =head1 AUTHOR
229              
230             Ricardo SIGNES 😏 <cpan@semiotic.systems>
231              
232             =head1 COPYRIGHT AND LICENSE
233              
234             This software is copyright (c) 2026 by Ricardo SIGNES.
235              
236             This is free software; you can redistribute it and/or modify it under
237             the same terms as the Perl 5 programming language system itself.
238              
239             =cut