Assets/Filter/YUICompressor.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 78 CC 4 SCAR 14.3
CC
4
Cov
0%
new
6.0
new_parse_cfg
6.0
build_content
6.0
CRAP 4.2
1 package File::Assets::Filter::YUICompressor;
2  
3 use strict;
4 use warnings;
5  
6 use base qw/File::Assets::Filter::Collect/;
7 use File::Assets::Carp;
8  
9 sub signature {
10 return "yuicompressor";
11 }
12  
13 my %default = (qw/
14 java java
15 /,
16 jar => undef,
17 opt => "",
18 );
19  
20 sub new {
21 my $class = shift;
22 my $self = $class->SUPER::new(@_);
23 local %_ = @_;
24 # TODO-? $self->parse_cfg(\%default, \%_);
25 while (my ($setting, $value) = each %default) {
26 $self->cfg->{$setting} = exists $_{$setting} ? $_{$setting} : $value;
27 }
28 $self->cfg->{jar} or croak "You need to specify the location of the YUI Compressor jar file (something like \"yuicompressor-?.?.?.jar\")";
29 -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 return $self;
47 }
48  
49 sub new_parse_cfg {
50 my $class = shift;
51 my $cfg = shift;
52 if ($cfg =~ m/[=;]/) {
53 return $class->SUPER::new_parse_cfg($cfg);
54 }
55 return ($class->SUPER::new_parse_cfg(""), jar => $cfg);
56 }
57  
58 sub build_content {
59 my $self = shift;
60  
61 my $matched = $self->matched;
62 my $output_asset = $self->output_asset;
63 my $file = $output_asset->file;
64 my $extension = $self->kind->extension;
65  
66 my $java = $self->cfg->{java};
67 my $jar = $self->cfg->{jar};
68 my $opt = $self->cfg->{opt};
69  
70 $file->parent->mkpath unless -d $file->parent;
71  
72 open my $yc_io, "| $java -jar $jar --type $extension $opt > $file" or die $!;
73 for my $match (@$matched) {
74 my $asset = $match->{asset};
75 print $yc_io ${ $asset->content };
76 }
77 close $yc_io or warn $!;
78  
79 # TODO-f Fallback to concat if it doesn't work
80  
81 return undef; # We (the jar) already put the content in the asset file, so we return undef here.
82 }
83  
84 1;