Make repo update script fill in the latest TS commit

I want to publish a new version of the Zed plugin, and it points to the
Tree-sitter grammar by commit. This is what got the whole "rcl patch"
thing started!

So now, upon export of the Zed extension, we'll pin it to the HEAD of
the adjacent tree-sitter-rcl repository.
This commit is contained in:
Ruud van Asseldonk 2025-08-30 11:24:07 +02:00
parent 78a9339d35
commit 8ccd47871b
2 changed files with 93 additions and 64 deletions

View file

@ -12,11 +12,9 @@ let root = import "//Cargo.rcl";
grammars = { grammars = {
rcl = { rcl = {
repository = "https://github.com/rcl-lang/tree-sitter-rcl", repository = "https://github.com/rcl-lang/tree-sitter-rcl",
// TODO: We should load this from a file, so that `update_repos.py` can // When tools/update_repos.py exports this to the zed-rcl repository, it
// write the new commit after it exports the new Tree-sitter grammar. // replaces this commit with the HEAD of the tree-sitter-rcl repository.
// But even nicer would be if we had an `rcl patch` command that could
// update the value in this file in-place.
commit = "e371920de7d1921a4ac9c6d78bd74ea3e94dfade", commit = "e371920de7d1921a4ac9c6d78bd74ea3e94dfade",
} },
} },
} }

View file

@ -39,15 +39,24 @@ repositories as a Git submodule here?
an external repository, we can have both. an external repository, we can have both.
""" """
import json
import shutil import shutil
import sys import sys
import textwrap import textwrap
import tomllib
from os import path from os import path
from subprocess import check_output from subprocess import check_output, check_call
from typing import List from typing import List
def get_rcl_version() -> str:
# TODO: Would be nice to load Cargo.rcl instead, with the RCL module.
with open("Cargo.toml", "rb") as f:
version: str = tomllib.load(f)["package"]["version"]
return version
def ignores_tree_sitter_rcl(dirname: str, _entries: List[str]) -> List[str]: def ignores_tree_sitter_rcl(dirname: str, _entries: List[str]) -> List[str]:
if dirname == "grammar/tree-sitter-rcl": if dirname == "grammar/tree-sitter-rcl":
return [".gitignore", "Cargo.rcl"] return [".gitignore", "Cargo.rcl"]
@ -62,76 +71,98 @@ def ignores_zed(dirname: str, _entries: List[str]) -> List[str]:
return [] return []
def git_head(repo_dir: str) -> str:
return check_output(
["git", "-C", repo_dir, "rev-parse", "HEAD"], encoding="ascii"
).strip()
def git_commit(repo_dir: str, message: str) -> None: def git_commit(repo_dir: str, message: str) -> None:
print(f"Creating commit in {repo_dir} ...") print(f"Creating commit in {repo_dir} ...")
git = ["git", "-C", repo_dir] git = ["git", "-C", repo_dir]
check_output([*git, "add", "."]) check_output([*git, "add", "."])
out = check_output([*git, "commit", "--message", message.strip()]) check_call([*git, "commit", "--message", message.strip()])
print(out.decode("utf-8"))
def get_message(src_dir: str) -> str:
rcl_version = get_rcl_version()
git_describe = check_output(["git", "describe", "--dirty"]).decode("utf-8").strip()
commit_hash = check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
return textwrap.dedent(
f"""
Update to RCL {rcl_version}
This is a generated export based on files in the {src_dir}
directory of the main RCL repository. It is generated by
tools/update_repos.py.
Upstream-Describe: {git_describe}
Upstream-Commit: {commit_hash}
"""
)
def update_zed_rcl() -> None:
assert path.isdir("../zed-rcl/.git")
src_dir = "grammar/zed"
dst_dir = "../zed-rcl"
# Before we export, update `extension.toml` to point to the correct Git commit.
ts_commit = git_head("../tree-sitter-rcl")
check_call(
[
"rcl",
"patch",
"--in-place",
"grammar/zed/extension.rcl",
"grammars.rcl.commit",
json.dumps(ts_commit),
]
)
check_call(["rcl", "build"])
# TODO: It would be nice to skip the index and just generate the right
# Git tree. This is possible with `git fast-import`, which is also what
# MkDocs uses under the hood. But let's not go there right now.
shutil.copytree(
src=src_dir,
dst=dst_dir,
ignore=ignores_zed,
dirs_exist_ok=True,
)
git_commit(dst_dir, get_message(src_dir))
def update_tree_sitter_rcl() -> None:
assert path.isdir("../tree-sitter-rcl/.git")
src_dir = "grammar/tree-sitter-rcl"
dst_dir = "../tree-sitter-rcl"
# Rebuild the Tree-sitter grammar to ensure that we don't copy anything
# stale.
check_output(["tree-sitter", "generate", "--build"], cwd=src_dir)
shutil.copytree(
src=src_dir,
dst=dst_dir,
ignore=ignores_tree_sitter_rcl,
dirs_exist_ok=True,
)
git_commit(dst_dir, get_message(src_dir))
def main() -> None: def main() -> None:
assert path.isdir("../tree-sitter-rcl/.git")
assert path.isdir("../zed-rcl/.git")
current_desc = check_output(["git", "describe", "--dirty"]).decode("utf-8")
current_commit = check_output(["git", "rev-parse", "HEAD"]).decode("ascii")
repos = sys.argv[1:] repos = sys.argv[1:]
if repos == []: if repos == []:
repos = ["tree-sitter-rcl", "zed-rcl"] repos = ["tree-sitter-rcl", "zed-rcl"]
if "zed-rcl" in repos:
src_dir = "grammar/zed"
dst_dir = "../zed-rcl"
# TODO: It would be nice to skip the index and just generate the right
# Git tree. This is possible with `git fast-import`, which is also what
# MkDocs uses under the hood. But let's not go there right now.
shutil.copytree(
src=src_dir,
dst=dst_dir,
ignore=ignores_zed,
dirs_exist_ok=True,
)
message = textwrap.dedent(
f"""
Update to RCL {current_desc}
This is a generated export based on files in the {src_dir}
directory of the main RCL repository. It is generated by
tools/update_repos.py.
Upstream-Commit: {current_commit}
"""
)
git_commit(dst_dir, message)
if "tree-sitter-rcl" in repos: if "tree-sitter-rcl" in repos:
src_dir = "grammar/tree-sitter-rcl" update_tree_sitter_rcl()
dst_dir = "../tree-sitter-rcl"
# Rebuild the Tree-sitter grammar to ensure that we don't copy anything if "zed-rcl" in repos:
# stale. update_zed_rcl()
check_output(["tree-sitter", "generate", "--build"], cwd=src_dir)
shutil.copytree(
src=src_dir,
dst=dst_dir,
ignore=ignores_tree_sitter_rcl,
dirs_exist_ok=True,
)
message = textwrap.dedent(
f"""
Update to RCL {current_desc}
This is a generated export based on files in the {src_dir}
directory of the main RCL repository. It is generated by
tools/update_repos.py.
Upstream-Commit: {current_commit}
"""
)
git_commit(dst_dir, message)
if __name__ == "__main__": if __name__ == "__main__":