File Coverage

blib/lib/Dist/Zilla/UtilRole/MaybeZilla.pm
Criterion Covered Total %
statement 30 30 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 12 12 100.0
pod n/a
total 46 47 97.8


line stmt bran cond sub pod time code
1 2     2   1479 use 5.006; # our
  2         4  
2 2     2   9 use strict;
  2         2  
  2         42  
3 2     2   15 use warnings;
  2         3  
  2         163  
4              
5             package Dist::Zilla::UtilRole::MaybeZilla;
6              
7             our $VERSION = '0.002000';
8              
9             # ABSTRACT: Soft-dependency on Dist::Zilla for Utilities.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13              
14              
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56 2     2   763 use Moose::Role qw( has );
  2         296621  
  2         7  
57 2     2   5642 use Scalar::Util qw(blessed);
  2         3  
  2         101  
58 2     2   606 use namespace::autoclean;
  2         6049  
  2         11  
59 2     2   491 use Log::Contextual::LogDispatchouli qw(log_fatal);
  2         53286  
  2         24  
60              
61              
62              
63              
64              
65              
66              
67              
68              
69              
70              
71             has zilla => ( isa => Object =>, is => ro =>, predicate => has_zilla =>, lazy_build => 1 );
72             has plugin => ( isa => Object =>, is => ro =>, predicate => has_plugin =>, lazy_build => 1 );
73              
74             sub _build_zilla {
75 2     2   3 my ($self) = @_;
76 2 100 66     62 if ( $self->has_plugin and $self->plugin->can('zilla') ) {
77 1         27 return $self->plugin->zilla;
78             }
79             return log_fatal {
80 1     1   156 'Neither `zilla` or `plugin` were specified,' . 'and one must be specified to ->new() for this method';
81 1         8 };
82             }
83              
84             sub _build_plugin {
85             return log_fatal {
86 2     2   168 '`plugin` needs to be specificed to ->new() for this method to work';
87 2     2   12 };
88             }
89              
90              
91              
92              
93              
94              
95              
96              
97              
98              
99              
100              
101              
102              
103              
104              
105              
106              
107              
108              
109              
110 2     2   27473 no Moose::Role;
  2         2  
  2         12  
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Dist::Zilla::UtilRole::MaybeZilla - Soft-dependency on Dist::Zilla for Utilities.
123              
124             =head1 VERSION
125              
126             version 0.002000
127              
128             =head1 DESCRIPTION
129              
130             C<Dist::Zilla> Is Great. Long Live C<Dist::Zilla> But when you're writing a utility class,
131             loading C<Dist::Zilla> may be not necessary, and can make testing things harder.
132              
133             Namely, because to test anything that B<requires> C<Dist::Zilla>, B<requires> that you
134             have a valid build tree, which may be lots of unnecessary work if you only need C<dzil> for
135             simple things like error logging.
136              
137             Or perhaps, you have other resources that you only conventionally fetch from C<dzil>,
138             such as the C<dzil build-root>, for the sake of making a C<Git::Wrapper>, but you're quite
139             happy with passing C<Git::Wrapper> instances directly for testing.
140              
141             And I found myself doing quite a lot of the latter, and re-writing the same code everywhere to do it.
142              
143             So, this role provides a C<zilla> attribute that is B<ONLY> required if something directly calls C<< $self->zilla >>, and it fails on invocation.
144              
145             And provides a few utility methods, that will try to use C<zilla> where possible, but fallback to
146             a somewhat useful default if those are not available to you.
147              
148             package MyPlugin;
149             use Moose;
150             with 'Dist::Zilla::UtilRole::MaybeZilla';
151              
152             ...
153              
154             sub foo {
155             if ( $self->has_zilla ) {
156             $self->zilla->whatever
157             } else {
158             $slightlymessyoption
159             }
160             }
161              
162             Additionally, it provides a few compatibility methods to make life easier, namely
163              
164             log_debug, log, log_fatal
165              
166             Which will invoke the right places in C<dzil> if possible, but revert to a sensible
167             default if not.
168              
169             =head1 ATTRIBUTES
170              
171             =head2 C<zilla>
172              
173             A lazy attribute, populated from C<plugin> where possible, C<die>ing if not.
174              
175             =head2 C<plugin>
176              
177             A lazy attribute that C<die>s if required and not specified.
178              
179             =head1 FOOTNOTES
180              
181             I had intended to have logging methods on this, but they proved too messy and problematic.
182              
183             More, I discovered the way Dist::Zilla handles logs is kinda problematic too, because you may have noticed,
184              
185             $self->log_fatal()
186              
187             May just have a predisposition from reporting the failure context being
188              
189             Moose/Method/Deferred.pm
190              
191             Most cases. ( ☹ )
192              
193             So I'm experimentally toying with using more L<< C<Log::Contextual>|Log::Contextual >>.
194              
195             See L<< C<[LogContextual]>|Dist::Zilla::Plugin::LogContextual >>
196              
197             =head1 AUTHOR
198              
199             Kent Fredric <kentnl@cpan.org>
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
204              
205             This is free software; you can redistribute it and/or modify it under
206             the same terms as the Perl 5 programming language system itself.
207              
208             =cut