lib/Plack/Middleware/Assets/RailsLike/Compiler.pm

filter: Statementstmt 96.1% 74 / 77 Branchbran 94.4% 17 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 2 totaltotal 93.7% 105 / 112 CC 12 SCAR 14.2
CC
12
Cov
96%
_cmd_require
9.1
compile
3.0
_minify
2.0
CRAP 4.1
« lib/Plack/Middleware/Assets/RailsLike.pm ↑ Summary
1 package Plack::Middleware::Assets::RailsLike::Compiler;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use CSS::LESSp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use CSS::Minifier::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Spec::Functions qw(catdir catfile canonpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use JavaScript::Minifier::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Text::Sass::XS qw(:const);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 13 my $class = shift;
16 13 my %args = (
17 minify => 0,
18 search_path => ['.'],
19 @_
20 );
21 13 my $self = bless \%args, $class;
22  
23 $self->{sass_compiler} = Text::Sass::XS->new(
24 output_style => SASS_STYLE_COMPRESSED,
25 source_comments => SASS_SOURCE_COMMENTS_NONE,
26 include_paths => $self->{search_path},
27 13 image_path => undef, # TBD require option?
28 );
29  
30 13 return $self;
31 }
32  
33 27 ▶sub compile {
Subroutine compile: called CC=3 SCAR=11.0
Pod undocumented
34 27 my $self = shift;
35 27 my %args = (
36 manifest => undef,
37 type => 'js',
38 @_
39 );
40  
41 27 my $content = $args{manifest};
42  
43 27 ▶ if ( $args{type} eq 'css' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"type"} eq "css") 13 14 27
44 13 my $css_comment = qr!
45 /\*
46 .*?
47 (?:\r?\n)
48 ((?:\*= .+(?:\r?\n)){1,})
49 \*/
50 !x;
51 13 $content =~ s{$css_comment}{$1}g;
52 }
53  
54 27 my $parser = qr{
55 ^
56 (?://|\*)=
57 \s+
58 (require) # commands
59 \s+
60 ([0-9a-zA-Z_\-./]+) # basename
61 \s*
62 $
63 }xms;
64  
65 27 $content
66 47 =~ s/$parser/my $cmd = "_cmd_$1"; $self->$cmd($2, $args{type})/ge;
67  
68 27 ▶ $content = $self->_minify( $content, $args{type} ) if $self->{minify};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"minify"} 9 18 27
69 27 return $content;
70 }
71  
72 47 ▶sub _cmd_require {
Subroutine _cmd_require: called CC=9 SCAR=22.1
73 47 my $self = shift;
74 47 my ( $file, $type ) = @_;
75  
76 47 my @search_path = @{ $self->{search_path} };
77 47 ▶ my @type = $type eq 'js' ? qw(js) : qw(css scss sass less);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'js' ? : 26 21 47
78  
79 47 for my $path (@search_path) {
80 48 for my $type (@type) {
81 54 my $filename = canonpath(
82 catfile( $path, sprintf( '%s.%s', $file, $type ) ) );
83  
84 54 my $buff;
85 54 ▶ read_file( $filename, buf_ref => \$buff, err_mode => sub { } );
Subroutine __ANON__: not called CC=1 SCAR=0.0
86 54 ▶ unless ($!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($!) { } 7 47 54
if (do { $! == 2 }) { } 7 0 7
87  
88 47 my $content;
89 47 ▶ if ( $type eq 'scss' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scss') { } 1 46 47
elsif ($type eq 'sass') { } 1 45 46
elsif ($type eq 'less') { } 1 44 45
90 1 $content = $self->{sass_compiler}->scss2css($buff);
91 }
92 elsif ( $type eq 'sass' ) {
93 1 $content = $self->{sass_compiler}->sass2css($buff);
94 }
95 elsif ( $type eq 'less' ) {
96 1 $content = join '', CSS::LESSp->parse($buff);
97 }
98 else {
99 44 $content = $buff;
100 }
101  
102 47 chomp $content;
103 47 return $content;
104 }
105 elsif ( $! == Errno::ENOENT ) {
106 7 next;
107 }
108 else {
109 0 Carp::carp("read_file '$filename' failed - $!");
110 0 return;
111 }
112 }
113 }
114  
115 0 Carp::carp( sprintf "requires '%s' failed - No such file in %s",
116 $file, join( ', ', @search_path ) );
117 }
118  
119 9 ▶sub _minify {
Subroutine _minify: called CC=2 SCAR=6.9
120 9 my $self = shift;
121 9 my ( $content, $type ) = @_;
122 9 ▶ if ( $type eq 'js' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'js') { } 5 4 9
123 5 $content = JavaScript::Minifier::XS::minify($content);
124 }
125 else {
126 4 $content = CSS::Minifier::XS::minify($content);
127 }
128 9 return $content;
129 }
130  
131 12 1;
« lib/Plack/Middleware/Assets/RailsLike.pm ↑ Summary