aboutsummaryrefslogtreecommitdiffstats
path: root/bin/make_css_watch
blob: d15fa3a81ae956d1c992621eb72981dbdb1208a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../setenv.pl";
}

use Cwd qw(abs_path);
use File::ChangeNotify;
use File::Find::Rule;
use FindBin;
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,
);

my $script_compass = 'compass';
my $script_sass = 'sass';
my $gem_bin = abs_path("$FindBin::Bin/../../gem-bin");
if (-f "$gem_bin/compass") {
    $script_compass = "$gem_bin/compass";
    $script_sass = "$gem_bin/sass";
}

sub title {
    my $what = shift;
    print "\033]2;$what\007\n";
}

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 $script_compass,
                'compile',
                '--output-style' => 'compressed',
                $dir;
        }
        else {
            system $script_sass,
                '--scss',
                '--update',
                '--style' => 'compressed',
                $dir;
        }
        title "$dir updated";
    }
    title 'watching';
}