Skip to content

Commit

Permalink
Add auto-start support
Browse files Browse the repository at this point in the history
  • Loading branch information
seblemaguer committed Dec 24, 2019
1 parent 6ae71f2 commit 96b6d5b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ Services can be defined by setting the variable `prodigy-services`:
(:prop value ...)))
```

#### Auto start services

Prodigy.el supports auto start services.
To do so, you have to call the function `prodigy-enable-auto-start`.
To indicate if a service should be automatically started, you have to set `auto-start` property to `t`.

For example, you can use a configuration like this one:
```lisp
(prodigy-define-service
:name "auto-start-example"
:command "echo"
:args '("example")
:auto-start t)
(prodigy-enable-auto-start)
```

The call to `prodigy-enable-auto-start` can be either after or before the definition of the service via `prodigy-define-service`.

### Viewing process output

In the prodigy window, you can see a process' output with the `$` key.
Expand Down
59 changes: 55 additions & 4 deletions prodigy.el
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ The list is a property list with the following properties:
Call this function with (service, output), each time process gets
new output.
`auto-start'
Autostart the service
`ready-message'
The text that a service displays when it is ready. Will be
matched as a regexp.")
Expand All @@ -254,6 +257,7 @@ these (see `prodigy-services' doc-string for more information):
* `kill-process-buffer-on-stop'
* `on-output'
* `truncate-output'
* `auto-start'
These properties are also valid for a tag:
Expand Down Expand Up @@ -287,6 +291,11 @@ Supported filters:
`face'
The face to use for the status.")

(defvar prodigy-auto-start-activated nil
"Activation status of the auto start mode. nil implies
auto-start is not enabled; t implies that auto-start is
enabled.")

(defconst prodigy-buffer-name "*prodigy*"
"Name of Prodigy mode buffer.")

Expand Down Expand Up @@ -1034,8 +1043,8 @@ NAME, BUFFER, PROGRAM, and PROGRAM-ARGS are as in `start-process.'"
(let* ((sudo-args (cons program program-args))
(pwd (read-passwd (concat "Sudo password for `" (mapconcat #'identity sudo-args " ") "': ")))
(process
(start-process-shell-command
name buffer (concat "sudo " (mapconcat #'shell-quote-argument sudo-args " ")))))
(start-process-shell-command
name buffer (concat "sudo " (mapconcat #'shell-quote-argument sudo-args " ")))))
(process-send-string process pwd)
(clear-string pwd)
(process-send-string process "\r")
Expand Down Expand Up @@ -1258,8 +1267,8 @@ started."
(let* ((service (prodigy-current-service))
(envs (s-join " "
(-map (lambda (env-var-value)
(s-join "=" env-var-value))
(prodigy-service-env service))))
(s-join "=" env-var-value))
(prodigy-service-env service))))
(envs-string (if (not (s-equals? envs ""))
(concat "env " envs " ")))
(cmd (prodigy-service-command service))
Expand All @@ -1284,6 +1293,17 @@ started."
(prodigy-with-refresh
(-each (prodigy-relevant-services) 'prodigy-start-service)))

(defun prodigy-start-auto-start-service (service)
"Start service if indicated as `auto-start'."
(interactive)
(prodigy-with-refresh
(when (and prodigy-auto-start-activated
(or (eq (plist-get service :status) 'stopped)
(eq (plist-get service :status) 'ready)
(null (plist-get service :status)))
(plist-get service :auto-start))
(prodigy-start-service service))))

(defun prodigy-stop (&optional force)
"Stop service at line or marked services.
Expand Down Expand Up @@ -1431,10 +1451,14 @@ The old service process is transferred to the new service."
(lambda (service)
(string= (plist-get service :name) service-name)))
(service (-first fn prodigy-services)))

(when service
(-when-let (process (plist-get service :process))
(plist-put args :process process))

(setq prodigy-services (-reject fn prodigy-services)))

(prodigy-start-auto-start-service args)
(push args prodigy-services)))

;;;###autoload
Expand Down Expand Up @@ -1514,6 +1538,33 @@ beginning of the line."
(make-local-variable 'minor-mode-overriding-map-alist)
(push `(view-mode . ,newmap) minor-mode-overriding-map-alist)))


;;;###autoload
(defun prodigy-enable-auto-start ()
"Set `prodigy-auto-start-activated' to `t' and start already
defined services services whose property `auto-start' is set to
`t'."
(interactive)
(let ((saved-buffer (current-buffer))
(buffer-p (prodigy-buffer))
(buffer (get-buffer-create prodigy-buffer-name)))

(setq prodigy-auto-start-activated t)

(set-buffer buffer)
(unless buffer-p
(prodigy-mode))
(prodigy-start-status-check-timer)

(prodigy-with-refresh
(progn
(prodigy-mark-all)
(-each (prodigy-relevant-services) 'prodigy-start-auto-start-service)
(prodigy-unmark-all)))

(set-buffer saved-buffer)))


;;;###autoload
(defun prodigy ()
"Manage external services from within Emacs."
Expand Down
39 changes: 36 additions & 3 deletions test/prodigy-process-handling-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,45 @@
(funcall done)))))))

(ert-deftest prodigy-start-service-test/path ()

)

(ert-deftest prodigy-start-service-test/env ()

)


(ert-deftest-async prodigy-start-service-test/auto-start-start-service-with-enabling-after (done)
(with-sandbox
(let ((service))
(setq prodigy-auto-start-activated nil)
(setq service (prodigy-test/make-service
:name "auto-start-after"
:command "echo"
:args '("foo")
:auto-start t))
(prodigy-enable-auto-start)

(prodigy-test/delay 2
(lambda ()
(when (eq (plist-get service :status) 'failed)
(funcall done)))))))

(ert-deftest-async prodigy-start-service-test/auto-start-start-service-with-enabling-before (done)
(with-sandbox
(let ((service))
(setq prodigy-auto-start-activated nil)
(prodigy-enable-auto-start)
(setq service (prodigy-test/make-service
:name "auto-start-before"
:command "echo"
:args '("foo")
:auto-start t))
(prodigy-test/delay 2
(lambda ()
(when (eq (plist-get service :status) 'failed)
(funcall done)))))))

(ert-deftest-async prodigy-start-service-test/start-service-with-sudo (done)
(unwind-protect
(with-sandbox
Expand All @@ -83,6 +115,7 @@
(prodigy-start-service service)))
(ad-disable-advice 'start-process-shell-command 'before 'prodigy-sudo-test-spsc-advice)))


;;;; on-output

(ert-deftest-async prodigy-start-service-test/on-output/no-tags (done-log-foo done-stop)
Expand Down Expand Up @@ -168,7 +201,7 @@
;;;; init

(ert-deftest prodigy-start-service-test/init ()

)

;;;; init-async
Expand Down

0 comments on commit 96b6d5b

Please sign in to comment.