aboutsummaryrefslogtreecommitdiffstats
path: root/bin/make_css_watch
blob: d46ee899736148a17d99d6e8c7f5a18984d573f9 (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
#!/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';
}