From 206e9183080077b1715442a08f1610315a5dee91 Mon Sep 17 00:00:00 2001 From: Steve Purcell Date: Sat, 12 Aug 2023 09:49:31 +0200 Subject: [PATCH] Use "just --dump" to get recipes as JSON, to avoid string parsing --- justl.el | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/justl.el b/justl.el index 941232e..a86b15f 100644 --- a/justl.el +++ b/justl.el @@ -415,13 +415,16 @@ Logs the command run." (buf-string (buffer-substring-no-properties (point-min) (point-max)))) (cons justl-status buf-string)))))) -(defun justl--get-recipes (justfile) - "Return all the recipes from JUSTFILE." - (let ((recipes (split-string (cdr (justl--exec-to-string-with-exit-code - justl-executable - (concat "--justfile=" (tramp-file-local-name justfile)) - "--summary" "--unsorted" "--color=never"))))) - (mapcar #'string-trim-right recipes))) +(defun justl--dump (justfile) + "Dump info about JUSTFILE as JSON, returns (exit_code, json)." + (let ((result (justl--exec-to-string-with-exit-code + justl-executable + (concat "--justfile=" (tramp-file-local-name justfile)) + "--unstable" "--dump" "--dump-format=json"))) + (if (zerop (car result)) + (cons (car result) + (json-parse-string (cdr result) :null-object nil :object-type 'alist)) + result))) (defun justl--justfile-argument () "Provides justfile argument with the proper location." @@ -434,25 +437,12 @@ Logs the command run." (defun justl--get-recipes-with-desc (justfile) "Return all the recipes in JUSTFILE with description." - (let* ((recipe-status (apply 'justl--exec-to-string-with-exit-code - justl-executable - (append - (transient-args 'justl-help-popup) - (list (concat "--justfile=" (tramp-file-local-name justfile)) - "--list" "--unsorted" "--color=never")))) - (justl-status (car recipe-status)) - (recipe-lines (split-string - (cdr recipe-status) - "\n")) - (recipes (mapcar (lambda (x) (split-string x "# ")) - (cdr (seq-filter (lambda (x) (s-present? x)) recipe-lines))))) + (let* ((recipe-status (justl--dump justfile)) + (justl-status (car recipe-status))) (setq justl--list-command-exit-code justl-status) - (when (eq (nth 0 recipe-status) 0) - (mapcar (lambda (x) (list (justl--get-recipe-name (nth 0 x)) (nth 1 x))) recipes)))) - -(defun justl--list-to-jrecipe (list) - "Convert a single LIST of two elements to list of JRECIPE." - (make-justl-jrecipe :name (nth 0 list) :args (nth 1 list))) + (when (zerop (car recipe-status)) + (let-alist (cdr recipe-status) + (mapcar (lambda (r) (let-alist r (cons .name .doc))) .recipes))))) (defun justl-exec-recipe-in-dir () "Populate and execute the selected recipe." @@ -460,7 +450,7 @@ Logs the command run." (let* ((justfile (justl--find-justfile default-directory))) (if (not justfile) (error "No justfile found")) - (let* ((recipe (completing-read "Recipes: " (justl--get-recipes justfile) + (let* ((recipe (completing-read "Recipes: " (mapcar 'car (justl--get-recipes-with-desc justfile)) nil nil nil nil "default")) (justl-recipe (justl--get-recipe-from-file justfile recipe)) (recipe-has-args (justl--jrecipe-has-args-p justl-recipe)))