File Coverage

blib/lib/Dist/Zilla/Plugin/CoalescePod.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 12 83.3
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 47 51 92.1


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::CoalescePod;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: merge .pod files into their .pm counterparts
4             $Dist::Zilla::Plugin::CoalescePod::VERSION = '1.0.0';
5 1     1   4574852 use strict;
  1         4  
  1         46  
6 1     1   11 use warnings;
  1         4  
  1         36  
7              
8 1     1   8 use Moose;
  1         2  
  1         8  
9              
10             with qw(
11             Dist::Zilla::Role::FileMunger
12             Dist::Zilla::Role::FilePruner
13             );
14              
15             has _pod_files => (
16             is => 'rw',
17             isa => 'ArrayRef',
18             default => sub { [] },
19             );
20              
21             sub munge_file {
22 8     8 0 47937 my ( $self, $file ) = @_;
23              
24             # only look under /lib
25 8 100       26 return unless $file->name =~ m#^lib/.*\.pm$#;
26              
27 2         156 ( my $podname = $file->name ) =~ s/\.pm$/.pod/;
28              
29 4         93 my ( $podfile ) = grep { $_->name eq $podname }
30 2 50       95 @{ $self->_pod_files } or return;
  2         72  
31              
32 2         112 $self->log( "merged " . $podfile->name . " into " . $file->name );
33              
34 2         882 my @content = ( $file->content );
35              
36 2 100       865 push @content, $1 if $content[0] =~ s/(^__DATA__.*)//ms;
37              
38             # inject the pod
39 2         10 splice @content, 1, 0, $podfile->content;
40              
41 2         1514 $file->content( join "\n\n", @content );
42             }
43              
44             sub prune_files {
45 1     1 0 159254 my ($self) = @_;
46              
47 1         4 my @files = @{ $self->zilla->files };
  1         33  
48              
49 1         65 foreach my $file ( @files ) {
50 11 100       600 next unless $file->name =~ m/\.pod$/;
51 3 50       139 next if $file->name =~ /t\/corpus/;
52              
53 3         137 my $pm = $file->name =~ s/\.pod$/.pm/r;
54              
55             # only deal with pod files with associated pm files
56 3 100       136 next unless grep { $_->name eq $pm } @files;
  33         1277  
57              
58 2         99 push @{ $self->_pod_files }, $file;
  2         68  
59 2         56 $self->zilla->prune_file($file);
60             }
61             }
62              
63             1;
64              
65              
66             #PODNAME: Foo
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Foo - merge .pod files into their .pm counterparts
77              
78             =head1 VERSION
79              
80             version 1.0.0
81              
82             =head1 SYNOPSIS
83              
84             # in dist.ini
85             [CoalescePod]
86              
87             =head1 DESCRIPTION
88              
89             If the files I<Foo.pm> and I<Foo.pod> both exist, the pod file is removed and
90             its content appended to the end of the C<.pm> file (or just before a
91             C<__DATA__> marker if present)
92              
93             =head1 AUTHOR
94              
95             Yanick Champoux <yanick@cpan.org>
96              
97             =head1 COPYRIGHT AND LICENSE
98              
99             This software is copyright (c) 2023 by Yanick Champoux.
100              
101             This is free software; you can redistribute it and/or modify it under
102             the same terms as the Perl 5 programming language system itself.
103              
104             =cut