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