File Coverage

blib/lib/Dist/Zilla/Plugin/SyncCPANfile.pm
Criterion Covered Total %
statement 40 40 100.0
branch 8 8 100.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::SyncCPANfile;
2             $Dist::Zilla::Plugin::SyncCPANfile::VERSION = '0.02';
3             # ABSTRACT: Sync a cpanfile with the prereqs listed in dist.ini
4              
5             #use v5.10;
6              
7 1     1   3602693 use strict;
  1         5  
  1         42  
8 1     1   7 use warnings;
  1         2  
  1         28  
9              
10 1     1   6 use Moose;
  1         3  
  1         7  
11 1     1   7131 use namespace::autoclean;
  1         3  
  1         11  
12 1     1   76 use Path::Tiny;
  1         3  
  1         540  
13              
14             with qw(
15             Dist::Zilla::Role::AfterBuild
16             );
17              
18             has filename => (
19             is => 'ro',
20             isa => 'Str',
21             default => 'cpanfile',
22             );
23            
24             has comment => (
25             is => 'ro',
26             isa => 'ArrayRef[Str]',
27             default => sub {
28             [
29             ( sprintf 'This file is generated by %s v%s', __PACKAGE__, __PACKAGE__->VERSION // '<internal>' ),
30             'Do not edit this file directly. To change prereqs, edit the `dist.ini` file.',
31             ]
32             }
33             );
34              
35 3     3 0 82268 sub mvp_multivalue_args { qw( comment ) }
36            
37             sub after_build {
38 3     3 0 322728 my ($self) = @_;
39              
40 3         16 my $content = $self->_get_cpanfile();
41              
42             # need to write it to disk if we're in a
43             # phase that is not filemunge
44 3         114 path( $self->filename )->spew_raw( $content );
45             }
46              
47             sub _get_cpanfile {
48 3     3   12 my ($self) = @_;
49              
50 3         109 my $zilla = $self->zilla;
51 3         115 my $prereqs = $zilla->prereqs;
52            
53 3         39 my @types = qw(requires recommends suggests conflicts);
54 3         13 my @phases = qw(runtime build test configure develop);
55            
56 3         8 my $str = join "\n", ( map { "# $_" } @{ $self->comment } ), '', '';
  6         30  
  3         107  
57 3         14 for my $phase (@phases) {
58 15 100       134 my $prefix = $phase eq 'runtime' ? '' : (sprintf "\non '%s' => sub {\n", $phase );
59 15 100       40 my $postfix = $phase eq 'runtime' ? '' : "};\n";
60 15 100       37 my $indent = $phase eq 'runtime' ? '' : ' ';
61              
62 15         37 for my $type (@types) {
63 60         373 my $req = $prereqs->requirements_for($phase, $type);
64              
65 60 100       5401 next unless $req->required_modules;
66              
67 4         36 $str .= $prefix;
68            
69 4         14 for my $module ( sort $req->required_modules ) {
70 4         33 my $version = $req->requirements_for_module( $module );
71              
72 4         216 $str .= sprintf qq~%s%s "%s" => "%s";\n~,
73             $indent,
74             $type,
75             $module,
76             $version;
77             }
78              
79 4         13 $str .= $postfix;
80             }
81             }
82              
83 3         37 return $str;
84             }
85              
86             __PACKAGE__->meta->make_immutable;
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =encoding utf-8
95              
96             =head1 NAME
97              
98             Dist::Zilla::Plugin::SyncCPANfile - Sync a cpanfile with the prereqs listed in dist.ini
99              
100             =head1 VERSION
101              
102             version 0.02
103              
104             =head1 SYNOPSIS
105              
106             # in dist.ini
107             [SyncCPANfile]
108              
109             # configure it yourself
110             [SyncCPANfile]
111             filename = my-cpanfile
112             comment = This is my cpanfile
113              
114             Unlike L<Dist::Zilla::Plugin::CPANFile> this plugin does not
115             add a I<cpanfile> to the distribution but to the "disk".
116              
117             =head1 CONFIG
118              
119             =head2 filename
120              
121             With this config you can change the filename for the file. It defaults
122             to I<cpanfile>.
123              
124             [SyncCPANfile]
125             filename = my-cpanfile
126              
127             =head2 comment
128              
129             The default comment says, that the I<cpanfile> was generated by this plugin.
130             You can define your own comment.
131              
132             [SyncCPANfile]
133             comment = This is my cpanfile
134             comment = line 2
135              
136             =head1 SEE ALSO
137              
138             L<Dist::Zilla::Plugin::CPANFile>, L<Dist::Zilla::Plugin::GitHubREADME::Badge>
139              
140             =for Pod::Coverage after_build mvp_multivalue_args
141              
142             =head1 AUTHOR
143              
144             Renee Baecker <reneeb@cpan.org>
145              
146             =head1 COPYRIGHT AND LICENSE
147              
148             This software is Copyright (c) 2021 by Renee Baecker.
149              
150             This is free software, licensed under:
151              
152             The Artistic License 2.0 (GPL Compatible)
153              
154             =cut