blib/lib/Dist/Zilla/Plugin/GatherDir.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 95.0% 19 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 2 totaltotal 97.0% 132 / 136 CC 14 SCAR 14.6
CC
14
Cov
98%
gather_files
11.0
_file_from_filename
2.0
__ANON__
2.0
CRAP 4.3
« blib/lib/Dist/Zilla/Plugin/ManifestSkip.pm ↑ Summary blib/lib/Dist/Zilla/App/Command/build.pm »
1 package Dist::Zilla::Plugin::GatherDir 6.040;
2 # ABSTRACT: gather all the files in a directory
3  
4 44 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 44 with 'Dist::Zilla::Role::FileGatherer';
6  
7 44 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 44 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 44 ▶use Dist::Zilla::Types qw(Path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 44 ▶use Dist::Zilla::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #pod =head1 DESCRIPTION
15 #pod
16 #pod This is a very, very simple L<FileGatherer|Dist::Zilla::Role::FileGatherer>
17 #pod plugin. It looks in the directory named in the L</root> attribute and adds all
18 #pod the files it finds there. If the root begins with a tilde, the tilde is
19 #pod passed through C<glob()> first.
20 #pod
21 #pod Almost every dist will be built with one GatherDir plugin, since it's the
22 #pod easiest way to get files from disk into your dist. Most users just need:
23 #pod
24 #pod [GatherDir]
25 #pod [PruneCruft]
26 #pod
27 #pod ...and this will pick up all the files from the current directory into the
28 #pod dist. (L<PruneCruft|Dist::Zilla::Plugin::PruneCruft> is needed, here, to drop
29 #pod files that might present as build artifacts, but should not be shipped.) You
30 #pod can use it multiple times, as you can any other plugin, by providing a plugin
31 #pod name. For example, if you want to include external specification files into a
32 #pod subdir of your dist, you might write:
33 #pod
34 #pod [GatherDir]
35 #pod ; this plugin needs no config and gathers most of your files
36 #pod
37 #pod [GatherDir / SpecFiles]
38 #pod ; this plugin gets all the files in the root dir and adds them under ./spec
39 #pod root = ~/projects/my-project/spec
40 #pod prefix = spec
41 #pod
42 #pod =cut
43  
44 44 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 44 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 44 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 44 ▶use List::Util 1.33 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 #pod =attr root
50 #pod
51 #pod This is the directory in which to look for files. If not given, it defaults to
52 #pod the dist root -- generally, the place where your F<dist.ini> or other
53 #pod configuration file is located. It may begin with C<~> (or C<~user>)
54 #pod to mean your (or some other user's) home directory. If a relative path,
55 #pod it is relative to the dist root.
56 #pod
57 #pod =cut
58  
59 has root => (
60 is => 'ro',
61 isa => Path,
62 lazy => 1,
63 coerce => 1,
64 required => 1,
65 158 ▶ default => sub { shift->zilla->root },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 44 );
67  
68 #pod =attr prefix
69 #pod
70 #pod This parameter can be set to place the gathered files under a particular
71 #pod directory. See the L<description|DESCRIPTION> above for an example.
72 #pod
73 #pod =cut
74  
75 44 has prefix => (
76 is => 'ro',
77 isa => 'Str',
78 default => '',
79 );
80  
81 #pod =attr include_dotfiles
82 #pod
83 #pod By default, files will not be included if they begin with a dot. This goes
84 #pod both for files and for directories relative to the C<root>.
85 #pod
86 #pod In almost all cases, the default value (false) is correct.
87 #pod
88 #pod =cut
89  
90 44 has include_dotfiles => (
91 is => 'ro',
92 isa => 'Bool',
93 default => 0,
94 );
95  
96 #pod =attr follow_symlinks
97 #pod
98 #pod By default, symlinks pointing to directories will not be followed; set
99 #pod C<< follow_symlinks = 1 >> to traverse these links as if they were normal
100 #pod directories.
101 #pod
102 #pod In all followed directories, files which are symlinks are B<always> gathered,
103 #pod with the link turning into a normal file.
104 #pod
105 #pod =cut
106  
107 44 has follow_symlinks => (
108 is => 'ro',
109 isa => 'Bool',
110 default => 0,
111 );
112  
113 183 ▶sub mvp_multivalue_args { qw(exclude_filename exclude_match prune_directory) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
114  
115 #pod =attr exclude_filename
116 #pod
117 #pod To exclude certain files from being gathered, use the C<exclude_filename>
118 #pod option. The filename is matched exactly, relative to C<root>.
119 #pod This may be used multiple times to specify multiple files to exclude.
120 #pod
121 #pod =cut
122  
123 has exclude_filename => (
124 is => 'ro',
125 isa => 'ArrayRef',
126 182 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
127 44 );
128  
129 #pod =attr exclude_match
130 #pod
131 #pod This is just like C<exclude_filename> but provides a regular expression
132 #pod pattern. Filenames matching the pattern (relative to C<root>) are not
133 #pod gathered. This may be used
134 #pod multiple times to specify multiple patterns to exclude.
135 #pod
136 #pod =cut
137  
138 has exclude_match => (
139 is => 'ro',
140 isa => 'ArrayRef',
141 182 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
142 44 );
143  
144 #pod =attr prune_directory
145 #pod
146 #pod While traversing, any directory matching the regular expression pattern will
147 #pod not be traversed further. This may be used multiple times to specify multiple
148 #pod directories to skip.
149 #pod
150 #pod =cut
151  
152 has prune_directory => (
153 is => 'ro',
154 isa => 'ArrayRef',
155 182 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 44 );
157  
158 9 ▶around dump_config => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
159 9 my $orig = shift;
160 9 my $self = shift;
161  
162 9 my $config = $self->$orig;
163  
164 $config->{+__PACKAGE__} = {
165 prefix => $self->prefix,
166 # only report relative to dist root to avoid leaking private info
167 root => path($self->root)->relative($self->zilla->root),
168 18 ▶ (map { $_ => $self->$_ ? 1 : 0 } qw(include_dotfiles follow_symlinks)),
Branch: 1 branch
BranchTrueFalseTotal
$self->$_ ? : 2 16 18
169 9 (map { $_ => [ sort @{ $self->$_ } ] } qw(exclude_filename exclude_match prune_directory)),
170 };
171  
172 9 return $config;
173 44 };
174  
175 174 ▶sub gather_files {
Subroutine gather_files: called CC=11 SCAR=24.0
Pod undocumented
176 174 my ($self) = @_;
177  
178 174 my $exclude_regex = qr/\000/;
179 $exclude_regex = qr/(?:$exclude_regex)|$_/
180 174 for @{ $self->exclude_match };
181  
182 174 my $repo_root = $self->zilla->root;
183 174 my $root = "" . $self->root;
184 0 $root =~ s{^~([\\/])}{ Dist::Zilla::Util->homedir . $1 }e;
185 174 ▶ $root = path($root)->absolute($repo_root)->stringify if path($root)->is_relative;
Branch: 1 branch
BranchTrueFalseTotal
if path($root)->is_relative 35 139 174
186  
187 174 my $prune_regex = qr/\000/;
188 $prune_regex = qr/$prune_regex|$_/
189 174 ▶ for ( @{ $self->prune_directory },
Branch: 1 branch
BranchTrueFalseTotal
$self->include_dotfiles ? : 1 173 174
190 $self->include_dotfiles ? () : ( qr/^\.[^.]/ ) );
191  
192 # build up the rules
193 174 my $rule = File::Find::Rule->new();
194 174 $rule->extras({ follow => $self->follow_symlinks });
195  
196 80 ▶ $rule->exec(sub { $self->log_debug('considering ' . path($_[-1])->relative($repo_root)); 1 })
Subroutine __ANON__: called CC=1 SCAR=0.0
197 174 ▶ if $self->zilla->logger->get_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->zilla->logger->get_debug 12 162 174
198  
199 $rule->or(
200 174 ▶ $rule->new->directory->exec(sub { /$prune_regex/ })->prune->discard,
Subroutine __ANON__: called CC=1 SCAR=0.0
201 $rule->new,
202 );
203  
204 174 ▶ if ($self->follow_symlinks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->follow_symlinks) { } 1 173 174
205 1 $rule->or(
206 $rule->new->file, # symlinks to files still count as files
207 $rule->new->symlink, # traverse into the linked dir, but screen it out later
208 );
209 } else {
210 173 $rule->file;
211 }
212  
213 174 ▶ $rule->not_exec(sub { /^\.[^.]/ }) unless $self->include_dotfiles; # exec passes basename as $_
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->include_dotfiles 173 1 174
214 673 ▶ $rule->exec(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
215 673 my $relative = path($_[-1])->relative($root);
216 $relative !~ $exclude_regex &&
217 673 ▶ all { $relative ne $_ } @{ $self->exclude_filename }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
not $relative =~ /$exclude_regex/u and &all(sub { $relative ne $_; } , @{$self->exclude_filename;}) 671 2 673
218 174 });
219  
220 174 FILE: for my $filename ($rule->in($root)) {
221 669 ▶ next if -d $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filename 1 668 669
222  
223 # _file_from_filename is overloaded in GatherDir::Template
224 668 my $fileobj = $self->_file_from_filename($filename);
225  
226 # GatherDir::Template may rename the file
227 668 $filename = $fileobj->name;
228 668 my $file = path($filename)->relative($root);
229 668 ▶ $file = path($self->prefix, $file) if $self->prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->prefix 39 629 668
230  
231 668 $fileobj->name($file->stringify);
232 668 $self->add_file($fileobj);
233 }
234  
235 174 return;
236 }
237  
238 668 ▶sub _file_from_filename {
Subroutine _file_from_filename: called CC=2 SCAR=7.1
239 668 my ($self, $filename) = @_;
240  
241 668 ▶ my @stat = stat $filename or $self->log_fatal("$filename does not exist!");
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $filename or $self->log_fatal("$filename does not exist!") 668 0 668
242  
243 668 return Dist::Zilla::File::OnDisk->new({
244 name => $filename,
245 mode => $stat[2] & 0755, # kill world-writeability
246 });
247 }
248  
249 44 __PACKAGE__->meta->make_immutable;
250 44 1;
251  
252 __END__
« blib/lib/Dist/Zilla/Plugin/ManifestSkip.pm ↑ Summary blib/lib/Dist/Zilla/App/Command/build.pm »