File Coverage

blib/lib/Dist/Zilla/Role/FileFinderUser.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 25 25 100.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Role::FileFinderUser 6.037;
2             # ABSTRACT: something that uses FileFinder plugins
3              
4 18     34   14562 use MooseX::Role::Parameterized 1.01;
  18         142662  
  18         167  
5              
6 18     18   243888 use Dist::Zilla::Pragmas;
  18         70  
  18         498  
7              
8 18     18   594 use namespace::autoclean;
  18         42  
  18         191  
9              
10             #pod =head1 DESCRIPTION
11             #pod
12             #pod This role enables you to search for files in the dist. This makes it easy to find specific
13             #pod files and have the code factored out to common methods.
14             #pod
15             #pod Here's an example of a finder: ( taken from AutoPrereqs )
16             #pod
17             #pod with 'Dist::Zilla::Role::FileFinderUser' => {
18             #pod default_finders => [ ':InstallModules', ':ExecFiles' ],
19             #pod };
20             #pod
21             #pod Then you use it in your code like this:
22             #pod
23             #pod foreach my $file ( @{ $self->found_files }) {
24             #pod # $file is an object! Look at L<Dist::Zilla::Role::File>
25             #pod }
26             #pod
27             #pod =cut
28              
29             #pod =attr finder_arg_names
30             #pod
31             #pod Define the name of the attribute which will hold this finder. Be sure to specify different names
32             #pod if you have multiple finders!
33             #pod
34             #pod This is an ArrayRef.
35             #pod
36             #pod Default: [ qw( finder ) ]
37             #pod
38             #pod =cut
39              
40             parameter finder_arg_names => (
41             isa => 'ArrayRef',
42             default => sub { [ 'finder' ] },
43             );
44              
45             #pod =attr default_finders
46             #pod
47             #pod This attribute is an arrayref of plugin names for the default plugins the
48             #pod consuming plugin will use as finders.
49             #pod
50             #pod Example: C<< [ qw( :InstallModules :ExecFiles ) ] >>
51             #pod
52             #pod The default finders are:
53             #pod
54             #pod =begin :list
55             #pod
56             #pod = :InstallModules
57             #pod
58             #pod Searches your lib/ directory for pm/pod files
59             #pod
60             #pod = :IncModules
61             #pod
62             #pod Searches your inc/ directory for pm files
63             #pod
64             #pod = :MainModule
65             #pod
66             #pod Finds the C<main_module> of your dist
67             #pod
68             #pod = :TestFiles
69             #pod
70             #pod Searches your t/ directory and lists the files in it.
71             #pod
72             #pod = :ExtraTestFiles
73             #pod
74             #pod Searches your xt/ directory and lists the files in it.
75             #pod
76             #pod = :ExecFiles
77             #pod
78             #pod Searches your distribution for executable files. Hint: Use the
79             #pod L<Dist::Zilla::Plugin::ExecDir> plugin to mark those files as executables.
80             #pod
81             #pod = :PerlExecFiles
82             #pod
83             #pod A subset of C<:ExecFiles> limited just to perl scripts (those ending with
84             #pod F<.pl>, or with a recognizable perl shebang).
85             #pod
86             #pod = :ShareFiles
87             #pod
88             #pod Searches your ShareDir directory and lists the files in it.
89             #pod Hint: Use the L<Dist::Zilla::Plugin::ShareDir> plugin to set up the sharedir.
90             #pod
91             #pod = :AllFiles
92             #pod
93             #pod Returns all files in the distribution.
94             #pod
95             #pod = :NoFiles
96             #pod
97             #pod Returns nothing.
98             #pod
99             #pod =end :list
100             #pod
101             #pod =cut
102              
103             parameter default_finders => (
104             isa => 'ArrayRef',
105             required => 1,
106             );
107              
108             #pod =attr method
109             #pod
110             #pod This will be the name of the subroutine installed in your package for this
111             #pod finder. Be sure to specify different names if you have multiple finders!
112             #pod
113             #pod Default: found_files
114             #pod
115             #pod =cut
116              
117             parameter method => (
118             isa => 'Str',
119             default => 'found_files',
120             );
121              
122             role {
123             my ($p) = @_;
124              
125             my ($finder_arg, @finder_arg_aliases) = @{ $p->finder_arg_names };
126             confess "no finder arg names given!" unless $finder_arg;
127              
128             around mvp_multivalue_args => sub {
129             my ($orig, $self) = @_;
130              
131             my @start = $self->$orig;
132             return (@start, $finder_arg);
133             };
134              
135             if (@finder_arg_aliases) {
136             around mvp_aliases => sub {
137             my ($orig, $self) = @_;
138              
139             my $start = $self->$orig;
140              
141             for my $alias (@finder_arg_aliases) {
142             confess "$alias is already an alias to $start->{$alias}"
143             if exists $start->{$alias} and $orig->{$alias} ne $finder_arg;
144             $start->{ $alias } = $finder_arg;
145             }
146              
147             return $start;
148             };
149             }
150              
151             has $finder_arg => (
152             is => 'ro',
153             isa => 'ArrayRef[Str]',
154             default => sub { [ @{ $p->default_finders } ] },
155             );
156              
157             method $p->method => sub {
158 116     116   317 my ($self) = @_;
        116      
        151      
        116      
159              
160 119         4885 my @filesets = map {; $self->zilla->find_files($_) }
161 116         299 @{ $self->$finder_arg };
  116         6251  
162              
163 116         363 my %by_name = map {; $_->name, $_ } map { @$_ } @filesets;
  117         351  
  119         350  
164              
165 116         599 return [ map {; $by_name{$_} } sort keys %by_name ];
  117         572  
166             };
167             };
168              
169             1;
170              
171             __END__
172              
173             =pod
174              
175             =encoding UTF-8
176              
177             =head1 NAME
178              
179             Dist::Zilla::Role::FileFinderUser - something that uses FileFinder plugins
180              
181             =head1 VERSION
182              
183             version 6.037
184              
185             =head1 DESCRIPTION
186              
187             This role enables you to search for files in the dist. This makes it easy to find specific
188             files and have the code factored out to common methods.
189              
190             Here's an example of a finder: ( taken from AutoPrereqs )
191              
192             with 'Dist::Zilla::Role::FileFinderUser' => {
193             default_finders => [ ':InstallModules', ':ExecFiles' ],
194             };
195              
196             Then you use it in your code like this:
197              
198             foreach my $file ( @{ $self->found_files }) {
199             # $file is an object! Look at L<Dist::Zilla::Role::File>
200             }
201              
202             =head1 PERL VERSION
203              
204             This module should work on any version of perl still receiving updates from
205             the Perl 5 Porters. This means it should work on any version of perl
206             released in the last two to three years. (That is, if the most recently
207             released version is v5.40, then this module should work on both v5.40 and
208             v5.38.)
209              
210             Although it may work on older versions of perl, no guarantee is made that the
211             minimum required version will not be increased. The version may be increased
212             for any reason, and there is no promise that patches will be accepted to
213             lower the minimum required perl.
214              
215             =head1 ATTRIBUTES
216              
217             =head2 finder_arg_names
218              
219             Define the name of the attribute which will hold this finder. Be sure to specify different names
220             if you have multiple finders!
221              
222             This is an ArrayRef.
223              
224             Default: [ qw( finder ) ]
225              
226             =head2 default_finders
227              
228             This attribute is an arrayref of plugin names for the default plugins the
229             consuming plugin will use as finders.
230              
231             Example: C<< [ qw( :InstallModules :ExecFiles ) ] >>
232              
233             The default finders are:
234              
235             =over 4
236              
237             =item :InstallModules
238              
239             Searches your lib/ directory for pm/pod files
240              
241             =item :IncModules
242              
243             Searches your inc/ directory for pm files
244              
245             =item :MainModule
246              
247             Finds the C<main_module> of your dist
248              
249             =item :TestFiles
250              
251             Searches your t/ directory and lists the files in it.
252              
253             =item :ExtraTestFiles
254              
255             Searches your xt/ directory and lists the files in it.
256              
257             =item :ExecFiles
258              
259             Searches your distribution for executable files. Hint: Use the
260             L<Dist::Zilla::Plugin::ExecDir> plugin to mark those files as executables.
261              
262             =item :PerlExecFiles
263              
264             A subset of C<:ExecFiles> limited just to perl scripts (those ending with
265             F<.pl>, or with a recognizable perl shebang).
266              
267             =item :ShareFiles
268              
269             Searches your ShareDir directory and lists the files in it.
270             Hint: Use the L<Dist::Zilla::Plugin::ShareDir> plugin to set up the sharedir.
271              
272             =item :AllFiles
273              
274             Returns all files in the distribution.
275              
276             =item :NoFiles
277              
278             Returns nothing.
279              
280             =back
281              
282             =head2 method
283              
284             This will be the name of the subroutine installed in your package for this
285             finder. Be sure to specify different names if you have multiple finders!
286              
287             Default: found_files
288              
289             =head1 AUTHOR
290              
291             Ricardo SIGNES 😏 <cpan@semiotic.systems>
292              
293             =head1 COPYRIGHT AND LICENSE
294              
295             This software is copyright (c) 2026 by Ricardo SIGNES.
296              
297             This is free software; you can redistribute it and/or modify it under
298             the same terms as the Perl 5 programming language system itself.
299              
300             =cut