Watcher.pm

filter: Statementstmt 85.1% 166 / 195 Branchbran 75.7% 53 / 70 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 6 totaltotal 81.3% 248 / 305 CC 44 SCAR 22.0
CC
44
Cov
83%
_parse_less_and_imports
42.0
update
11.0
__ANON__
9.0
CRAP 9.0
« Watcher/Parser.pm ↑ Summary 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.5.0';
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 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
24 5 my $class= shift;
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 7 ▶sub update {
Subroutine update: called CC=11 SCAR=24.0
Pod undocumented
35 7 my $self = shift;
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) 5 2 7
Condition66%2/3
-f $obj or -d $obj
l!l&&r!l&&!r
052
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 5 my $proj_dir = $self->get_project_dir ($obj);
41 5 ▶ return unless (defined $proj_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proj_dir 0 5 5
42  
43 5 INFO "Update project: $proj_dir";
44  
45 5 my $prj = $self->_get_project ($proj_dir);
46 5 $prj->{parsed_files} = []; # clean old parsed file list
47  
48 5 my $changes = 0;
49  
50 5 my (@ignore, @allow, @skip_dirs);
51 5 my $cfg = path($proj_dir)->child('.csswatcher');
52  
53 # clear project cache if .csswatcher changed
54 5 ▶ if ($prj->{monitor}->is_changed($cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prj->{"monitor"}->is_changed($cfg)) 4 1 5
55 4 INFO ('.csswatcher changed, resetting');
56 4 $prj->{monitor}->make_dirty();
57 4 delete $prj->{parsed};
58 4 $changes++;
59 }
60  
61 5 ▶ if (-f $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cfg) 4 1 5
62 4 ▶ if (open (CFG, '<:encoding(UTF-8)', $cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open CFG, "<:encoding(UTF-8)", $cfg) 4 0 4
63 4 ▶ while (<CFG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CFG)) 11 4 15
64 11 chomp;
65 11 ▶ (m/^\s*ignore:\s*(.*?)\s*$/i) ? push @ignore, $1 :
Branch: 3 branches
BranchTrueFalseTotal
/^\s*ignore:\s*(.*?)\s*$/i ? : 4 7 11
/^\s*skip:\s*(.*?)\s*$/i ? : 1 6 7
/^\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 4 close CFG;
70 }
71 }
72  
73 # scan new or changed files, cache them
74 $prj->{monitor}->scan (
75 16 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
76 16 my $file = shift;
77  
78 16 ▶ 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 16 16
79  
80 16 my $allow = 0;
81 16 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 16 ▶ unless ($allow) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allow) 12 4 16
88 12 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 14 ▶ ($file =~ m/\.css$/) ? $changes += 1 && $self->_parse_css ($prj, $file) :
Branch: 2 branches
BranchTrueFalseTotal
$file =~ /\.css$/ ? : 6 8 14
$file =~ /\.less$/ ? : 0 8 8
96 ($file =~ m/\.less$/) ? $changes += 1 && $self->_parse_less_and_imports ($prj, $file) : 1;
97  
98 5 }, \@skip_dirs);
99 5 INFO "Update done.";
100 5 return ($changes, $proj_dir);
101 }
102 2 return;
103 }
104  
105 6 ▶sub _parse_css {
Subroutine _parse_css: called CC=1 SCAR=0.0
106 6 my ($self, $project, $file) = @_;
107  
108 6 INFO " (Re)parse css: $file";
109 6 my $data = read_file ($file);
110 6 my ($classes, $ids) = $self->{parser_css}->parse_css ($data);
111 6 $project->{parsed}{$file} = {CLASSES => $classes,
112 IDS => $ids};
113 6 push @{$project->{parsed_files}}, $file;
114 6 return 1;
115 }
116  
117 0 ▶sub _parse_less {
Subroutine _parse_less: not called CC=1 SCAR=6.9
118 0 my ($self, $project, $file) = @_;
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 0 ▶sub _parse_less_and_imports {
Subroutine _parse_less_and_imports: not called CC=6 SCAR=37.4
140 0 my ($self, $project, $file) = @_;
141  
142 0 my $parsed_files = 1; # 1, cause we parse $file for sure., ++ if dependencies parsed too
143  
144 eval {
145 0 $self->_parse_less ($project, $file);
146  
147 0 ▶ while (my ($less_fname, $imports) = each %{$project->{imports_less}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$project{"imports_less"};}) 0 0 0
148 0 foreach (@{$imports}) {
149 0 ▶ if ($file eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $_) 0 0 0
150 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
151 0 INFO sprintf " %s required by %s, parse them too.", path($file)->basename, path($less_fname)->basename;
152 0 $self->_parse_less($project, $less_fname);
153 0 $parsed_files++;
154 }
155 }
156 }
157 0 1;
158 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_parse_less($project, $file); while (() = each %{$$project{"imports_less"};}) { do { foreach $_ (@{$imports;}) { if ($file eq $_) { next if &any(sub { $_ eq $less_fname; } , @{$$project{"parsed_files"};}); INFO(sprintf(" %s required by %s, parse them too.", path($file)->basename, path($less_fname)->basename)); $self->_parse_less($project, $less_fname); ++$parsed_files; } ; } }; } ; 1 } } or do { ERROR(qq[Less parse fail: "$file", reason: "$@"]); return 1 } 0 0 0
159 0 ERROR qq{Less parse fail: "$file", reason: "$@"};
160 0 return 1;
161 };
162  
163 0 return $parsed_files;
164 }
165  
166 4 ▶sub project_stuff {
Subroutine project_stuff: called CC=7 SCAR=19.5
Pod undocumented
167 4 my $self = shift;
168 4 my $proj_dir = shift;
169  
170 4 my $prj = $self->_get_project ($proj_dir);
171  
172 # build unique tag - class,id
173 4 my (%classes, %ids);
174 4 my ($total_classes, $total_ids) = (0, 0);
175 4 ▶ while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$prj{"parsed"};}) 6 4 10
176 6 ▶ while ( my ( $tag, $classes ) = each %{$completions->{CLASSES}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$completions{"CLASSES"};}) 9 6 15
177 9 foreach (keys %{$classes}) {
178 9 $classes{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n';
179 9 $total_classes++;
180 }
181 }
182 }
183 4 ▶ while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$prj{"parsed"};}) 6 4 10
184 6 ▶ while ( my ( $tag, $ids ) = each %{$completions->{IDS}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$completions{"IDS"};}) 3 6 9
185 3 foreach (keys %{$ids}) {
186 3 $ids{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n';
187 3 $total_ids++;
188 }
189 }
190 }
191 4 INFO "Total for $proj_dir:";
192 4 INFO " Classes: $total_classes, ids: $total_ids";
193  
194 4 return (\%classes, \%ids);
195 }
196  
197 # clean old output html complete stuff and build new
198 3 ▶sub build_ac_html_stuff {
Subroutine build_ac_html_stuff: called CC=3 SCAR=11.0
Pod undocumented
199 3 my $self = shift;
200 3 my $proj_dir = shift;
201  
202 3 my ($classes, $ids) = $self->project_stuff ($proj_dir);
203  
204 3 my $ac_html_stuff_dir = path ($self->{outputdir})->child (md5_hex( ''.$proj_dir ));
205 3 my $attrib_dir = path ($ac_html_stuff_dir)->child ('html-attributes-complete');
206  
207 3 $attrib_dir->remove_tree({safe => 0});
208 3 $attrib_dir->mkpath;
209  
210 3 ▶ while ( my ( $tag, $class ) = each %{$classes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$classes;}) 4 3 7
211 4 my $fname = File::Spec->catfile ($attrib_dir, $tag . '-class');
212 4 DEBUG "Write $fname";
213 write_file ($fname, join "\n", map {
214 4 $_ . ' ' . $class->{$_} } sort keys %{$class});
215 }
216 3 ▶ while ( my ( $tag, $id ) = each %${ids} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ids) 2 3 5
217 2 my $fname = File::Spec->catfile ($attrib_dir, $tag . '-id');
218 2 DEBUG "Write $fname";
219 write_file ($fname, join "\n", map {
220 2 $_ . ' ' . $id->{$_} } sort keys %{$id});
221 }
222 3 DEBUG "Done writing. Reply to client.";
223 3 return $ac_html_stuff_dir;
224 }
225  
226 4 ▶sub get_html_stuff {
Subroutine get_html_stuff: called CC=3 SCAR=11.0
Pod undocumented
227 4 my $self = shift;
228 4 my $obj = shift;
229  
230 4 my ($changes, $project_dir) = $self->update ($obj);
231 4 ▶ return unless defined $changes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $changes 1 3 4
232  
233 3 my $prj = $self->_get_project ($project_dir);
234  
235 3 my $ac_html_stuff_dir;
236  
237 3 ▶ if ($changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes) { } 2 1 3
238 2 $ac_html_stuff_dir = $self->build_ac_html_stuff ($project_dir);
239 2 $prj->{'ac_html_stuff'} = $ac_html_stuff_dir;
240 } else {
241 1 $ac_html_stuff_dir = $prj->{'ac_html_stuff'};
242 }
243 3 return ($project_dir, $ac_html_stuff_dir);
244 }
245  
246 12 ▶sub _get_project {
Subroutine _get_project: called CC=3 SCAR=11.0
247 12 my $self = shift;
248 12 my $dir = shift;
249 12 ▶ return unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 12 12
250  
251 12 ▶ unless (exists $self->{PROJECTS}{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"PROJECTS"}{$dir}) 4 8 12
252 4 $self->{PROJECTS}{$dir} =
253 bless ( {monitor => CSS::Watcher::Monitor->new({dir => $dir})}, 'CSS::Watcher::Project' );
254 }
255 12 return $self->{PROJECTS}{$dir};
256 }
257  
258 # Lookup for project dir similar to projectile.el
259 12 ▶sub get_project_dir {
Subroutine get_project_dir: called CC=8 SCAR=21.3
Pod undocumented
260 12 my $self = shift;
261 12 my $obj = shift;
262
263 12 ▶ my $pdir = ! defined ($obj) ? undef:
Branch: 3 branches
BranchTrueFalseTotal
!defined($obj) ? : 0 12 12
-f $obj ? : 1 11 12
-d $obj ? : 11 0 11
264 (-f $obj) ? path ($obj)->parent :
265 (-d $obj) ? $obj : undef;
266 12 ▶ return unless (defined $pdir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdir 0 12 12
267  
268 12 $pdir = path( $pdir );
269  
270 12 foreach (qw/.projectile .csswatcher .git .hg .fslckout .bzr _darcs/) {
271 50 ▶ if (-e ($pdir->child( $_ ))) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pdir->child($_)) 7 43 50
272 7 return $pdir;
273 }
274 }
275 5 ▶ return if ($pdir->is_rootdir());
Branch: 1 branch
BranchTrueFalseTotal
if $pdir->is_rootdir 0 5 5
276 #parent dir
277 5 return $self->get_project_dir ($pdir->parent);
278 }
279  
280 1 1;
281  
282 __END__
« Watcher/Parser.pm ↑ Summary Watcher/Monitor.pm »