blib/lib/CSS/LESS.pm

filter: Statementstmt 79.6% 90 / 113 Branchbran 60.0% 24 / 40 Conditioncond 66.6% 16 / 24 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 73.3% 151 / 206 CC 32 SCAR 23.6
CC
32
Cov
73%
_exec_lessc
22.2
_generate_cmd_lessc
13.7
compile
8.0
CRAP 10.5
↑ Summary
1 package CSS::LESS;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use File::Temp qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use version; our $VERSION = qv('0.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our @LESSC_PARAMS = qw/ include_paths compress strict_imports
12 relative_urls rootpath line_numbers version /;
13 5 our %DEFAULT_PARAMS = (
14 # Parameters for module
15 dont_die => 0,
16 dry_run => 0,
17 path_lessc_bin => 'lessc',
18 path_tmp => undef,
19 # Parameters for lessc
20 include_paths => undef, # Array
21 compress => 'false', # Bool
22 strict_imports => 'false', # Bool
23 relative_urls => 'false', # Bool
24 rootpath => undef, # String (URL)
25 line_numbers => undef, # String ('comments', 'mediaquery', 'both', or undef)
26 );
27  
28 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
29 8 my ($class, %params) = @_;
30 8 my $s = bless({}, $class);
31  
32 # Proceess for parameters of constructor
33 8 foreach ( keys %DEFAULT_PARAMS ) {
34 80 ▶ if(defined $params{$_}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{$_}) { } 11 69 80
35 11 $s->{$_} = $params{$_};
36 } else {
37 69 $s->{$_} = $DEFAULT_PARAMS{$_};
38 }
39 }
40  
41 8 $s->{last_error} = undef;
42 8 $s->{is_lessc_installed} = undef;
43  
44 8 return $s;
45 }
46  
47 # Ccompile a less style-sheet (return: Compiled CSS style-sheet)
48 7 ▶sub compile {
Subroutine compile: called CC=8 SCAR=20.8
Pod documented
49 7 my ($s, $buf, %params) = @_;
50  
51 7 ▶ unless (defined $s->{is_lessc_installed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->{"is_lessc_installed"}) 7 0 7
52 7 ▶ if($s->is_lessc_installed() == 0 && $s->{dont_die} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->is_lessc_installed == 0 and $s->{"dont_die"} == 0) 1 6 7
Condition66%2/3
$s->is_lessc_installed == 0 and $s->{"dont_die"} == 0
!ll&&!rl&&r
601
MC/DC50%1/2
$s->is_lessc_installed == 0 and $s->{"dont_die"} == 0
$s->is_lessc_installed == 0 $s->{"dont_die"} == 0
Truth table66%2/3
A and B
A= $s->is_lessc_installed == 0B= $s->{"dont_die"} == 0
ABresult
0X0
100
111
53 1 die('lessc is not installed');
54 }
55 }
56  
57 6 ▶ unless (%params) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%params) 3 3 6
58 3 %params = ();
59 }
60  
61 # Process for parameters (Set property in instance)
62 6 foreach ( keys %DEFAULT_PARAMS ) {
63 60 ▶ if(!defined $params{$_} && defined $s->{$_}){ # Not defined on params AND defined on member of $self
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $params{$_} and defined $s->{$_}) 35 25 60
Condition100%3/3
not defined $params{$_} and defined $s->{$_}
!ll&&!rl&&r
32235
MC/DC100%2/2
not defined $params{$_} and defined $s->{$_}
not defined $params{$_} defined $s->{$_}
Truth table100%3/3
not(A) and B
A= not defined $params{$_}B= defined $s->{$_}
ABresult
0X0
100
111
64 35 $params{$_} = $s->{$_};
65 }
66 }
67 6 $params{content} = $buf;
68  
69 # Compile less to css
70 6 return $s->_exec_lessc(%params);
71 }
72  
73 # Get last error
74 0 ▶sub last_error {
Subroutine last_error: not called CC=1 SCAR=6.9
Pod documented
75 0 my $s = shift;
76 0 return $s->{last_error};
77 }
78  
79 # Check for lessc has installed
80 11 ▶sub is_lessc_installed {
Subroutine is_lessc_installed: called CC=5 SCAR=20.4
Pod documented
81 11 my $s = shift;
82  
83 11 ▶ if($s->{dry_run}){ # Dry run
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"dry_run"}) 7 4 11
84 7 return 1;
85 }
86  
87 4 my $lessc_ver;
88 4 eval {
89 4 $lessc_ver = $s->_exec_lessc(version => undef);
90 4 ▶ }; if($@) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 0 4
91 0 ▶ if(defined $lessc_ver && $lessc_ver =~ /^lessc .*(LESS Compiler).*/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lessc_ver and $lessc_ver =~ /^lessc .*(LESS Compiler).*/i) 0 0 0
Condition0%0/3
defined $lessc_ver and $lessc_ver =~ /^lessc .*(LESS Compiler).*/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lessc_ver and $lessc_ver =~ /^lessc .*(LESS Compiler).*/i
defined $lessc_ver $lessc_ver =~ /^lessc .*(LESS Compiler).*/i
Truth table0%0/3
A and B
A= defined $lessc_verB= $lessc_ver =~ /^lessc .*(LESS Compiler).*/i
ABresult
0X0
100
111
92 0 $s->{is_lessc_installed} = 1;
93 0 return 1;
94 }
95 0 return 0;
96 }
97  
98 # Execute a command with lessc
99 10 ▶sub _exec_lessc {
Subroutine _exec_lessc: called CC=7 SCAR=31.0
100 10 my ($s, %options) = @_;
101  
102 # Prepare a command
103 10 my ($cmd_args_ref, $path_tmpfile) = $s->_generate_cmd_lessc(%options);
104 10 my @cmd_args = @{$cmd_args_ref};
105  
106 # Execute a command
107  
108 10 ▶ if($s->{dry_run}){ # Dry run
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"dry_run"}) 6 4 10
109 6 $" = ' ';
110 6 return "@cmd_args"; # return generated command
111 }
112  
113 4 my ($fh_in, $fh_out, $fh_err);
114 #open $fh, '-|', @cmd_args, '2>&1' or die('Can not open a pipe to:'. $s->{path_lessc_bin});
115 4 my $pid = IPC::Open3::open3($fh_in, $fh_out, 0, @cmd_args);
116 0 my ($ret);
117 0 ▶ while (my $l = <$fh_out>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh_out)) 0 0 0
118 0 $ret .= $l;
119 }
120 0 waitpid($pid, 0);
121  
122 # Error process
123 0 ▶ if($? != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
124 0 $s->{last_error} = $ret;
125 0 ▶ unless($s->{dont_die}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->{"dont_die"}) 0 0 0
126 0 ▶ if(defined $ret){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) { } 0 0 0
127 0 die ('Compile error: '. $ret);
128 } else {
129 0 die ('Compile error: Unknown');
130 }
131 }
132 }
133 # Delete tmp file
134 0 ▶ if(defined $path_tmpfile){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path_tmpfile) 0 0 0
135 0 unlink($path_tmpfile);
136 }
137  
138 0 return $ret;
139 }
140  
141 # Generate a command for lessc (Return: \@args, $path of temp-file)
142 10 ▶sub _generate_cmd_lessc {
Subroutine _generate_cmd_lessc: called CC=13 SCAR=26.2
143 10 my ($s, %options) = @_;
144 10 my @cmd_args = ();
145  
146 # Execute path
147 10 push(@cmd_args, $s->{path_lessc_bin});
148  
149 # Process for content
150 10 my $path_tmpfile;
151 10 ▶ if(defined $options{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"content"}) 6 4 10
152 6 my $content = $options{content};
153 6 delete $options{content};
154  
155 6 my $tempfh;
156 6 ($tempfh, $path_tmpfile) = File::Temp::tempfile(DIR => $s->{path_tmp});
157 6 print $tempfh $content;
158 6 close($tempfh);
159  
160 6 push(@cmd_args, $path_tmpfile);
161 }
162  
163 # Process for include paths
164 10 ▶ if(defined $options{include_paths}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"include_paths"}) 2 8 10
165 2 ▶ if(@{$options{include_paths}} <= 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$options{'include_paths'};} <= 1) { } 0 2 2
166 0 push(@cmd_args, '--include-path='.$options{include_paths}->[0]);
167 } else {
168 2 my $paths = '--include-path=';
169 {
170 2 local $" = ':';
171 2 $paths .= "@{$options{include_paths}}";
172 }
173 2 $paths .= '';
174 2 push(@cmd_args, $paths);
175 }
176 2 delete $options{include_paths};
177 }
178  
179 # Process for other parameters
180 10 foreach my $key (@LESSC_PARAMS) {
181 70 ▶ if(defined $options{$key} && (!defined $DEFAULT_PARAMS{$key} || $DEFAULT_PARAMS{$key} ne 'false')) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $options{$key} and !defined($DEFAULT_PARAMS{$key}) || $DEFAULT_PARAMS{$key} ne 'false') { } 0 70 70
elsif (defined $options{$key} and $options{$key} eq 'false' || $options{$key} == '0' and $DEFAULT_PARAMS{$key} eq 'false') { } 15 55 70
elsif (exists $options{$key}) { } 7 48 55
Condition50%3/6
defined $options{$key} and !defined($DEFAULT_PARAMS{$key}) || $DEFAULT_PARAMS{$key} ne "false"
!ll&&!rl&&r
52180
not defined $DEFAULT_PARAMS{$key} or $DEFAULT_PARAMS{$key} ne "false"
l!l&&r!l&&!r
0018
MC/DC0%0/3
defined $options{$key} and !defined($DEFAULT_PARAMS{$key}) || $DEFAULT_PARAMS{$key} ne "false"
defined $options{$key} not defined $DEFAULT_PARAMS{$key} $DEFAULT_PARAMS{$key} ne "false"
Truth table50%2/4
A and not(B) or C
A= defined $options{$key}B= not defined $DEFAULT_PARAMS{$key}C= $DEFAULT_PARAMS{$key} ne "false"
ABCresult
0XX0
1000
11X1
1011
182 0 my $arg_name = $key;
183 0 $arg_name =~ s/\_/\-/g;
184 0 push(@cmd_args, "--".$arg_name."=".$options{$key});
185 ▶ } elsif(defined $options{$key} && ($options{$key} eq 'false' || $options{$key} == '0') && $DEFAULT_PARAMS{$key} eq 'false') {
Condition88%8/9
defined $options{$key} and $options{$key} eq "false" || $options{$key} == 0 and $DEFAULT_PARAMS{$key} eq "false"
!ll&&!rl&&r
55015
defined $options{$key} and $options{$key} eq "false" || $options{$key} == 0
!ll&&!rl&&r
52315
$options{$key} eq "false" or $options{$key} == 0
l!l&&r!l&&!r
1413
MC/DC75%3/4
defined $options{$key} and $options{$key} eq "false" || $options{$key} == 0 and $DEFAULT_PARAMS{$key} eq "false"
defined $options{$key} $options{$key} eq "false" $options{$key} == 0 $DEFAULT_PARAMS{$key} eq "false"
Truth table66%4/6
A and B or C and D
A= defined $options{$key}B= $options{$key} eq "false"C= $options{$key} == 0D= $DEFAULT_PARAMS{$key} eq "false"
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
186 # Do not anything
187 } elsif(exists $options{$key}){
188 7 my $arg_name = $key;
189 7 $arg_name =~ s/\_/\-/g;
190 7 push(@cmd_args, "--".$arg_name);
191 }
192 }
193  
194 10 push(@cmd_args, '--verbose');
195 10 push(@cmd_args, '--no-color');
196 # Return a args (with command path) and a path of temp-file
197 10 return (\@cmd_args, $path_tmpfile);
198 }
199  
200 5 1;
201 __END__
↑ Summary