File Coverage

blib/lib/Dist/Zilla/Plugin/GatherFromManifest.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GatherFromManifest;
2              
3             # $Id: GatherFromManifest.pm 23 2010-09-25 12:01:20Z stro $
4              
5 1     1   40915 use strict;
  1         4  
  1         45  
6 1     1   6 use warnings;
  1         2  
  1         197  
7              
8             BEGIN {
9 1     1   29 $Dist::Zilla::Plugin::GatherFromManifest::VERSION = '1.001';
10             }
11              
12             =head1 NAME
13              
14             Dist::Zilla::Plugin::GatherFromManifest - gather all files from MANIFEST
15              
16             =head1 VERSION
17              
18             version 1.001
19              
20             =head1 SYNOPSIS
21              
22             [GatherFromManifest]
23              
24             =head1 DESCRIPTION
25              
26             This is a L<FileGatherer|Dist::Zilla::Role::FileGatherer>plugin, which
27             allows you to implicitly specify which files should be built into
28             distibution.
29              
30             This module offers a different approach to default
31             L<Dist::Zilla::Plugin::GatherDir> which adds ALL files to distribution and
32             L<Dist::Zilla::Plugin::ManifestSkip> which prunes files listed in
33             MANIFEST.SKIP.
34              
35             You should replace [GatherDir] with [GatherFromManifest] and make sure you
36             have a MANIFEST file. It's quite useful if you're converting existing
37             package to Dist::Zilla.
38              
39             If you plan to use [License], [Readme] or any other module producing
40             autogenerated files, you should remove corresponding file names from
41             MANIFEST to avoid duplication errors.
42              
43             =head1 ATTRIBUTES
44              
45             =head2 root
46              
47             This is the directory in which to look for files. If not given, it defaults to
48             the dist root -- generally, the place where your F<dist.ini> or other
49             configuration file is located.
50              
51             =head2 prefix
52              
53             This parameter can be set to gather all the files found under a common
54             directory. See the L<description block in GatherDir documentation|Dist::Zilla::Plugin::GatherDir#DESCRIPTION> for an example.
55              
56             =head1 SUBROUTINES/METHODS
57              
58             =cut
59              
60             # ABSTRACT: build files that appear in a MANIFEST file
61              
62 1     1   5630 use Moose;
  0            
  0            
63             use Moose::Autobox;
64             use MooseX::Types::Path::Class qw(Dir File);
65             with 'Dist::Zilla::Role::FileGatherer';
66              
67             use ExtUtils::Manifest;
68              
69             use File::Find::Rule;
70             use File::HomeDir;
71             use File::Spec;
72             use Path::Class;
73              
74             use namespace::autoclean;
75              
76             has root => (
77             is => 'ro',
78             isa => Dir,
79             lazy => 1,
80             coerce => 1,
81             required => 1,
82             default => sub { shift->zilla->root },
83             );
84              
85             has prefix => (
86             is => 'ro',
87             isa => 'Str',
88             default => '',
89             );
90              
91             has manifest => (is => 'ro', required => 1, default => 'MANIFEST');
92              
93             =head2 gather_files
94              
95             Overridden method implementing gathering functionality.
96              
97             =cut
98              
99             sub gather_files {
100             my ($self) = @_;
101              
102             my $manifest = $self->zilla->root->file( $self->manifest );
103             unless (-f $manifest) {
104             $self->log_fatal("Cannot read manifest file: ", $manifest);
105             }
106              
107             my $root = "" . $self->root;
108             $root =~ s{^~([\\/])}{File::HomeDir->my_home . $1}ex;
109             $root = Path::Class::dir($root);
110              
111             $manifest = File::Spec->catdir($root, $manifest);
112              
113             my $list = ExtUtils::Manifest::maniread($manifest);
114              
115             my @files;
116             FILE: for my $filename (keys %$list) {
117             push @files, $self->_file_from_filename($filename);
118             }
119              
120             for my $file (@files) {
121             (my $newname = $file->name) =~ s{\A\Q$root\E[\\/]}{}gx;
122             $newname = File::Spec->catdir($self->prefix, $newname) if $self->prefix;
123             $newname = Path::Class::dir($newname)->as_foreign('Unix')->stringify;
124              
125             $file->name($newname);
126             $self->add_file($file);
127             }
128              
129             return;
130             }
131              
132             sub _file_from_filename {
133             my ($self, $filename) = @_;
134              
135             unless (-f $filename) {
136             $self->log_fatal("Cannot read file from manifest: ", $filename);
137             }
138              
139             return Dist::Zilla::File::OnDisk->new({
140             name => $filename,
141             mode => (stat $filename)[2] & oct(755), # kill world-writeability
142             });
143             }
144              
145             __PACKAGE__->meta->make_immutable;
146             no Moose;
147             1;
148              
149             =head1 AUTHOR
150              
151             Serguei Trouchelle <stro@cpan.org>
152              
153             Some parts of code is borrowed from L<Dist::Zilla::Plugin::GatherDir> and L<Dist::Zilla::Plugin::ManifestSkip> written by Ricardo SIGNES <rjbs@cpan.org>
154              
155             =head1 LICENSE AND COPYRIGHT
156              
157             Copyright (c) 2010 by Serguei Trouchelle
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut
163