-
Notifications
You must be signed in to change notification settings - Fork 1
/
morbo.p6
executable file
·53 lines (46 loc) · 1.35 KB
/
morbo.p6
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
#!/usr/bin/env perl6
use File::Find;
sub MAIN ( Str $app, Str :$w = 'lib,bin') {
my @watchlist = $w.split: /<!after \\> \,/;
s/\\\,/,/ for @watchlist;
say "Attempting to boot up the app";
my $p = bootup-app $app;
react {
whenever watch-recursive(@watchlist.grep: *.IO.e) -> $e {
say "Change detected [$e.path(), $e.event()]. Restarting app";
$p.kill;
$p = bootup-app $app;
}
}
}
########
my sub bootup-app ($app) {
my Proc::Async $p .= new: 'perl6', $app;
$p.stdout.tap: -> $v { $*OUT.print: $v };
$p.stderr.tap: -> $v { $*ERR.print: $v };
$p.start;
return $p;
}
########
my sub watch-recursive(@dirs) {
supply {
my sub watch-it($p) {
if ( $p ~~ rx{ '/'? '.precomp' [ '/' | $ ] } ) {
say "Skipping .precomp dir [$p]";
return;
}
say "Starting watch on `$p`";
whenever IO::Notification.watch-path($p) -> $e {
if $e.event ~~ FileRenamed && $e.path.IO ~~ :d {
watch-it($_) for find-dirs $e.path;
}
emit($e);
}
}
watch-it(~$_) for |@dirs.map: { find-dirs $_ };
}
}
my sub find-dirs (Str:D $p) {
state $seen = {};
return slip ($p.IO, slip find :dir($p), :type<dir>).grep: { !$seen{$_}++ };
}