Skip to content

Commit

Permalink
Merge pull request #45 from TristanCacqueray/per-recipe-buffer
Browse files Browse the repository at this point in the history
Add justl-per-recipe-buffer custom variable
  • Loading branch information
psibi authored Jan 24, 2024
2 parents 1364da3 + acc30bf commit 476bf9c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions Changelog.org
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add ~justl-include-private-recipes~ to show private recipes in the
list (defaults to on).
- Rewritten internals, using ~just~ itself to parse recipe information.
- Add ~justl-per-recipe-buffer~ to create new buffers per recipe.

* 0.13

Expand Down
4 changes: 4 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ M-x justl-exec-recipe-in-dir

To execute default recipe, call *justl-exec-default-recipe*

To execute multiple recipes in parallel, customize *justl-per-recipe-buffer* to
create dedicated buffers named /*just-RECIPE*/. This is useful to manage long
running process.

* Shortcuts

On the just screen, place your cursor on a recipe:
Expand Down
32 changes: 23 additions & 9 deletions justl.el
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ other cases, it's a known path."
:type 'boolean
:safe 'booleanp)

(defcustom justl-per-recipe-buffer nil
"If non-nil, create a new buffer per recipe."
:type 'boolean
:safe 'booleanp)

(defun justl--recipe-output-buffer (recipe-name)
"Return the buffer name for the RECIPE-NAME."
(if justl-per-recipe-buffer
(format "*just-%s*" recipe-name)
justl--output-process-buffer))

(defun justl--process-error-buffer (process-name)
"Return the error buffer name for the PROCESS-NAME."
(format "*%s:err*" process-name))
Expand Down Expand Up @@ -167,12 +178,14 @@ CMD is the just command as a list."
(justl--append-to-process-buffer
(format "[%s] \ncommand: %S" process-name cmd))))

(defun justl--sentinel (process _)
"Sentinel function for PROCESS."
(defun justl--sentinel (process buffer)
"Sentinel function for PROCESS.
BUFFER is the name of the output buffer."
(let ((process-name (process-name process))
(inhibit-read-only t)
(exit-status (process-exit-status process)))
(with-current-buffer (get-buffer justl--output-process-buffer)
(with-current-buffer buffer
(goto-char (point-max))
(if (zerop exit-status)
(insert (format "\nTarget execution finished at %s" (substring (current-time-string) 0 19)))
Expand Down Expand Up @@ -230,14 +243,12 @@ ARGS is a plist that affects how the process is run.
- `:buffer' name for process buffer
- `:process' name for compilation process
- `:mode' mode for process buffer
- `:directory' set `default-directory'
- `:sentinel' process sentinel"
- `:directory' set `default-directory'"
(let* ((buf (get-buffer-create
(or (plist-get args :buffer) justl--output-process-buffer)))
(process-name (or (plist-get args :process) justl--compilation-process-name))
(mode (or (plist-get args :mode) 'justl-compile-mode))
(directory (or (plist-get args :directory) (f-dirname justl-justfile)))
(sentinel (or (plist-get args :sentinel) #'justl--sentinel))
(inhibit-read-only t))
(setq next-error-last-buffer buf)
(justl-compilation-setup-buffer buf directory mode)
Expand All @@ -249,7 +260,7 @@ ARGS is a plist that affects how the process is run.
(setq-local justl-justfile (justl--justfile-from-arg (elt command 1)))
(run-hook-with-args 'compilation-start-hook process)
(set-process-filter process 'justl--process-filter)
(set-process-sentinel process sentinel)
(set-process-sentinel process (lambda (proc _) (justl--sentinel proc buf)))
(set-process-coding-system process 'utf-8-emacs-unix 'utf-8-emacs-unix)
(pop-to-buffer buf)))))

Expand Down Expand Up @@ -295,14 +306,15 @@ Error matching regexes from compile.el are removed."
(setq-local overlay-arrow-string "")
(setq next-error-overlay-arrow-position nil))

(defun justl--exec (process-name args)
(defun justl--exec (process-name recipe-name args)
"Utility function to run commands in the proper setting.
PROCESS-NAME is an identifier for the process. Default to \"just\".
RECIPE-NAME is the name of the recipe.
ARGS is a ist of arguments."
(when (equal process-name "")
(setq process-name justl-executable))
(let ((buffer-name justl--output-process-buffer)
(let ((buffer-name (justl--recipe-output-buffer recipe-name))
(error-buffer (justl--process-error-buffer process-name))
(cmd (append (list justl-executable (justl--justfile-argument)) args))
(mode 'justl-compile-mode))
Expand Down Expand Up @@ -542,6 +554,7 @@ not executed."
(let* ((recipe (justl--get-recipe-under-cursor)))
(justl--exec
justl-executable
(justl--recipe-name recipe)
(append (transient-args 'justl-help-popup)
(cons (justl--recipe-name recipe)
(mapcar 'justl--read-arg
Expand All @@ -557,6 +570,7 @@ not executed."
recipe-name))))
(justl--exec
justl-executable
recipe-name
(append (transient-args 'justl-help-popup)
(cons
recipe-name
Expand Down
46 changes: 31 additions & 15 deletions test/justl-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,36 @@
(with-current-buffer (justl--buffer-name)
(search-forward "plan")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "plan")))
(with-current-buffer (justl--recipe-output-buffer "plan")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "planner" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "plan")))

(ert-deftest justl--execute-test-exit-status ()
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "plan")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "plan")))
(with-current-buffer (justl--recipe-output-buffer "plan")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "Target execution finished" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "plan")))

(ert-deftest justl--fail-recipe ()
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "fail")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "fail"))
(with-current-buffer (justl--recipe-output-buffer "fail")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "exited abnormally" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "fail"))))

(ert-deftest justl--find-justfile-check ()
(should (equal (f-filename (justl--find-justfile default-directory)) "justfile")))
Expand All @@ -85,26 +85,26 @@
(with-current-buffer (justl--buffer-name)
(search-forward "carriage-return")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "carriage-return")))
(with-current-buffer (justl--recipe-output-buffer "carriage-return")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "DONE\n" buf-string))
(should-not (s-contains? "1/3\r2/3\r3/3\rDONE\n" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "carriage-return")))

(ert-deftest justl--execute-recipe-with-color ()
"A target printing color is handled properly."
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "color")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "color")))
(with-current-buffer (justl--recipe-output-buffer "color")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "This is red text\n" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "color")))

(ert-deftest justl--execute-default-recipe ()
"Checks that default recipe is printed."
Expand Down Expand Up @@ -142,6 +142,22 @@
(should (s-contains? "ver1" buf-string))))
(kill-buffer justl--output-process-buffer))

(ert-deftest justl--test-per-recipe-buffer ()
"This test is a copy of 'justl--execute-recipe' setting the per-recipe and hardcoding the desired buffer name."
(let ((current justl-per-recipe-buffer))
(customize-set-variable 'justl-per-recipe-buffer 't)
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "plan")
(justl-exec-recipe)
(justl--wait-till-exit "*just-plan*"))
(with-current-buffer "*just-plan*"
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "Target execution finished" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer "*just-plan*")
(customize-set-variable 'justl-per-recipe-buffer current)))

;; (ert "justl--**")

(provide 'justl-test)
Expand Down

0 comments on commit 476bf9c

Please sign in to comment.