File Coverage

blib/lib/Dist/Zilla/Plugin/Bootstrap/lib.pm
Criterion Covered Total %
statement 27 28 96.4
branch 4 6 66.6
condition n/a
subroutine 6 6 100.0
pod n/a
total 37 40 92.5


line stmt bran cond sub pod time code
1 7     7   4598 use 5.006; # our
  7         17  
2 7     7   28 use strict;
  7         9  
  7         144  
3 7     7   30 use warnings;
  7         7  
  7         594  
4              
5             ## no critic ( NamingConventions::Capitalization )
6             package Dist::Zilla::Plugin::Bootstrap::lib;
7             ## use critic;
8              
9             our $VERSION = '1.001002';
10              
11             # ABSTRACT: A minimal boot-strapping for Dist::Zilla Plug-ins.
12              
13             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
14              
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27 7     7   3111 use Moose qw( with around );
  7         2119338  
  7         62  
28             with 'Dist::Zilla::Role::Bootstrap';
29              
30             around dump_config => sub {
31             my ( $orig, $self, @args ) = @_;
32             my $config = $self->$orig(@args);
33             my $localconf = {};
34              
35             $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION unless __PACKAGE__ eq ref $self;
36             $config->{ +__PACKAGE__ } = $localconf if keys %{$localconf};
37             return $config;
38             };
39              
40             __PACKAGE__->meta->make_immutable;
41 7     7   28995 no Moose;
  7         13  
  7         32  
42              
43             sub bootstrap {
44 6     6   39 my ($self) = @_;
45              
46 6         197 my $bootstrap_root = $self->_bootstrap_root;
47              
48 6 100       4994 if ( not $bootstrap_root ) {
49 1         2 return;
50             }
51              
52 5         23 my $bootstrap_path = $bootstrap_root->child('lib');
53 5         114 $self->_add_inc("$bootstrap_path");
54 5         5566 $self->log( [ 'Bootstrapping %s', "$bootstrap_path" ] );
55 5         2298 my $it = $bootstrap_path->iterator( { recurse => 1 } );
56              
57 5         181 while ( my $file = $it->() ) {
58 5 50       693 next unless $file->basename =~ /[.]pm$/msx;
59 5         150 my $rpath = $file->relative($bootstrap_path)->stringify;
60 5 50       824 if ( exists $INC{$rpath} ) {
61 0         0 $self->log( [ '%s was not bootstrapped. You need to move Bootstrap::lib higher', $rpath ] );
62             }
63             }
64              
65 5         149 return 1;
66              
67             }
68              
69             1;
70              
71             __END__
72              
73             =pod
74              
75             =encoding UTF-8
76              
77             =head1 NAME
78              
79             Dist::Zilla::Plugin::Bootstrap::lib - A minimal boot-strapping for Dist::Zilla Plug-ins.
80              
81             =head1 VERSION
82              
83             version 1.001002
84              
85             =head1 SYNOPSIS
86              
87             [Bootstrap::lib]
88             try_built = 1 ; try using an existing built distribution named Dist-Name-*
89             fallback = 0 ; if try_built can't find a built distribution, or there's more than one, don't bootstrap
90             ; using lib/ instead
91              
92             =head1 DESCRIPTION
93              
94             This module exists for loading either C</lib> or C</Dist-Name-$VERSION/lib> into your C<@INC> early on.
95              
96             This is mostly useful for writing L<< C<Dist::Zilla>|Dist::Zilla >> plug-ins, so that you may build and release
97             a plug-in using itself.
98              
99             =begin MetaPOD::JSON v1.1.0
100              
101             {
102             "namespace":"Dist::Zilla::Plugin::Bootstrap::lib",
103             "interface":"class",
104             "does":"Dist::Zilla::Role::Bootstrap"
105             }
106              
107              
108             =end MetaPOD::JSON
109              
110             =head1 USE CASES
111              
112             =head2 Simple single-phase self-dependency
113              
114             This module really is only useful in the case where you need to use something like
115              
116             dzil -Ilib
117              
118             For I<every> call to L<< C<Dist::Zilla>|Dist::Zilla >>, and this is mainly a convenience.
119              
120             For that
121              
122             [Bootstrap::lib]
123              
124             on its own will do the right thing.
125              
126             =head2 Installed-Only self-dependency
127              
128             The other useful case is when you would normally do
129              
130             dzil build # pass 1 that generates Foo-1.234 with a pre-installed Foo-1.233
131             dzil -IFoo-1.234/lib build # pass 2 that generates Foo-1.234 with Foo-1.234
132              
133             For that
134              
135             [Bootstap::lib]
136             try_built = 1
137             fallback = 0
138              
139             Will do what you want.
140              
141             dzil build # pass1 -> creates Foo-1.234 without bootstrapping
142             dzil build # pass2 -> creates Foo-1.234 boot-strapped from the previous build
143              
144             =head2 2-step self-dependency
145              
146             There's a 3rd useful case which is a hybrid of the 2, where you /can/ build from your own sources without needing a
147             pre-installed version, just you don't want that for release code ( e.g.: $VERSION being C<undef> in code that is run during
148             release is "bad" )
149              
150             [Bootstrap::lib]
151             try_built = 1
152             fallback = 1
153              
154             Then
155              
156             dzil build # pass1 -> creates Foo-1.234 from bootstrapped $root/lib
157             dzil build # pass2 -> creates Foo-1.234 from bootstrapped $root/Foo-1.234
158              
159             =head1 PRECAUTIONS
160              
161             =head2 DO NOT
162              
163             B<DO NOT> use this library from inside a bundle. It will not likely work as expected, and you B<DO NOT> want
164             to bootstrap everything in all cases.
165              
166             =head2 NO VERSION
167              
168             On its own,
169              
170             [Bootstrap::lib]
171              
172             At present, using this module in conjunction with a module with no explicitly defined version in the
173             source will result in the I<executed> instance of that plug-in I<also> having B<NO VERSION>.
174              
175             If this is a problem for you, then its suggested you try either variation of using
176              
177             [Bootstrap::lib]
178             try_built = 1
179             ; no_fallback = 1 #
180              
181             =head2 SUCKS AT GUESSING
182              
183             The core mechanism behind C<try_built> relies on looking in your project directory for a previous build directory of some kind.
184              
185             And there's no way for it to presently pick a "best" version when there are more than one, or magically provide a better
186             solution if there are "zero" versions readily available.
187              
188             This is mostly because there is no way to determine the "current" version we are building for, because the point in the
189             execution cycle is so early, no version plugins are likely to be even instantiated yet, and some version plugins are dependent on
190             incredibly complex precursors ( like git ), so by even trying to garner the version we're currently building, we could be
191             prematurely cutting off a vast majority of modules from even being able to bootstrap.
192              
193             Even as it is, us using C<< zilla->name >> means that if your dist relies on some process to divine its name, the module that
194             does this must:
195              
196             =over 4
197              
198             =item * be loaded and declared prior to C<Bootstrap::lib> in the C<dist.ini>
199              
200             =item * not itself be the module you are presently developing/bootstrapping
201              
202             =back
203              
204             The only way of working around that I can envision is adding parameters to C<Bootstrap::lib> to specify the dist name and version
205             name... but if you're going to do that, you may as well stop using external plugins to discover that, and hard-code those values
206             in C<dist.ini> to start with.
207              
208             =head2 STILL NOT REALLY A PLUGIN
209              
210             Starting at version 0.04000000 ( read: 0.04_0000_00 aka 0.04 + 0 x 4 + 0 x 2 ) this module is a fully fledged class, different
211             only from standard Dist::Zilla Plugins in that it doesn't partake in normal phase order, and only executes during a special
212             custom C<::Bootstrap> phase, which is more or less a different name and implementation of C<BUILD>, in that C<bootstrap> is
213             invoked after C<plugin_from_config> is called ( where C<new> is called ), which occurs somewhere in the middle of
214             C<register_component>
215              
216             This module also appears on the plugin stash, and responds naturally to C<metaconfig> requests.
217              
218             =head1 AUTHOR
219              
220             Kent Fredric <kentnl@cpan.org>
221              
222             =head1 COPYRIGHT AND LICENSE
223              
224             This software is copyright (c) 2017 by Kent Fredric <kentnl@cpan.org>.
225              
226             This is free software; you can redistribute it and/or modify it under
227             the same terms as the Perl 5 programming language system itself.
228              
229             =cut