From 3faf0e635410208b8a612e50aaaae274335b83f8 Mon Sep 17 00:00:00 2001 From: Tristan Date: Wed, 3 Jan 2024 06:47:16 +0000 Subject: [PATCH] rework watch command --- README.md | 11 ----------- flake.nix | 18 +++++++++++++++--- test/watch.test.nix | 46 +++++---------------------------------------- tix/default.nix | 1 + tix/run.nix | 1 + tix/watch.nix | 16 ++++++++++------ 6 files changed, 32 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index ddabb7a..4f746d6 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,3 @@ Then run the generated package. nix run .#test --show-trace ``` -Watch for changes while you are developing your package by adding a new output. - -```flake.nix -tix.watch "nix run .#test | ${pkgs.fx}/bin/fx" "fx"; -``` - -I am piping the output into `fx` so I can navigate it easier. - -`"fx"` is the process name that must be killed before rerunning. - - diff --git a/flake.nix b/flake.nix index 20042b1..2e66630 100644 --- a/flake.nix +++ b/flake.nix @@ -17,13 +17,25 @@ ./test/describe.test.nix ./test/watch.test.nix ]; + packages.x86_64-linux.example = tix.run [ ./mytest.nix ]; + packages.x86_64-linux.watch = tix.watch { - cmd = "nix run .#test --show-trace"; - view = "${pkgs.fx}/bin/fx"; - stop = "pkill fx"; + cmd = "nix run --show-trace .#test file"; + view = "tee -a /tmp/tix-output"; }; + + packages.x86_64-linux.results = pkgs.writeShellScriptBin "results" '' + tail -f -n1 /tmp/tix-output | { + while read out + do + clear + jq '${tix.filters.overview}' $out + done + } + ''; }; } + diff --git a/test/watch.test.nix b/test/watch.test.nix index 1175cc5..44fa907 100644 --- a/test/watch.test.nix +++ b/test/watch.test.nix @@ -10,46 +10,10 @@ watch = import ../tix/watch.nix mockpkgs; in describe "watch" [ - (it "watches a the local directory and runs command on change" { - actual = watch { - cmd = "run me"; - }; - expected = '' - while true - do - run me & - inotify-tools/bin/inotifywait -e modify -r . - ${""} - done - ''; - }) - (it "runs the command provided to stop execution" { - actual = watch { - cmd = "run me"; - stop = "kill me"; - }; - expected = '' - while true - do - run me & - inotify-tools/bin/inotifywait -e modify -r . - kill me - done - ''; - }) - (it "pipes output to given command" { - actual = watch { - cmd = "run me"; - stop = "kill me"; - view = "less"; - }; - expected = '' - while true - do - run me | less & - inotify-tools/bin/inotifywait -e modify -r . - kill me - done - ''; + (it "contains the command" { + actual = builtins.match ".*(run me).*" ( watch { + cmd = "run me"; + } ); + expected = ["run me"]; }) ] diff --git a/tix/default.nix b/tix/default.nix index 9436ea1..07b5d77 100644 --- a/tix/default.nix +++ b/tix/default.nix @@ -3,4 +3,5 @@ it = import ./it.nix; test = import ./test.nix; watch = import ./watch.nix pkgs; + filters = import ./filters.nix; } diff --git a/tix/run.nix b/tix/run.nix index fc291df..f2cb88f 100644 --- a/tix/run.nix +++ b/tix/run.nix @@ -9,6 +9,7 @@ pkgs: files: let in (pkgs.writeShellScriptBin "test" '' t=$1 case "$t" in + "file") echo '${resFile}' ;; "raw") cat '${resFile}' ;; "json") jq '.' '${resFile}' ;; "nix") ${extractNix} | ${pkgs.alejandra}/bin/alejandra -q ;; diff --git a/tix/watch.nix b/tix/watch.nix index 6c701f6..548d776 100644 --- a/tix/watch.nix +++ b/tix/watch.nix @@ -1,17 +1,21 @@ pkgs: { cmd, + setup ? null, stop ? null, + process ? "echo \"$out\"", view ? null, }: pkgs.writeShellScriptBin "watch" '' + path=''${1:-.} + ${toString setup} while true do - ${cmd} ${ - if view == null - then "" - else "| ${view} " - }& - ${pkgs.inotify-tools}/bin/inotifywait -e modify -r . + out=$(${cmd}) + ${process} + ${if view == null then "" else '' + echo "$out" | ${view} + ''} + ${pkgs.inotify-tools}/bin/inotifywait -q -e modify -r $path ${toString stop} done ''