mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:10:12 +01:00
TextMate doesn't support JSON grammars, so convert our JSON grammar to a plist automatically as a pre-commit check. Fix malformed info.plist file. Add missing uuid to grammar file. Assisted-by: Gemini via Antigravity
63 lines
1.7 KiB
Python
Executable File
63 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env -S uv run --script
|
|
|
|
# /// script
|
|
# requires-python = ">=3.12"
|
|
# ///
|
|
|
|
"""Updates the TextMate plist grammar from the VS Code JSON grammar."""
|
|
|
|
__copyright__ = """
|
|
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
Exceptions. See /LICENSE for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
"""
|
|
|
|
import json
|
|
import plistlib
|
|
from pathlib import Path
|
|
|
|
|
|
def main() -> None:
|
|
# Find repository root.
|
|
repo_root = Path(__file__).resolve().parents[1]
|
|
|
|
json_path = repo_root / "utils" / "vscode" / "carbon.tmLanguage.json"
|
|
plist_path = (
|
|
repo_root / "utils" / "textmate" / "Syntaxes" / "carbon.tmLanguage"
|
|
)
|
|
|
|
# Read and parse the JSON grammar.
|
|
with open(json_path, "r", encoding="utf-8") as f:
|
|
grammar_data = json.load(f)
|
|
|
|
# Generate plist bytes.
|
|
plist_bytes = plistlib.dumps(grammar_data, fmt=plistlib.FMT_XML)
|
|
|
|
# Replace the file header.
|
|
file_header = b"""<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!--
|
|
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
Exceptions. See /LICENSE for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
-->
|
|
|
|
<!--
|
|
THIS FILE IS AUTOGENERATED FROM carbon.tmLanguage.json. DO NOT EDIT.
|
|
-->
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
"""
|
|
(_, body) = plist_bytes.split(b"<plist", 1)
|
|
plist_bytes = file_header + b"<plist" + body
|
|
|
|
# Write the plist grammar.
|
|
with open(plist_path, "wb") as f:
|
|
f.write(plist_bytes)
|
|
|
|
print(f"Successfully generated {plist_path.relative_to(repo_root)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|