File Coverage

blib/lib/File/Assets/Filter/YUICompressor.pm
Criterion Covered Total %
statement 12 42 28.5
branch 0 14 0.0
condition 0 3 0.0
subroutine 4 8 50.0
pod 0 4 0.0
total 16 71 22.5


line stmt bran cond sub pod time code
1             package File::Assets::Filter::YUICompressor;
2              
3 9     9   8033 use strict;
  9         19  
  9         267  
4 9     9   43 use warnings;
  9         16  
  9         307  
5              
6 9     9   127 use base qw/File::Assets::Filter::Collect/;
  9         18  
  9         656  
7 9     9   47 use File::Assets::Carp;
  9         15  
  9         94  
8              
9             sub signature {
10 0     0 0   return "yuicompressor";
11             }
12              
13             my %default = (qw/
14             java java
15             /,
16             jar => undef,
17             opt => "",
18             );
19              
20             sub new {
21 0     0 0   my $class = shift;
22 0           my $self = $class->SUPER::new(@_);
23 0           local %_ = @_;
24             # TODO-? $self->parse_cfg(\%default, \%_);
25 0           while (my ($setting, $value) = each %default) {
26 0 0         $self->cfg->{$setting} = exists $_{$setting} ? $_{$setting} : $value;
27             }
28 0 0         $self->cfg->{jar} or croak "You need to specify the location of the YUI Compressor jar file (something like \"yuicompressor-?.?.?.jar\")";
29 0 0 0       -f $self->cfg->{jar} && -r _ or croak "Doesn't exist/can't read: ", $self->cfg->{jar};
30             # TODO-f Test if we can execute "java"
31              
32             # -f $self->cfg->{java} && -x _ or croak "Doesn't exist/can't execute: ", $self->cfg->{java};
33              
34             # croak "You must specify a type to filter by (either js or css)" unless $self->where->{type};
35              
36             # if ($self->where->{type}->type eq "text/css") {
37             # }
38             # elsif ($self->where->{type}->type eq "application/javascript" ||
39             # $self->where->{type}->type eq "application/x-javascript" || # Handle different MIME::Types versions.
40             # $self->where->{type}->type =~ m/\bjavascript\b/) {
41             # }
42             # else {
43             # carp "Not sure YUI compressor can handle the type: ", $self->where->{type}->type;
44             # }
45              
46 0           return $self;
47             }
48              
49             sub new_parse_cfg {
50 0     0 0   my $class = shift;
51 0           my $cfg = shift;
52 0 0         if ($cfg =~ m/[=;]/) {
53 0           return $class->SUPER::new_parse_cfg($cfg);
54             }
55 0           return ($class->SUPER::new_parse_cfg(""), jar => $cfg);
56             }
57              
58             sub build_content {
59 0     0 0   my $self = shift;
60              
61 0           my $matched = $self->matched;
62 0           my $output_asset = $self->output_asset;
63 0           my $file = $output_asset->file;
64 0           my $extension = $self->kind->extension;
65              
66 0           my $java = $self->cfg->{java};
67 0           my $jar = $self->cfg->{jar};
68 0           my $opt = $self->cfg->{opt};
69              
70 0 0         $file->parent->mkpath unless -d $file->parent;
71              
72 0 0         open my $yc_io, "| $java -jar $jar --type $extension $opt > $file" or die $!;
73 0           for my $match (@$matched) {
74 0           my $asset = $match->{asset};
75 0           print $yc_io ${ $asset->content };
  0            
76             }
77 0 0         close $yc_io or warn $!;
78              
79             # TODO-f Fallback to concat if it doesn't work
80              
81 0           return undef; # We (the jar) already put the content in the asset file, so we return undef here.
82             }
83              
84             1;