rcl/tools/update_repos.py
Ruud van Asseldonk ce38b059e0 Finish initial version of tree-sitter-rcl dumper
It can generate commits, it's fine for now.
2024-08-02 21:33:05 +02:00

90 lines
2.8 KiB
Python
Executable file

#!/usr/bin/env python3
# RCL -- A reasonable configuration language.
# Copyright 2024 Ruud van Asseldonk
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# A copy of the License has been included in the root of the repository.
"""
Publish files tracked in the RCL (mono)repository into separate repositories.
Some tools -- in particular around Tree-sitter -- are repository-oriented and
expect some code that we rather track in subdirectories, to live in their own
repository. This script updates those external repositories to match the source
of truth in this repository.
REPOSITORIES
We expect the following directories to exist relative to the repository root:
../tree-sitter-rcl
../zed-rcl
RATIONALE
Why export to an external repository rather than including those external
repositories as a Git submodule here?
* Git submodules currently do not work very nicely with Nix flakes, which
would make it harder to use the current Nix-based CI.
* Some consumers of Tree-sitter grammars prefer to have the generated files
be part of the repository, but in this repository we prefer to be minimal
and don't have large assets that are effectively opaque like binaries. In
an external repository, we can have both.
"""
import os
import shutil
import textwrap
from os import path
from subprocess import check_output
from typing import List
def ignores_tree_sitter_rcl(dirname: str, _entries: List[str]) -> List[str]:
if dirname == "grammar/tree-sitter-rcl":
return [".gitignore"]
else:
return []
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")
# 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.
src_dir = "grammar/tree-sitter-rcl"
dst_dir = "../tree-sitter-rcl"
shutil.copytree(
src=src_dir,
dst=dst_dir,
ignore=ignores_tree_sitter_rcl,
dirs_exist_ok=True,
)
git = ["git", "-C", dst_dir]
check_output([*git, "add", "."])
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}
"""
)
out = check_output([*git, "commit", "--message", message.strip()])
print(out.decode("utf-8"))
if __name__ == "__main__":
main()