summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Tropin <andrew@trop.in>2023-02-14 13:02:43 +0400
committerAndrew Tropin <andrew@trop.in>2023-02-14 17:08:16 +0400
commit76ac9edb0d9452189d7533f0059465fb885ace6a (patch)
treee75a672a464f73e7749ee1a6a6546b59b5441609
parent542fcc3dfef4e3f1dc7a61cedf6f1fde6ad7ba4d (diff)
downloadguix-76ac9edb0d9452189d7533f0059465fb885ace6a.tar.gz
gnu: tree-sitter-grammar: Extend functionality.
* gnu/packages/tree-sitter.scm (tree-sitter-delete-generated-files): Add
support for grammar-directories.
(tree-sitter-grammar): Add support for grammar-directories, license, article,
reorder and rename arguments, update description and synopsis generation
logic.
(tree-sitter-html): Migrate to new tree-sitter-grammar.
-rw-r--r--gnu/packages/tree-sitter.scm52
1 files changed, 35 insertions, 17 deletions
diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index bf6cd55b40..937c519cd6 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -188,26 +188,42 @@ can be embedded in any application.
 This package includes the @command{tree-sitter} command-line tool.")
     (license license:expat)))
 
-(define tree-sitter-delete-generated-files
+(define (tree-sitter-delete-generated-files grammar-directories)
   #~(begin
       (use-modules (guix build utils))
       (delete-file "binding.gyp")
       (delete-file-recursively "bindings")
-      (delete-file "src/grammar.json")
-      (delete-file "src/node-types.json")
-      (delete-file "src/parser.c")
-      (delete-file-recursively "src/tree_sitter")))
+      (for-each
+       (lambda (lang)
+         (with-directory-excursion lang
+           (delete-file "src/grammar.json")
+           (delete-file "src/node-types.json")
+           (delete-file "src/parser.c")
+           (delete-file-recursively "src/tree_sitter")))
+       '#$grammar-directories)))
 
 (define* (tree-sitter-grammar
-          language language-for-synopsis version commit hash
+          name text hash version
           #:key
+          (commit (string-append "v" version))
           (repository-url
-           (format #f "https://github.com/tree-sitter/tree-sitter-~a"
-                   language))
-          (inputs '()))
-  (let ((synopsis (string-append language-for-synopsis
-                                 " grammar for tree-sitter"))
-        (name (string-append "tree-sitter-" language)))
+           (format #f "https://github.com/tree-sitter/tree-sitter-~a" name))
+          (grammar-directories '("."))
+          (article "a")
+          (inputs '())
+          (license license:expat))
+  "Returns a package for Tree-sitter grammar.  NAME will be used with
+tree-sitter- prefix to generate package name and also for generating
+REPOSITORY-URL value if it's not specified explicitly, TEXT is a string which
+will be used in description and synopsis."
+  (let* ((multiple? (> (length grammar-directories) 1))
+         (grammar-names (string-append text " grammar" (if multiple? "s" "")))
+         (synopsis (string-append "Tree-sitter " grammar-names))
+         (description
+          (string-append "This package provides "
+                         (if multiple? "" article) (if multiple? "" " ")
+                         grammar-names " for the Tree-sitter library."))
+         (name (string-append "tree-sitter-" name)))
     (package
       (name name)
       (version version)
@@ -219,15 +235,17 @@ This package includes the @command{tree-sitter} command-line tool.")
                       (commit commit)))
                 (file-name (git-file-name name version))
                 (sha256 (base32 hash))
-                (snippet tree-sitter-delete-generated-files)))
+                (snippet
+                 (tree-sitter-delete-generated-files grammar-directories))))
       (build-system tree-sitter-build-system)
+      (arguments (list #:grammar-directories grammar-directories))
       (inputs inputs)
       (synopsis synopsis)
-      (description (string-append synopsis "."))
-      (license license:expat))))
+      (description description)
+      (license license))))
 
 (define-public tree-sitter-html
   (tree-sitter-grammar
    "html" "HTML"
-   "0.19.0" "v0.19.0"
-   "1hg7vbcy7bir6b8x11v0a4x0glvqnsqc3i2ixiarbxmycbgl3axy"))
+   "1hg7vbcy7bir6b8x11v0a4x0glvqnsqc3i2ixiarbxmycbgl3axy"
+   "0.19.0"))