tree-sitter/update: Write files atomically

Otherwise you can’t interrupt the process without creating
broken/half-written files.
This commit is contained in:
Profpatsch 2022-09-07 02:18:47 +02:00
parent 26cb66b681
commit a64a9d5552

View file

@ -430,8 +430,9 @@ let
mkdir -p "$outputDir" mkdir -p "$outputDir"
${foreachSh allGrammars ${foreachSh allGrammars
({name, orga, repo}: '' ({name, orga, repo}: ''
${fetchImpl} fetch-repo '${lib.generators.toJSON {} {inherit orga repo;}}' \ ${atomically-write} \
> $outputDir/${name}.json $outputDir/${name}.json \
${fetchImpl} fetch-repo '${lib.generators.toJSON {} {inherit orga repo;}}'
'')} '')}
( echo "{ lib }:" ( echo "{ lib }:"
echo "{" echo "{"
@ -443,5 +444,30 @@ let
> "$outputDir/default.nix" > "$outputDir/default.nix"
''; '';
# Atomically write a file (just `>` redirection in bash
# empties a file even if the command crashes).
#
# Maybe there is an existing tool for that?
# But its easy enough to implement.
#
# Example:
# atomically-write
# ./to
# echo "foo"
#
# will atomically write the string "foo" into ./to
atomically-write = writeShellScript "atomically-write" ''
set -e
to=$1
shift
# assumes that the tempfile is on the same file system, (or in memory)
# for the `mv` at the end to be more-or-less atomic.
tmp=$(${coreutils}/bin/mktemp -d)
trap 'rm -r "$tmp"' EXIT
"$@" \
> "$tmp/out"
mv "$tmp/out" "$to"
'';
in in
update-all-grammars update-all-grammars