File Coverage

lib/Dist/Zilla/Util/CurrentCmd.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


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