File Coverage

blib/lib/Dist/Zilla/Plugin/GenerateFile/ShareDir.pm
Criterion Covered Total %
statement 40 40 100.0
branch 2 4 50.0
condition n/a
subroutine 12 12 100.0
pod 0 2 0.0
total 54 58 93.1


line stmt bran cond sub pod time code
1 2     2   1068435 use strict;
  2         4  
  2         89  
2 2     2   7 use warnings;
  2         3  
  2         120  
3             package Dist::Zilla::Plugin::GenerateFile::ShareDir;
4             BEGIN {
5 2     2   43 $Dist::Zilla::Plugin::GenerateFile::ShareDir::AUTHORITY = 'cpan:ETHER';
6             }
7             # git description: v0.004-1-g7d7dbc7
8             $Dist::Zilla::Plugin::GenerateFile::ShareDir::VERSION = '0.005';
9             # ABSTRACT: Create files in the build, based on a template located in a dist sharedir
10             # vim: set ts=8 sw=4 tw=78 et :
11              
12 2     2   8 use Moose;
  2         4  
  2         15  
13             with (
14             'Dist::Zilla::Role::FileGatherer',
15             'Dist::Zilla::Role::FileMunger',
16             'Dist::Zilla::Role::TextTemplate',
17             );
18              
19 2     2   11677 use MooseX::SlurpyConstructor 1.2;
  2         37015  
  2         10  
20 2     2   56697 use Scalar::Util 'blessed';
  2         4  
  2         126  
21 2     2   22 use File::ShareDir 'dist_file';
  2         3  
  2         118  
22 2     2   9 use Path::Tiny 0.04;
  2         58  
  2         82  
23 2     2   11 use Encode;
  2         2  
  2         130  
24 2     2   11 use namespace::autoclean;
  2         4  
  2         12  
25              
26             has dist => (
27             is => 'ro', isa => 'Str',
28             init_arg => '-dist',
29             lazy => 1,
30             default => sub { (my $dist = blessed(shift)) =~ s/::/-/g; $dist },
31             );
32              
33             has filename => (
34             init_arg => '-destination_filename',
35             is => 'ro', isa => 'Str',
36             required => 1,
37             );
38              
39             has source_filename => (
40             init_arg => '-source_filename',
41             is => 'ro', isa => 'Str',
42             lazy => 1,
43             default => sub { shift->filename },
44             );
45              
46             has encoding => (
47             init_arg => '-encoding',
48             is => 'ro', isa => 'Str',
49             lazy => 1,
50             default => 'UTF-8',
51             );
52              
53             has _extra_args => (
54             isa => 'HashRef[Str]',
55             init_arg => undef,
56             lazy => 1,
57             default => sub { {} },
58             traits => ['Hash'],
59             handles => { _extra_args => 'elements' },
60             slurpy => 1,
61             );
62              
63             around BUILDARGS => sub
64             {
65             my $orig = shift;
66             my $class = shift;
67              
68             my $args = $class->$orig(@_);
69             $args->{'-destination_filename'} = delete $args->{'-filename'} if exists $args->{'-filename'};
70              
71             return $args;
72             };
73              
74             around dump_config => sub
75             {
76             my ($orig, $self) = @_;
77             my $config = $self->$orig;
78              
79             $config->{'' . __PACKAGE__} = {
80             # XXX FIXME - it seems META.* does not like the leading - in field
81             # names! something is wrong with the serialization process.
82             'dist' => $self->dist,
83             'encoding' => $self->encoding,
84             'source_filename' => $self->source_filename,
85             'destination_filename' => $self->filename,
86             $self->_extra_args,
87             };
88             return $config;
89             };
90              
91             sub gather_files
92             {
93 2     2 0 95440 my $self = shift;
94              
95             # this should die if the file does not exist
96 2         93 my $file = dist_file($self->dist, $self->source_filename);
97              
98 2         306 my $content = path($file)->slurp_raw;
99 2         597 $content = Encode::decode($self->encoding, $content, Encode::FB_CROAK());
100              
101 2         1862 require Dist::Zilla::File::InMemory;
102 2         117609 $self->add_file(Dist::Zilla::File::InMemory->new(
103             name => $self->filename,
104             encoding => $self->encoding, # only used in Dist::Zilla 5.000+
105             content => $content,
106             ));
107             }
108              
109             sub munge_file
110             {
111 2     2 0 3858 my ($self, $file) = @_;
112              
113 2 50       49 return unless $file->name eq $self->filename;
114 2         69 $self->log_debug([ 'updating contents of %s in memory', $file->name ]);
115              
116 2         121 my $content = $self->fill_in_string(
117             $file->content,
118             {
119             $self->_extra_args, # must be first
120             dist => \($self->zilla),
121             plugin => \$self,
122             },
123             );
124              
125             # older Dist::Zilla wrote out all files :raw, so we need to encode manually here.
126 2 50       580 $content = Encode::encode($self->encoding, $content, Encode::FB_CROAK()) if Dist::Zilla->VERSION < 5.000;
127              
128 2         13 $file->content($content);
129             }
130              
131             __PACKAGE__->meta->make_immutable;
132              
133             __END__
134              
135             =pod
136              
137             =encoding UTF-8
138              
139             =for :stopwords Karen Etheridge sharedir irc
140              
141             =head1 NAME
142              
143             Dist::Zilla::Plugin::GenerateFile::ShareDir - Create files in the build, based on a template located in a dist sharedir
144              
145             =head1 VERSION
146              
147             version 0.005
148              
149             =head1 SYNOPSIS
150              
151             In your F<dist.ini>:
152              
153             [GenerateFile::ShareDir]
154             -dist = Dist::Zilla::PluginBundle::Author::ME
155             -source_filename = my_data_template.txt
156             -destination_filename = examples/my_data.txt
157             key1 = value to pass to template
158             key2 = another value to pass to template
159              
160             =head1 DESCRIPTION
161              
162             Generates a file in your dist, indicated by C<-destination_file>, based on the
163             L<Text::Template> located in the C<-source_file> of C<-dist>'s
164             L<distribution sharedir|File::ShareDir>. Any extra config values are passed
165             along to the template, in addition to C<$zilla> and C<$plugin> objects.
166              
167             I expect that usually the C<-dist> that contains the template will be either a
168             plugin bundle, so you can generate a custom-tailored file in your dist, or a
169             plugin that subclasses this one. (Otherwise, you can just as easily use
170             L<Dist::Zilla::Plugin::ShareDir|[GatherDir::Template]> to generate the file
171             directly, without needing a sharedir.)
172              
173             =for Pod::Coverage::TrustPod gather_files
174             munge_file
175              
176             =head1 OPTIONS
177              
178             This plugin accepts the following options:
179              
180             =over 4
181              
182             =item * C<-dist>
183              
184             The distribution name to use when finding the sharedir (see L<File::ShareDir>
185             and L<Dist::Zilla::Plugin::ShareDir>). Defaults to the dist corresponding to
186             the running plugin.
187              
188             =item * C<-destination_filename> or C<-filename>
189              
190             The filename to generate in the distribution being built. Required.
191              
192             =item * C<-source_filename>
193              
194             The filename in the sharedir to use to generate the new file. Defaults to the
195             same filename and path as C<-destination_file>.
196              
197             =item * C<-encoding>
198              
199             The encoding of the source file; will also be used for the encoding of the
200             destination file. Defaults to UTF-8.
201              
202             =item * C<arbitrary option>
203              
204             All other keys/values provided will be passed to the template as is.
205              
206             =back
207              
208             =head1 SUPPORT
209              
210             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-GenerateFile-ShareDir>
211             (or L<bug-Dist-Zilla-Plugin-GenerateFile-ShareDir@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-GenerateFile-ShareDir@rt.cpan.org>).
212             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
213              
214             =head1 SEE ALSO
215              
216             =over 4
217              
218             =item *
219              
220             L<File::ShareDir>
221              
222             =item *
223              
224             L<Dist::Zilla::Plugin::ShareDir>
225              
226             =item *
227              
228             L<Text::Template>
229              
230             =back
231              
232             =head1 AUTHOR
233              
234             Karen Etheridge <ether@cpan.org>
235              
236             =head1 COPYRIGHT AND LICENSE
237              
238             This software is copyright (c) 2013 by Karen Etheridge.
239              
240             This is free software; you can redistribute it and/or modify it under
241             the same terms as the Perl 5 programming language system itself.
242              
243             =cut