File Coverage

lib/Dist/Zilla/PluginBundle/Author/AJNN/Readme.pm
Criterion Covered Total %
statement 75 77 97.4
branch 7 10 70.0
condition 4 6 66.6
subroutine 18 18 100.0
pod 0 1 0.0
total 104 112 92.8


line stmt bran cond sub pod time code
1 3     3   1275 use 5.026;
  3         12  
2 3     3   19 use warnings;
  3         7  
  3         180  
3              
4             package Dist::Zilla::PluginBundle::Author::AJNN::Readme;
5             # ABSTRACT: Build a README file for AJNN's distributions
6             $Dist::Zilla::PluginBundle::Author::AJNN::Readme::VERSION = '0.05';
7              
8 3     3   594 use Dist::Zilla;
  3         838247  
  3         111  
9 3     3   1793 use Dist::Zilla::File::FromCode;
  3         227747  
  3         145  
10 3     3   27 use Encode;
  3         10  
  3         320  
11 3     3   27 use Moose;
  3         36  
  3         24  
12 3     3   22417 use namespace::autoclean;
  3         10  
  3         28  
13 3     3   1850 use Pod::Elemental;
  3         1157071  
  3         41  
14 3     3   3775 use Pod::Text;
  3         110403  
  3         272  
15              
16 3     3   1120 use Pod::Weaver::PluginBundle::Author::AJNN::License;
  3         12  
  3         3110  
17              
18             with 'Dist::Zilla::Role::FileGatherer';
19              
20              
21             has cpan_release => (
22             is => 'ro',
23             isa => 'Str',
24             lazy => 1,
25             default => sub { '1' },
26             );
27              
28              
29             sub gather_files {
30 3     3 0 674156 my ($self, $arg) = @_;
31            
32             $self->add_file(
33             Dist::Zilla::File::FromCode->new(
34             name => 'README',
35 3     3   169200 code => sub { $self->_readme },
36 3         127 ),
37             );
38             }
39              
40              
41             sub _readme {
42 3     3   13 my ($self) = @_;
43            
44 3         20 return join "\n\n", (
45             $self->_readme_header,
46             $self->_readme_install,
47             $self->_readme_license,
48             );
49             }
50              
51              
52             sub _readme_header {
53 3     3   11 my ($self) = @_;
54            
55 3         36 my $main_module = $self->_main_module_name;
56 3         101 my $dist_version = $self->zilla->version;
57 3         214 my $dist_name = $self->zilla->name;
58 3 50       214 my $trial_rel = $self->zilla->is_trial ? " (TRIAL RELEASE)" : "";
59            
60 3         265 my $description = $self->_main_module_description;
61 3         2421 $description =~ s/\n\n.*$//; # only keep the first paragraph
62            
63 3         93 my $link = $self->zilla->distmeta->{resources}{repository}{web};
64 3 50       247 $link = "https://metacpan.org/release/$dist_name" if $self->cpan_release;
65            
66 3         38 return <<END;
67             $main_module $dist_version$trial_rel
68              
69             $description
70              
71             More information about this software:
72             $link
73             END
74             }
75              
76              
77             sub _readme_install {
78 3     3   12 my ($self) = @_;
79            
80 3         11 my $main_module = $self->_main_module_name;
81 3         89 my $archive = $self->zilla->name . '-' . $self->zilla->version . '.tar.gz';
82            
83 3         293 my $text = <<END;
84             INSTALLATION
85              
86             END
87 3 50       118 if ($self->cpan_release) {
88 3         19 $text .= <<END;
89             The recommended way to install this Perl module distribution is directly
90             from CPAN with whichever tool you use to manage your installation of Perl.
91             For example:
92              
93             cpanm $main_module
94              
95             If you already have downloaded the distribution, you can alternatively
96             point your tool directly at the archive file or the directory:
97              
98             END
99             }
100             else {
101 0         0 $text .= <<END;
102             To install this Perl module distribution, point whichever tool you use
103             to manage your installation of Perl directly at the archive file or the
104             directory. For example:
105              
106             END
107             }
108 3         17 $text .= <<END;
109             cpanm $archive
110              
111             You can also install the module manually by following these steps:
112              
113             perl Makefile.PL
114             make
115             make test
116             make install
117              
118             See https://www.cpan.org/modules/INSTALL.html for general information
119             on installing CPAN modules.
120             END
121            
122 3         15 return $text;
123             }
124              
125              
126             sub _readme_license {
127 3     3   10 my ($self) = @_;
128            
129 3         91 my $notice = Pod::Weaver::PluginBundle::Author::AJNN::License->notice_maybe_mangled(
130             $self->zilla->license,
131             $self->zilla->authors,
132             );
133 3         50 return "COPYRIGHT AND LICENSE\n\n" . $notice;
134             }
135              
136              
137             sub _main_module_name {
138 6     6   27 my ($self) = @_;
139            
140 6         207 my $name = $self->zilla->main_module->name;
141 6         673 $name =~ s{^lib/|\.pm$}{}g;
142 6         22 $name =~ s{/}{::}g;
143 6         22 return $name;
144             }
145              
146            
147             sub _main_module_description {
148 3     3   70 my ($self) = @_;
149            
150 3         120 my $pod = $self->zilla->main_module->content;
151 3         845 $pod = Encode::encode( 'UTF-8', $pod, Encode::FB_CROAK );
152 3         150 my $document = Pod::Elemental->read_string( $pod );
153 3         24954 my $desc_found;
154 3         187 for my $element ($document->children->@*) {
155 36 100       528 if ($desc_found) {
156 6 100       31 next unless $element->isa('Pod::Elemental::Element::Generic::Text');
157 3         38 my $parser = Pod::Text->new( indent => 0 );
158 3         704 $parser->output_string( \( my $text ) );
159 3         2683 $parser->parse_string_document( "=pod\n\n" . $element->content );
160 3         3207 $text =~ s/^\s+//;
161 3         30 $text =~ s/\s+$//;
162 3   33     177 return $text || $self->zilla->abstract;
163             }
164 30   100     470 $desc_found = $element->isa('Pod::Elemental::Element::Generic::Command')
165             && $element->command eq 'head1'
166             && $element->content =~ m/\s*DESCRIPTION\s*/;
167             }
168            
169 0           return $self->zilla->abstract;
170             }
171              
172              
173             __PACKAGE__->meta->make_immutable;
174              
175             1;
176              
177             __END__
178              
179             =pod
180              
181             =encoding UTF-8
182              
183             =head1 NAME
184              
185             Dist::Zilla::PluginBundle::Author::AJNN::Readme - Build a README file for AJNN's distributions
186              
187             =head1 VERSION
188              
189             version 0.05
190              
191             =head1 DESCRIPTION
192              
193             Provides a F<README> file which only contains the most important information
194             for someone who may have extracted the distribution archive, but is unsure
195             what it is and what to do with it.
196              
197             In particular, the following content is included in the F<README>:
198              
199             =over
200              
201             =item * main module name
202              
203             =item * distribution version number
204              
205             =item * first paragraph of the distribution POD's description section
206             (or the abstract, if the description can't be found or is empty)
207              
208             =item * URL of the distribution's home page on MetaCPAN
209              
210             =item * installation instructions (for both tools and manual)
211              
212             =item * author identification
213              
214             =item * license statement
215              
216             =back
217              
218             It may be assumed that people who are already familiar with Perl and
219             its ecosystem won't usually read the F<README> accompanying a CPAN
220             distribution. They typically get all they need to know from MetaCPAN,
221             and are accustomed to C<cpanm> and other tools. Non-Perl people, however,
222             might not know how to install a Perl distribution or how to access the
223             documentation. In my opinion, I<this> is the information a CPAN distro
224             F<README> really needs to provide.
225              
226             Identification of the module, on the other hand, may be kept very brief.
227             A license file is included with the distribution, so stating the license
228             is generally not required; however, this plugin will pick up any mangling
229             done by L<Pod::Weaver::PluginBundle::Author::AJNN::License>.
230              
231             =head1 ATTRIBUTES
232              
233             =head2 cpan_release
234              
235             Whether the distribution is available on L<CPAN|https://www.cpan.org/>.
236             The default is yes. If set to no, the link in the readme will be changed
237             to GitHub and CPAN will no longer be mentioned in the installation
238             instructions.
239              
240             cpan_release = 0
241              
242             =head1 SEE ALSO
243              
244             L<Dist::Zilla::PluginBundle::Author::AJNN>
245              
246             L<Pod::Weaver::PluginBundle::Author::AJNN::License>
247              
248             L<Dist::Zilla::Plugin::Readme>
249              
250             L<Dist::Zilla::Plugin::Readme::Brief>
251              
252             =head1 AUTHOR
253              
254             Arne Johannessen <ajnn@cpan.org>
255              
256             If you contact me by email, please make sure you include the word
257             "Perl" in your subject header to help beat the spam filters.
258              
259             =head1 COPYRIGHT AND LICENSE
260              
261             Arne Johannessen has dedicated the work to the Commons by waiving all of his
262             or her rights to the work worldwide under copyright law and all related or
263             neighboring legal rights he or she had in the work, to the extent allowable by
264             law.
265              
266             Works under CC0 do not require attribution. When citing the work, you should
267             not imply endorsement by the author.
268              
269             =cut