File Coverage

lib/Dist/Zilla/UtilRole/MaybeZilla.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


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