4 # $ nix run .\#vimPluginsUpdater
6 # $ nix run nixpkgs#python3Packages.black -- update.py
8 # $ nix run nixpkgs#python3Packages.mypy -- update.py
10 # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
12 # If you see `HTTP Error 429: too many requests` errors while running this
15 # https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
17 # (or the equivalent file /doc/languages-frameworks/vim.section.md
18 # from Nixpkgs master tree).
27 from typing
import List
, Tuple
28 from pathlib
import Path
31 log
= logging
.getLogger("vim-updater")
33 sh
= logging
.StreamHandler()
34 formatter
= logging
.Formatter("%(name)s:%(levelname)s: %(message)s")
35 sh
.setFormatter(formatter
)
38 # Import plugin update library from maintainers/scripts/pluginupdate.py
39 ROOT
= Path(os
.path
.dirname(os
.path
.abspath(inspect
.getfile(inspect
.currentframe()))))
42 from pluginupdate
import run_nix_expr
, PluginDesc
44 treesitter
= importlib
.import_module('nvim-treesitter.update')
49 "# GENERATED by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!"
52 NIXPKGS_NVIMTREESITTER_FOLDER
= "pkgs/applications/editors/vim/plugins/nvim-treesitter"
55 class VimEditor(pluginupdate
.Editor
):
56 nvim_treesitter_updated
= False
60 plugins
: List
[Tuple
[PluginDesc
, pluginupdate
.Plugin
]],
63 log
.info("Generating nix code")
64 sorted_plugins
= sorted(plugins
, key
=lambda v
: v
[0].name
.lower())
65 log
.debug("Loading nvim-treesitter revision from nix...")
66 nvim_treesitter_rev
= pluginupdate
.run_nix_expr(
67 "(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev",
73 with import <localpkgs> {};
74 lib.attrNames lua51Packages"""
75 log
.debug("Loading list of lua plugins...")
76 luaPlugins
= run_nix_expr(GET_PLUGINS_LUA
, self
.nixpkgs
, timeout
=30)
78 def _isNeovimPlugin(plug
: pluginupdate
.Plugin
) -> bool:
80 Whether it's a neovim-only plugin
81 We can check if it's available in lua packages
83 if plug
.normalized_name
in luaPlugins
:
84 log
.debug("%s is a neovim plugin", plug
)
88 with
open(outfile
, "w+") as f
:
89 log
.debug("Writing to %s", outfile
)
94 { lib, buildVimPlugin, buildNeovimPlugin, fetchFromGitHub, fetchgit }:
101 for pdesc
, plugin
in sorted_plugins
:
102 content
= self
.plugin2nix(pdesc
, plugin
, _isNeovimPlugin(plugin
))
105 plugin
.name
== "nvim-treesitter"
106 and plugin
.commit
!= nvim_treesitter_rev
108 self
.nvim_treesitter_updated
= True
110 print(f
"updated {outfile}")
112 def plugin2nix(self
, pdesc
: PluginDesc
, plugin
: pluginupdate
.Plugin
, isNeovim
: bool) -> str:
116 content
= f
" {plugin.normalized_name} = "
117 src_nix
= repo
.as_nix(plugin
)
118 content
+= """{buildFn} {{
119 pname = "{plugin.name}";
120 version = "{plugin.version}";
122 meta.homepage = "{repo.uri}";
126 buildFn
="buildNeovimPlugin" if isNeovim
else "buildVimPlugin",
134 def update(self
, args
):
135 pluginupdate
.update_plugins(self
, args
)
137 # TODO this should probably be skipped when running outside a nixpkgs checkout
138 if self
.nvim_treesitter_updated
:
139 print("updating nvim-treesitter grammars")
142 "vimPlugins.nvim-treesitter.src", "-f", self
.nixpkgs
143 , "--print-out-paths"
145 log
.debug("Running command: %s", " ".join(cmd
))
146 nvim_treesitter_dir
= subprocess
.check_output(cmd
, text
=True, timeout
=90).strip()
148 generated
= treesitter
.update_grammars(nvim_treesitter_dir
)
149 treesitter_generated_nix_path
= os
.path
.join(
150 NIXPKGS_NVIMTREESITTER_FOLDER
,
153 open(os
.path
.join(args
.nixpkgs
, treesitter_generated_nix_path
), "w").write(generated
)
155 if self
.nixpkgs_repo
:
156 index
= self
.nixpkgs_repo
.index
157 for diff
in index
.diff(None):
158 if diff
.a_path
== treesitter_generated_nix_path
:
159 msg
= "vimPlugins.nvim-treesitter: update grammars"
160 print(f
"committing to nixpkgs: {msg}")
161 index
.add([treesitter_generated_nix_path
])
164 print("no updates to nvim-treesitter grammars")
170 log
.debug(f
"Loading from {ROOT}/get-plugins.nix")
171 with
open(f
"{ROOT}/get-plugins.nix") as f
:
172 GET_PLUGINS
= f
.read()
174 "vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS
179 if __name__
== "__main__":