about summary refs log tree commit diff
path: root/pkgs/applications/editors/neovim/update-treesitter-parsers.py
blob: 27260ca64917211942942be2dc2316238eb4e463 (plain) (blame)
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
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3

import re
import subprocess
from pathlib import Path

parsers = {}
dir = Path(__file__).parent
regex = re.compile(r"^set\(TREESITTER_([A-Z_]+)_(URL|SHA256)\s+([^ \)]+)\s*\)\s*$")

src = subprocess.check_output(
    [
        "nix-build",
        dir.parent.parent.parent.parent,
        "-A",
        "neovim-unwrapped.src",
        "--no-out-link",
    ],
    text=True,
).strip()

for line in open(f"{src}/cmake.deps/CMakeLists.txt"):
    m = regex.fullmatch(line)
    if m is None:
        continue

    lang = m[1].lower()
    ty = m[2]
    val = m[3]

    if not lang in parsers:
        parsers[lang] = {}
    parsers[lang][ty] = val

with open(dir / "treesitter-parsers.nix", "w") as f:
    f.write("{ fetchurl }:\n\n{\n")
    for lang, src in parsers.items():
        f.write(
            f"""  {lang} = fetchurl {{
    url = "{src["URL"]}";
    hash = "sha256:{src["SHA256"]}";
  }};
"""
        )
    f.write("}\n")