nvim-treesitter grammars updater: avoid silent fails (#408463)

This commit is contained in:
Matthieu Coudron 2025-06-16 10:25:13 +02:00 committed by GitHub
commit e4424cba4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,56 +1,58 @@
#!/usr/bin/env nix-shell
#!nix-shell update-shell.nix -i python
#!nix-shell ./update-shell.nix -i python
import json
import logging
import os
import subprocess
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import requests
log = logging.getLogger("vim-updater")
NURR_JSON_URL = "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json"
NURR_JSON_URL = (
"https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json"
)
def generate_grammar(lang, parser_info):
"""Generate grammar for a language based on the parser info"""
try:
if "install_info" not in parser_info:
log.warning(f"Parser {lang} does not have install_info, skipping")
return ""
if "install_info" not in parser_info:
log.warning(f"Parser {lang} does not have install_info, skipping")
return ""
install_info = parser_info["install_info"]
install_info = parser_info["install_info"]
url = install_info["url"]
rev = install_info["revision"]
url = install_info["url"]
rev = install_info["revision"]
generated = f""" {lang} = buildGrammar {{
generated = f""" {lang} = buildGrammar {{
language = "{lang}";
version = "0.0.0+rev={rev[:7]}";
src = """
generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True)
generated += ";"
generated += subprocess.check_output(
["nurl", url, rev, "--indent=4"], text=True
)
generated += ";"
location = install_info.get("location", "")
if location:
generated += f"""
location = install_info.get("location", "")
if location:
generated += f"""
location = "{location}";"""
if install_info.get("generate", False):
generated += """
if install_info.get("generate", False):
generated += """
generate = true;"""
generated += f"""
generated += f"""
meta.homepage = "{url}";
}};
"""
return generated
except Exception as e:
log.error(f"Error generating grammar for {lang}: {e}")
return ""
return generated
def fetch_nurr_parsers():
@ -81,12 +83,7 @@ def fetch_nurr_parsers():
def process_parser_info(parser_info):
"""Process a single parser info entry and generate grammar for it"""
try:
lang = parser_info["lang"]
return generate_grammar(lang, parser_info)
except Exception as e:
log.error(f"Error processing parser: {e}")
return ""
return generate_grammar(parser_info["lang"], parser_info)
def update_grammars():
@ -119,13 +116,10 @@ def update_grammars():
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
generated = update_grammars()
output_path = os.path.join(
os.path.dirname(__file__),
"../../nvim-treesitter/generated.nix"
)
output_path = Path(__file__).parent.parent / "nvim-treesitter/generated.nix"
log.info("Writing output to %s", output_path)
with open(output_path, "w") as f:
f.write(generated)