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
```
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/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
}
'';
};
}

View file

@ -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"];
})
]

View file

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

View file

@ -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 ;;

View file

@ -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
''