diff options
author | Hakim Cassimally <hakim@mysociety.org> | 2014-02-28 10:01:48 +0000 |
---|---|---|
committer | Hakim Cassimally <hakim@mysociety.org> | 2014-03-13 13:01:35 +0000 |
commit | cb2580600bb39c546641d4bced4a988c0df89b2e (patch) | |
tree | ddfb66c4d5734ed20b72040b6849bb67f8e0a888 /bin/make_css_watch | |
parent | d2ebd056a2bd0484959d47b763dc6918b5b35015 (diff) |
CSS watcher
Run with bin/make_css_watch
This watches the .scss files in web/cobrands/* and runs the appropriate
sass/compass commands (as per bin/make_css) on just those directories
with changed files.
As a special case, a change to the partials in web/cobrands/sass will
cause a rebuild of all cobrands.
Diffstat (limited to 'bin/make_css_watch')
-rwxr-xr-x | bin/make_css_watch | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/bin/make_css_watch b/bin/make_css_watch new file mode 100755 index 000000000..d46ee8997 --- /dev/null +++ b/bin/make_css_watch @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature 'say'; +use File::ChangeNotify; +use File::Find::Rule; +use Path::Tiny; + +my @exts = qw/ + scss +/; + +my @dirs = qw/ + web +/; + +my $filter = do { + my $exts = join '|', @exts; + qr/\.(?:$exts)$/ +}; + +my $watcher = File::ChangeNotify->instantiate_watcher( + directories => \@dirs, + filter => $filter, +); + +sub title { + my $what = shift; + # TODO, check if xtitle is installed and if so, run following command: + # system 'xtitle', $what; +} + +say sprintf "Watching [%s] for %s", (join ',' => @dirs), $filter; +title 'watching'; + +while ( my @events = $watcher->wait_for_events() ) { + my %seen; + my @update_dirs; + title 'updating'; + for my $event (@events) { + my $file = path( $event->path ); + say "$file was updated..."; + my $dir = $file->dirname; + next if $seen{$dir}++; + + if ($dir eq 'web/cobrands/sass/') { + # contains only partials, so we don't need to update + # this directory, but we *do* need to update everything else + push @update_dirs, + grep { + ! ($seen{$_}++) + } + map { + path($_)->dirname + } + File::Find::Rule->file->name($filter)->in( @dirs ); + } + else { + push @update_dirs, $dir; + } + } + for my $dir (@update_dirs) { + if (-e "$dir/config.rb") { + system compass => + 'compile', + '--output-style' => 'compressed', + $dir; + } + else { + system sass => + '--scss', + '--update', + '--style' => 'compressed', + $dir; + } + } + title 'watching'; +} |