File Coverage

blib/lib/Dist/Dzpl.pm
Criterion Covered Total %
statement 34 117 29.0
branch 0 38 0.0
condition 0 12 0.0
subroutine 12 19 63.1
pod 0 5 0.0
total 46 191 24.0


line stmt bran cond sub pod time code
1             package Dist::Dzpl;
2             BEGIN {
3 1     1   86834 $Dist::Dzpl::VERSION = '0.0020';
4             }
5             # ABSTRACT: An alternative configuration format (.pl) and invoker for Dist::Zilla
6              
7 1     1   10 use strict;
  1         2  
  1         28  
8 1     1   4 use warnings;
  1         1  
  1         22  
9              
10              
11             # You can actually interact with Dist::Zilla from within your configuration .pl, allowing you to easily tweak your Zilla specification on the fly
12              
13 1     1   874 use Moose;
  1         659961  
  1         10  
14              
15 1     1   9548 use Dist::Dzpl::Parser;
  1         4  
  1         43  
16              
17 1     1   9 use Dist::Zilla::Chrome::Term;
  1         2  
  1         22  
18 1     1   6 use Dist::Zilla::Util;
  1         15  
  1         19  
19 1     1   5 use Class::MOP;
  1         3  
  1         21  
20 1     1   6 use Moose::Autobox;
  1         2  
  1         6  
21 1     1   612 use Carp;
  1         2  
  1         1777  
22              
23             has zilla => qw/ is ro required 1 isa Dist::Zilla /;
24              
25             sub from_file {
26 0     0 0 0 my $self = shift;
27 0         0 my $file = shift;
28 0 0       0 $file = './' unless defined $file;
29              
30 0         0 my $source;
31 0 0       0 if ( -f $file ) {
    0          
32 0         0 $source = $file;
33             }
34             elsif ( -d $file ) {
35 0         0 my @try = qw/ dzpl dz.pl dist.pl /;
36 0         0 for ( map { "$file/$_" } @try ) {
  0         0  
37 0 0 0     0 -e $_ and ( $source = $_ ) and last;
38             }
39 0 0       0 croak "Could not find ", join( ' or ', map { "\"$_\"" } @try ), " in $file" unless $source;
  0         0  
40             }
41             else {
42 0         0 croak "Missing file";
43             }
44 0 0       0 croak "Could not read \"$source\"" unless -r $source;
45              
46 0         0 my $dzpl = $self->_from_file_sandbox( $source );
47 0 0       0 die "Error while loading $source: $@" if $@;
48 0         0 return $dzpl;
49             }
50              
51             sub _from_file_sandbox {
52 0     0   0 my $self = shift;
53 0         0 my $file = shift;
54              
55 0         0 my $package = $file;
56 0         0 $package =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
  0         0  
57              
58 0         0 return eval sprintf <<'_END_', $package;
59             package Dist::Dzpl::Sandbox::%s;
60             {
61             require Dzpl;
62             do $file;
63             Dzpl->dzpl_from_package( __PACKAGE__ ) or die $@;
64             }
65             _END_
66             }
67              
68             sub from_arguments {
69 1     1 0 81 my $self = shift;
70 1         13 my $zilla = Dist::Dzpl::Parser->parse( @_ );
71 1         44 return __PACKAGE__->new( zilla => $zilla );
72             }
73              
74             sub run {
75 0     0 0   my $self = shift;
76 0           my @arguments = @_;
77              
78 0           my $zilla = $self->zilla;
79             # TODO This should only be run once...
80 0           $zilla->_setup_default_plugins;
81              
82 0 0         return unless @arguments;
83              
84 0           my $do = shift @arguments;
85              
86 0 0         if ( $do eq 'dzil' ) {
87 0           require Dist::Zilla::App;
88 0           my $app = Dist::Zilla::App->new;
89 0           $app->{__chrome__} = $zilla->chrome;
90 0           $app->{__PACKAGE__}{zilla} = $zilla; # Cover case 1...
91 0           $app->{'Dist::Zilla::App'}{zilla} = $zilla; # ...and case 2
92             {
93 0           local @ARGV = @arguments;
  0            
94 0           $app->run;
95             }
96             }
97             else {
98 0 0         die "Dist::Zilla cannot do \"$do\"" unless $zilla->can( $do );
99 0 0         warn "Dropping arguments [@arguments]" if @arguments;
100 0           return $zilla->$do;
101             }
102             }
103              
104             sub _include_plugin_bundle {
105 0     0     my $self = shift;
106 0           my $name = shift;
107 0           my $package = shift;
108 0           my $payload = shift;
109 0           my $filter = shift;
110              
111 0           Class::MOP::load_class( $package );
112              
113 0           my @bundle = $package->bundle_config({
114             name => $name,
115             zilla => $self->zilla,
116             payload => $payload,
117             });
118              
119 0           for my $plugin ( @bundle ) {
120 0           my ( $name, $package, $payload ) = @$plugin;
121 0 0 0       next if $filter && $package =~ $filter;
122 0           $self->_include_plugin( $name, $package, $payload );
123             }
124             }
125              
126             sub _include_plugin {
127 0     0     my $self = shift;
128 0           my $name = shift;
129 0           my $package = shift;
130 0           my $payload = shift;
131              
132 0           Class::MOP::load_class( $package );
133              
134 0           my @arguments;
135 0 0         if ( ref $payload eq 'HASH' ) {
    0          
    0          
136 0           push @arguments, payload => $payload;
137             }
138             elsif ( ref $payload eq 'ARRAY' ) {
139 0           push @arguments, @$payload;
140             }
141             elsif ( defined $payload ) {
142 0           die "Invalid payload ($payload)";
143             }
144              
145 0           $self->zilla->plugins->push( $package->new(
146             plugin_name => $name,
147             zilla => $self->zilla,
148             @arguments,
149             ) );
150             }
151              
152             sub plugin {
153 0     0 0   my $self = shift;
154            
155 0           while( @_ ) {
156 0           my $name_package = shift;
157 0           my ($package, $name) = $name_package =~ m{\A\s*(?:([^/\s]+)\s*/\s*)?(\S+)\z};
158 0 0 0       $package = $name unless defined $package and length $package;
159 0           $package = Dist::Zilla::Util->expand_config_package_name( $package );
160 0           Class::MOP::load_class( $package );
161 0           my $includer = '_include_plugin';
162 0           my $payload = {};
163 0 0 0       $payload = shift if ref $_[0] eq 'HASH' || ref $_[0] eq 'ARRAY';
164 0           my @arguments = ( $name, $package, $payload );
165 0 0         if ( $package->does( 'Dist::Zilla::Role::PluginBundle' ) ) {
166 0           $includer = '_include_plugin_bundle';
167 0 0         my $filter = shift if ref $_[0] eq 'Regexp';
168 0           push @arguments, $filter;
169             }
170 0           $self->$includer( @arguments );
171             }
172              
173             }
174              
175             sub prune {
176 0     0 0   my $self = shift;
177 0           my $pruner = shift;
178              
179 0           require Dist::Dzpl::Plugin::Prune;
180 0           $self->zilla->plugins->push( Dist::Dzpl::Plugin::Prune->new(
181             plugin_name => 'Dist::Dzpl::Plugin::Prune',
182             payload => {},
183             zilla => $self->zilla,
184             pruner => $pruner,
185             ) );
186             }
187              
188             __PACKAGE__->meta->make_immutable;
189 1     1   8 no Moose;
  1         2  
  1         9  
190             1;
191              
192             __END__
193             =pod
194              
195             =head1 NAME
196              
197             Dist::Dzpl - An alternative configuration format (.pl) and invoker for Dist::Zilla
198              
199             =head1 VERSION
200              
201             version 0.0020
202              
203             =head1 SYNOPSIS
204              
205             Below is an example file that would exist in your distribution root, called C<dist.pl> or C<dzpl>:
206              
207             #!/usr/bin/env perl
208             use Dzpl
209             name => 'Acme-Xyzzy',
210             version => '0.0001',
211             author => 'Ja P. Hacker <japh@example.com>',
212             license => 'Perl5',
213             copyright => 'Ja P. Hacker', # Will automaticaly fill in the current year
214              
215             # Declare prerequisites for runtime and testing (building)
216             # Alternatively, you can specify 'recommend' or 'prefer'
217             require => q/
218             Moose
219              
220             [Test]
221             Test::Most
222             /;
223             ;
224              
225             # Declare some plugins to use. The regular expression following
226             # the @Basic bundle is a filter excluding Dist::Zilla::Plugin::Readme
227             plugin
228             '@Basic' => qr/Readme$/,
229             'PodWeaver',
230             'PkgVersion',
231             'ReadmeFromPod',
232             '=Dist::Dzpl::Plugin::DynamicManifest',
233             '=Dist::Dzpl::Plugin::CopyReadmeFromBuild',
234             ;
235              
236             run;
237              
238             Then, from the commandline:
239              
240             dzpl build # Build the distribution via $zilla->build
241             dzpl dzil help # The usual Dist::Dzil::App help message
242              
243             =head1 DESCRIPTION
244              
245             Dist::Dzpl is a wrapper around Dist::Zilla, allowing an alternative, flexible configuration mechanism. Instead of describing your distribution using an .ini file, you can use a Perl .pl script
246              
247             Your configuation file can be named C<dzpl>, C<dz.pl>, or C<dist.pl>, and will be picked in that order
248              
249             Dist::Dzpl is dz*P*l is to .pl as Dist::Zilla is dz*I*l is to .ini
250              
251             =head1 SEE ALSO
252              
253             L<Dist::Zilla>
254              
255             =head1 AUTHOR
256              
257             Robert Krimen <robertkrimen@gmail.com>
258              
259             =head1 COPYRIGHT AND LICENSE
260              
261             This software is copyright (c) 2010 by Robert Krimen.
262              
263             This is free software; you can redistribute it and/or modify it under
264             the same terms as the Perl 5 programming language system itself.
265              
266             =cut
267