| 1 | package Dist::Zilla::Plugin::GatherDir::Template 6.040; | ||||||||||
| 2 | # ABSTRACT: gather all the files in a directory and use them as templates | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | extends 'Dist::Zilla::Plugin::GatherDir'; | |||||||||
| 6 | 1 | with 'Dist::Zilla::Role::TextTemplate'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Dist::Zilla::Pragmas; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Dist::Zilla::File::FromCode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Dist::Zilla::Path; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 0 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 51 | 1 | ); | |||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | around BUILDARGS => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 54 | 0 | my $orig = shift; | |||||||||
| 55 | 0 | my ($class, @arg) = @_; | |||||||||
| 56 | |||||||||||
| 57 | 0 | my $args = $class->$orig(@arg); | |||||||||
| 58 | 0 | my %retargs = %$args; | |||||||||
| 59 | |||||||||||
| 60 | 0 | for my $rename (grep /^rename/, keys %retargs) { | |||||||||
| 61 | 0 | my $expr = delete $retargs{$rename}; | |||||||||
| 62 | 0 | $rename =~ s/^rename\.//; | |||||||||
| 63 | 0 | $retargs{_rename}->{$rename} = $expr; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | return \%retargs; | |||||||||
| 67 | 1 | }; | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub _file_from_filename { | ||||||||
|
Subroutine
_file_from_filename: not called CC=3 SCAR=24.8
| |||||||||||
| 70 | 0 | my ($self, $filename) = @_; | |||||||||
| 71 | |||||||||||
| 72 | 0 | my $template = path($filename)->slurp_utf8; | |||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | my @stat = stat $filename or $self->log_fatal("$filename does not exist!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 0 | my $new_filename = $filename; | |||||||||
| 77 | |||||||||||
| 78 | 0 | for my $token (keys %{$self->_rename}) { | |||||||||
| 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 | 0 | ▶ | code => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 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 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 109 | 1 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||