| 1 | package CSS::Watcher; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | use Log::Log4perl qw(:easy); | ||
| 10 | use File::Slurp qw/read_file write_file/; | ||
| 11 | use Path::Tiny; | ||
| 12 | use Digest::MD5 qw/md5_hex/; | ||
| 13 | use List::MoreUtils qw(any); | ||
| 14 | |||
| 15 | use CSS::Watcher::Parser; | ||
| 16 | use CSS::Watcher::ParserLess; | ||
| 17 | use CSS::Watcher::Monitor; | ||
| 18 | |||
| 19 | our $VERSION = '0.4.7'; | ||
| 20 | |||
| 21 | use constant DEFAULT_HTML_STUFF_DIR => '~/.emacs.d/ac-html-csswatcher/completion/'; | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class= shift; | ||
| 25 | my $options = shift; | ||
| 26 | |||
| 27 | return bless ({ | ||
| 28 | outputdir => $options->{'outputdir'} // DEFAULT_HTML_STUFF_DIR, | ||
| 29 | parser_css => CSS::Watcher::Parser->new(), | ||
| 30 | parser_less => CSS::Watcher::ParserLess->new(), | ||
| 31 | }, $class); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub update { | ||
| 35 | my $self = shift; | ||
| 36 | my $obj = shift; | ||
| 37 | |||
| 38 | # check what is the monobj. file? dir? | ||
| 39 | if (-f $obj || -d $obj) { | ||
| 40 | my $proj_dir = $self->get_project_dir ($obj); | ||
| 41 | return unless (defined $proj_dir); | ||
| 42 | |||
| 43 | INFO "Update project: $proj_dir"; | ||
| 44 | |||
| 45 | my $prj = $self->_get_project ($proj_dir); | ||
| 46 | $prj->{parsed_files} = []; # clean old parsed file list | ||
| 47 | |||
| 48 | my $changes = 0; | ||
| 49 | |||
| 50 | my (@ignore, @allow, @skip_dirs); | ||
| 51 | my $cfg = path($proj_dir)->child('.csswatcher'); | ||
| 52 | |||
| 53 | # clear project cache if .csswatcher changed | ||
| 54 | if ($prj->{monitor}->is_changed($cfg)) { | ||
| 55 | INFO ('.csswatcher changed, resetting'); | ||
| 56 | $prj->{monitor}->make_dirty(); | ||
| 57 | delete $prj->{parsed}; | ||
| 58 | $changes++; | ||
| 59 | } | ||
| 60 | |||
| 61 | if (-f $cfg) { | ||
| 62 | if (open (CFG, '<:encoding(UTF-8)', $cfg)) { | ||
| 63 | while (<CFG>) { | ||
| 64 | chomp; | ||
| 65 | (m/^\s*ignore:\s*(.*?)\s*$/i) ? push @ignore, $1 : | ||
| 66 | (m/^\s*skip:\s*(.*?)\s*$/i) ? push @skip_dirs, $1 : | ||
| 67 | (m/^\s*use:\s*(.*?)\s*$/i) ? push @allow, $1 : 1; | ||
| 68 | } | ||
| 69 | close CFG; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | # scan new or changed files, cache them | ||
| 74 | $prj->{monitor}->scan ( | ||
| 75 | sub { | ||
| 76 | my $file = shift; | ||
| 77 | |||
| 78 | return if (any {$_ eq $file} @{$prj->{parsed_files}}); | ||
| 79 | |||
| 80 | my $allow = 0; | ||
| 81 | foreach (@allow) { | ||
| 82 | if ($file =~ m/$_/) { | ||
| 83 | $allow = 1; | ||
| 84 | last; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | unless ($allow) { | ||
| 88 | foreach (@ignore) { | ||
| 89 | if ($file =~ m/$_/) { | ||
| 90 | INFO " Ignored $file =~\"$_\""; | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | ($file =~ m/\.css$/) ? $changes += 1 && $self->_parse_css ($prj, $file) : | ||
| 96 | ($file =~ m/\.less$/) ? $changes += 1 && $self->_parse_less_and_imports ($prj, $file) : 1; | ||
| 97 | |||
| 98 | }, \@skip_dirs); | ||
| 99 | INFO "Update done."; | ||
| 100 | return ($changes, $proj_dir); | ||
| 101 | } | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _parse_css { | ||
| 106 | my ($self, $project, $file) = @_; | ||
| 107 | |||
| 108 | INFO " (Re)parse css: $file"; | ||
| 109 | my $data = read_file ($file); | ||
| 110 | my ($classes, $ids) = $self->{parser_css}->parse_css ($data); | ||
| 111 | $project->{parsed}{$file} = {CLASSES => $classes, | ||
| 112 | IDS => $ids}; | ||
| 113 | push @{$project->{parsed_files}}, $file; | ||
| 114 | return 1; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _parse_less { | ||
| 118 | my ($self, $project, $file) = @_; | ||
| 119 | |||
| 120 | INFO " (Re)parse less: $file"; | ||
| 121 | my ($classes, $ids, $requiries) = $self->{parser_less}->parse_less ($file); | ||
| 122 | $project->{parsed}{$file} = {CLASSES => $classes, | ||
| 123 | IDS => $ids}; | ||
| 124 | 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 | [ map {path($file)->parent->child($_)->realpath()->stringify} @{$requiries} ]; | ||
| 136 | return 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _parse_less_and_imports { | ||
| 140 | my ($self, $project, $file) = @_; | ||
| 141 | |||
| 142 | my $parsed_files = 1; # 1, cause we parse $file for sure., ++ if dependencies parsed too | ||
| 143 | |||
| 144 | $self->_parse_less ($project, $file); | ||
| 145 | |||
| 146 | while (my ($less_fname, $imports) = each %{$project->{imports_less}}) { | ||
| 147 | foreach (@{$imports}) { | ||
| 148 | if ($file eq $_) { | ||
| 149 | next if (any {$_ eq $less_fname} @{$project->{parsed_files}}); | ||
| 150 | INFO sprintf " %s required by %s, parse them too.", path($file)->basename, path($less_fname)->basename; | ||
| 151 | $self->_parse_less($project, $less_fname); | ||
| 152 | $parsed_files++; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | return $parsed_files; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub project_stuff { | ||
| 160 | my $self = shift; | ||
| 161 | my $proj_dir = shift; | ||
| 162 | |||
| 163 | my $prj = $self->_get_project ($proj_dir); | ||
| 164 | |||
| 165 | # build unique tag - class,id | ||
| 166 | my (%classes, %ids); | ||
| 167 | my ($total_classes, $total_ids) = (0, 0); | ||
| 168 | while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) { | ||
| 169 | while ( my ( $tag, $classes ) = each %{$completions->{CLASSES}} ) { | ||
| 170 | foreach (keys %{$classes}) { | ||
| 171 | $classes{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n'; | ||
| 172 | $total_classes++; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | while ( my ( $file, $completions ) = each %{$prj->{parsed}} ) { | ||
| 177 | while ( my ( $tag, $ids ) = each %{$completions->{IDS}} ) { | ||
| 178 | foreach (keys %{$ids}) { | ||
| 179 | $ids{$tag}{$_} .= 'Defined in ' . path( $file )->relative( $proj_dir ) . '\n'; | ||
| 180 | $total_ids++; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | INFO "Total for $proj_dir:"; | ||
| 185 | INFO " Classes: $total_classes, ids: $total_ids"; | ||
| 186 | |||
| 187 | return (\%classes, \%ids); | ||
| 188 | } | ||
| 189 | |||
| 190 | # clean old output html complete stuff and build new | ||
| 191 | sub build_ac_html_stuff { | ||
| 192 | my $self = shift; | ||
| 193 | my $proj_dir = shift; | ||
| 194 | |||
| 195 | my ($classes, $ids) = $self->project_stuff ($proj_dir); | ||
| 196 | |||
| 197 | my $ac_html_stuff_dir = path ($self->{outputdir})->child (md5_hex( ''.$proj_dir )); | ||
| 198 | my $attrib_dir = path ($ac_html_stuff_dir)->child ('html-attributes-complete'); | ||
| 199 | |||
| 200 | $attrib_dir->remove_tree({safe => 0}); | ||
| 201 | $attrib_dir->mkpath; | ||
| 202 | |||
| 203 | while ( my ( $tag, $class ) = each %{$classes} ) { | ||
| 204 | my $fname = File::Spec->catfile ($attrib_dir, $tag . '-class'); | ||
| 205 | DEBUG "Write $fname"; | ||
| 206 | write_file ($fname, join "\n", map { | ||
| 207 | $_ . ' ' . $class->{$_} } sort keys %{$class}); | ||
| 208 | } | ||
| 209 | while ( my ( $tag, $id ) = each %${ids} ) { | ||
| 210 | my $fname = File::Spec->catfile ($attrib_dir, $tag . '-id'); | ||
| 211 | DEBUG "Write $fname"; | ||
| 212 | write_file ($fname, join "\n", map { | ||
| 213 | $_ . ' ' . $id->{$_} } sort keys %{$id}); | ||
| 214 | } | ||
| 215 | DEBUG "Done writing. Reply to client."; | ||
| 216 | return $ac_html_stuff_dir; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub get_html_stuff { | ||
| 220 | my $self = shift; | ||
| 221 | my $obj = shift; | ||
| 222 | |||
| 223 | my ($changes, $project_dir) = $self->update ($obj); | ||
| 224 | return unless defined $changes; | ||
| 225 | |||
| 226 | my $prj = $self->_get_project ($project_dir); | ||
| 227 | |||
| 228 | my $ac_html_stuff_dir; | ||
| 229 | |||
| 230 | if ($changes) { | ||
| 231 | $ac_html_stuff_dir = $self->build_ac_html_stuff ($project_dir); | ||
| 232 | $prj->{'ac_html_stuff'} = $ac_html_stuff_dir; | ||
| 233 | } else { | ||
| 234 | $ac_html_stuff_dir = $prj->{'ac_html_stuff'}; | ||
| 235 | } | ||
| 236 | return ($project_dir, $ac_html_stuff_dir); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _get_project { | ||
| 240 | my $self = shift; | ||
| 241 | my $dir = shift; | ||
| 242 | return unless defined $dir; | ||
| 243 | |||
| 244 | unless (exists $self->{PROJECTS}{$dir}) { | ||
| 245 | $self->{PROJECTS}{$dir} = | ||
| 246 | bless ( {monitor => CSS::Watcher::Monitor->new({dir => $dir})}, 'CSS::Watcher::Project' ); | ||
| 247 | } | ||
| 248 | return $self->{PROJECTS}{$dir}; | ||
| 249 | } | ||
| 250 | |||
| 251 | # Lookup for project dir similar to projectile.el | ||
| 252 | sub get_project_dir { | ||
| 253 | my $self = shift; | ||
| 254 | my $obj = shift; | ||
| 255 | |||
| 256 | my $pdir = ! defined ($obj) ? undef: | ||
| 257 | (-f $obj) ? path ($obj)->parent : | ||
| 258 | (-d $obj) ? $obj : undef; | ||
| 259 | return unless (defined $pdir); | ||
| 260 | |||
| 261 | $pdir = path( $pdir ); | ||
| 262 | |||
| 263 | foreach (qw/.projectile .csswatcher .git .hg .fslckout .bzr _darcs/) { | ||
| 264 | if (-e ($pdir->child( $_ ))) { | ||
| 265 | return $pdir; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | return if ($pdir->is_rootdir()); | ||
| 269 | #parent dir | ||
| 270 | return $self->get_project_dir ($pdir->parent); | ||
| 271 | } | ||
| 272 | |||
| 273 | 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 |