vimPluginsUpdater: fix nvim-tree-sitter update (#393721)

authored by Gaétan Lepage and committed by GitHub 35a59011 3272eda1

+102 -99
+1 -1
pkgs/applications/editors/vim/plugins/nvim-treesitter/update-shell.nix pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update-shell.nix
··· 1 1 { 2 - pkgs ? import ../../../../../.. { }, 2 + pkgs ? import ../../../../../../.. { }, 3 3 }: 4 4 5 5 with pkgs;
-98
pkgs/applications/editors/vim/plugins/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 subprocess 7 - from concurrent.futures import ThreadPoolExecutor 8 - import os 9 - import sys 10 - from os.path import join 11 - 12 - log = logging.getLogger("vim-updater") 13 - 14 - 15 - def generate_grammar(lang, rev, cfg): 16 - """Generate grammar for a language""" 17 - info = cfg["install_info"] 18 - url = info["url"] 19 - 20 - generated = f""" {lang} = buildGrammar {{ 21 - language = "{lang}"; 22 - version = "0.0.0+rev={rev[:7]}"; 23 - src = """ 24 - 25 - generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True) 26 - generated += ";" 27 - 28 - location = info.get("location") 29 - if location: 30 - generated += f""" 31 - location = "{location}";""" 32 - 33 - if info.get("requires_generate_from_grammar"): 34 - generated += """ 35 - generate = true;""" 36 - 37 - generated += f""" 38 - meta.homepage = "{url}"; 39 - }}; 40 - """ 41 - 42 - return generated 43 - 44 - 45 - def update_grammars(nvim_treesitter_dir: str): 46 - """ 47 - The lockfile contains just revisions so we start neovim to dump the 48 - grammar information in a better format 49 - """ 50 - # the lockfile 51 - cmd = [ 52 - "nvim", 53 - "--headless", 54 - "-u", 55 - "NONE", 56 - "--cmd", 57 - f"set rtp^={nvim_treesitter_dir}", 58 - "+lua io.write(vim.json.encode(require('nvim-treesitter.parsers').get_parser_configs()))", 59 - "+quit!", 60 - ] 61 - log.debug("Running command: %s", ' '.join(cmd)) 62 - configs = json.loads(subprocess.check_output(cmd)) 63 - 64 - generated_file = """# generated by pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py 65 - 66 - { buildGrammar, """ 67 - 68 - generated_file += subprocess.check_output(["nurl", "-Ls", ", "], text=True) 69 - 70 - generated_file += """ }: 71 - 72 - { 73 - """ 74 - 75 - lockfile_path = os.path.join(nvim_treesitter_dir, "lockfile.json") 76 - log.debug("Opening %s", lockfile_path) 77 - with open(lockfile_path) as lockfile_fd: 78 - lockfile = json.load(lockfile_fd) 79 - 80 - def _generate_grammar(item): 81 - lang, lock = item 82 - cfg = configs.get(lang) 83 - if not cfg: 84 - return "" 85 - return generate_grammar(lang, lock["revision"], cfg) 86 - 87 - for generated in ThreadPoolExecutor(max_workers=5).map( 88 - _generate_grammar, lockfile.items() 89 - ): 90 - generated_file += generated 91 - 92 - generated_file += "}\n" 93 - return generated_file 94 - 95 - 96 - if __name__ == "__main__": 97 - generated = update_grammars(sys.argv[1]) 98 - open(join(os.path.dirname(__file__), "generated.nix"), "w").write(generated)
+101
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 + import sys 9 + from concurrent.futures import ThreadPoolExecutor 10 + 11 + log = logging.getLogger("vim-updater") 12 + 13 + 14 + def generate_grammar(lang, rev, cfg): 15 + """Generate grammar for a language""" 16 + info = cfg["install_info"] 17 + url = info["url"] 18 + 19 + generated = f""" {lang} = buildGrammar {{ 20 + language = "{lang}"; 21 + version = "0.0.0+rev={rev[:7]}"; 22 + src = """ 23 + 24 + generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True) 25 + generated += ";" 26 + 27 + location = info.get("location") 28 + if location: 29 + generated += f""" 30 + location = "{location}";""" 31 + 32 + if info.get("requires_generate_from_grammar"): 33 + generated += """ 34 + generate = true;""" 35 + 36 + generated += f""" 37 + meta.homepage = "{url}"; 38 + }}; 39 + """ 40 + 41 + return generated 42 + 43 + 44 + def update_grammars(nvim_treesitter_dir: str): 45 + """ 46 + The lockfile contains just revisions so we start neovim to dump the 47 + grammar information in a better format 48 + """ 49 + # the lockfile 50 + cmd = [ 51 + "nvim", 52 + "--headless", 53 + "-u", 54 + "NONE", 55 + "--cmd", 56 + f"set rtp^={nvim_treesitter_dir}", 57 + "+lua io.write(vim.json.encode(require('nvim-treesitter.parsers').get_parser_configs()))", 58 + "+quit!", 59 + ] 60 + log.debug("Running command: %s", ' '.join(cmd)) 61 + configs = json.loads(subprocess.check_output(cmd)) 62 + 63 + generated_file = """# generated by pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py 64 + 65 + { buildGrammar, """ 66 + 67 + generated_file += subprocess.check_output(["nurl", "-Ls", ", "], text=True) 68 + 69 + generated_file += """ }: 70 + 71 + { 72 + """ 73 + 74 + lockfile_path = os.path.join(nvim_treesitter_dir, "lockfile.json") 75 + log.debug("Opening %s", lockfile_path) 76 + with open(lockfile_path) as lockfile_fd: 77 + lockfile = json.load(lockfile_fd) 78 + 79 + def _generate_grammar(item): 80 + lang, lock = item 81 + cfg = configs.get(lang) 82 + if not cfg: 83 + return "" 84 + return generate_grammar(lang, lock["revision"], cfg) 85 + 86 + for generated in ThreadPoolExecutor(max_workers=5).map( 87 + _generate_grammar, lockfile.items() 88 + ): 89 + generated_file += generated 90 + 91 + generated_file += "}\n" 92 + return generated_file 93 + 94 + 95 + if __name__ == "__main__": 96 + generated = update_grammars(sys.argv[1]) 97 + output_path = os.path.join( 98 + os.path.dirname(__file__), 99 + "../../nvim-treesitter/generated.nix" 100 + ) 101 + open(output_path, "w").write(generated)