File Coverage

blib/lib/Dist/Zilla/Plugin/GatherDir/Template.pm
Criterion Covered Total %
statement 18 31 58.0
branch 0 2 0.0
condition n/a
subroutine 6 8 75.0
pod n/a
total 24 41 58.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GatherDir::Template 6.029;
2             # ABSTRACT: gather all the files in a directory and use them as templates
3              
4 1     1   1354 use Moose;
  1         2  
  1         10  
5             extends 'Dist::Zilla::Plugin::GatherDir';
6             with 'Dist::Zilla::Role::TextTemplate';
7              
8 1     1   7509 use Dist::Zilla::Pragmas;
  1         4  
  1         10  
9              
10 1     1   8 use namespace::autoclean;
  1         6  
  1         53  
11              
12 1     1   220 use autodie;
  1         5  
  1         11  
13 1     1   10456 use Dist::Zilla::File::FromCode;
  1         2  
  1         54  
14 1     1   8 use Dist::Zilla::Path;
  1         3  
  1         15  
15              
16             #pod =head1 DESCRIPTION
17             #pod
18             #pod This is a subclass of the L<GatherDir|Dist::Zilla::Plugin::GatherDir>
19             #pod plugin. It works just like its parent class, except that each
20             #pod gathered file is processed through L<Text::Template>.
21             #pod
22             #pod The variables C<$plugin> and C<$dist> will be provided to the
23             #pod template, set to the GatherDir::Template plugin and the Dist::Zilla
24             #pod object, respectively.
25             #pod
26             #pod It is meant to be used when minting dists with C<dzil new>, but could be used
27             #pod in building existing dists, too.
28             #pod
29             #pod =head1 ATTRIBUTES
30             #pod
31             #pod =head2 rename
32             #pod
33             #pod Use this to rename files while they are being gathered. This is a list of
34             #pod key/value pairs, specified thus:
35             #pod
36             #pod [GatherDir::Template]
37             #pod rename.DISTNAME = $dist->name =~ s/...//r
38             #pod rename.DISTVER = $dist->version
39             #pod
40             #pod This example will replace the tokens C<DISTNAME> and C<DISTVER> with the
41             #pod expressions they are associated with. These expressions will be treated as
42             #pod though they were miniature Text::Template sections, and hence will receive the
43             #pod same variables that the file itself receives, i.e. C<$dist> and C<$plugin>.
44             #pod
45             #pod =cut
46              
47             has _rename => (
48             is => 'ro',
49             isa => 'HashRef',
50             default => sub { +{} },
51             );
52              
53             around BUILDARGS => sub {
54             my $orig = shift;
55             my ($class, @arg) = @_;
56              
57             my $args = $class->$orig(@arg);
58             my %retargs = %$args;
59              
60             for my $rename (grep /^rename/, keys %retargs) {
61             my $expr = delete $retargs{$rename};
62             $rename =~ s/^rename\.//;
63             $retargs{_rename}->{$rename} = $expr;
64             }
65              
66             return \%retargs;
67             };
68              
69             sub _file_from_filename {
70 0     0     my ($self, $filename) = @_;
71              
72 0           my $template = path($filename)->slurp_utf8;
73              
74 0 0         my @stat = stat $filename or $self->log_fatal("$filename does not exist!");
75              
76 0           my $new_filename = $filename;
77              
78 0           for my $token (keys %{$self->_rename}) {
  0            
79 0           my $expr = $self->_rename->{$token};
80 0           my $temp_temp = "{{ $expr }}";
81 0           my $replacement = $self->fill_in_string(
82             $temp_temp,
83             {
84             dist => \($self->zilla),
85             plugin => \($self),
86             },
87             );
88              
89 0           $new_filename =~ s/\Q$token/$replacement/g;
90             }
91              
92             return Dist::Zilla::File::FromCode->new({
93             name => $new_filename,
94             mode => ($stat[2] & 0755) | 0200, # kill world-writeability, make sure owner-writable.
95             code => sub {
96 0     0     my ($file_obj) = @_;
97 0           $self->fill_in_string(
98             $template,
99             {
100             dist => \($self->zilla),
101             plugin => \($self),
102             },
103             );
104             },
105 0           });
106             }
107              
108             __PACKAGE__->meta->make_immutable;
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Dist::Zilla::Plugin::GatherDir::Template - gather all the files in a directory and use them as templates
120              
121             =head1 VERSION
122              
123             version 6.029
124              
125             =head1 DESCRIPTION
126              
127             This is a subclass of the L<GatherDir|Dist::Zilla::Plugin::GatherDir>
128             plugin. It works just like its parent class, except that each
129             gathered file is processed through L<Text::Template>.
130              
131             The variables C<$plugin> and C<$dist> will be provided to the
132             template, set to the GatherDir::Template plugin and the Dist::Zilla
133             object, respectively.
134              
135             It is meant to be used when minting dists with C<dzil new>, but could be used
136             in building existing dists, too.
137              
138             =head1 PERL VERSION
139              
140             This module should work on any version of perl still receiving updates from
141             the Perl 5 Porters. This means it should work on any version of perl released
142             in the last two to three years. (That is, if the most recently released
143             version is v5.40, then this module should work on both v5.40 and v5.38.)
144              
145             Although it may work on older versions of perl, no guarantee is made that the
146             minimum required version will not be increased. The version may be increased
147             for any reason, and there is no promise that patches will be accepted to lower
148             the minimum required perl.
149              
150             =head1 ATTRIBUTES
151              
152             =head2 rename
153              
154             Use this to rename files while they are being gathered. This is a list of
155             key/value pairs, specified thus:
156              
157             [GatherDir::Template]
158             rename.DISTNAME = $dist->name =~ s/...//r
159             rename.DISTVER = $dist->version
160              
161             This example will replace the tokens C<DISTNAME> and C<DISTVER> with the
162             expressions they are associated with. These expressions will be treated as
163             though they were miniature Text::Template sections, and hence will receive the
164             same variables that the file itself receives, i.e. C<$dist> and C<$plugin>.
165              
166             =head1 AUTHOR
167              
168             Ricardo SIGNES 😏 <cpan@semiotic.systems>
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             This software is copyright (c) 2022 by Ricardo SIGNES.
173              
174             This is free software; you can redistribute it and/or modify it under
175             the same terms as the Perl 5 programming language system itself.
176              
177             =cut