rework watch command

This commit is contained in:
Tristan 2024-01-03 06:47:16 +00:00
parent 4f6c21ec77
commit 3faf0e6354
6 changed files with 32 additions and 61 deletions

View file

@ -49,14 +49,3 @@ Then run the generated package.
nix run .#test --show-trace 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.

View file

@ -17,13 +17,25 @@
./test/describe.test.nix ./test/describe.test.nix
./test/watch.test.nix ./test/watch.test.nix
]; ];
packages.x86_64-linux.example = tix.run [ packages.x86_64-linux.example = tix.run [
./mytest.nix ./mytest.nix
]; ];
packages.x86_64-linux.watch = tix.watch { packages.x86_64-linux.watch = tix.watch {
cmd = "nix run .#test --show-trace"; cmd = "nix run --show-trace .#test file";
view = "${pkgs.fx}/bin/fx"; view = "tee -a /tmp/tix-output";
stop = "pkill fx";
}; };
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
}
'';
}; };
} }

View file

@ -10,46 +10,10 @@
watch = import ../tix/watch.nix mockpkgs; watch = import ../tix/watch.nix mockpkgs;
in in
describe "watch" [ describe "watch" [
(it "watches a the local directory and runs command on change" { (it "contains the command" {
actual = watch { actual = builtins.match ".*(run me).*" ( watch {
cmd = "run me"; cmd = "run me";
}; } );
expected = '' expected = ["run me"];
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
'';
}) })
] ]

View file

@ -3,4 +3,5 @@
it = import ./it.nix; it = import ./it.nix;
test = import ./test.nix; test = import ./test.nix;
watch = import ./watch.nix pkgs; watch = import ./watch.nix pkgs;
filters = import ./filters.nix;
} }

View file

@ -9,6 +9,7 @@ pkgs: files: let
in (pkgs.writeShellScriptBin "test" '' in (pkgs.writeShellScriptBin "test" ''
t=$1 t=$1
case "$t" in case "$t" in
"file") echo '${resFile}' ;;
"raw") cat '${resFile}' ;; "raw") cat '${resFile}' ;;
"json") jq '.' '${resFile}' ;; "json") jq '.' '${resFile}' ;;
"nix") ${extractNix} | ${pkgs.alejandra}/bin/alejandra -q ;; "nix") ${extractNix} | ${pkgs.alejandra}/bin/alejandra -q ;;

View file

@ -1,17 +1,21 @@
pkgs: { pkgs: {
cmd, cmd,
setup ? null,
stop ? null, stop ? null,
process ? "echo \"$out\"",
view ? null, view ? null,
}: }:
pkgs.writeShellScriptBin "watch" '' pkgs.writeShellScriptBin "watch" ''
path=''${1:-.}
${toString setup}
while true while true
do do
${cmd} ${ out=$(${cmd})
if view == null ${process}
then "" ${if view == null then "" else ''
else "| ${view} " echo "$out" | ${view}
}& ''}
${pkgs.inotify-tools}/bin/inotifywait -e modify -r . ${pkgs.inotify-tools}/bin/inotifywait -q -e modify -r $path
${toString stop} ${toString stop}
done done
'' ''