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 Mar 5, 2019
1 parent 42b2b5b commit 0d07429
Show file tree
Hide file tree
Showing 3 changed files with 116 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
65 changes: 61 additions & 4 deletions prodigy.el
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,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 @@ -253,6 +256,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 @@ -286,6 +290,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 @@ -997,8 +1006,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 @@ -1221,8 +1230,8 @@ started."
(let* ((service (prodigy-service-at-pos))
(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 @@ -1237,6 +1246,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 @@ -1387,10 +1407,16 @@ The old service process is transfered to the new service."
(lambda (service)
(string= (plist-get service :name) service-name)))
(service (-first fn prodigy-services)))

;; Update the service if necessary
(when service
(-when-let (process (plist-get service :process))
(plist-put args :process process))

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

;; Start the service if auto-start is activated
(prodigy-start-auto-start-service args)
(push args prodigy-services)))

;;;###autoload
Expand Down Expand Up @@ -1461,6 +1487,37 @@ beginning of the line."
(font-lock-mode 1)
(use-local-map prodigy-view-mode-map))


;;;###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)))

;; Indicate that auto start is enabled
(setq prodigy-auto-start-activated t)

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

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

;; Go back to the previous one
(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 0d07429

Please sign in to comment.