Skip to content

Commit

Permalink
Globally set indium-workspace-configuration
Browse files Browse the repository at this point in the history
The configuration has to be accessible outside of the context of the connection
to the backend, so set it globally.
  • Loading branch information
NicolasPetton committed Jul 24, 2018
1 parent b2068f8 commit 841fbe2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 33 deletions.
23 changes: 12 additions & 11 deletions indium-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@
(interactive)
(indium-maybe-quit)
(unless-indium-connected
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-connect-to-nodejs))
("chrome" (indium-connect-to-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file"))))))
(indium-workspace-read-configuration)
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-connect-to-nodejs))
("chrome" (indium-connect-to-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file")))))

;;;###autoload
(defun indium-launch ()
"Start a process (web browser or NodeJS) and attempt to connect to it."
(interactive)
(indium-maybe-quit)
(unless-indium-connected
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-launch-nodejs))
("chrome" (indium-launch-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file"))))))
(indium-workspace-read-configuration)
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-launch-nodejs))
("chrome" (indium-launch-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file")))))

(defun indium-quit ()
"Close the current connection and kill its REPL buffer if any.
Expand All @@ -93,7 +93,8 @@ When called interactively, prompt for a confirmation first."
(memq (process-status process)
'(run stop open listen)))
(kill-process process))
(setq indium-current-connection nil))))
(setq indium-current-connection nil)
(setq indium-workspace-configuration nil))))

(defun indium-maybe-quit ()
"Close the current connection.
Expand Down
42 changes: 22 additions & 20 deletions indium-workspace.el
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@
"Name of the configuration file containing the Indium project settings.")

(defvar indium-workspace-configuration nil
"Configuration in the settings file used for connecting.")
"Configuration in the settings file used for connecting.
Do not set this variable directly.")

(defmacro with-indium-workspace-configuration (&rest body)
"Promt the users for a configuration and evaluate BODY.
During the evaluation of BODY, `indium-workspace-configuration'
is set to the choosen configuration."
(declare (indent 0) (debug t))
`(let ((indium-workspace-configuration
(or indium-workspace-configuration
(indium-workspace--read-configuration))))
`(progn
(unless indium-workspace-configuration
(indium-workspace-read-configuration))
,@body))

(defun indium-workspace-root ()
Expand Down Expand Up @@ -144,10 +145,9 @@ If the root directory does not exist, signal an error."
(defun indium-workspace--root-from-configuration ()
"Return the root directory read from the project configuration.
If no root is specified, return nil."
(with-indium-workspace-configuration
(when-let ((root (or (map-elt indium-workspace-configuration 'root)
(map-elt indium-workspace-configuration 'webRoot))))
(expand-file-name root (indium-workspace--project-directory)))))
(when-let ((root (or (map-elt indium-workspace-configuration 'root)
(map-elt indium-workspace-configuration 'webRoot))))
(expand-file-name root (indium-workspace--project-directory))))

(defun indium-workspace--project-directory ()
"Return the directory containing the \".indium.json\" file."
Expand Down Expand Up @@ -175,26 +175,28 @@ Return nil if not found."
(ignore-errors
(json-read))))

(defun indium-workspace--read-configuration ()
(defun indium-workspace-read-configuration ()
"Prompt for the configuration used for connecting to a backend.
If the settings file contains only one configuration, return it."
Set `indium-workspace-configuration' to the choosen configuration.
If the settings file contains only one configuration, set it."
(let* ((settings (indium-workspace-settings))
(configurations (map-elt settings 'configurations))
(configuration-names (seq-map (lambda (configuration)
(map-elt configuration 'name))
configurations)))
(unless configurations
(user-error "No configuration provided in the project file"))
(if (= (seq-length configurations) 1)
(seq-elt configurations 0)
(let ((name (completing-read "Choose a configuration: "
configuration-names
nil
t)))
(seq-find (lambda (configuration)
(string-equal (map-elt configuration 'name)
name))
configurations)))))
(setq indium-workspace-configuration
(if (= (seq-length configurations) 1)
(seq-elt configurations 0)
(let ((name (completing-read "Choose a configuration: "
configuration-names
nil
t)))
(seq-find (lambda (configuration)
(string-equal (map-elt configuration 'name)
name))
configurations))))))



Expand Down
26 changes: 26 additions & 0 deletions test/unit/indium-interaction-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
(require 'indium-interaction)

(describe "Launching and connecting Indium"
(after-each
(setq indium-workspace-configuration nil))

(it "should fail to connect when there is no .indium.json file"
(assess-with-filesystem '()
(expect (indium-connect) :to-throw)))
Expand Down Expand Up @@ -77,6 +80,9 @@
'foo)))

(describe "Killing previous connections when connecting"
(after-each
(when-indium-connected (indium-quit)))

(it "should kill the previous connection process when there is one"
(let ((indium-current-connection (make-indium-connection
:process 'first-process)))
Expand All @@ -93,6 +99,26 @@
(expect #'kill-process :to-have-been-called-with 'first-process)
(expect #'indium-backend-close-connection :to-have-been-called))))

(describe "Setting indium-workspace-connection"
(after-each
(setq indium-workspace-configuration nil))

(it "should should set `indium-workspace-connection' to nil when disconnecting"
(setq indium-workspace-configuration '(type . "node"))
(let ((indium-current-connection (make-indium-connection
:process 'first-process)))
(spy-on #'indium-connect-to-nodejs)
(spy-on 'y-or-n-p :and-return-value t)

(spy-on 'kill-process)
(spy-on 'process-buffer)
(spy-on 'process-status :and-return-value 'run)
(spy-on 'indium-backend-close-connection)

(indium-quit)

(expect indium-workspace-configuration :to-be nil))))

(describe "Finding the AST node to evaluate"
(it "can find variable nodes"
(with-js2-buffer "var foo = 2;\nfoo"
Expand Down
19 changes: 17 additions & 2 deletions test/unit/indium-workspace-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"Fake filesystem used in workspace tests.")

(describe "Workspace root"
(after-each
(setq indium-workspace-configuration nil))

(it "Returns the current connection's project root when there is a connection"
(assess-with-filesystem indium-workspace--test-fs
(let* ((root (expand-file-name "js"))
Expand All @@ -59,22 +62,28 @@
(directory-file-name (expand-file-name "foo" default-directory)))))))

(describe "Invalid root directory"
(after-each
(setq indium-workspace-configuration nil))

(it "should signal an error when the root directory does not exist"
(assess-with-filesystem '((".indium.json" "{\"configurations\": [{\"webRoot\": \"foo\"}]}"))
(with-indium-workspace-configuration
(expect (indium-workspace-root) :to-throw)))))

(describe "Choosing a configuration"
(after-each
(setq indium-workspace-configuration nil))

(it "should not prompt for a configuration when there is only one"
(assess-with-filesystem '((".indium.json" "{\"configurations\": [{}]}"))
(spy-on #'completing-read)
(indium-workspace--read-configuration)
(indium-workspace-read-configuration)
(expect #'completing-read :not :to-have-been-called)))

(it "should prompt for a configuration when there are many"
(assess-with-filesystem '((".indium.json" "{\"configurations\": [{\"name\": \"a\"}, {\"name\": \"b\"}]}"))
(spy-on #'completing-read)
(indium-workspace--read-configuration)
(indium-workspace-read-configuration)
(expect #'completing-read :to-have-been-called-with "Choose a configuration: " '("a" "b") nil t))))

(describe "Looking up files"
Expand Down Expand Up @@ -112,6 +121,9 @@
:to-equal file)))))

(describe "Making workspace urls from file names"
(after-each
(setq indium-workspace-configuration nil))

(it "cannot make a url when no workspace is set"
(with-indium-connection (make-indium-connection :url "http://localhost:9229")
(expect (indium-workspace-make-url "js/app.js")
Expand Down Expand Up @@ -156,6 +168,9 @@
(expect #'convert-standard-filename :to-have-been-called-with file))))))

(describe "File protocol"
(after-each
(setq indium-workspace-configuration nil))

(it "can lookup files using the file:// protocol"
(assess-with-filesystem indium-workspace--test-fs
(with-indium-connection (make-indium-connection :url "file:///foo/bar/index.html")
Expand Down

0 comments on commit 841fbe2

Please sign in to comment.