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