File Coverage

blib/lib/Dist/Zilla/Plugin/ShareDir/Tarball.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod 0 5 0.0
total 48 53 90.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::ShareDir::Tarball;
2             BEGIN {
3 3     3   1999816 $Dist::Zilla::Plugin::ShareDir::Tarball::AUTHORITY = 'cpan:YANICK';
4             }
5             # ABSTRACT: Bundle your shared dir into a tarball
6             $Dist::Zilla::Plugin::ShareDir::Tarball::VERSION = '0.6.0';
7              
8 3     3   21 use strict;
  3         3  
  3         115  
9 3     3   11 use warnings;
  3         3  
  3         1843  
10              
11 3     3   14 use Moose;
  3         3  
  3         19  
12              
13 3     3   13060 use Dist::Zilla::File::InMemory;
  3         55822  
  3         72  
14 3     3   1802 use Compress::Zlib;
  3         111188  
  3         577  
15 3     3   1785 use Archive::Tar;
  3         65228  
  3         1097  
16              
17             has dir => (
18             is => 'ro',
19             isa => 'Str',
20             default => 'share',
21             );
22              
23             has archive => (
24             is => 'ro',
25             lazy => 1,
26             predicate => 'has_archive',
27             default => sub {
28             Archive::Tar->new;
29             },
30             );
31              
32             has share_dir_map => (
33             is => 'ro',
34             lazy => 1,
35             default => sub {
36             my $self = shift;
37              
38             return $self->has_archive ? { dist => $self->dir } : {};
39             },
40             );
41              
42             has archive_dz_file => (
43             is => 'ro',
44             lazy => 1,
45             default => sub {
46             Dist::Zilla::File::InMemory->new(
47             content => 'placeholder',
48             encoding => 'bytes',
49             name => join '/', $_[0]->dir, 'shared-files.tar.gz',
50             );
51             },
52             );
53              
54             sub compressed_archive {
55 4     4 0 107 Compress::Zlib::memGzip($_[0]->archive->write)
56             }
57              
58             sub find_files {
59 4     4 0 5 my $self = shift;
60              
61 4         110 my $dir = $self->dir . '/';
62 11         289 return grep { $_->name ne $self->archive_dz_file->name }
  28         695  
63 4         88 grep { !index $_->name, $dir }
64 4         7 @{ $self->zilla->files };
65             }
66              
67             sub gather_files {
68 4     4 0 387789 my $self = shift;
69            
70 4         124 $self->add_file( $self->archive_dz_file );
71             }
72              
73              
74             sub prune_files {
75 4     4 0 13614 my $self = shift;
76              
77 4         110 my $src = $self->dir;
78              
79 4         15 for ( $self->find_files ) {
80 7         308 ( my $archive_name = $_->name ) =~ s#$src/##;
81 7         412 $self->archive->add_data( $archive_name => $_->encoded_content );
82 7         3371 $self->zilla->prune_file($_);
83             }
84              
85             }
86              
87             sub munge_files {
88 4     4 0 6988 my $self = shift;
89              
90 4         136 $self->archive_dz_file->content( $self->compressed_archive );
91             }
92              
93              
94             with 'Dist::Zilla::Role::ShareDir',
95             'Dist::Zilla::Role::FileInjector',
96             'Dist::Zilla::Role::FileGatherer',
97             'Dist::Zilla::Role::FileMunger',
98             'Dist::Zilla::Role::FilePruner';
99              
100             1;
101              
102             __END__
103              
104             =pod
105              
106             =encoding UTF-8
107              
108             =head1 NAME
109              
110             Dist::Zilla::Plugin::ShareDir::Tarball - Bundle your shared dir into a tarball
111              
112             =head1 VERSION
113              
114             version 0.6.0
115              
116             =head1 SYNOPSIS
117              
118             # in dist.ini
119              
120             [ShareDir::Tarball]
121              
122             =head1 DESCRIPTION
123              
124             Using L<File::ShareDir> to deploy non-Perl files alongside a distribution is
125             great, but it has a problem. Just like for modules, upon installation CPAN clients
126             don't remove any of the files that were already present in the I</lib>
127             directories beforehand. So if version 1.0 of the distribution was sharing
128              
129             share/foo
130             share/bar
131              
132             and version 1.1 changed that to
133              
134             share/foo
135             share/baz
136              
137             then a user installing first version 1.0 then 1.1 will end up with
138              
139             share/foo
140             share/bar
141             share/baz
142              
143             which might be a problem (or not).
144              
145             Fortunately, there is a sneaky
146             workaround in the case where you don't want the files of past distributions to
147             linger around. The trick is simple: bundle all the files to be shared into
148             a tarball called I<shared-files.tar.gz>. As there is only that one file, any
149             new install is conveniently clobbering the old version.
150              
151             But archiving the content of the I<share> directory is no fun. Hence
152             L<Dist::Zilla::Plugin::ShareDir::Tarball> which, upon the file munging stage, gathers all
153             files in the I<share> directory and build the I<shared-files.tar.gz> archive
154             with them. If there is no such files, the process is simply skipped.
155              
156             =head1 OPTIONS
157              
158             =head2 dir
159              
160             The source directory to be bundled into the shared tarball. Defaults to
161             C<share>.
162              
163             =head1 SEE ALSO
164              
165             L<Dist::Zilla::Plugin::ShareDir>, which is similar to this module, but without
166             the conversion of the shared directory into a tarball.
167              
168             L<File::ShareDir::Tarball> - transparently extract the tarball behind the
169             scene so that the shared directory can be accessed just like it is in
170             L<File::ShareDir>.
171              
172             L<Module::Build::CleanInstall> - A subclass of L<Module::Build> which
173             deinstall the files from previous installations via their I<packlist>.
174              
175             =head1 AUTHOR
176              
177             Yanick Champoux <yanick@cpan.org>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2012 by Yanick Champoux.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut