aboutsummaryrefslogtreecommitdiff
path: root/lib/ScanDir.rakumod
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ScanDir.rakumod')
-rw-r--r--lib/ScanDir.rakumod49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/ScanDir.rakumod b/lib/ScanDir.rakumod
new file mode 100644
index 0000000..f3173a4
--- /dev/null
+++ b/lib/ScanDir.rakumod
@@ -0,0 +1,49 @@
+use v6.d;
+unit module ScanDir;
+
+class End {};
+
+sub scan-dir(*@paths --> Supply) is export {
+ supply {
+ my %watched-dirs;
+
+ CATCH { when X::IO { }; default { warn $_; done } }
+
+ sub start-watching(IO::Path $dir) {
+ return if %watched-dirs{$dir.Str};
+ %watched-dirs{$dir.Str} = True;
+
+ whenever $dir.watch {
+ my $path-io = .path.IO;
+ emit $path-io;
+ when $path-io ~~ :e & :d {
+ add-dir($path-io) unless %watched-dirs{$path-io.Str};
+ }
+ when $path-io ~~ :!e {
+ %watched-dirs{$path-io.Str}:delete
+ }
+ }
+ }
+
+ sub add-dir(*@todo) {
+ while @todo {
+ my $next = @todo.shift;
+
+ next unless $next ~~ :e & :r & :d;
+ start-watching($next);
+
+ for $next.dir {
+ emit $_;
+ when .e && .d {
+ @todo.push($_);
+ start-watching($_);
+ }
+ }
+ }
+
+ }
+
+ add-dir(@paths».IO);
+ emit End;
+ }
+}