lib/Dancer/Plugin/Preprocess/Less.pm

filter: Statementstmt 66.3% 63 / 95 Branchbran 33.3% 8 / 24 Conditioncond 25.0% 2 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 58.7% 84 / 143 CC 10 SCAR 28.8
CC
10
Cov
57%
__ANON__
46.2
__ANON__
5.8
_process_with_lessc
2.0
CRAP 17.8
↑ Summary
1 package Dancer::Plugin::Preprocess::Less;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Generate CSS files from .less files
7  
8 2 our $VERSION = '0.021'; # VERSION
9  
10 2 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 my $settings = plugin_setting;
17 2 my $paths;
18  
19 2 ▶my $public_dir = abs_path(setting('public') || '');
Condition100%2/2
setting('public') || ''
l!l
11
MC/DC100%1/1
setting('public') || ''
setting('public')
Truth table100%2/2
A
A= setting('public')
Aresult
11
00
20  
21 2 ▶if (exists $settings->{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{'paths'}) { } 1 1 2
22 1 $paths = $settings->{paths};
23 }
24 else {
25 1 $paths = ['css'];
26 }
27  
28 # Translate URL paths to filesystem paths
29 2 my @fs_paths = map { catfile(split('/'), "") } @$paths;
30  
31 2 my $process_file;
32  
33 2 ▶if (`lessc -v`) {
Branch: 2 branches
BranchTrueFalseTotal
if (`lessc -v`) { } 0 2 2
elsif (eval { do { require CSS::LESSp } }) { } 2 0 2
34 0 $process_file = \&_process_with_lessc;
35 }
36 2 elsif (eval { require CSS::LESSp }) {
37 2 $process_file = \&_process_with_CSS_LESSp;
38 }
39 else {
40 0 error __PACKAGE__ .
41 ": lessc binary or CSS::LESSp is required but neither was found";
42 # This is a fatal error
43 0 return false;
44 }
45  
46 2 ▶if ($settings->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"save"}) 0 2 2
47 # Check if the directories are writable
48 0 for my $path (@fs_paths) {
49 0 my $full_path = catfile($public_dir, $path);
50 0 ▶ if (!-w $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $full_path) 0 0 0
51 0 warning __PACKAGE__ . ": Can't write to $full_path";
52 }
53 }
54 }
55  
56 # Make a regular expression to match URL paths
57 my $paths_re = join '|', map {
58 2 my $s = $_;
59 3 $s =~ s{^[^/]}{/$&}; # Add leading slash, if missing
60 3 $s =~ s{/$}{}; # Remove trailing slash
61 3 quotemeta $s;
62 } reverse sort @$paths;
63  
64 0 ▶sub _process_with_lessc {
Subroutine _process_with_lessc: not called CC=1 SCAR=6.9
65 0 my $less_file = shift;
66
67 0 my $pid = open3(my $chld_in, my $chld_out, my $chld_err, 'lessc',
68 $less_file);
69
70 0 my $css = join '', <$chld_out>;
71 0 waitpid $pid, 0;
72  
73 0 return $css;
74 }
75  
76 2 ▶sub _process_with_CSS_LESSp {
Subroutine _process_with_CSS_LESSp: called CC=1 SCAR=0.0
77 2 my $less_file = shift;
78
79 2 open (my $f, '<', $less_file);
80 2 my $contents;
81 {
82 2 local $/;
83 2 $contents = <$f>;
84 }
85 2 close($f);
86
87 2 return join '', CSS::LESSp->parse($contents);
88 }
89  
90 2 ▶hook before_file_render => sub {
Subroutine __ANON__: called CC=7 SCAR=38.3
91 # If saving is not enabled, then there's nothing for us to do (since we
92 # won't be able to save the generated CSS rules in a file, duh)
93 2 ▶ return if !$settings->{save};
Branch: 1 branch
BranchTrueFalseTotal
unless $settings->{"save"} 2 0 2
94  
95 0 my $path = abs_path(shift);
96
97 # Build a regular expression to match filesystem paths
98 0 my $fs_paths_re = join '|', map { quotemeta } @fs_paths;
99
100 0 my $path_re = '^' . quotemeta(catfile($public_dir, "")) .
101 '(?:' . $fs_paths_re . ')';
102  
103 0 ▶ if ($path =~ qr{$path_re} && $path =~ qr{\.css$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ qr/$path_re/ and $path =~ qr/\.css$/) 0 0 0
Condition0%0/3
$path =~ qr/$path_re/ and $path =~ qr/\.css$/
!ll&&!rl&&r
000
MC/DC0%0/2
$path =~ qr/$path_re/ and $path =~ qr/\.css$/
$path =~ qr/$path_re/ $path =~ qr/\.css$/
Truth table0%0/3
A and B
A= $path =~ qr/$path_re/B= $path =~ qr/\.css$/
ABresult
0X0
100
111
104 0 (my $input_file = $path) =~ s/\.css$/.less/;
105  
106 0 ▶ if (-f $input_file && (stat($path))[9] < (stat($input_file))[9]) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $input_file and (stat $path)[9] < (stat $input_file)[9]) 0 0 0
Condition0%0/3
-f $input_file and (stat $path)[9] < (stat $input_file)[9]
!ll&&!rl&&r
000
MC/DC0%0/2
-f $input_file and (stat $path)[9] < (stat $input_file)[9]
-f $input_file (stat $path)[9] < (stat $input_file)[9]
Truth table0%0/3
A and B
A= -f $input_fileB= (stat $path)[9] < (stat $input_file)[9]
ABresult
0X0
100
111
107 # There is a Less file newer than the CSS file
108 0 my $css = $process_file->($input_file);
109
110 0 ▶ if (defined $css) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $css) 0 0 0
111 # Save the generated CSS data
112 0 open(my $f, '>', $path);
113 0 print {$f} $css;
114 0 close($f);
115 }
116 }
117 }
118 2 };
119  
120 2 ▶get qr{($paths_re)/([^/]*\.css)} => sub {
Subroutine __ANON__: called CC=4 SCAR=17.5
121 2 my ($path, $css_file) = splat;
122  
123 # Prepend the path to the CSS file name
124 2 $path = catfile(split('/', $path));
125 2 $css_file = catfile($path, $css_file);
126
127 # Prepend public directory location
128 2 my $css_file_abs = catfile($public_dir, $css_file);
129
130 2 (my $input_file = $css_file_abs) =~ s/\.css$/.less/;
131  
132 2 ▶ if (-f $input_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $input_file) { } 2 0 2
133 # Less file exists
134 2 my $css = $process_file->($input_file);
135  
136 2 ▶ if ($settings->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"save"}) 0 2 2
137 # Saving enabled -- save the generated CSS as the requested file
138 0 open(my $f, '>', $css_file_abs);
139 0 print {$f} $css;
140 0 close($f);
141 }
142
143 2 header 'content-type' => 'text/css';
144 2 return $css;
145 }
146 else {
147 # Check if the CSS file exists. Probably not, because if it does exist,
148 # then Dancer should have already served it as a static file, and we
149 # shouldn't even end up in this route handler. Still, we'll do this
150 # check in case we're in some wacky parallel universe where route
151 # handlers are run before static files.
152 0 ▶ if (-f $css_file_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $css_file_abs) { } 0 0 0
153 0 return send_file($css_file);
154 }
155 else {
156 0 return send_error("Not found", 404);
157 }
158 }
159 2 };
160  
161 2 register_plugin;
162  
163 2 1;
164  
165 __END__
↑ Summary