lib/Dancer/Plugin/Preprocess/Sass.pm

filter: Statementstmt 70.3% 64 / 91 Branchbran 33.3% 8 / 24 Conditioncond 21.4% 3 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 58.5% 86 / 147 CC 14 SCAR 31.6
CC
14
Cov
58%
__ANON__
60.7
__ANON__
7.8
_process_sass_file
3.1
CRAP 23.5
↑ Summary blib/lib/Dancer/Plugin/Preprocess/Sass.pm »
1 package Dancer::Plugin::Preprocess::Sass;
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 Sass/SCSS files
7  
8 2 our $VERSION = '0.031'; # 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 Text::Sass;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 my $settings = plugin_setting;
17 2 my $paths;
18  
19 2 my $sass = Text::Sass->new;
20 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
21  
22 2 ▶if (exists $settings->{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{'paths'}) { } 1 1 2
23 1 $paths = $settings->{paths};
24 }
25 else {
26 1 $paths = ['css'];
27 }
28  
29 # Translate URL paths to filesystem paths
30 2 my @fs_paths = map { catfile(split('/'), "") } @$paths;
31  
32 2 ▶if ($settings->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"save"}) 0 2 2
33 # Check if the directories are writable
34 0 for my $path (@fs_paths) {
35 0 my $full_path = catfile($public_dir, $path);
36 0 ▶ if (!-w $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $full_path) 0 0 0
37 0 warning __PACKAGE__ . ": Can't write to $full_path";
38 }
39 }
40 }
41  
42 # Make a regular expression to match URL paths
43 my $paths_re = join '|', map {
44 2 my $s = $_;
45 3 $s =~ s{^[^/]}{/$&}; # Add leading slash, if missing
46 3 $s =~ s{/$}{}; # Remove trailing slash
47 3 quotemeta $s;
48 } reverse sort @$paths;
49  
50 2 ▶sub _process_sass_file {
Subroutine _process_sass_file: called CC=3 SCAR=11.4
51 2 my $sass_file = shift;
52 2 my $method;
53
54 2 ▶ if ($sass_file =~ /\.sass$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sass_file =~ /\.sass$/) { } 0 2 2
elsif ($sass_file =~ /\.scss$/) { } 2 0 2
55 0 $method = 'sass2css';
56 }
57 elsif ($sass_file =~ /\.scss$/) {
58 2 $method = 'scss2css';
59 }
60 else {
61 0 return;
62 }
63
64 2 open (my $f, '<', $sass_file);
65 2 my $contents;
66 {
67 2 local $/;
68 2 $contents = <$f>;
69 }
70 2 close($f);
71
72 2 return $sass->$method($contents);
73 }
74  
75 2 ▶hook before_file_render => sub {
Subroutine __ANON__: called CC=8 SCAR=41.1
76 # If saving is not enabled, then there's nothing for us to do (since we
77 # won't be able to save the generated CSS rules in a file, duh)
78 2 ▶ return if !$settings->{save};
Branch: 1 branch
BranchTrueFalseTotal
unless $settings->{"save"} 2 0 2
79
80 0 my $path = abs_path(shift);
81
82 # Build a regular expression to match filesystem paths
83 0 my $fs_paths_re = join '|', map { quotemeta } @fs_paths;
84
85 0 my $path_re = '^' . quotemeta(catfile($public_dir, "")) .
86 '(?:' . $fs_paths_re . ')';
87  
88 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
89 0 (my $filename = $path) =~ s/\.css$//;
90 0 my $input_file;
91
92 0 ▶ if ((-f ($input_file = $filename . '.sass') ||
Branch: 1 branch
BranchTrueFalseTotal
if (-f ($input_file = $filename . '.sass') || -f ($input_file = $filename . '.scss') and (stat $path)[9] < (stat $input_file)[9]) 0 0 0
Condition0%0/6
-f ($input_file = $filename . '.sass') || -f ($input_file = $filename . '.scss') and (stat $path)[9] < (stat $input_file)[9]
!ll&&!rl&&r
000
-f ($input_file = $filename . '.sass') or -f ($input_file = $filename . '.scss')
l!l&&r!l&&!r
000
MC/DC0%0/3
-f ($input_file = $filename . '.sass') || -f ($input_file = $filename . '.scss') and (stat $path)[9] < (stat $input_file)[9]
-f ($input_file = $filename . '.sass') -f ($input_file = $filename . '.scss') (stat $path)[9] < (stat $input_file)[9]
Truth table0%0/5
A or B and C
A= -f ($input_file = $filename . '.sass')B= -f ($input_file = $filename . '.scss')C= (stat $path)[9] < (stat $input_file)[9]
ABCresult
00X0
1X00
0100
1X11
0111
93 -f ($input_file = $filename . '.scss')) &&
94 (stat($path))[9] < (stat($input_file))[9])
95 {
96 # There is a Sass/Scss file newer than the CSS file
97 0 my $css = _process_sass_file($input_file);
98
99 0 ▶ if (defined $css) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $css) 0 0 0
100 # Save the generated CSS data
101 0 open(my $f, '>', $path);
102 0 print {$f} $css;
103 0 close($f);
104 }
105 }
106 }
107 2 };
108  
109 2 ▶get qr{($paths_re)/([^/]*\.css)} => sub {
Subroutine __ANON__: called CC=5 SCAR=20.5
110 2 my ($path, $css_file) = splat;
111  
112 # Prepend the path to the CSS file name
113 2 $path = catfile(split('/', $path));
114 2 $css_file = catfile($path, $css_file);
115
116 # Prepend public directory location
117 2 my $css_file_abs = catfile($public_dir, $css_file);
118
119 2 (my $filename = $css_file_abs) =~ s/\.css$//;
120 2 my $input_file;
121  
122 2 ▶ if (-f ($input_file = $filename . '.sass') ||
Branch: 1 branch
BranchTrueFalseTotal
if (-f ($input_file = $filename . '.sass') or -f ($input_file = $filename . '.scss')) { } 2 0 2
Condition33%1/3
-f ($input_file = $filename . '.sass') or -f ($input_file = $filename . '.scss')
l!l&&r!l&&!r
020
MC/DC0%0/2
-f ($input_file = $filename . '.sass') or -f ($input_file = $filename . '.scss')
-f ($input_file = $filename . '.sass') -f ($input_file = $filename . '.scss')
Truth table33%1/3
A or B
A= -f ($input_file = $filename . '.sass')B= -f ($input_file = $filename . '.scss')
ABresult
1X1
011
000
123 -f ($input_file = $filename . '.scss'))
124 {
125 # Sass/Scss file exists
126 2 my $css = _process_sass_file($input_file);
127
128 2 ▶ if ($settings->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"save"}) 0 2 2
129 # Saving enabled -- save the generated CSS as the requested file
130 0 open(my $f, '>', $css_file_abs);
131 0 print {$f} $css;
132 0 close($f);
133 }
134
135 2 header 'content-type' => 'text/css';
136 2 return $css;
137 }
138 else {
139 # Check if the CSS file exists. Probably not, because if it does exist,
140 # then Dancer should have already served it as a static file, and we
141 # shouldn't even end up in this route handler. Still, we'll do this
142 # check in case we're in some wacky parallel universe where route
143 # handlers are run before static files.
144 0 ▶ if (-f $css_file_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $css_file_abs) { } 0 0 0
145 0 return send_file($css_file);
146 }
147 else {
148 0 return send_error("Not found", 404);
149 }
150 }
151 2 };
152  
153 2 register_plugin;
154  
155 2 1; # End of Dancer::Plugin::Preprocess::Sass
156  
157 __END__
↑ Summary blib/lib/Dancer/Plugin/Preprocess/Sass.pm »