vimPluginsUpdater: fix treesitter updates

+70 -53
+2 -4
maintainers/scripts/pluginupdate.py
··· 327 327 :param expr nix expression to fetch current plugins 328 328 :param nixpkgs Path towards a nixpkgs checkout 329 329 ''' 330 - # local_pkgs = str(Path(__file__).parent.parent.parent) 331 330 with CleanEnvironment(nixpkgs) as nix_path: 332 331 cmd = [ 333 332 "nix", ··· 341 340 "--nix-path", 342 341 nix_path, 343 342 ] 344 - log.debug("Running command %s", " ".join(cmd)) 345 - out = subprocess.check_output(cmd) 343 + log.debug("Running command: %s", " ".join(cmd)) 344 + out = subprocess.check_output(cmd, timeout=90) 346 345 data = json.loads(out) 347 346 return data 348 347 ··· 572 571 self.empty_config = NamedTemporaryFile() 573 572 self.empty_config.write(b"{}") 574 573 self.empty_config.flush() 575 - # os.environ["NIXPKGS_CONFIG"] = self.empty_config.name 576 574 return f"localpkgs={self.local_pkgs}" 577 575 578 576 def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
+45 -29
pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py
··· 2 2 #!nix-shell update-shell.nix -i python 3 3 4 4 import json 5 + import logging 5 6 import subprocess 6 7 from concurrent.futures import ThreadPoolExecutor 7 - from os import environ 8 - from os.path import dirname, join 8 + import os 9 + import sys 10 + from os.path import join 9 11 10 - configs = json.loads( 11 - subprocess.check_output( 12 - [ 13 - "nvim", 14 - "--headless", 15 - "-u", 16 - "NONE", 17 - "+lua io.write(vim.json.encode(require('nvim-treesitter.parsers').get_parser_configs()))", 18 - "+quit!", 19 - ] 20 - ) 21 - ) 12 + log = logging.getLogger("vim-updater") 22 13 23 14 24 - def generate_grammar(item): 25 - lang, lock = item 26 - cfg = configs.get(lang) 27 - if not cfg: 28 - return "" 29 - 15 + def generate_grammar(lang, rev, cfg): 16 + """Generate grammar for a language""" 30 17 info = cfg["install_info"] 31 18 url = info["url"] 32 - rev = lock["revision"] 33 19 34 20 generated = f""" {lang} = buildGrammar {{ 35 21 language = "{lang}"; ··· 56 42 return generated 57 43 58 44 59 - def update_grammars(lockfile: str): 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)) 60 63 61 64 generated_file = """# generated by pkgs/applications/editors/vim/plugins/nvim-treesitter/update.py 62 65 ··· 68 71 69 72 { 70 73 """ 71 - for generated in ThreadPoolExecutor().map(generate_grammar, lockfile.items()): 72 - generated_file += generated 73 - generated_file += "}\n" 74 - generated_file += "}\n" 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) 75 86 76 - open(join(dirname(__file__), "generated.nix"), "w").write(generated_file) 87 + for generated in ThreadPoolExecutor(max_workers=5).map( 88 + _generate_grammar, lockfile.items() 89 + ): 90 + generated_file += generated 91 + generated_file += "}\n" 92 + return generated_file 77 93 78 94 79 95 if __name__ == "__main__": 80 - # TODO add lockfile 81 - update_grammars() 96 + generated = update_grammars(sys.args[1]) 97 + open(join(os.path.dirname(__file__), "generated.nix"), "w").write(generated)
+20 -13
pkgs/applications/editors/vim/plugins/update.py
··· 23 23 import logging 24 24 import textwrap 25 25 import json 26 + import subprocess 26 27 from typing import List, Tuple 27 28 from pathlib import Path 28 29 29 30 30 - log = logging.getLogger() 31 + log = logging.getLogger("vim-updater") 31 32 32 33 sh = logging.StreamHandler() 33 34 formatter = logging.Formatter("%(name)s:%(levelname)s: %(message)s") ··· 39 40 import pluginupdate 40 41 import importlib 41 42 from pluginupdate import run_nix_expr, PluginDesc 42 - from treesitter import update_grammars 43 + import treesitter 43 44 44 45 45 46 HEADER = ( 46 47 "# GENERATED by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!" 47 48 ) 48 49 49 - NIXPKGS_NVIMTREESITTER_FOLDER = \ 50 - "pkgs/applications/editors/vim/plugins/nvim-treesitter/generated.nix" 50 + NIXPKGS_NVIMTREESITTER_FOLDER = "pkgs/applications/editors/vim/plugins/nvim-treesitter" 51 51 52 52 53 53 class VimEditor(pluginupdate.Editor): ··· 58 58 ): 59 59 sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower()) 60 60 nvim_treesitter_rev = pluginupdate.run_nix_expr( 61 - "(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev", 62 - self.nixpkgs 61 + "(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev", self.nixpkgs 63 62 ) 64 63 65 64 with open(outfile, "w+") as f: ··· 78 77 content = self.plugin2nix(pdesc, plugin) 79 78 f.write(content) 80 79 if ( 81 - plugin.name == "nvim-treesitter" and plugin.commit != nvim_treesitter_rev 80 + plugin.name == "nvim-treesitter" 81 + and plugin.commit != nvim_treesitter_rev 82 82 ): 83 83 self.nvim_treesitter_updated = True 84 84 f.write("\n}\n") ··· 126 126 def update(self, args): 127 127 pluginupdate.update_plugins(self, args) 128 128 129 + # TODO this should probably be skipped when running outside a nixpkgs checkout 129 130 if self.nvim_treesitter_updated: 130 131 print("updating nvim-treesitter grammars") 131 - nvim_treesitter_dir = ROOT.joinpath("nvim-treesitter") 132 - lockfile = os.path.join(args.nixpkgs.join(NIXPKGS_NVIMTREESITTER_FOLDER, "lockfile.json")) 133 - lockfile = json.load(open(lockfile)) 132 + cmd = [ 133 + "nix", "build", 134 + "vimPlugins.nvim-treesitter.src", "-f", self.nixpkgs 135 + , "--print-out-paths" 136 + ] 137 + log.debug("Running command: %s", " ".join(cmd)) 138 + nvim_treesitter_dir = subprocess.check_output(cmd, text=True, timeout=90).strip() 134 139 135 - nvim_treesitter.update_grammars(lockfile) 140 + generated = treesitter.update_grammars(nvim_treesitter_dir) 141 + open(os.path.join(args.nixpkgs, "generated.nix"), "w").write(generated) 136 142 137 143 if self.nixpkgs_repo: 138 144 index = self.nixpkgs_repo.index ··· 147 153 148 154 149 155 def main(): 150 - 151 156 global luaPlugins 152 157 153 158 log.debug(f"Loading from {ROOT}/../get-plugins.nix") 154 159 with open(f"{ROOT}/../get-plugins.nix") as f: 155 160 GET_PLUGINS = f.read() 156 - editor = VimEditor("vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS) 161 + editor = VimEditor( 162 + "vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS 163 + ) 157 164 editor.run() 158 165 159 166
+3 -7
pkgs/applications/editors/vim/plugins/updater.nix
··· 4 4 , python3Packages 5 5 , lib 6 6 , nix-prefetch-git 7 + , nurl 7 8 8 9 # optional 9 10 , vimPlugins 10 11 , neovim 11 12 }: 12 - let 13 - my_neovim = neovim.override { 14 - configure.packages.all.start = [ vimPlugins.nvim-treesitter ]; 15 - }; 16 - 17 - in 18 13 buildPythonApplication { 19 14 format = "other"; 20 15 pname = "vim-plugins-updater"; ··· 39 34 cp ${../../../../../maintainers/scripts/pluginupdate.py} $out/lib/pluginupdate.py 40 35 41 36 # wrap python scripts 42 - makeWrapperArgs+=( --prefix PATH : "${lib.makeBinPath [ nix nix-prefetch-git my_neovim ]}" --prefix PYTHONPATH : "$out/lib" ) 37 + makeWrapperArgs+=( --prefix PATH : "${lib.makeBinPath [ 38 + nix nix-prefetch-git neovim nurl ]}" --prefix PYTHONPATH : "$out/lib" ) 43 39 wrapPythonPrograms 44 40 ''; 45 41