File Coverage

blib/lib/Dist/Zilla/Util/FileGenerator.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1 1     1   32169 use strictures;
  1         3  
  1         7  
2              
3             package Dist::Zilla::Util::FileGenerator;
4              
5             our $VERSION = '0.120090'; # VERSION
6              
7             # ABSTRACT: helper to generate files with little repetition in a PluginBundle
8              
9             #
10             # This file is part of Dist-Zilla-Util-FileGenerator
11             #
12             # This software is Copyright (c) 2012 by Christian Walde.
13             #
14             # This is free software, licensed under:
15             #
16             # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004
17             #
18              
19 1     1   2156 use Moose;
  0            
  0            
20             use MooseX::HasDefaults::RO;
21              
22             sub {
23             has $_ => ( required => 1 ) for qw( files source );
24              
25             has generators_added => ( is => 'rw' );
26              
27             has move => ( default => 0 );
28             has is_template => ( default => 1 );
29             has source_type => ( default => 'module' );
30             has generate_before_plugin => ( default => 'Manifest' );
31             has exclusion_keys => ( default => sub { { GatherDir => 'exclude_filename', PruneCruft => 'except' } } );
32              
33             has prepared_files => ( lazy => 1, builder => "_build_prepared_files" );
34             }
35             ->();
36              
37              
38             sub combine_with {
39             my ( $self, @plugins ) = @_;
40              
41             $self->generators_added( 0 );
42              
43             @plugins = map $self->_add_file_exclusions( $_ ), @plugins;
44             @plugins = map $self->_add_generators_copiers( $_ ), @plugins;
45              
46             die "could not find the plugin before which generators should be inserted" if !$self->generators_added;
47             $self->generators_added( 0 );
48              
49             return @plugins;
50             }
51              
52             sub _add_file_exclusions {
53             my ( $self, $entry ) = @_;
54              
55             my ( $plugin, $name, $config ) = $self->_parse_plugin_entry( $entry );
56              
57             if ( my $key = $self->exclusion_keys->{$plugin} ) {
58             $config->{$key} ||= [];
59             $config->{$key} = [ $config->{$key} ] if !ref $config->{$key};
60             push @{ $config->{$key} }, $self->file_names;
61              
62             return $self->_build_plugin_entry( $plugin, $name, $config );
63             }
64              
65             return $entry;
66             }
67              
68             sub _add_generators_copiers {
69             my ( $self, $entry ) = @_;
70              
71             my ( $plugin, $name, $config ) = $self->_parse_plugin_entry( $entry );
72              
73             return $entry if $plugin ne $self->generate_before_plugin;
74              
75             $self->generators_added( 1 );
76              
77             return ( $self->generators, $self->copiers, $entry );
78             }
79              
80             sub _parse_plugin_entry {
81             my ( $self, $entry ) = @_;
82              
83             return ( $entry, undef, {} ) if !ref $entry;
84              
85             my @spec = @{$entry};
86             return ( $spec[0], undef, {} ) if @spec == 1;
87             return ( $spec[0], $spec[1], {} ) if @spec == 2 and !ref $spec[1];
88             return ( $spec[0], undef, $spec[1] ) if @spec == 2 and ref $spec[1];
89             return ( $spec[0], $spec[1], $spec[2] ) if @spec == 3;
90              
91             die "weird plugin spec: @spec";
92             }
93              
94             sub _build_plugin_entry {
95             my ( $self, $plugin, $name, $config ) = @_;
96              
97             my @entry = ( $plugin );
98             push @entry, $name if $name;
99             push @entry, $config;
100              
101             return \@entry;
102             }
103              
104             sub _build_prepared_files {
105             my ( $self ) = @_;
106             return [ map $self->_prepare_file( $_ ), @{ $self->files } ];
107             }
108              
109             sub _prepare_file {
110             my ( $self, $file ) = @_;
111              
112             my %file = ( name => $file );
113             %file = ( name => shift @{$file}, @{$file} ) if ref $file;
114              
115             for my $key ( qw( move source_type source is_template ) ) {
116             $file{$key} = $self->$key if !exists $file{$key};
117             }
118              
119             return \%file;
120             }
121              
122              
123             sub generators {
124             my ( $self ) = @_;
125              
126             return map $self->_file_generator( $_ ), @{ $self->prepared_files };
127             }
128              
129              
130             sub file_names {
131             my ( $self ) = @_;
132             return map $_->{name}, @{ $self->prepared_files };
133             }
134              
135              
136             sub copiers {
137             my ( $self ) = @_;
138             return map $self->_file_copier( $_ ), @{ $self->prepared_files };
139             }
140              
141             sub _file_generator {
142             my ( $self, $file ) = @_;
143              
144             my $content;
145             $content = $self->_module_template( $file->{source}, $file->{name} ) if $file->{source_type} eq 'module';
146             $content .= $file->{extra_content} if $file->{extra_content};
147              
148             my $generator = [
149             GenerateFile => "Generate-$file->{name}" => {
150             filename => $file->{name},
151             is_template => $file->{is_template},
152             content => $content,
153             }
154             ];
155              
156             return $generator;
157             }
158              
159             sub _module_template {
160             my ( $self, $class, $file_name ) = @_;
161             return $class->data( $file_name );
162             }
163              
164             sub _file_copier {
165             my ( $self, $file ) = @_;
166              
167             my $op = $file->{move} ? "move" : "copy";
168              
169             return [ CopyFilesFromBuild => "Copy-$file->{name}" => { $op => $file->{name} } ];
170             }
171              
172             1;
173              
174             __END__
175             =pod
176              
177             =head1 NAME
178              
179             Dist::Zilla::Util::FileGenerator - helper to generate files with little repetition in a PluginBundle
180              
181             =head1 VERSION
182              
183             version 0.120090
184              
185             =head1 METHODS
186              
187             =head2 combine_with
188              
189             my @plugins_with_generated_files = $gen->combine_with( @plain_plugins );
190              
191             Given an array containing specs for a plugin bundle this method returns an array
192             with the necessary exclusions, generators and copiers added.
193              
194             =head2 generators
195              
196             my @generator_plugins = $gen->generators;
197              
198             Returns an array with the necessary generators for inclusion in a plugin bundle.
199              
200             =head2 file_names
201              
202             my @generated_files = $gen->file_names;
203              
204             Returns an array with the file names of the generated files.
205              
206             =head2 copiers
207              
208             my @generated_files = $gen->copiers;
209              
210             Returns an array with the necessary copiers for inclusion in a plugin bundle.
211              
212             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders
213              
214             =head1 SUPPORT
215              
216             =head2 Bugs / Feature Requests
217              
218             Please report any bugs or feature requests through the issue tracker
219             at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Util-FileGenerator>.
220             You will be notified automatically of any progress on your issue.
221              
222             =head2 Source Code
223              
224             This is open source software. The code repository is available for
225             public review and contribution under the terms of the license.
226              
227             L<https://github.com/wchristian/dist-zilla-util-filegenerator>
228              
229             git clone https://github.com/wchristian/dist-zilla-util-filegenerator.git
230              
231             =head1 AUTHOR
232              
233             Christian Walde <walde.christian@googlemail.com>
234              
235             =head1 COPYRIGHT AND LICENSE
236              
237             This software is Copyright (c) 2012 by Christian Walde.
238              
239             This is free software, licensed under:
240              
241             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004
242              
243             =cut
244