Browse Source

Globally set indium-workspace-configuration

The configuration has to be accessible outside of the context of the connection
to the backend, so set it globally.
workspaces
Nicolas Petton 2 years ago
parent
commit
841fbe249f
No known key found for this signature in database GPG Key ID: E8BCD7866AFCF978
4 changed files with 77 additions and 33 deletions
  1. +12
    -11
      indium-interaction.el
  2. +22
    -20
      indium-workspace.el
  3. +26
    -0
      test/unit/indium-interaction-test.el
  4. +17
    -2
      test/unit/indium-workspace-test.el

+ 12
- 11
indium-interaction.el View File

@ -58,11 +58,11 @@
(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 ()
@ -70,11 +70,11 @@
(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.
@ -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.


+ 22
- 20
indium-workspace.el View File

@ -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 ()
@ -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."
@ -175,9 +175,10 @@ 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)
@ -185,16 +186,17 @@ If the settings file contains only one configuration, return it."
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))))))


+ 26
- 0
test/unit/indium-interaction-test.el View File

@ -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)))
@ -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)))
@ -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"


+ 17
- 2
test/unit/indium-workspace-test.el View File

@ -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"))
@ -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"
@ -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")
@ -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")


Loading…
Cancel
Save