File Coverage

blib/lib/Dist/Zilla/Plugin/GenerateFile.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 0 3 0.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GenerateFile 6.029;
2             # ABSTRACT: build a custom file from only the plugin configuration
3              
4 2     2   2127 use Moose;
  2         7  
  2         19  
5             with (
6             'Dist::Zilla::Role::FileGatherer',
7             'Dist::Zilla::Role::TextTemplate',
8             );
9              
10 2     2   14164 use Dist::Zilla::Pragmas;
  2         7  
  2         23  
11              
12 2     2   16 use namespace::autoclean;
  2         5  
  2         19  
13              
14 2     2   572 use Dist::Zilla::File::InMemory;
  2         9  
  2         1128  
15              
16             #pod =head1 SYNOPSIS
17             #pod
18             #pod In your F<dist.ini>:
19             #pod
20             #pod [GenerateFile]
21             #pod filename = todo/{{ $dist->name }}-master-plan.txt
22             #pod name_is_template = 1
23             #pod content_is_template = 1
24             #pod content = # Outlines the plan for world domination by {{$dist->name}}
25             #pod content =
26             #pod content = Item 1: Think of an idea!
27             #pod content = Item 2: ?
28             #pod content = Item 3: Profit!
29             #pod
30             #pod =head1 DESCRIPTION
31             #pod
32             #pod This plugin adds a file to the distribution.
33             #pod
34             #pod You can specify the content, as a sequence of lines, in your configuration.
35             #pod The specified filename and content might be literals or might be L<Text::Template>
36             #pod templates.
37             #pod
38             #pod =head2 Templating of the content
39             #pod
40             #pod If you provide C<content_is_template> (or C<is_template>) parameter of C<"1">, the
41             #pod content will be run through L<Text::Template>. The variables C<$plugin> and
42             #pod C<$dist> will be provided, set to the [GenerateFile] plugin and the L<Dist::Zilla>
43             #pod object respectively.
44             #pod
45             #pod If you provide a C<name_is_template> parameter of "1", the filename will be run
46             #pod through L<Text::Template>. The variables C<$plugin> and C<$dist> will be
47             #pod provided, set to the [GenerateFile] plugin and the L<Dist::Zilla> object
48             #pod respectively.
49             #pod
50             #pod =cut
51              
52 4     4 0 745 sub mvp_aliases { +{ is_template => 'content_is_template' } }
53              
54 4     4 0 1218 sub mvp_multivalue_args { qw(content) }
55              
56             #pod =attr filename
57             #pod
58             #pod This attribute names the file you want to generate. It is required.
59             #pod
60             #pod =cut
61              
62             has filename => (
63             is => 'ro',
64             isa => 'Str',
65             required => 1,
66             );
67              
68             #pod =attr content
69             #pod
70             #pod The C<content> attribute is an arrayref of lines that will be joined together
71             #pod with newlines to form the file content.
72             #pod
73             #pod =cut
74              
75             has content => (
76             is => 'ro',
77             isa => 'ArrayRef',
78             );
79              
80             #pod =attr content_is_template, is_template
81             #pod
82             #pod This attribute is a bool indicating whether or not the content should be
83             #pod treated as a Text::Template template. By default, it is false.
84             #pod
85             #pod =cut
86              
87             has content_is_template => (
88             is => 'ro',
89             isa => 'Bool',
90             default => 0,
91             );
92              
93             #pod =cut
94             #pod
95             #pod =attr name_is_template
96             #pod
97             #pod This attribute is a bool indicating whether or not the filename should be
98             #pod treated as a Text::Template template. By default, it is false.
99             #pod
100             #pod =cut
101              
102             has name_is_template => (
103             is => 'ro',
104             isa => 'Bool',
105             default => 0,
106             );
107              
108             sub gather_files {
109 4     4 0 13 my ($self, $arg) = @_;
110              
111 4         21 my $file = Dist::Zilla::File::InMemory->new({
112             name => $self->_filename,
113             content => $self->_content,
114             });
115              
116 4         36 $self->add_file($file);
117 4         16 return;
118             }
119              
120             sub _content {
121 4     4   9 my $self = shift;
122              
123 4         10 my $content = join "\n", @{ $self->content };
  4         141  
124 4         13 $content .= qq{\n};
125              
126 4 100       132 if ($self->content_is_template) {
127 2         62 $content = $self->fill_in_string(
128             $content,
129             {
130             dist => \($self->zilla),
131             plugin => \($self),
132             },
133             );
134             }
135              
136 4         173 return $content;
137             }
138              
139             sub _filename {
140 4     4   11 my $self = shift;
141              
142 4         152 my $filename = $self->filename;
143              
144 4 100       144 if ($self->name_is_template) {
145 1         32 $filename = $self->fill_in_string(
146             $filename,
147             {
148             dist => \($self->zilla),
149             plugin => \($self),
150             },
151             );
152             }
153              
154 4         47 return $filename;
155             }
156              
157             __PACKAGE__->meta->make_immutable;
158             1;
159              
160             __END__
161              
162             =pod
163              
164             =encoding UTF-8
165              
166             =head1 NAME
167              
168             Dist::Zilla::Plugin::GenerateFile - build a custom file from only the plugin configuration
169              
170             =head1 VERSION
171              
172             version 6.029
173              
174             =head1 SYNOPSIS
175              
176             In your F<dist.ini>:
177              
178             [GenerateFile]
179             filename = todo/{{ $dist->name }}-master-plan.txt
180             name_is_template = 1
181             content_is_template = 1
182             content = # Outlines the plan for world domination by {{$dist->name}}
183             content =
184             content = Item 1: Think of an idea!
185             content = Item 2: ?
186             content = Item 3: Profit!
187              
188             =head1 DESCRIPTION
189              
190             This plugin adds a file to the distribution.
191              
192             You can specify the content, as a sequence of lines, in your configuration.
193             The specified filename and content might be literals or might be L<Text::Template>
194             templates.
195              
196             =head2 Templating of the content
197              
198             If you provide C<content_is_template> (or C<is_template>) parameter of C<"1">, the
199             content will be run through L<Text::Template>. The variables C<$plugin> and
200             C<$dist> will be provided, set to the [GenerateFile] plugin and the L<Dist::Zilla>
201             object respectively.
202              
203             If you provide a C<name_is_template> parameter of "1", the filename will be run
204             through L<Text::Template>. The variables C<$plugin> and C<$dist> will be
205             provided, set to the [GenerateFile] plugin and the L<Dist::Zilla> object
206             respectively.
207              
208             =head1 PERL VERSION
209              
210             This module should work on any version of perl still receiving updates from
211             the Perl 5 Porters. This means it should work on any version of perl released
212             in the last two to three years. (That is, if the most recently released
213             version is v5.40, then this module should work on both v5.40 and v5.38.)
214              
215             Although it may work on older versions of perl, no guarantee is made that the
216             minimum required version will not be increased. The version may be increased
217             for any reason, and there is no promise that patches will be accepted to lower
218             the minimum required perl.
219              
220             =head1 ATTRIBUTES
221              
222             =head2 filename
223              
224             This attribute names the file you want to generate. It is required.
225              
226             =head2 content
227              
228             The C<content> attribute is an arrayref of lines that will be joined together
229             with newlines to form the file content.
230              
231             =head2 content_is_template, is_template
232              
233             This attribute is a bool indicating whether or not the content should be
234             treated as a Text::Template template. By default, it is false.
235              
236             =head2 name_is_template
237              
238             This attribute is a bool indicating whether or not the filename should be
239             treated as a Text::Template template. By default, it is false.
240              
241             =head1 AUTHOR
242              
243             Ricardo SIGNES 😏 <cpan@semiotic.systems>
244              
245             =head1 COPYRIGHT AND LICENSE
246              
247             This software is copyright (c) 2022 by Ricardo SIGNES.
248              
249             This is free software; you can redistribute it and/or modify it under
250             the same terms as the Perl 5 programming language system itself.
251              
252             =cut