| 1 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||
| 2 | # | ||||||||||||||||||
| 3 | # file: lib/Dist/Zilla/Plugin/MetaResources/Template.pm | ||||||||||||||||||
| 4 | # | ||||||||||||||||||
| 5 | # Copyright © 2015 Van de Bugger | ||||||||||||||||||
| 6 | # | ||||||||||||||||||
| 7 | # This file is part of perl-Dist-Zilla-Plugin-MetaResources-Template. | ||||||||||||||||||
| 8 | # | ||||||||||||||||||
| 9 | # perl-Dist-Zilla-Plugin-MetaResources-Template is free software: you can redistribute it and/or | ||||||||||||||||||
| 10 | # modify it under the terms of the GNU General Public License as published by the Free Software | ||||||||||||||||||
| 11 | # Foundation, either version 3 of the License, or (at your option) any later version. | ||||||||||||||||||
| 12 | # | ||||||||||||||||||
| 13 | # perl-Dist-Zilla-Plugin-MetaResources-Template is distributed in the hope that it will be | ||||||||||||||||||
| 14 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||||||||||||||||
| 15 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||||||||||||||||
| 16 | # | ||||||||||||||||||
| 17 | # You should have received a copy of the GNU General Public License along with | ||||||||||||||||||
| 18 | # perl-Dist-Zilla-Plugin-MetaResources-Template. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||
| 19 | # | ||||||||||||||||||
| 20 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | #pod =for :this This is C<Dist::Zilla::Plugin::MetaResources::Template> module documentation. Read this if you are going to hack or | ||||||||||||||||||
| 23 | #pod extend C<Dist-Zilla-Plugin-MetaResources-Template>. | ||||||||||||||||||
| 24 | #pod | ||||||||||||||||||
| 25 | #pod =for :that If you want to use Perl code in distribution "resource" metadata, read the L<manual|Dist::Zilla::Plugin::MetaResources::Template::Manual>. General | ||||||||||||||||||
| 26 | #pod topics like getting source, building, installing, bug reporting and some others are covered in the | ||||||||||||||||||
| 27 | #pod F<README>. | ||||||||||||||||||
| 28 | #pod | ||||||||||||||||||
| 29 | #pod =head1 SYNOPSIS | ||||||||||||||||||
| 30 | #pod | ||||||||||||||||||
| 31 | #pod Oops. | ||||||||||||||||||
| 32 | #pod | ||||||||||||||||||
| 33 | #pod =head1 DESCRIPTION | ||||||||||||||||||
| 34 | #pod | ||||||||||||||||||
| 35 | #pod C<Dist::Zilla::Plugin::MetaResources::Template> extends C<Dist::Zilla::Plugin::MetaResources>. The class implements C<BUILD> | ||||||||||||||||||
| 36 | #pod method, which expand templates in option values, all other work is done by the parent class. | ||||||||||||||||||
| 37 | #pod | ||||||||||||||||||
| 38 | #pod Template processing abilities achieved by consuming C<Dist::Zilla::Role::TextTemplater> role. | ||||||||||||||||||
| 39 | #pod | ||||||||||||||||||
| 40 | #pod =head1 SEE ALSO | ||||||||||||||||||
| 41 | #pod | ||||||||||||||||||
| 42 | #pod =for :list | ||||||||||||||||||
| 43 | #pod = L<Dist::Zilla> | ||||||||||||||||||
| 44 | #pod = L<Dist::Zilla::Role> | ||||||||||||||||||
| 45 | #pod = L<Dist::Zilla::Role::Plugin> | ||||||||||||||||||
| 46 | #pod = L<Dist::Zilla::Role::TextTemplater> | ||||||||||||||||||
| 47 | #pod = L<Dist::Zilla::Plugin::MetaResources> | ||||||||||||||||||
| 48 | #pod = L<Dist::Zilla::Plugin::MetaResources::Template::Manual> | ||||||||||||||||||
| 49 | #pod = L<CPAN::Meta::Spec> | ||||||||||||||||||
| 50 | #pod = L<CPAN::Meta::Spec/"resources"> | ||||||||||||||||||
| 51 | #pod = L<Moose::Manual::Construction/"BUILD"> | ||||||||||||||||||
| 52 | #pod | ||||||||||||||||||
| 53 | #pod =cut | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | package Dist::Zilla::Plugin::MetaResources::Template; | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 2 | ▶ | use Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 60 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 61 | 2 | ▶ | use version 0.77; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | # ABSTRACT: Use Perl code in distribution "resource" metadata | ||||||||||||||||||
| 64 | 2 | our $VERSION = 'v0.4.7'; # VERSION | |||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 2 | extends 'Dist::Zilla::Plugin::MetaResources'; | |||||||||||||||||
| 67 | 2 | with 'Dist::Zilla::Role::TextTemplater' => { -version => 'v0.4.0' }; | |||||||||||||||||
| 68 | # ^ `TextTemplater` v0.4.0 supports `filename` option. | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | #pod =Method BUILDARGS | ||||||||||||||||||
| 73 | #pod | ||||||||||||||||||
| 74 | #pod Parent's C<BUILDARGS> mangles all the arguments: it moves them into resources, including | ||||||||||||||||||
| 75 | #pod C<TextTemplater> arguments: C<delimiters>, C<package>, C<prepend>. We have to protect | ||||||||||||||||||
| 76 | #pod C<TextTemplater> arguments from being misinterpreted. | ||||||||||||||||||
| 77 | #pod | ||||||||||||||||||
| 78 | #pod =cut | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 8 | ▶ | around BUILDARGS => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 81 | 8 | my ( $orig, $class, $args ) = @_; | |||||||||||||||||
| 82 | 8 | my $save = {}; | |||||||||||||||||
| 83 | 8 | for my $attr ( qw{ delimiters package prepend } ) { | |||||||||||||||||
| 84 | 24 | ▶ | if ( exists( $args->{ $attr } ) ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 7 | $save->{ $attr } = delete( $args->{ $attr } ); | |||||||||||||||||
| 86 | }; | ||||||||||||||||||
| 87 | }; | ||||||||||||||||||
| 88 | 8 | $args = $class->$orig( $args ); | |||||||||||||||||
| 89 | 8 | for my $attr ( keys( %$save ) ) { | |||||||||||||||||
| 90 | 7 | $args->{ $attr } = $save->{ $attr }; | |||||||||||||||||
| 91 | }; | ||||||||||||||||||
| 92 | 8 | return $args; | |||||||||||||||||
| 93 | 2 | }; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | #pod =method BUILD | ||||||||||||||||||
| 98 | #pod | ||||||||||||||||||
| 99 | #pod The method is automatically called after object creation. The method recursively walks through the | ||||||||||||||||||
| 100 | #pod C<< $self->{resources} >> and expand templates in string by calling C<< $self->fill_in_string >> | ||||||||||||||||||
| 101 | #pod (it is a method from C<TextTemplater> role). | ||||||||||||||||||
| 102 | #pod | ||||||||||||||||||
| 103 | #pod Defining C<BUILDARGS> seems like a simpler approach because all the options are in plain list and | ||||||||||||||||||
| 104 | #pod so there is no need in recursive walking trough a complex data structure (C<< $self->{ resources } | ||||||||||||||||||
| 105 | #pod >>), but we need C<$self> to perform template expansion while C<BUILDARGS> is a class method. | ||||||||||||||||||
| 106 | #pod | ||||||||||||||||||
| 107 | #pod =cut | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 8 | ▶ | sub BUILD { | ||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 110 | 8 | my ( $self ) = @_; | |||||||||||||||||
| 111 | 8 | $self->{ resources } = $self->_expand( $self->{ resources } ); | |||||||||||||||||
| 112 | 5 | return; | |||||||||||||||||
| 113 | }; | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 30 | ▶ | sub _expand { | ||||||||||||||||
|
Subroutine
_expand: called CC=8 SCAR=21.0
| |||||||||||||||||||
| 118 | 30 | my ( $self, $item, $name ) = @_; | |||||||||||||||||
| 119 | 30 | ▶ | if ( 0 ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 120 | } elsif ( ref( $item ) eq '' ) { | ||||||||||||||||||
| 121 | 15 | $item = $self->fill_in_string( $item, undef, { filename => $name } ); | |||||||||||||||||
| 122 | } elsif ( ref( $item ) eq 'ARRAY' ) { | ||||||||||||||||||
| 123 | 2 | ▶ | my $prefix = defined( $name ) ? $name . '#' : ''; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 124 | 2 | my $idx = 0; | |||||||||||||||||
| 125 | 2 | for my $elem ( @$item ) { | |||||||||||||||||
| 126 | 2 | ++ $idx; | |||||||||||||||||
| 127 | 2 | $elem = $self->_expand( $elem, $prefix . $idx ); | |||||||||||||||||
| 128 | }; | ||||||||||||||||||
| 129 | } elsif ( ref( $item ) eq 'HASH' ) { | ||||||||||||||||||
| 130 | 13 | ▶ | my $prefix = defined( $name ) ? $name . '.' : ''; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 131 | 13 | for my $key ( keys( %$item ) ) { | |||||||||||||||||
| 132 | 20 | $item->{ $key } = $self->_expand( $item->{ $key }, $prefix . $key ); | |||||||||||||||||
| 133 | }; | ||||||||||||||||||
| 134 | } else { | ||||||||||||||||||
| 135 | 0 | die "Unexpected ref type: " . ref( $item ); ## no critic ( RequireCarping ) | |||||||||||||||||
| 136 | }; | ||||||||||||||||||
| 137 | 22 | return $item; | |||||||||||||||||
| 138 | }; | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 2 | 1; | |||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | # -------------------------------------------------------------------------------------------------- | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | #pod =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||
| 149 | #pod | ||||||||||||||||||
| 150 | #pod Copyright (C) 2015 Van de Bugger | ||||||||||||||||||
| 151 | #pod | ||||||||||||||||||
| 152 | #pod License GPLv3+: The GNU General Public License version 3 or later | ||||||||||||||||||
| 153 | #pod <http://www.gnu.org/licenses/gpl-3.0.txt>. | ||||||||||||||||||
| 154 | #pod | ||||||||||||||||||
| 155 | #pod This is free software: you are free to change and redistribute it. There is | ||||||||||||||||||
| 156 | #pod NO WARRANTY, to the extent permitted by law. | ||||||||||||||||||
| 157 | #pod | ||||||||||||||||||
| 158 | #pod | ||||||||||||||||||
| 159 | #pod =cut | ||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | # doc/what.pod # | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | #pod =encoding UTF-8 | ||||||||||||||||||
| 164 | #pod | ||||||||||||||||||
| 165 | #pod =head1 WHAT? | ||||||||||||||||||
| 166 | #pod | ||||||||||||||||||
| 167 | #pod C<Dist-Zilla-Plugin-MetaResources-Template> is a C<Dist::Zilla> plugin, a replacement for standard plugin C<MetaResources>. | ||||||||||||||||||
| 168 | #pod Both provide resources for distribution metadata, but this one treats values as text templates. | ||||||||||||||||||
| 169 | #pod | ||||||||||||||||||
| 170 | #pod =cut | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | # end of file # | ||||||||||||||||||
| 173 | |||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | # end of file # | ||||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||