| 1 | # Copyright (c) 2013-2014 David Caldwell. | ||
| 2 | # Copyright (c) 2014-2017 Marcel Greter. | ||
| 3 | # | ||
| 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 5 | # of this software and associated documentation files (the "Software"), to deal | ||
| 6 | # in the Software without restriction, including without limitation the rights | ||
| 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 8 | # copies of the Software, and to permit persons to whom the Software is | ||
| 9 | # furnished to do so, subject to the following conditions: | ||
| 10 | # | ||
| 11 | # The above copyright notice and this permission notice shall be included in | ||
| 12 | # all copies or substantial portions of the Software. | ||
| 13 | # | ||
| 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 20 | # THE SOFTWARE. | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use CSS::Sass; | ||
| 25 | |||
| 26 | ################################################################################ | ||
| 27 | package CSS::Sass::Watchdog; | ||
| 28 | our $VERSION = "3.6.4"; | ||
| 29 | ################################################################################ | ||
| 30 | |||
| 31 | use Exporter 'import'; # gives you Exporter's import() method directly | ||
| 32 | our @EXPORT = qw(start_watchdog); # symbols to export by default | ||
| 33 | |||
| 34 | #################################################################################################### | ||
| 35 | #################################################################################################### | ||
| 36 | |||
| 37 | # load function from core module | ||
| 38 | use List::Util 1.45 qw(uniq); | ||
| 39 | |||
| 40 | # check if the benchmark module is available | ||
| 41 | my $benchmark = eval "use Benchmark; 1"; | ||
| 42 | |||
| 43 | # declare package variables | ||
| 44 | my ($parent_pid, $child_pid) = ($$, 0); | ||
| 45 | |||
| 46 | #################################################################################################### | ||
| 47 | # reset/reinit is needed to avoid a bug on windows | ||
| 48 | # version strawberry-perl-5.22.2.1-32bit-portable | ||
| 49 | # https://github.com/sass/perl-libsass/issues/33 | ||
| 50 | #################################################################################################### | ||
| 51 | |||
| 52 | sub reset_encoding() | ||
| 53 | { | ||
| 54 | binmode(STDIN, ":raw"); | ||
| 55 | binmode(STDOUT, ":raw"); | ||
| 56 | binmode(STDERR, ":raw"); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub reinit_encoding() | ||
| 60 | { | ||
| 61 | binmode(STDIN, ":encoding(console_in)"); | ||
| 62 | binmode(STDOUT, ":encoding(console_out)"); | ||
| 63 | binmode(STDERR, ":encoding(console_out)"); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub forks { | ||
| 67 | reset_encoding(); | ||
| 68 | my $pid = fork(); | ||
| 69 | reinit_encoding(); | ||
| 70 | return $pid; | ||
| 71 | } | ||
| 72 | |||
| 73 | #################################################################################################### | ||
| 74 | # the watchdog process (maybe put in own module) | ||
| 75 | #################################################################################################### | ||
| 76 | |||
| 77 | # the parent is the main (current) process | ||
| 78 | # wait for events to arive to the message queue | ||
| 79 | # wait some time until we start the re-compilation | ||
| 80 | sub watchdog_parent ($$$) | ||
| 81 | { | ||
| 82 | |||
| 83 | # Try to be smart here. We will not start the compilation after | ||
| 84 | # each event right away. There are at least two scenarios where | ||
| 85 | # this would be inefficient. First if a user clicks "save all" | ||
| 86 | # in its editor, or if multiple files are copied over. We wait | ||
| 87 | # for a certain period and only start the compilation when we | ||
| 88 | # got no more change events. | ||
| 89 | |||
| 90 | # get input variables | ||
| 91 | my ($changes, $files, $compile) = @_; | ||
| 92 | |||
| 93 | # aggregated events | ||
| 94 | my @aggregated; | ||
| 95 | |||
| 96 | # print delimiter line | ||
| 97 | print '=' x 78, "\n"; | ||
| 98 | |||
| 99 | # go into endless loop | ||
| 100 | while (1) | ||
| 101 | { | ||
| 102 | |||
| 103 | # check if we have something to | ||
| 104 | # dequeue in the next seconds | ||
| 105 | if ($changes->can_dequeue(0.25)) | ||
| 106 | { | ||
| 107 | |||
| 108 | # dequeue a key from notifier | ||
| 109 | my $item = $changes->dequeue(); | ||
| 110 | |||
| 111 | # wait for exit command | ||
| 112 | exit if $item eq "stop"; | ||
| 113 | |||
| 114 | # push the real hash to the queue | ||
| 115 | push(@aggregated, $item); | ||
| 116 | |||
| 117 | # make aggregated list unique | ||
| 118 | @aggregated = uniq @aggregated; | ||
| 119 | |||
| 120 | } | ||
| 121 | # nothing to dequeue, we waited 0.25 seconds | ||
| 122 | # maybe we have something in our to do list | ||
| 123 | else | ||
| 124 | { | ||
| 125 | |||
| 126 | # autoflush | ||
| 127 | local $| = 1; | ||
| 128 | |||
| 129 | # count errors | ||
| 130 | my $beeps = 1; | ||
| 131 | |||
| 132 | # do nothing of aggregated is empty | ||
| 133 | next if scalar(@aggregated) == 0; | ||
| 134 | |||
| 135 | # now call the compilation step | ||
| 136 | my $t0 = $benchmark ? Benchmark->new : 0; | ||
| 137 | print "compilation started\n"; | ||
| 138 | my ($css, $err, $stats) = $compile->(); | ||
| 139 | my $t1 = $benchmark ? Benchmark->new : 0; | ||
| 140 | if (!$err) { print "sucessfully finished\n"; } | ||
| 141 | else { print "finished with an error\n"; } | ||
| 142 | |||
| 143 | # only print benchmark result when module is available | ||
| 144 | if ($benchmark) { print timestr(timediff($t1, $t0)), "\n"; } | ||
| 145 | |||
| 146 | my @includes = @{$stats->{'included_files'} || []}; | ||
| 147 | # use the simples equality test | ||
| 148 | # should work since they are sorted | ||
| 149 | if (!$err && "@{$files}" ne "@includes") | ||
| 150 | { | ||
| 151 | print "re-start file watcher\n"; | ||
| 152 | # make sure our child is terminated | ||
| 153 | kill 9, $child_pid; | ||
| 154 | waitpid($child_pid, 0); | ||
| 155 | # new watch file list | ||
| 156 | $files = \@includes; | ||
| 157 | # start new child process | ||
| 158 | unless ($child_pid = forks()) | ||
| 159 | { watchdog_child($changes, $files); } | ||
| 160 | } | ||
| 161 | |||
| 162 | # beep more in case of errors | ||
| 163 | $beeps += 2 if defined $err; | ||
| 164 | |||
| 165 | # ring the bell now | ||
| 166 | print "\a" x ($beeps); | ||
| 167 | # clear aggregated | ||
| 168 | undef @aggregated; | ||
| 169 | # print delimiter line | ||
| 170 | print '=' x 78, "\n"; | ||
| 171 | |||
| 172 | } | ||
| 173 | # EO can dequeue | ||
| 174 | |||
| 175 | } | ||
| 176 | # EO endless loop | ||
| 177 | |||
| 178 | } | ||
| 179 | # EO sub watchdog_parent | ||
| 180 | |||
| 181 | # the child watches all registered files for changes | ||
| 182 | # changes will be sent to the parent via our fork queue | ||
| 183 | # the parent will decide when to start the next compilation | ||
| 184 | sub watchdog_child ($$) | ||
| 185 | { | ||
| 186 | |||
| 187 | # get input variables | ||
| 188 | my ($changes, $files) = @_; | ||
| 189 | |||
| 190 | # try to load the watch module conditional | ||
| 191 | unless (eval { require Filesys::Notify::Simple; 1 }) | ||
| 192 | { die "module Filesys::Notify::Simple not found"; } | ||
| 193 | |||
| 194 | # print message with watched files | ||
| 195 | print "waiting for changes now\n"; | ||
| 196 | print map { (" ", $_, "\n") } @{$files || []}; | ||
| 197 | # print delimiter line | ||
| 198 | print '=' x 78, "\n"; | ||
| 199 | |||
| 200 | # create the watcher object on all filepaths | ||
| 201 | my $watcher = Filesys::Notify::Simple->new($files); | ||
| 202 | |||
| 203 | # go into endless loop | ||
| 204 | while (1) | ||
| 205 | { | ||
| 206 | |||
| 207 | # watch for file changes | ||
| 208 | # this will block forever | ||
| 209 | $watcher->wait(sub | ||
| 210 | { | ||
| 211 | |||
| 212 | # get all events | ||
| 213 | for my $event (@_) | ||
| 214 | { | ||
| 215 | # print a message when a change occurs | ||
| 216 | printf "changed: %s\n", $event->{path}; | ||
| 217 | # get the normalized path string | ||
| 218 | my $path = $event->{path}; | ||
| 219 | # enqueue changed file | ||
| 220 | $changes->enqueue($path); | ||
| 221 | |||
| 222 | } | ||
| 223 | # EO all events | ||
| 224 | |||
| 225 | }); | ||
| 226 | # EO wait for watcher | ||
| 227 | |||
| 228 | } | ||
| 229 | # EO endless loop | ||
| 230 | |||
| 231 | } | ||
| 232 | # EO sub watchdog_child | ||
| 233 | |||
| 234 | sub start_watchdog ($$) | ||
| 235 | { | ||
| 236 | |||
| 237 | local $SIG{CHLD} = 'IGNORE'; | ||
| 238 | |||
| 239 | # pass compile stats | ||
| 240 | my ($stats, $compile) = @_; | ||
| 241 | |||
| 242 | # A simple sequence of compile, wait, compile etc. will not work | ||
| 243 | # correctly, since we will miss changes while we are compiling. | ||
| 244 | # So a change done while a compile is still running, will not | ||
| 245 | # trigger any event, when the previous compilation is done. | ||
| 246 | |||
| 247 | # This could also be solved by using File::ChangeNotify, since | ||
| 248 | # it should provide an API that seems to be non blocking. I guess | ||
| 249 | # that some platform-specific implementation does not support non- | ||
| 250 | # blocking io, which should be the case, as otherwise I would expect | ||
| 251 | # File::ChangeNotify::Simple to provide a non-blocking interface. But | ||
| 252 | # I really like that deps are optional in File::ChangeNotify::Simple! | ||
| 253 | |||
| 254 | # Therefore we create two "threads" that communicate via | ||
| 255 | # a very simple queue object. This allows us to sync changes | ||
| 256 | # and running compilations. Since this creates quite a lot | ||
| 257 | # of additional dependencies, we try to load them only when | ||
| 258 | # this feature is in use (no hard dependencies for core lib). | ||
| 259 | |||
| 260 | # Most of the code and ideas have been copied from webmerge! | ||
| 261 | # ToDo: reference optional (should have) deps in build file! | ||
| 262 | |||
| 263 | # create new queue to pass commands/events around | ||
| 264 | my $changes = CSS::Sass::Watchdog::Queue->new(); | ||
| 265 | |||
| 266 | # get the files from the previous compile stats | ||
| 267 | my @files = @{$stats->{'included_files'} || []}; | ||
| 268 | |||
| 269 | # start child process | ||
| 270 | if ($child_pid = forks()) | ||
| 271 | { watchdog_parent($changes, \@files, $compile); } | ||
| 272 | else { watchdog_child($changes, \@files); } | ||
| 273 | |||
| 274 | } | ||
| 275 | # EO sub start_watchdog | ||
| 276 | |||
| 277 | ################################################################################################### | ||
| 278 | |||
| 279 | END | ||
| 280 | { | ||
| 281 | # check if we actually are the parent | ||
| 282 | if ($parent_pid && $parent_pid == $$) | ||
| 283 | { | ||
| 284 | # print "parent is terminating\n"; | ||
| 285 | # make sure our child is terminated | ||
| 286 | kill 'TERM', $child_pid if $child_pid; | ||
| 287 | } | ||
| 288 | # check if we actually are the parent | ||
| 289 | elsif ($parent_pid && $parent_pid != $$) | ||
| 290 | { | ||
| 291 | # print "file watcher was terminated\n"; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | ################################################################################ | ||
| 296 | # from http://www.perlmonks.org/?node_id=49335 | ||
| 297 | # 26.06.2012 added can_dequeue function (mgr@rtp.ch) | ||
| 298 | ################################################################################ | ||
| 299 | package CSS::Sass::Watchdog::Queue; | ||
| 300 | ################################################################################ | ||
| 301 | |||
| 302 | use Socket; | ||
| 303 | |||
| 304 | sub new { | ||
| 305 | my($this) = @_; | ||
| 306 | my $class = ref($this) || $this; | ||
| 307 | my $self = {}; | ||
| 308 | bless $self, $class; | ||
| 309 | $self->mksockpair(); | ||
| 310 | return $self; | ||
| 311 | } | ||
| 312 | |||
| 313 | # make the socketpair | ||
| 314 | sub mksockpair { | ||
| 315 | my($self)=@_; | ||
| 316 | socketpair(my $reader, my $writer, AF_UNIX, SOCK_STREAM, PF_UNSPEC); | ||
| 317 | if ($^O ne "MSWin32") | ||
| 318 | { | ||
| 319 | shutdown($reader,1); | ||
| 320 | shutdown($writer,0); | ||
| 321 | } | ||
| 322 | $self->{'READER'}=$reader; | ||
| 323 | $self->{'WRITER'}=$writer; | ||
| 324 | } | ||
| 325 | |||
| 326 | # method to put something on the queue | ||
| 327 | sub enqueue { | ||
| 328 | my($self,@data)=@_; | ||
| 329 | my($header,$buffer,$tosend); | ||
| 330 | my($handle)=$self->{'WRITER'}; | ||
| 331 | foreach my $item (@data) { | ||
| 332 | $header=pack("N",length($item)); | ||
| 333 | $buffer=$header . $item; | ||
| 334 | $tosend=length($buffer); | ||
| 335 | my $rv = print $handle $buffer; | ||
| 336 | die "write error : $!" unless defined $rv; | ||
| 337 | die "write disconnected" if $rv eq 0; | ||
| 338 | $handle->flush; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | # method to pull something off the queue | ||
| 343 | sub dequeue { | ||
| 344 | my($self)=@_; | ||
| 345 | my($header,$data); | ||
| 346 | my($toread)=4; | ||
| 347 | my($bytes_read)=0; | ||
| 348 | my($handle)=$self->{'READER'}; | ||
| 349 | # read 4 byte header | ||
| 350 | while ($bytes_read < $toread) { | ||
| 351 | my $rv=read($handle,$header,$toread); | ||
| 352 | die "read error : $!" unless defined $rv; | ||
| 353 | die "read disconnected" if $rv eq 0; | ||
| 354 | $bytes_read+=$rv; | ||
| 355 | } | ||
| 356 | $toread=unpack("N",$header); | ||
| 357 | $bytes_read=0; | ||
| 358 | # read the actual data | ||
| 359 | while ($bytes_read < $toread) { | ||
| 360 | my $rv=read($handle,$data,$toread,0); | ||
| 361 | die "read error : $!" unless defined $rv; | ||
| 362 | die "read disconnected" if $rv eq 0; | ||
| 363 | $bytes_read+=$rv; | ||
| 364 | } | ||
| 365 | return $data; | ||
| 366 | } | ||
| 367 | |||
| 368 | # method to check if something can be dequeued | ||
| 369 | sub can_dequeue { | ||
| 370 | my($self,$timeout)=@_; | ||
| 371 | my($handle)=$self->{'READER'}; | ||
| 372 | if (defined(my $fileno = $handle->fileno())) { | ||
| 373 | vec(my $rbit = '', $fileno, 1) = 1; # enable fd in vector table | ||
| 374 | vec(my $ebit = '', $fileno, 1) = 1; # enable fd in vector table | ||
| 375 | my $rv = select($rbit, undef, $ebit, $timeout); # select for readable handles | ||
| 376 | die "can dequeue errors" if vec($ebit, $fileno, 1); | ||
| 377 | return vec($rbit, $fileno, 1); # check fd in vector table | ||
| 378 | } else { return undef; } | ||
| 379 | # my($io) = IO::Select->new($handle); | ||
| 380 | # return $io->can_read($timeout); | ||
| 381 | } | ||
| 382 | |||
| 383 | ################################################################################ | ||
| 384 | package CSS::Sass::Watchdog; | ||
| 385 | ################################################################################ | ||
| 386 | 1; | ||
| 387 | |||
| 388 | __END__ |