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

authored by Matthieu Coudron and committed by GitHub e4424cba 559a668d

+27 -33
+27 -33
pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py
··· 1 #!/usr/bin/env nix-shell 2 - #!nix-shell update-shell.nix -i python 3 4 import json 5 import logging 6 import os 7 import subprocess 8 from concurrent.futures import ThreadPoolExecutor 9 10 import requests 11 12 log = logging.getLogger("vim-updater") 13 14 - NURR_JSON_URL = "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" 15 16 def generate_grammar(lang, parser_info): 17 """Generate grammar for a language based on the parser info""" 18 - try: 19 - if "install_info" not in parser_info: 20 - log.warning(f"Parser {lang} does not have install_info, skipping") 21 - return "" 22 23 - install_info = parser_info["install_info"] 24 25 - url = install_info["url"] 26 - rev = install_info["revision"] 27 28 - generated = f""" {lang} = buildGrammar {{ 29 language = "{lang}"; 30 version = "0.0.0+rev={rev[:7]}"; 31 src = """ 32 33 - generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True) 34 - generated += ";" 35 36 - location = install_info.get("location", "") 37 - if location: 38 - generated += f""" 39 location = "{location}";""" 40 41 - if install_info.get("generate", False): 42 - generated += """ 43 generate = true;""" 44 45 - generated += f""" 46 meta.homepage = "{url}"; 47 }}; 48 """ 49 50 - return generated 51 - except Exception as e: 52 - log.error(f"Error generating grammar for {lang}: {e}") 53 - return "" 54 55 56 def fetch_nurr_parsers(): ··· 81 82 def process_parser_info(parser_info): 83 """Process a single parser info entry and generate grammar for it""" 84 - try: 85 - lang = parser_info["lang"] 86 - return generate_grammar(lang, parser_info) 87 - except Exception as e: 88 - log.error(f"Error processing parser: {e}") 89 - return "" 90 91 92 def update_grammars(): ··· 119 120 121 if __name__ == "__main__": 122 - logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') 123 124 generated = update_grammars() 125 - output_path = os.path.join( 126 - os.path.dirname(__file__), 127 - "../../nvim-treesitter/generated.nix" 128 - ) 129 log.info("Writing output to %s", output_path) 130 with open(output_path, "w") as f: 131 f.write(generated)
··· 1 #!/usr/bin/env nix-shell 2 + #!nix-shell ./update-shell.nix -i python 3 4 import json 5 import logging 6 import os 7 import subprocess 8 from concurrent.futures import ThreadPoolExecutor 9 + from pathlib import Path 10 11 import requests 12 13 log = logging.getLogger("vim-updater") 14 15 + NURR_JSON_URL = ( 16 + "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" 17 + ) 18 + 19 20 def generate_grammar(lang, parser_info): 21 """Generate grammar for a language based on the parser info""" 22 + if "install_info" not in parser_info: 23 + log.warning(f"Parser {lang} does not have install_info, skipping") 24 + return "" 25 26 + install_info = parser_info["install_info"] 27 28 + url = install_info["url"] 29 + rev = install_info["revision"] 30 31 + generated = f""" {lang} = buildGrammar {{ 32 language = "{lang}"; 33 version = "0.0.0+rev={rev[:7]}"; 34 src = """ 35 36 + generated += subprocess.check_output( 37 + ["nurl", url, rev, "--indent=4"], text=True 38 + ) 39 + generated += ";" 40 41 + location = install_info.get("location", "") 42 + if location: 43 + generated += f""" 44 location = "{location}";""" 45 46 + if install_info.get("generate", False): 47 + generated += """ 48 generate = true;""" 49 50 + generated += f""" 51 meta.homepage = "{url}"; 52 }}; 53 """ 54 55 + return generated 56 57 58 def fetch_nurr_parsers(): ··· 83 84 def process_parser_info(parser_info): 85 """Process a single parser info entry and generate grammar for it""" 86 + return generate_grammar(parser_info["lang"], parser_info) 87 88 89 def update_grammars(): ··· 116 117 118 if __name__ == "__main__": 119 + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") 120 121 generated = update_grammars() 122 + output_path = Path(__file__).parent.parent / "nvim-treesitter/generated.nix" 123 log.info("Writing output to %s", output_path) 124 with open(output_path, "w") as f: 125 f.write(generated)