lib/CSS/Watcher.pm

filter: Statementstmt 87.1% 169 / 194 Branchbran 76.4% 52 / 68 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 88.8% 24 / 27 Podpod 0.0% 0 / 6 totaltotal 82.8% 251 / 303 CC 43 SCAR 20.2
CC
43
Cov
84%
_parse_less_and_imports
30.0
update
11.1
__ANON__
9.0
CRAP 7.5
« blib/lib/CSS/Watcher/Monitor.pm ↑ Summary lib/CSS/Watcher/Monitor.pm »
1 package CSS::Watcher;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Slurp qw/read_file write_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use CSS::Watcher::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use CSS::Watcher::ParserLess;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use CSS::Watcher::Monitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '0.4.7';
20  
21 1 ▶use constant DEFAULT_HTML_STUFF_DIR => '~/.emacs.d/ac-html-csswatcher/completion/';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub new {
24 5 ▶ my $class= shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
25 5 my $options = shift;
26  
27 return bless ({
28 5 ▶ outputdir => $options->{'outputdir'} // DEFAULT_HTML_STUFF_DIR,
Condition100%2/2
$options->{"outputdir"} // "~/.emacs.d/ac-html-csswatcher/completion/"
l!l
41
MC/DC100%1/1
$options->{"outputdir"} // "~/.emacs.d/ac-html-csswatcher/completion/"
$options->{"outputdir"}
Truth table100%2/2
A
A= $options->{"outputdir"}
Aresult
11
00
29 parser_css => CSS::Watcher::Parser->new(),
30 parser_less => CSS::Watcher::ParserLess->new(),
31 }, $class);
32 }
33  
34 sub update {
35 7 ▶ my $self = shift;
Subroutine update: called CC=11 SCAR=24.0
Pod undocumented
36 7 my $obj = shift;
37  
38 # check what is the monobj. file? dir?
39 7 ▶ if (-f $obj || -d $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $obj or -d $obj) 4 3 7
Condition66%2/3
-f $obj or -d $obj
l!l&&r!l&&!r
043
MC/DC50%1/2
-f $obj or -d $obj
-f $obj -d $obj
Truth table66%2/3
A or B
A= -f $objB= -d $obj
ABresult
1X1
011
000
40 4 my $proj_dir = $self->get_project_dir ($obj);
41 4 ▶ return unless (defined $proj_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proj_dir 0 4 4
42  
43 4 INFO "Update project: $proj_dir";
44  
45 4 my $prj = $self->_get_project ($proj_dir);
46 4 $prj->{parsed_files} = []; # clean old parsed file list
47  
48 4 my $changes = 0;
49  
50 4 my (@ignore, @allow, @skip_dirs);
51 4 my $cfg = path($proj_dir)->child('.csswatcher');
52  
53 # clear project cache if .csswatcher changed
54 4 ▶ if ($prj->{monitor}->is_changed($cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prj->{"monitor"}->is_changed($cfg)) 3 1 4
55 3 INFO ('.csswatcher changed, resetting');
56 3 $prj->{monitor}->make_dirty();
57 3 delete $prj->{parsed};
58 3 $changes++;
59 }
60  
61 4 ▶ if (-f $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cfg) 3 1 4
62 1 ▶ if (open (CFG, '<:encoding(UTF-8)', $cfg)) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (open CFG, "<:encoding(UTF-8)", $cfg) 3 0 3
63 3 ▶ while (<CFG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CFG)) 10 3 13
64 10 chomp;
65 10 ▶ (m/^\s*ignore:\s*(.*?)\s*$/i) ? push @ignore, $1 :
Branch: 3 branches
BranchTrueFalseTotal
/^\s*ignore:\s*(.*?)\s*$/i ? : 4 6 10
/^\s*skip:\s*(.*?)\s*$/i ? : 0 6 6
/^\s*use:\s*(.*?)\s*$/i ? : 4 2 6
66 (m/^\s*skip:\s*(.*?)\s*$/i) ? push @skip_dirs, $1 :
67 (m/^\s*use:\s*(.*?)\s*$/i) ? push @allow, $1 : 1;
68 }
69 3 close CFG;
70 }
71 }
72  
73 # scan new or changed files, cache them
74 $prj->{monitor}->scan (
75 sub {
76 13 ▶ my $file = shift;
Subroutine __ANON__: called CC=9 SCAR=22.0
77  
78 13 ▶ return if (any {$_ eq $file} @{$prj->{parsed_files}});
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $file; } , @{$$prj{"parsed_files"};}) 0 13 13
79  
80 13 my $allow = 0;
81 13 foreach (@allow) {
82 18 ▶ if ($file =~ m/$_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$_/) 4 14 18
83 4 $allow = 1;
84 4 last;
85 }
86 }
87 13 ▶ unless ($allow) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allow) 9 4 13
88 9 foreach (@ignore) {
89 10 ▶ if ($file =~ m/$_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$_/) 2 8 10
90 2 INFO " Ignored $file =~\"$_\"";
91 2 return;
92 }
93 }
94 }
95 11 ▶ ($file =~ m/\.css$/) ? $changes += 1 && $self->_parse_css ($prj, $file) :
Branch: 2 branches
BranchTrueFalseTotal
$file =~ /\.css$/ ? : 4 7 11
$file =~ /\.less$/ ? : 0 7 7
96 ($file =~ m/\.less$/) ? $changes += 1 && $self->_parse_less_and_imports ($prj, $file) : 1;
97  
98 4 }, \@skip_dirs);
99 4 INFO "Update done.";
100 4 return ($changes, $proj_dir);
101 }
102 3 return;
103 }
104  
105 sub _parse_css {
106 4 ▶ my ($self, $project, $file) = @_;
Subroutine _parse_css: called CC=1 SCAR=0.0
107  
108 4 INFO " (Re)parse css: $file";
109 4 my $data = read_file ($file);
110 4 my ($classes, $ids) = $self->{parser_css}->parse_css ($data);
111 4 $project->{parsed}{$file} = {CLASSES => $classes,
112 IDS => $ids};
113 4 push @{$project->{parsed_files}}, $file;
114 4 return 1;
115 }
116  
117 sub _parse_less {
118 0 ▶ my ($self, $project, $file) = @_;
Subroutine _parse_less: not called CC=1 SCAR=6.9
119  
120 0 INFO " (Re)parse less: $file";
121 0 my ($classes, $ids, $requiries) = $self->{parser_less}->parse_less ($file);
122 0 $project->{parsed}{$file} = {CLASSES => $classes,
123 IDS => $ids};
124 0 push @{$project->{parsed_files}}, $file;
125  
126 # normilize path of requiried files, they may have .././
127 # eval {
128 # $project->{imports_less}{$file} =
129 # [ map {path($file)->parent->child($_)->realpath()->stringify} @{$requiries} ];
130 # };
131 # if ($@) {
132 # WARN $@;
133 # }
134 $project->{imports_less}{$file} =
135 0 [ map {path($file)->parent->child($_)->realpath()->stringify} @{$requiries} ];
136 0 return 1;
137 }
138  
139 sub _parse_less_and_imports {
140 0 ▶ my ($self, $project, $file) = @_;
Subroutine _parse_less_and_imports: not called CC=5 SCAR=34.0
141  
142 0 my $parsed_files = 1; # 1, cause we parse $file for sure., ++ if dependencies parsed too
143  
144 0 $self->_parse_less ($project, $file);
145  
146 0 ▶ while (my ($less_fname, $imports) = each %{$project->{imports_less}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$project{"imports_less"};}) 0 0 0
147 0 foreach (@{$imports}) {
148 0 ▶ if ($file eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $_) 0 0 0
149 0 ▶ next if (any {$_ eq $less_fname} @{$project->{parsed_files}});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $less_fname; } , @{$$project{"parsed_files"};}) 0 0 0
150 0 INFO sprintf " %s required by %s, parse them too.", path($file)->basename, path($less_fname)->basename;
151 0 $self->_parse_less($project, $less_fname);
152 0 $parsed_files++;
153 }
154 }
155 }
156 0 return $parsed_files;
157 }
158  
159 sub project_stuff {
160 4 ▶ my $self = shift;
Subroutine project_stuff: called CC=7 SCAR=19.5
Pod undocumented
161 4 my $proj_dir = shift;
162  
163 4 my $prj = $self->_get_project ($proj_dir);
164  
165 # build unique tag - class,id
166 4 my (%classes, %ids);
167 4 my ($total_classes, $total_ids) = (0, 0);
168 4 ▶ while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$prj{"parsed"};}) 6 4 10
169 6 ▶ while ( my ( $tag, $classes ) = each %{$completions->{CLASSES}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$completions{"CLASSES"};}) 9 6 15
170 9 foreach (keys %{$classes}) {
171 9 $classes{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n';
172 9 $total_classes++;
173 }
174 }
175 }
176 4 ▶ while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$prj{"parsed"};}) 6 4 10
177 6 ▶ while ( my ( $tag, $ids ) = each %{$completions->{IDS}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$completions{"IDS"};}) 3 6 9
178 3 foreach (keys %{$ids}) {
179 3 $ids{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n';
180 3 $total_ids++;
181 }
182 }
183 }
184 4 INFO "Total for $proj_dir:";
185 4 INFO " Classes: $total_classes, ids: $total_ids";
186  
187 4 return (\%classes, \%ids);
188 }
189  
190 # clean old output html complete stuff and build new
191 sub build_ac_html_stuff {
192 3 ▶ my $self = shift;
Subroutine build_ac_html_stuff: called CC=3 SCAR=11.0
Pod undocumented
193 3 my $proj_dir = shift;
194  
195 3 my ($classes, $ids) = $self->project_stuff ($proj_dir);
196  
197 3 my $ac_html_stuff_dir = path ($self->{outputdir})->child (md5_hex( ''.$proj_dir ));
198 3 my $attrib_dir = path ($ac_html_stuff_dir)->child ('html-attributes-complete');
199  
200 3 $attrib_dir->remove_tree({safe => 0});
201 3 $attrib_dir->mkpath;
202  
203 3 ▶ while ( my ( $tag, $class ) = each %{$classes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$classes;}) 4 3 7
204 4 my $fname = File::Spec->catfile ($attrib_dir, $tag . '-class');
205 4 DEBUG "Write $fname";
206 write_file ($fname, join "\n", map {
207 4 $_ . ' ' . $class->{$_} } sort keys %{$class});
208 }
209 3 ▶ while ( my ( $tag, $id ) = each %${ids} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ids) 2 3 5
210 2 my $fname = File::Spec->catfile ($attrib_dir, $tag . '-id');
211 2 DEBUG "Write $fname";
212 write_file ($fname, join "\n", map {
213 2 $_ . ' ' . $id->{$_} } sort keys %{$id});
214 }
215 3 DEBUG "Done writing. Reply to client.";
216 3 return $ac_html_stuff_dir;
217 }
218  
219 sub get_html_stuff {
220 4 ▶ my $self = shift;
Subroutine get_html_stuff: called CC=3 SCAR=11.0
Pod undocumented
221 4 my $obj = shift;
222  
223 4 my ($changes, $project_dir) = $self->update ($obj);
224 4 ▶ return unless defined $changes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $changes 1 3 4
225  
226 3 my $prj = $self->_get_project ($project_dir);
227  
228 3 my $ac_html_stuff_dir;
229  
230 3 ▶ if ($changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes) { } 2 1 3
231 2 $ac_html_stuff_dir = $self->build_ac_html_stuff ($project_dir);
232 2 $prj->{'ac_html_stuff'} = $ac_html_stuff_dir;
233 } else {
234 1 $ac_html_stuff_dir = $prj->{'ac_html_stuff'};
235 }
236 3 return ($project_dir, $ac_html_stuff_dir);
237 }
238  
239 sub _get_project {
240 11 ▶ my $self = shift;
Subroutine _get_project: called CC=3 SCAR=11.0
241 11 my $dir = shift;
242 11 ▶ return unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 11 11
243  
244 11 ▶ unless (exists $self->{PROJECTS}{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"PROJECTS"}{$dir}) 3 8 11
245 3 $self->{PROJECTS}{$dir} =
246 bless ( {monitor => CSS::Watcher::Monitor->new({dir => $dir})}, 'CSS::Watcher::Project' );
247 }
248 11 return $self->{PROJECTS}{$dir};
249 }
250  
251 # Lookup for project dir similar to projectile.el
252 sub get_project_dir {
253 11 ▶ my $self = shift;
Subroutine get_project_dir: called CC=8 SCAR=21.3
Pod undocumented
254 11 my $obj = shift;
255
256 11 ▶ my $pdir = ! defined ($obj) ? undef:
Branch: 3 branches
BranchTrueFalseTotal
!defined($obj) ? : 0 11 11
-f $obj ? : 1 10 11
-d $obj ? : 10 0 10
257 (-f $obj) ? path ($obj)->parent :
258 (-d $obj) ? $obj : undef;
259 11 ▶ return unless (defined $pdir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdir 0 11 11
260  
261 11 $pdir = path( $pdir );
262  
263 11 foreach (qw/.projectile .csswatcher .git .hg .fslckout .bzr _darcs/) {
264 48 ▶ if (-e ($pdir->child( $_ ))) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pdir->child($_)) 6 42 48
265 6 return $pdir;
266 }
267 }
268 5 ▶ return if ($pdir->is_rootdir());
Branch: 1 branch
BranchTrueFalseTotal
if $pdir->is_rootdir 0 5 5
269 #parent dir
270 5 return $self->get_project_dir ($pdir->parent);
271 }
272  
273 1 1;
274  
275 __END__
276  
277 =head1 NAME
278  
279 CSS::Watcher - class, id completion for ac-html.
280  
281 =head1 SYNOPSIS
282  
283 use CSS::Watcher;
284 my $cw = CSS::Watcher->new ();
285  
286 my ($project_dir, $ac_html_stuff_dir) = $cw->get_html_stuff ('~/work/css/');
287 if (!defined $ac_html_stuff_dir) {
288 print "oops, there no css files "
289 }
290  
291 =head1 DESCRIPTION
292  
293 Watch for changes in css less files, parse them and populate ac-html completion.
294 Supported imports in less, reparse if imported file changed.
295  
296 =head1 AUTHOR
297  
298 Olexandr Sydorchuk (olexandr.syd@gmail.com)
299  
300 =head1 COPYRIGHT AND LICENSE
301  
302 Copyright (C) 2014 by Olexandr Sydorchuk
303  
304 This program is free software; you can redistribute it and/or modify
305 it under the same terms as Perl itself, either Perl version 5.8.2 or,
306 at your option, any later version of Perl 5 you may have available.
307  
308 =head1 BUGS
309  
310 None reported... yet.
311  
312 =cut
« blib/lib/CSS/Watcher/Monitor.pm ↑ Summary lib/CSS/Watcher/Monitor.pm »