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