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.037;
2             # ABSTRACT: build a custom file from only the plugin configuration
3              
4 2     2   2187 use Moose;
  2         7  
  2         19  
5             with (
6             'Dist::Zilla::Role::FileGatherer',
7             'Dist::Zilla::Role::TextTemplate',
8             );
9              
10 2     2   16053 use Dist::Zilla::Pragmas;
  2         5  
  2         20  
11              
12 2     2   17 use namespace::autoclean;
  2         4  
  2         21  
13              
14 2     2   803 use Dist::Zilla::File::InMemory;
  2         21  
  2         1155  
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 686 sub mvp_aliases { +{ is_template => 'content_is_template' } }
53              
54 4     4 0 1282 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 18 my ($self, $arg) = @_;
110              
111 4         33 my $file = Dist::Zilla::File::InMemory->new({
112             name => $self->_filename,
113             content => $self->_content,
114             });
115              
116 4         42 $self->add_file($file);
117 4         48 return;
118             }
119              
120             sub _content {
121 4     4   11 my $self = shift;
122              
123 4         9 my $content = join "\n", @{ $self->content };
  4         194  
124 4         13 $content .= qq{\n};
125              
126 4 100       199 if ($self->content_is_template) {
127 2         91 $content = $self->fill_in_string(
128             $content,
129             {
130             dist => \($self->zilla),
131             plugin => \($self),
132             },
133             );
134             }
135              
136 4         252 return $content;
137             }
138              
139             sub _filename {
140 4     4   10 my $self = shift;
141              
142 4         221 my $filename = $self->filename;
143              
144 4 100       207 if ($self->name_is_template) {
145 1         45 $filename = $self->fill_in_string(
146             $filename,
147             {
148             dist => \($self->zilla),
149             plugin => \($self),
150             },
151             );
152             }
153              
154 4         23 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.037
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
212             released in the last two to three years. (That is, if the most recently
213             released version is v5.40, then this module should work on both v5.40 and
214             v5.38.)
215              
216             Although it may work on older versions of perl, no guarantee is made that the
217             minimum required version will not be increased. The version may be increased
218             for any reason, and there is no promise that patches will be accepted to
219             lower the minimum required perl.
220              
221             =head1 ATTRIBUTES
222              
223             =head2 filename
224              
225             This attribute names the file you want to generate. It is required.
226              
227             =head2 content
228              
229             The C<content> attribute is an arrayref of lines that will be joined together
230             with newlines to form the file content.
231              
232             =head2 content_is_template, is_template
233              
234             This attribute is a bool indicating whether or not the content should be
235             treated as a Text::Template template. By default, it is false.
236              
237             =head2 name_is_template
238              
239             This attribute is a bool indicating whether or not the filename should be
240             treated as a Text::Template template. By default, it is false.
241              
242             =head1 AUTHOR
243              
244             Ricardo SIGNES 😏 <cpan@semiotic.systems>
245              
246             =head1 COPYRIGHT AND LICENSE
247              
248             This software is copyright (c) 2026 by Ricardo SIGNES.
249              
250             This is free software; you can redistribute it and/or modify it under
251             the same terms as the Perl 5 programming language system itself.
252              
253             =cut