-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·193 lines (164 loc) · 6.16 KB
/
update.py
File metadata and controls
executable file
·193 lines (164 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# run with:
# $ nix run .\#vimPluginsUpdater
# format:
# $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check:
# $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted:
# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
# If you see `HTTP Error 429: too many requests` errors while running this
# script, refer to:
#
# https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
#
# (or the equivalent file /doc/languages-frameworks/vim.section.md
# from Nixpkgs master tree).
#
import inspect
import json
import logging
import os
import textwrap
from pathlib import Path
from typing import List, Tuple
log = logging.getLogger("vim-updater")
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
import importlib
import nixpkgs_plugin_update
from nixpkgs_plugin_update import PluginDesc, run_nix_expr
treesitter = importlib.import_module("nvim-treesitter.update")
HEADER = "# GENERATED by ./pkgs/applications/editors/vim/plugins/utils/update.py. Do not edit!"
NIXPKGS_NVIMTREESITTER_FOLDER = "pkgs/applications/editors/vim/plugins/nvim-treesitter"
SPDX_LICENSE_NORMALIZATION = {
"AGPL-3.0": "AGPL-3.0-only",
"GPL-2.0": "GPL-2.0-only",
"GPL-3.0": "GPL-3.0-only",
"LGPL-2.0": "LGPL-2.0-only",
"LGPL-2.1": "LGPL-2.1-only",
"LGPL-3.0": "LGPL-3.0-only",
}
class VimEditor(nixpkgs_plugin_update.Editor):
nvim_treesitter_updated = False
def generate_nix(
self,
plugins: List[Tuple[PluginDesc, nixpkgs_plugin_update.Plugin]],
outfile: str,
):
log.info("Generating nix code")
log.debug("Loading nvim-treesitter source reference from nix...")
nvim_treesitter_ref = nixpkgs_plugin_update.run_nix_expr(
"(let src = (import <localpkgs> { }).vimPlugins.nvim-treesitter.src; in if src.tag != null then src.tag else src.rev)",
self.nixpkgs,
timeout=10,
)
GET_PLUGINS_LUA = """
with import <localpkgs> {};
lib.attrNames lua51Packages"""
log.debug("Loading list of lua plugins...")
luaPlugins = run_nix_expr(GET_PLUGINS_LUA, self.nixpkgs, timeout=30)
def _isNeovimPlugin(plug: nixpkgs_plugin_update.Plugin) -> bool:
"""
Whether it's a neovim-only plugin
We can check if it's available in lua packages
"""
if plug.normalized_name in luaPlugins:
log.debug("%s is a neovim plugin", plug)
return True
return False
with open(outfile, "w+") as f:
log.debug("Writing to %s", outfile)
f.write(HEADER)
f.write(
textwrap.dedent(
"""
{
lib,
buildVimPlugin,
fetchFromGitHub,
fetchgit,
}:
final: prev: {
"""
)
)
for pdesc, plugin in plugins:
content = self.plugin2nix(pdesc, plugin, _isNeovimPlugin(plugin))
f.write(content)
if (
plugin.name == "nvim-treesitter"
and (plugin.tag or plugin.commit) != nvim_treesitter_ref
):
self.nvim_treesitter_updated = True
f.write("}\n")
print(f"updated {outfile}")
def plugin2nix(
self, pdesc: PluginDesc, plugin: nixpkgs_plugin_update.Plugin, isNeovim: bool
) -> str:
if isNeovim:
raise RuntimeError(
f"Plugin {plugin.name} is already packaged in `luaPackages`, please use that"
)
repo = pdesc.repo
content = f" {plugin.normalized_name} = "
src_nix = repo.as_nix(plugin)
license_spdx_id = plugin.license
if license_spdx_id is not None:
license_spdx_id = SPDX_LICENSE_NORMALIZATION.get(
license_spdx_id, license_spdx_id
)
license_nix = (
f" meta.license = lib.meta.getLicenseFromSpdxId {json.dumps(license_spdx_id)};\n"
if license_spdx_id
else " meta.license = lib.licenses.unfree;\n"
)
content += """{buildFn} {{
pname = "{plugin.name}";
version = "{plugin.version}";
src = {src_nix};
meta.homepage = "{repo.uri}";
{license_nix}\
meta.hydraPlatforms = [ ];
}};
""".format(
buildFn="buildNeovimPlugin" if isNeovim else "buildVimPlugin",
plugin=plugin,
src_nix=src_nix,
repo=repo,
license_nix=license_nix,
)
log.debug(content)
return content
def update(self, args):
nixpkgs_plugin_update.update_plugins(self, args)
# TODO this should probably be skipped when running outside a nixpkgs checkout
if self.nvim_treesitter_updated:
print("updating nvim-treesitter grammars")
generated = treesitter.update_grammars()
treesitter_generated_nix_path = os.path.join(
NIXPKGS_NVIMTREESITTER_FOLDER, "generated.nix"
)
open(os.path.join(args.nixpkgs, treesitter_generated_nix_path), "w").write(
generated
)
if self.nixpkgs_repo:
index = self.nixpkgs_repo.index
for diff in index.diff(None):
if diff.a_path == treesitter_generated_nix_path:
msg = "vimPlugins.nvim-treesitter: update grammars"
print(f"committing to nixpkgs: {msg}")
index.add([treesitter_generated_nix_path])
index.commit(msg)
return
print("no updates to nvim-treesitter grammars")
def main():
global luaPlugins
log.debug(f"Loading from {ROOT}/get-plugins.nix")
with open(f"{ROOT}/get-plugins.nix") as f:
GET_PLUGINS = f.read()
editor = VimEditor(
"vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS
)
editor.run()
if __name__ == "__main__":
main()