tree-sitter/update: move printing of the import nix file to python

This is a bit crappy, but the complexity should go away once it’s done
in all python. Might have been wiser to rewrite the other stuff first.
This commit is contained in:
Profpatsch 2022-09-12 17:43:26 +02:00
parent a953387d22
commit 484bce31b7
2 changed files with 53 additions and 11 deletions

View file

@ -393,6 +393,7 @@ let
curl = "${curl}/bin/curl";
nix-prefetch-git = "${nix-prefetch-git}/bin/nix-prefetch-git";
inherit atomically-write;
printf = "${coreutils}/bin/printf";
};
inherit
knownTreeSitterOrgGrammarRepos
@ -446,16 +447,17 @@ let
})
allGrammars)
}
${atomically-write} \
"${outputDir}/default.nix" \
${writeShellScript "print-all-grammars" ''
echo "{ lib }:"
echo "{"
${foreachSh allGrammars
({name, ...}: ''
printf " %s = lib.importJSON ./%s.json;\n" "${name}" "${name}"'')}
echo "}"
''}
${fetchImpl} print-all-grammars-nix-file "$(< ${
jsonFile "all-grammars.json" {
allGrammars =
(lib.mapAttrsToList
(nixRepoAttrName: attrs: attrs // {
inherit nixRepoAttrName;
})
allGrammars);
inherit outputDir;
}
})"
'';
# Atomically write a file (just `>` redirection in bash

View file

@ -3,7 +3,7 @@ import json
import subprocess as sub
import os
import sys
from typing import Iterator, Any, Literal
from typing import Iterator, Any, Literal, TypedDict
debug: bool = True if os.environ.get("DEBUG", False) else False
Bin = str
@ -160,6 +160,44 @@ def checkTreeSitterRepos() -> None:
sys.exit(f"These repositories are neither known nor ignored:\n{unknown}")
Grammar = TypedDict(
"Grammar",
{
"nixRepoAttrName": str,
"orga": str,
"repo": str
}
)
def printAllGrammarsNixFile() -> None:
"""Print a .nix file that imports all grammars."""
allGrammars: list[dict[str, Grammar]] = jsonArg["allGrammars"]
outputDir: Dir = jsonArg["outputDir"]
def file() -> Iterator[str]:
yield "{ lib }:"
yield "{"
for grammar in allGrammars:
n = grammar["nixRepoAttrName"]
yield f" {n} = lib.importJSON ./{n}.json;"
yield "}"
yield ""
out = run_cmd(
# TODO: implement atomic file write in python
atomically_write_args(
os.path.join(
outputDir,
"default.nix"
),
iter([bins["printf"], "%s", "\n".join(list(file()))])
)
)
if out:
log(str(out))
match mode:
case "fetch-repo":
fetchRepo()
@ -167,5 +205,7 @@ match mode:
fetchOrgaLatestRepos()
case "check-tree-sitter-repos":
checkTreeSitterRepos()
case "print-all-grammars-nix-file":
printAllGrammarsNixFile()
case _:
sys.exit(f"mode {mode} unknown")