tree-sitter/update: Write files atomically
Otherwise you can’t interrupt the process without creating broken/half-written files.
This commit is contained in:
parent
26cb66b681
commit
a64a9d5552
1 changed files with 28 additions and 2 deletions
|
@ -430,8 +430,9 @@ let
|
|||
mkdir -p "$outputDir"
|
||||
${foreachSh allGrammars
|
||||
({name, orga, repo}: ''
|
||||
${fetchImpl} fetch-repo '${lib.generators.toJSON {} {inherit orga repo;}}' \
|
||||
> $outputDir/${name}.json
|
||||
${atomically-write} \
|
||||
$outputDir/${name}.json \
|
||||
${fetchImpl} fetch-repo '${lib.generators.toJSON {} {inherit orga repo;}}'
|
||||
'')}
|
||||
( echo "{ lib }:"
|
||||
echo "{"
|
||||
|
@ -443,5 +444,30 @@ let
|
|||
> "$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 it’s 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
|
||||
update-all-grammars
|
||||
|
|
Loading…
Reference in a new issue