blib/lib/Dist/Zilla/Plugin/GenerateFile.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 94.3% 50 / 53 CC 3 SCAR 3.1
CC
3
Cov
100%
_content
2.0
_filename
2.0
BEGIN
1.0
CRAP 1.4
« blib/lib/Dist/Zilla/Plugin/MetaJSON.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/ShareDir.pm »
1 package Dist::Zilla::Plugin::GenerateFile 6.040;
2 # ABSTRACT: build a custom file from only the plugin configuration
3  
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 with (
6 'Dist::Zilla::Role::FileGatherer',
7 'Dist::Zilla::Role::TextTemplate',
8 );
9  
10 2 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶sub mvp_aliases { +{ is_template => 'content_is_template' } }
Subroutine mvp_aliases: called CC=1 SCAR=0.0
Pod undocumented
53  
54 4 ▶sub mvp_multivalue_args { qw(content) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
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 2 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 2 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 2 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 2 has name_is_template => (
103 is => 'ro',
104 isa => 'Bool',
105 default => 0,
106 );
107  
108 4 ▶sub gather_files {
Subroutine gather_files: called CC=1 SCAR=0.0
Pod undocumented
109 4 my ($self, $arg) = @_;
110  
111 4 my $file = Dist::Zilla::File::InMemory->new({
112 name => $self->_filename,
113 content => $self->_content,
114 });
115  
116 4 $self->add_file($file);
117 4 return;
118 }
119  
120 4 ▶sub _content {
Subroutine _content: called CC=2 SCAR=6.9
121 4 my $self = shift;
122  
123 4 my $content = join "\n", @{ $self->content };
124 4 $content .= qq{\n};
125  
126 4 ▶ if ($self->content_is_template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_is_template) 2 2 4
127 2 $content = $self->fill_in_string(
128 $content,
129 {
130 dist => \($self->zilla),
131 plugin => \($self),
132 },
133 );
134 }
135  
136 4 return $content;
137 }
138  
139 4 ▶sub _filename {
Subroutine _filename: called CC=2 SCAR=6.9
140 4 my $self = shift;
141  
142 4 my $filename = $self->filename;
143  
144 4 ▶ if ($self->name_is_template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name_is_template) 1 3 4
145 1 $filename = $self->fill_in_string(
146 $filename,
147 {
148 dist => \($self->zilla),
149 plugin => \($self),
150 },
151 );
152 }
153  
154 4 return $filename;
155 }
156  
157 2 __PACKAGE__->meta->make_immutable;
158 2 1;
159  
160 __END__
« blib/lib/Dist/Zilla/Plugin/MetaJSON.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/ShareDir.pm »