File Coverage

blib/lib/Dist/Zilla/App/Command/lsplugins.pm
Criterion Covered Total %
statement 11 101 10.8
branch 0 40 0.0
condition 0 5 0.0
subroutine 4 20 20.0
pod 2 2 100.0
total 17 168 10.1


line stmt bran cond sub pod time code
1 1     1   431 use 5.006;
  1         2  
2 1     1   3 use strict;
  1         1  
  1         24  
3 1     1   15 use warnings;
  1         1  
  1         112  
4              
5             package Dist::Zilla::App::Command::lsplugins;
6              
7             our $VERSION = '0.003001';
8              
9             # ABSTRACT: Show all dzil plugins on your system, with descriptions
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 1     1   503 use Dist::Zilla::App '-command';
  1         51985  
  1         17  
14              
15             sub _inc_scanner { ## no critic (RequireArgUnpacking)
16 0 0   0     return $_[0]->{_inc_scanner} if exists $_[0]->{_inc_scanner};
17 0           return ( $_[0]->{_inc_scanner} = $_[0]->_build__inc_scanner );
18             }
19              
20             sub _plugin_dirs { ## no critic (RequireArgUnpacking)
21 0 0   0     return $_[0]->{_plugin_dirs} if exists $_[0]->{_plugin_dirs};
22 0           return ( $_[0]->{_plugin_dirs} = $_[0]->_build__plugin_dirs );
23             }
24              
25             sub _build__inc_scanner {
26 0     0     require Path::ScanINC;
27 0           return Path::ScanINC->new();
28             }
29              
30             sub _build__plugin_dirs {
31 0     0     my ($self) = @_;
32 0           return [ $self->_inc_scanner->all_dirs( 'Dist', 'Zilla', 'Plugin' ) ];
33             }
34              
35             sub _plugin_dir_iterator {
36 0     0     my ($self) = @_;
37 0           my @dirs = @{ $self->_plugin_dirs };
  0            
38             return sub {
39 0 0   0     return unless @dirs;
40 0           return shift @dirs;
41 0           };
42             }
43              
44             sub _plugin_all_files_iterator {
45 0     0     my ($self) = @_;
46 0           my $dir_iterator = $self->_plugin_dir_iterator;
47 0           my $dir;
48             my $file_iterator;
49 0           my $code;
50             $code = sub {
51 0 0   0     if ( not defined $dir ) {
52 0 0         if ( not defined( $dir = $dir_iterator->() ) ) {
53 0           return;
54             }
55 0           require Path::Tiny;
56 0           $file_iterator = Path::Tiny->new($dir)->iterator(
57             {
58             recurse => 1,
59             follow_symlinks => 0,
60             },
61             );
62             }
63 0           my $file = $file_iterator->();
64 0 0 0       if ( not defined $file and defined $dir ) {
65 0           $dir = undef;
66 0           goto $code;
67             }
68 0           return [ $dir, $file ];
69 0           };
70 0           return $code;
71             }
72              
73             sub _plugin_iterator {
74 0     0     my ($self) = @_;
75              
76 0           my $file_iterator = $self->_plugin_all_files_iterator;
77              
78             my $is_plugin = sub {
79 0     0     my ($file) = @_;
80 0 0         return unless $file =~ /[.]pm\z/msx;
81 0 0         return if -d $file;
82 0           return 1;
83 0           };
84              
85 0           my $code;
86             my $end;
87             $code = sub {
88 0 0   0     return if $end;
89 0           my $file = $file_iterator->();
90 0 0         if ( not defined $file ) {
91 0           $end = 1;
92 0           return;
93             }
94 0 0         if ( $is_plugin->( $file->[1] ) ) {
95 0           require Dist::Zilla::lsplugins::Module;
96 0           return Dist::Zilla::lsplugins::Module->new(
97             file => $file->[1],
98             plugin_root => $file->[0],
99             plugin_basename => 'Dist::Zilla::Plugin',
100             );
101             }
102 0           goto $code;
103 0           };
104 0           return $code;
105             }
106              
107              
108              
109              
110              
111              
112              
113              
114              
115              
116              
117              
118              
119              
120              
121              
122              
123              
124              
125              
126              
127              
128              
129              
130              
131              
132              
133              
134              
135              
136              
137              
138              
139              
140              
141              
142              
143              
144              
145              
146              
147              
148              
149              
150              
151              
152              
153              
154              
155              
156              
157              
158              
159             sub opt_spec {
160             return (
161 0     0 1   [ q[sort!], q[Sort by module name] ],
162             [ q[versions!], q[Show versions] ],
163             [ q[abstract!], q[Show Abstracts] ],
164             [ q[roles=s], q[Show applied roles] ],
165             [ q[with=s], q[Filter plugins to ones that 'do' the specified role] ]
166             );
167             }
168              
169             sub _filter_dzil {
170 0     0     my ($value) = @_;
171 0           return ( $value =~ /(\A|[|])Dist::Zilla::Role::/msx );
172             }
173              
174             sub _shorten_dzil {
175 0     0     my ($value) = @_;
176 0           $value =~ s/(\A|[|])Dist::Zilla::Role::/$1-/msxg;
177 0           return $value;
178             }
179              
180             sub _process_plugin {
181 0     0     my ( undef, $plugin, $opt, undef ) = @_;
182 0 0         if ( defined $opt->with ) {
183 0 0         return unless $plugin->loaded_module_does( $opt->with );
184             }
185 0           printf q[%s], $plugin->plugin_name;
186 0 0         if ( $opt->versions ) {
187 0           printf q[ (%s)], $plugin->version;
188             }
189 0 0         if ( $opt->abstract ) {
190 0   0       printf q[ - %s], $plugin->abstract || ' NO ABSTRACT DEFINED';
191             }
192 0 0         if ( defined $opt->roles ) {
193 0 0         if ( 'all' eq $opt->roles ) {
    0          
    0          
194 0           printf q{ [%s]}, join q[, ], @{ $plugin->roles };
  0            
195             }
196             elsif ( 'dzil-full' eq $opt->roles ) {
197 0           printf q{ [%s]}, join q[, ], grep { _filter_dzil($_) } @{ $plugin->roles };
  0            
  0            
198             }
199             elsif ( 'dzil' eq $opt->roles ) {
200 0           printf q{ [%s]}, join q[, ], map { _shorten_dzil($_) } grep { _filter_dzil($_) } @{ $plugin->roles };
  0            
  0            
  0            
201             }
202             }
203 0           printf "\n";
204 0           return;
205             }
206              
207              
208              
209              
210              
211              
212              
213              
214              
215             sub execute {
216 0     0 1   my ( $self, $opt, $args ) = @_;
217              
218 0 0         if ( !$opt->sort ) {
219 0           my $plugin_iterator = $self->_plugin_iterator;
220              
221 0           while ( my $plugin = $plugin_iterator->() ) {
222 0           $self->_process_plugin( $plugin, $opt, $args );
223             }
224 0           return 0;
225             }
226              
227 0           my $plugin_iterator = $self->_plugin_iterator;
228 0           my @plugins;
229 0           while ( my $plugin = $plugin_iterator->() ) {
230 0           push @plugins, $plugin;
231             }
232 0           for my $plugin ( sort { $a->plugin_name cmp $b->plugin_name } @plugins ) {
  0            
233 0           $self->_process_plugin( $plugin, $opt, $args );
234             }
235 0           return 0;
236              
237             }
238              
239             1;
240              
241             __END__
242              
243             =pod
244              
245             =encoding UTF-8
246              
247             =head1 NAME
248              
249             Dist::Zilla::App::Command::lsplugins - Show all dzil plugins on your system, with descriptions
250              
251             =head1 VERSION
252              
253             version 0.003001
254              
255             =head1 SYNOPSIS
256              
257             dzil lsplugins # see a list of all plugins on your system
258             dzil lsplugins --version # with versions!
259             dzil lsplugins --sort # sort them!
260             dzil lsplugins --abstract # show their ABSTRACTs!
261             dzil lsplugins --with=-FilePruner # show only file pruners
262             dzil lsplugins --roles=dzil # show all the dzil related role data!
263              
264             =head1 METHODS
265              
266             =head2 C<opt_spec>
267              
268             Supported parameters:
269              
270             =over 4
271              
272             =item * C<--sort>
273              
274             Sorting.
275              
276             =item * C<--no-sort>
277              
278             No Sorting ( B<Default> )
279              
280             =item * C<--versions>
281              
282             Versions
283              
284             =item * C<--no-versions>
285              
286             No Versions ( B<Default> )
287              
288             =item * C<--abstract>
289              
290             Show abstracts
291              
292             =item * C<--no-abstract>
293              
294             Don't show abstracts ( B<Default> )
295              
296             =item * C<--roles=all>
297              
298             Show all roles, un-abbreviated.
299              
300             =item * C<--roles=dzil-full>
301              
302             Show only C<dzil> roles, un-abbreviated.
303              
304             =item * C<--roles=dzil>
305              
306             Show only C<dzil> roles, abbreviated.
307              
308             =item * C<--with=$ROLENAME>
309              
310             Show only plugins that C<< does($rolename) >>
311              
312             ( A - prefix will be expanded to C<Dist::Zilla::Role::> for convenience )
313              
314             =back
315              
316             =for Pod::Coverage execute
317              
318             =head1 AUTHOR
319              
320             Kent Fredric <kentnl@cpan.org>
321              
322             =head1 COPYRIGHT AND LICENSE
323              
324             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
325              
326             This is free software; you can redistribute it and/or modify it under
327             the same terms as the Perl 5 programming language system itself.
328              
329             =cut