File Coverage

lib/Dist/Zilla/Util/CurrentCmd.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 2     2   15662 use 5.006;
  2         5  
  2         69  
2 2     2   8 use strict;
  2         3  
  2         61  
3 2     2   15 use warnings;
  2         3  
  2         123  
4              
5             package Dist::Zilla::Util::CurrentCmd;
6              
7             our $VERSION = '0.002002';
8              
9             # ABSTRACT: Attempt to determine the current command Dist::Zilla is running under.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 2     2   1040 use Moose;
  0            
  0            
14              
15             use Sub::Exporter '-setup' => { exports => [qw( current_cmd is_build is_install as_cmd )], };
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30             our $_FORCE_CMD;
31              
32             sub current_cmd {
33             my $i = 0;
34             if ($_FORCE_CMD) {
35             return $_FORCE_CMD;
36             }
37             while ( my @frame = caller $i ) {
38             $i++;
39             next unless ( my ( $command, ) = $frame[3] =~ /\ADist::Zilla::App::Command::(.*)::([^:\s]+)\z/msx );
40             return $command;
41             }
42             return;
43             }
44              
45              
46              
47              
48              
49              
50              
51             sub is_build {
52             my $cmd = current_cmd();
53             return ( defined $cmd and 'build' eq $cmd );
54             }
55              
56              
57              
58              
59              
60              
61              
62             sub is_install {
63             my $cmd = current_cmd();
64             return ( defined $cmd and 'install' eq $cmd );
65             }
66              
67              
68              
69              
70              
71              
72              
73              
74              
75              
76              
77              
78              
79             sub as_cmd {
80             my ( $cmd, $callback ) = @_;
81             ## no critic ( Variables::ProhibitLocalVars )
82             local $_FORCE_CMD = $cmd;
83             return $callback->();
84             }
85              
86             __PACKAGE__->meta->make_immutable;
87             no Moose;
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Dist::Zilla::Util::CurrentCmd - Attempt to determine the current command Dist::Zilla is running under.
100              
101             =head1 VERSION
102              
103             version 0.002002
104              
105             =head1 SYNOPSIS
106              
107             use Dist::Zilla::Util::CurrentCmd qw(current_cmd);
108              
109             ...
110              
111             if ( is_install() ) {
112             die "This plugin hates installing things for some reason!"
113             }
114             if ( is_build() ) {
115             print "I Love you man\n";
116             }
117             if ( current_cmd() eq 'run' ) {
118             die "RUN THE OTHER WAY"
119             }
120              
121             =head1 DESCRIPTION
122              
123             This module exists in case you are absolutely certain you want to have different behaviors for either a plugin, or a bundle, to
124             trigger on ( or off ) a specific phase.
125              
126             Usually, this is a bad idea, and the need to do this suggests a poor choice of work-flow to begin with.
127              
128             That said, this utility is I<probably> more useful in a bundle than in a plugin, in that it will be slightly more optimal than
129             say, having an C<ENV> flag to control this difference.
130              
131             =head1 FUNCTIONS
132              
133             =head2 C<current_cmd>
134              
135             Returns the name of the of the B<first> C<command> entry in the C<caller> stack that matches
136              
137             /\ADist::Zilla::App::Command::(.*)::([^:\s]+)\z/msx
138              
139             For instance:
140              
141             Dist::Zilla::App::Command::build::execute ->
142             build
143              
144             =head2 C<is_build>
145              
146             Convenience shorthand for C<current_cmd() eq 'build'>
147              
148             =head2 C<is_install>
149              
150             Convenience shorthand for C<current_cmd() eq 'install'>
151              
152             =head2 C<as_cmd>
153              
154             Internals wrapper to lie to code operating in the callback that the C<current_cmd> is.
155              
156             as_cmd('install' => sub {
157              
158             is_install(); # true
159              
160             });
161              
162             =head1 CAVEATS
163              
164             User beware, this code is both hackish and new, and relies on using C<caller> to determine which
165             C<Dist::Zilla::App::Command::> we are running under.
166              
167             There may be conditions that there are no C<Command>s in the C<caller> stack which meet this definition, or the I<first> such
168             thing may be a misleading representation of what is actually running.
169              
170             And there's a degree of uncertainty of reliability, because I haven't yet devised reliable ways of testing it that don't
171             involve invoking C<dzil> ( which is problematic on testers where C<Dist::Zilla> is in C<@INC> but C<dzil> is not in
172             C<ENV{PATH}> )
173              
174             To that extent, I don't even know for sure if this module works yet, or if it works in a bundle, or if it works in all
175             commands, or if it works under C<Dist::Zilla::App::Tester> as expected.
176              
177             =head1 AUTHOR
178              
179             Kent Fredric <kentnl@cpan.org>
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2014 by Kent Fredric <kentfredric@gmail.com>.
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut