about summary refs log tree commit diff
path: root/pkgs/applications/editors/emacs
diff options
context:
space:
mode:
authoradisbladis <adisbladis@gmail.com>2022-04-24 07:18:42 +1200
committeradisbladis <adisbladis@gmail.com>2022-04-24 07:35:00 +1200
commitb4f90318fecf53ca26a104a39ac42c914582dfe4 (patch)
tree7fcf2418543e1fd439faf2a1790d469a8afb60f1 /pkgs/applications/editors/emacs
parentb15c703495742190e6d5cf37ac0980391c323428 (diff)
downloadnixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar.gz
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar.bz2
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar.lz
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar.xz
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.tar.zst
nixlib-b4f90318fecf53ca26a104a39ac42c914582dfe4.zip
emacs.pkgs.tree-sitter-langs: Create script to keep grammars up to date with upstream defaults
Diffstat (limited to 'pkgs/applications/editors/emacs')
-rw-r--r--pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json32
-rw-r--r--pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix49
-rwxr-xr-xpkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py74
3 files changed, 114 insertions, 41 deletions
diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json
new file mode 100644
index 000000000000..6a5608cbf8d5
--- /dev/null
+++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default-grammars.json
@@ -0,0 +1,32 @@
+[
+  "tree-sitter-agda",
+  "tree-sitter-bash",
+  "tree-sitter-c",
+  "tree-sitter-c-sharp",
+  "tree-sitter-cpp",
+  "tree-sitter-css",
+  "tree-sitter-elixir",
+  "tree-sitter-elm",
+  "tree-sitter-fluent",
+  "tree-sitter-go",
+  "tree-sitter-haskell",
+  "tree-sitter-hcl",
+  "tree-sitter-html",
+  "tree-sitter-java",
+  "tree-sitter-javascript",
+  "tree-sitter-jsdoc",
+  "tree-sitter-json",
+  "tree-sitter-julia",
+  "tree-sitter-nix",
+  "tree-sitter-ocaml",
+  "tree-sitter-php",
+  "tree-sitter-prisma",
+  "tree-sitter-python",
+  "tree-sitter-ruby",
+  "tree-sitter-rust",
+  "tree-sitter-scala",
+  "tree-sitter-swift",
+  "tree-sitter-typescript",
+  "tree-sitter-verilog",
+  "tree-sitter-zig"
+]
diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix
index 93b4436e9520..99fa87abf018 100644
--- a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix
+++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/default.nix
@@ -12,55 +12,22 @@
 }:
 
 let
-
   inherit (melpaStablePackages) tree-sitter-langs;
 
-  # Note: Commented grammars are in upstream bundle but missing from our packaged grammars
-  grammars = [
-    "agda"
-    "bash"
-    "c"
-    "c-sharp"
-    "cpp"
-    "css"
-    # "d"
-    "elixir"
-    "elm"
-    "fluent"
-    "go"
-    "haskell"
-    "hcl"
-    "html"
-    # "janet-simple"
-    "java"
-    "javascript"
-    "jsdoc"
-    "json"
-    "julia"
-    "nix"
-    "ocaml"
-    "ocaml-interface"
-    # "pgn"
-    "php"
-    "prisma"
-    "python"
-    "ruby"
-    "rust"
-    "scala"
-    "swift"
-    "tsx"
-    "typescript"
-    "verilog"
-    "zig"
-  ];
+  grammars = map (g: tree-sitter-grammars.${g}) (lib.importJSON ./default-grammars.json);
+
+  libSuffix = if stdenv.isDarwin then "dylib" else "so";
+  soName = g: lib.removeSuffix "-grammar" (lib.removePrefix "tree-sitter-" g.pname) + "." + libSuffix;
 
   grammarDir = runCommand "emacs-tree-sitter-grammars" {
-    # Fake same version number as upstream language bundle to prevent triggering downloads
+    # Fake same version number as upstream language bundle to prevent triggering runtime downloads
     inherit (tree-sitter-langs) version;
   } (''
     install -d $out/langs/bin
     echo -n $version > $out/langs/bin/BUNDLE-VERSION
-  '' + lib.concatStringsSep "\n" (map (g: "ln -s ${tree-sitter-grammars."tree-sitter-${g}"}/parser $out/langs/bin/${g}.so") grammars));
+  '' + lib.concatStringsSep "\n" (map (
+    g: "ln -s ${g}/parser $out/langs/bin/${soName g}") grammars
+  ));
 
 in
 melpaStablePackages.tree-sitter-langs.overrideAttrs(old: {
diff --git a/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py
new file mode 100755
index 000000000000..19bcb8989c30
--- /dev/null
+++ b/pkgs/applications/editors/emacs/elisp-packages/tree-sitter-langs/update-defaults.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env nix-shell
+#! nix-shell ../../../../../../. -i python3 -p python3 -p nix
+from os.path import (
+    dirname,
+    abspath,
+    join,
+)
+from typing import (
+    List,
+    Any,
+)
+import subprocess
+import json
+import sys
+import os
+
+
+def fmt_grammar(grammar: str) -> str:
+    return "tree-sitter-" + grammar
+
+
+def eval_expr(nixpkgs: str, expr: str) -> Any:
+    p = subprocess.run(
+        [
+            "nix-instantiate",
+            "--json",
+            "--eval",
+            "--expr",
+            ("with import %s {}; %s" % (nixpkgs, expr)),
+        ],
+        check=True,
+        stdout=subprocess.PIPE,
+    )
+    return json.loads(p.stdout)
+
+
+def check_grammar_exists(nixpkgs: str, grammar: str) -> bool:
+    return eval_expr(
+        nixpkgs, f'lib.hasAttr "{fmt_grammar(grammar)}" tree-sitter-grammars'
+    )
+
+
+def build_attr(nixpkgs, attr: str) -> str:
+    return (
+        subprocess.run(
+            ["nix-build", "--no-out-link", nixpkgs, "-A", attr],
+            check=True,
+            stdout=subprocess.PIPE,
+        )
+        .stdout.decode()
+        .strip()
+    )
+
+
+if __name__ == "__main__":
+    cwd = dirname(abspath(__file__))
+    nixpkgs = abspath(join(cwd, "../../../../../.."))
+
+    src_dir = build_attr(nixpkgs, "emacs.pkgs.tree-sitter-langs.src")
+
+    existing: List[str] = []
+
+    grammars = os.listdir(join(src_dir, "repos"))
+    for g in grammars:
+        exists = check_grammar_exists(nixpkgs, g)
+        if exists:
+            existing.append(fmt_grammar(g))
+        else:
+            sys.stderr.write("Missing grammar: " + fmt_grammar(g) + "\n")
+            sys.stderr.flush()
+
+    with open(join(cwd, "default-grammars.json"), mode="w") as f:
+        json.dump(sorted(existing), f, indent=2)
+        f.write("\n")