File Coverage

blib/lib/Dist/Zilla/Plugin/GatherFile.pm
Criterion Covered Total %
statement 46 47 97.8
branch 7 8 87.5
condition n/a
subroutine 12 12 100.0
pod 0 3 0.0
total 65 70 92.8


line stmt bran cond sub pod time code
1             # ABSTRACT: gather individual file(s)
2              
3             use Moose;
4 2     2   1932 use Dist::Zilla::Types qw(Path ArrayRefOfPaths);
  2         6  
  2         14  
5 2     2   13503 with 'Dist::Zilla::Role::FileGatherer';
  2         7  
  2         20  
6              
7             use Dist::Zilla::Pragmas;
8 2     2   5831  
  2         5  
  2         21  
9             use MooseX::Types::Moose 'ArrayRef';
10 2     2   15 use Path::Tiny;
  2         7  
  2         23  
11 2     2   9852 use Dist::Zilla::File::OnDisk;
  2         7  
  2         126  
12 2     2   15 use Dist::Zilla::Util;
  2         19  
  2         56  
13 2     2   12 use namespace::autoclean;
  2         5  
  2         41  
14 2     2   11  
  2         4  
  2         18  
15             #pod =head1 SYNOPSIS
16             #pod
17             #pod [GatherFile]
18             #pod filename = examples/file.txt
19             #pod
20             #pod =head1 DESCRIPTION
21             #pod
22             #pod This is a very, very simple L<FileGatherer|Dist::Zilla::Role::FileGatherer>
23             #pod plugin. It adds all the files referenced by the C<filename> option that are
24             #pod found in the directory named in the L</root> attribute. If the root begins
25             #pod with a tilde, the tilde is passed through C<glob()> first.
26             #pod
27             #pod Since normally every distribution will use a GatherDir plugin, you would only
28             #pod need to use the GatherFile plugin if the file was already being excluded (e.g.
29             #pod from an C<exclude_match> configuration).
30             #pod
31             #pod =cut
32              
33             #pod =attr root
34             #pod
35             #pod This is the directory in which to look for files. If not given, it defaults to
36             #pod the dist root -- generally, the place where your F<dist.ini> or other
37             #pod configuration file is located.
38             #pod
39             #pod =cut
40              
41             has root => (
42             is => 'ro',
43             isa => Path,
44             lazy => 1,
45             coerce => 1,
46             required => 1,
47             default => sub { shift->zilla->root },
48             );
49              
50             #pod =attr prefix
51             #pod
52             #pod This parameter can be set to place the gathered files under a particular
53             #pod directory. See the L<description|DESCRIPTION> above for an example.
54             #pod
55             #pod =cut
56              
57             has prefix => (
58             is => 'ro',
59             isa => 'Str',
60             default => '',
61             );
62              
63             #pod =attr filename
64             #pod
65             #pod The name of the file to gather, relative to the C<root>.
66             #pod Can be used more than once.
67             #pod
68             #pod =cut
69              
70             has filenames => (
71             is => 'ro', isa => ArrayRefOfPaths,
72             lazy => 1,
73             coerce => 1,
74             default => sub { [] },
75             );
76              
77              
78 4     4 0 693 around dump_config => sub {
79 4     4 0 1664 my $orig = shift;
80             my $self = shift;
81              
82             my $config = $self->$orig;
83              
84             $config->{+__PACKAGE__} = {
85             prefix => $self->prefix,
86             # only report relative to dist root to avoid leaking private info
87             root => path($self->root)->relative($self->zilla->root),
88             filenames => [ sort @{ $self->filenames } ],
89             };
90              
91             return $config;
92             };
93              
94             my ($self) = @_;
95              
96             my $repo_root = $self->zilla->root;
97             my $root = "" . $self->root;
98 4     4 0 11 $root =~ s{^~([\\/])}{ Dist::Zilla::Util->homedir . $1 }e;
99             $root = path($root);
100 4         133 $root = $root->absolute($repo_root) if path($root)->is_relative;
101 4         121  
102 4         34 for my $filename (@{ $self->filenames })
  0         0  
103 4         21 {
104 4 100       167 $filename = $root->child($filename);
105             $self->log_fatal("$filename is a directory! Use [GatherDir] instead?") if -d $filename;
106 4         364  
  4         142  
107             my $fileobj = $self->_file_from_filename($filename->stringify);
108 4         17  
109 4 50       217 $filename = $fileobj->name;
110             my $file = path($filename)->relative($root);
111 4         128 $file = path($self->prefix, $file) if $self->prefix;
112              
113 3         14 $fileobj->name($file->stringify);
114 3         16 $self->add_file($fileobj);
115 3 100       1271 }
116              
117 3         59 return;
118 3         25 }
119              
120             # as in GatherDir
121 3         14 my ($self, $filename) = @_;
122              
123             my @stat = stat $filename or $self->log_fatal("$filename does not exist!");
124              
125             return Dist::Zilla::File::OnDisk->new({
126 4     4   50 name => $filename,
127             mode => $stat[2] & 0755, # kill world-writeability
128 4 100       81 });
129             }
130 3         151  
131             __PACKAGE__->meta->make_immutable;
132              
133              
134             =pod
135              
136             =encoding UTF-8
137              
138             =head1 NAME
139              
140             Dist::Zilla::Plugin::GatherFile - gather individual file(s)
141              
142             =head1 VERSION
143              
144             version 6.028
145              
146             =head1 SYNOPSIS
147              
148             [GatherFile]
149             filename = examples/file.txt
150              
151             =head1 DESCRIPTION
152              
153             This is a very, very simple L<FileGatherer|Dist::Zilla::Role::FileGatherer>
154             plugin. It adds all the files referenced by the C<filename> option that are
155             found in the directory named in the L</root> attribute. If the root begins
156             with a tilde, the tilde is passed through C<glob()> first.
157              
158             Since normally every distribution will use a GatherDir plugin, you would only
159             need to use the GatherFile plugin if the file was already being excluded (e.g.
160             from an C<exclude_match> configuration).
161              
162             =head1 PERL VERSION
163              
164             This module should work on any version of perl still receiving updates from
165             the Perl 5 Porters. This means it should work on any version of perl released
166             in the last two to three years. (That is, if the most recently released
167             version is v5.40, then this module should work on both v5.40 and v5.38.)
168              
169             Although it may work on older versions of perl, no guarantee is made that the
170             minimum required version will not be increased. The version may be increased
171             for any reason, and there is no promise that patches will be accepted to lower
172             the minimum required perl.
173              
174             =head1 ATTRIBUTES
175              
176             =head2 root
177              
178             This is the directory in which to look for files. If not given, it defaults to
179             the dist root -- generally, the place where your F<dist.ini> or other
180             configuration file is located.
181              
182             =head2 prefix
183              
184             This parameter can be set to place the gathered files under a particular
185             directory. See the L<description|DESCRIPTION> above for an example.
186              
187             =head2 filename
188              
189             The name of the file to gather, relative to the C<root>.
190             Can be used more than once.
191              
192             =head1 AUTHOR
193              
194             Ricardo SIGNES 😏 <cpan@semiotic.systems>
195              
196             =head1 COPYRIGHT AND LICENSE
197              
198             This software is copyright (c) 2022 by Ricardo SIGNES.
199              
200             This is free software; you can redistribute it and/or modify it under
201             the same terms as the Perl 5 programming language system itself.
202              
203             =cut