diff options
-rw-r--r-- | pkgs/development/tools/parsing/tree-sitter/update.nix | 22 | ||||
-rw-r--r-- | pkgs/development/tools/parsing/tree-sitter/update_impl.py | 42 |
2 files changed, 53 insertions, 11 deletions
diff --git a/pkgs/development/tools/parsing/tree-sitter/update.nix b/pkgs/development/tools/parsing/tree-sitter/update.nix index 17b13bad8b0a6..055f42dc720a3 100644 --- a/pkgs/development/tools/parsing/tree-sitter/update.nix +++ b/pkgs/development/tools/parsing/tree-sitter/update.nix @@ -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 diff --git a/pkgs/development/tools/parsing/tree-sitter/update_impl.py b/pkgs/development/tools/parsing/tree-sitter/update_impl.py index a65b7bef664a4..e9113c1ebd31b 100644 --- a/pkgs/development/tools/parsing/tree-sitter/update_impl.py +++ b/pkgs/development/tools/parsing/tree-sitter/update_impl.py @@ -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") |