File Coverage

blib/lib/Dist/Zilla/Plugin/CSS/Compressor.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::CSS::Compressor;
2              
3 1     1   1163 use Moose;
  0            
  0            
4             use CSS::Compressor qw( css_compress );
5             use Dist::Zilla::File::FromCode;
6              
7             # ABSTRACT: Compress CSS files
8             our $VERSION = '0.02'; # VERSION
9              
10              
11             with 'Dist::Zilla::Role::FileGatherer';
12             with 'Dist::Zilla::Role::FileInjector';
13              
14             use namespace::autoclean;
15              
16              
17             has finder => (
18             is => 'ro',
19             isa => 'Str',
20             );
21              
22              
23             has output_regex => (
24             is => 'ro',
25             isa => 'Str',
26             default => '/\.css$/.min.css/',
27             );
28              
29              
30             has output => (
31             is => 'ro',
32             isa => 'Str',
33             );
34              
35              
36             sub gather_files
37             {
38             my($self, $arg) = @_;
39            
40             my $list = sub {
41             defined $self->finder
42             ? @{ $self->zilla->find_files($self->finder) }
43             : grep { $_->name =~ /\.css$/ && $_->name !~ /\.min\./ } @{ $self->zilla->files };
44             };
45            
46             if(defined $self->output)
47             {
48             my $min_file;
49             $min_file = Dist::Zilla::File::FromCode->new({
50             name => $self->output,
51             code => sub {
52             my @list = $list->();
53             $self->log("compressing " . join(', ', map { $_->name } @list) . " => " . $min_file->name);
54             css_compress(join("\n", map { $_->content } @list));
55             },
56             });
57            
58             $self->add_file($min_file);
59             }
60             else
61             {
62             foreach my $file ($list->()) {
63             my $min_file;
64             $min_file = Dist::Zilla::File::FromCode->new({
65             name => do {
66             my $min_filename = $file->name;
67             eval q{ $min_filename =~ s} . $self->output_regex;
68             $min_filename;
69             },
70             code => sub {
71             $self->log("compressing " . $file->name . " => " . $min_file->name);
72             css_compress($file->content);
73             },
74             });
75            
76             $self->add_file($min_file);
77             }
78             }
79             }
80              
81             __PACKAGE__->meta->make_immutable;
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Dist::Zilla::Plugin::CSS::Compressor - Compress CSS files
94              
95             =head1 VERSION
96              
97             version 0.02
98              
99             =head1 SYNOPSIS
100              
101             [CSS::Compressor]
102              
103             =head1 DESCRIPTION
104              
105             Compress CSS files in your distribution using L<CSS::Compressor>. By default for
106             each C<foo.css> file in your distribution this plugin will create a C<foo.min.css>
107             which has been compressed.
108              
109             =head1 ATTRIBUTES
110              
111             =head2 finder
112              
113             Specifies a L<FileFinder|Dist::Zilla::Role::FileFinder> for the CSS files that
114             you want compressed. If this is not specified, it will compress all the CSS
115             files that do not have a C<.min.> in their filenames. Roughly equivalent to
116             this:
117              
118             [FileFinder::ByName / CSSFiles]
119             file = *.css
120             skip = .min.
121             [CSS::Compressor]
122             finder = CSSFile
123              
124             =head2 output_regex
125              
126             Regular expression substitution used to generate the output filenames. By default
127             this is
128              
129             [CSS::Compressor]
130             output_regex = /\.css$/.min.css/
131              
132             which generates a C<foo.min.css> for each C<foo.css>.
133              
134             =head2 output
135              
136             Output filename. Not used by default, but if specified, all CSS files are merged and
137             compressed into a single file using this as the output filename.
138              
139             =head1 METHODS
140              
141             =head2 $plugin-E<gt>gather_files( $arg )
142              
143             This method adds the compressed CSS files to your distribution.
144              
145             =head1 AUTHOR
146              
147             Graham Ollis <plicease@cpan.org>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2012 by Graham Ollis.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut