File Coverage

blib/lib/Dist/Zilla/Plugin/jQuery.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::jQuery;
2              
3 1     1   721 use strict;
  1         1  
  1         33  
4 1     1   6 use warnings;
  1         1  
  1         27  
5 1     1   412 use Moose;
  0            
  0            
6             use Resource::Pack::jQuery;
7             use File::Temp qw( tempdir );
8             use Path::Class qw( file dir );
9             use Moose::Util::TypeConstraints qw( enum );
10             use File::HomeDir;
11              
12             with 'Dist::Zilla::Role::FileGatherer';
13              
14             use namespace::autoclean;
15              
16             # ABSTRACT: Include jQuery in your distribution
17             our $VERSION = '0.03'; # VERSION
18              
19              
20             has version => (
21             is => 'ro',
22             isa => 'Str',
23             default => '1.8.2',
24             );
25              
26              
27             has minified => (
28             is => 'ro',
29             isa => enum([qw(yes no both)]),
30             default => 'both',
31             );
32              
33              
34             has dir => (
35             is => 'ro',
36             isa => 'Str',
37             lazy => 1,
38             default => sub {
39             my $self = shift;
40             my $main_module = file( $self->zilla->main_module->name );
41             (my $base = $main_module->basename) =~ s/\.pm//;
42             my $dir = $main_module->dir->subdir($base, 'public', 'js')->stringify;
43             $self->log("using default dir $dir");
44             $dir;
45             },
46             );
47              
48              
49             has location => (
50             is => 'ro',
51             isa => enum([qw(build root)]),
52             default => 'build',
53             );
54              
55              
56             has cache => (
57             is => 'ro',
58             isa => 'Bool',
59             default => 0,
60             );
61              
62             has _cache_dir => (
63             is => 'ro',
64             isa => 'Path::Class::Dir',
65             lazy => 1,
66             default => sub {
67             my $self = shift;
68             if(!$self->cache)
69             {
70             return dir( tempdir( CLEANUP => 1) );
71             }
72             else
73             {
74             my $dir = dir( File::HomeDir->my_dist_data("Dist-Zilla-Plugin-jQuery", { create => 1 }) );
75             $dir = $dir->subdir( $self->version, $self->minified );
76             unless(-d $dir)
77             {
78             $dir->mkpath(0, 0755);
79             }
80             return $dir;
81             }
82             },
83             );
84              
85              
86             sub _install_temp
87             {
88             my($self) = @_;
89             my $dir = $self->_cache_dir;
90            
91             # keep caches around for at least 30 days
92             my $timestamp = $dir->file('.timestamp');
93             if(-e $timestamp && time < $timestamp->slurp + 60*60*24*30)
94             {
95             return $dir;
96             }
97            
98             unlink $_->stringify for $dir->children(no_hidden => 1);
99            
100             my %args = (
101             install_to => $dir->stringify,
102             version => $self->version,
103             );
104              
105             if($self->minified =~ /^(yes|both)$/i)
106             { Resource::Pack::jQuery->new(%args, minified => 1)->install }
107             if($self->minified =~ /^(no|both)$/i)
108             { Resource::Pack::jQuery->new(%args, minified => 0)->install }
109             $timestamp->spew(time);
110             return $dir;
111             }
112              
113             sub gather_files
114             {
115             my($self, $arg) = @_;
116            
117             my $temp = $self->_install_temp;
118            
119             foreach my $child ($temp->children(no_hidden => 1))
120             {
121             $self->log("adding " . $child->basename . " to " . $self->dir );
122             if($self->location eq 'build')
123             {
124             $self->add_file(
125             Dist::Zilla::File::InMemory->new(
126             content => scalar $child->slurp(iomode => '<:encoding(UTF-8)'),
127             name => dir( $self->dir )->file( $child->basename )->stringify,
128             ),
129             );
130             }
131             else
132             {
133             my $file = $self->zilla->root->file( $self->dir, $child->basename );
134             $file->parent->mkpath(0, 0755);
135             $file->spew( iomode => '>:encoding(UTF-8)', scalar $child->slurp(iomode => '<:encoding(UTF-8)') );
136             }
137             }
138             return;
139             }
140              
141             __PACKAGE__->meta->make_immutable;
142              
143             1;
144              
145             __END__
146              
147             =pod
148              
149             =encoding UTF-8
150              
151             =head1 NAME
152              
153             Dist::Zilla::Plugin::jQuery - Include jQuery in your distribution
154              
155             =head1 VERSION
156              
157             version 0.03
158              
159             =head1 SYNOPSIS
160              
161             [jQuery]
162             version = 1.8.2
163             minified = both
164              
165             =head1 DESCRIPTION
166              
167             This plugin fetches jQuery from the Internet
168             using L<Resource::Pack::jQuery> and includes it into your distribution.
169              
170             =head1 ATTRIBUTES
171              
172             =head2 version
173              
174             The jQuery version to download. Defaults to 1.8.2 (the default may
175             change in the future).
176              
177             =head2 minified
178              
179             Whether or not the JavaScript should be minified. Defaults to both.
180             Possible values.
181              
182             =over 4
183              
184             =item * yes
185              
186             =item * no
187              
188             =item * both
189              
190             =back
191              
192             =head2 dir
193              
194             Which directory to put jQuery into. Defaults to public/js under
195             the same location of your main module, so if your module is
196             Foo::Bar (lib/Foo/Bar.pm), then the default dir will be
197             lib/Foo/Bar/public/js.
198              
199             =head2 location
200              
201             Where to put jQuery. Choices are:
202              
203             =over 4
204              
205             =item build
206              
207             This puts jQuery in the directory where the dist is currently
208             being built, where it will be incorporated into the dist.
209              
210             =item root
211              
212             This puts jQuery in the root directory (The same directory
213             that contains F<dist.ini>). It will also be included in the
214             built distribution.
215              
216             =back
217              
218             =head2 cache
219              
220             Cache the results so that the Internet is required less frequently.
221             Defaults to 0.
222              
223             =head1 METHODS
224              
225             =head2 $plugin-E<gt>gather_files
226              
227             This method places the fetched jQuery sources into your distribution.
228              
229             =head1 CAVEATS
230              
231             If you bundle jQuery into your distribution, you should update the copyright
232             section to include a notice that bundled copy of jQuery is copyright
233             the jQuery Project and is licensed under either the MIT or GPLv2 license.
234             This module does not bundle jQuery, but its dependency L<Resource::Pack::jQuery>
235             does.
236              
237             =head1 SEE ALSO
238              
239             L<Resource::Pack::jQuery>
240              
241             =cut
242              
243             =head1 AUTHOR
244              
245             Graham Ollis <perl@wdlabs.com>
246              
247             =head1 COPYRIGHT AND LICENSE
248              
249             This software is copyright (c) 2012 by Graham Ollis.
250              
251             This is free software; you can redistribute it and/or modify it under
252             the same terms as the Perl 5 programming language system itself.
253              
254             =cut