Skip to content

Commit

Permalink
Add configurable user scripts location via env var
Browse files Browse the repository at this point in the history
* Add smoke test for user-activate
* Add JS example script to default user scripts content

Addressing #136

TODO:

* [ ] Add more smoke tests
* [ ] Handle new my-lib location somehow
  • Loading branch information
PEZ committed Jan 5, 2023
1 parent cd1528a commit 7584cb2
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes to Joyride

- [Enable using JS files as user and workspace scripts](https://github.com/BetterThanTomorrow/joyride/issues/132)
- Fix [Allow `js/require` to be used in joyride](https://github.com/BetterThanTomorrow/joyride/issues/134)
- Dev internals [Add basic e2e tests for user scripts](https://github.com/BetterThanTomorrow/joyride/issues/136)

## [0.0.29] - 2023-01-02

Expand Down
28 changes: 28 additions & 0 deletions assets/getting-started-content/user/hello_joyride_user_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const vscode = require("vscode");

// You can write your Joyride scripts in JavaScript, if you want.

const hello = () => {
return "Hello World!";
};

const showHelloMessage = async () => {
const button = await vscode.window.showInformationMessage("Hello World!", "Cancel", "OK");
if (button === "OK") {
vscode.window.showInformationMessage("You clicked OK! Try clicking Cancel too?.");
} else {
const name = await vscode.window.showInputBox({
title: "CIA wants to know",
prompt: "What is your name?",
});
vscode.window.showInformationMessage(`Hello ${name}!`);
}
};

hello();
showHelloMessage();

exports = {
hello,
showHelloMessage,
}
39 changes: 39 additions & 0 deletions assets/getting-started-content/user/problem_hover.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(ns problem-hover
(:require ["vscode" :as vscode]))

;; Adding diagnostics info to the top of the hover to get it above the fold

(defonce !problems (atom {}))

(defn on-changed-diagnostics [event]
(doseq [uri (.-uris event)]
(swap! !problems assoc (.-fsPath uri) (vscode/languages.getDiagnostics uri))))

(defn- provide-hover [document position]
(let [hover (vscode/MarkdownString.)
problems (->> (get @!problems (-> document .-uri .-fsPath))
(keep (fn [problem]
(let [range (.-range problem)]
(when (.contains range position)
problem)))))]
(doseq [problem problems]
(.appendCodeblock hover (str (.-message problem)
"; " (.-source problem)
(when (.-code problem)
(str "(" (.-code problem) ")")))
; highlight hover as 'ini', because works
"ini"))
(new vscode/Hover #js [hover])))

(defn register-diagnostics-handler! []
(vscode/languages.onDidChangeDiagnostics on-changed-diagnostics))

(defn register-provider! []
; Use "*" instead of "clojure" to add this to all file types
(vscode/languages.registerHoverProvider "clojure" #js {:provideHover provide-hover}))

(comment
foo
(remove 1 2 3)
:rcf)

4 changes: 3 additions & 1 deletion src/joyride/config.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
(ns joyride.config
(:require ["os" :as os]
["path" :as path]
["process" :as process]
[joyride.db :as db]))

(def user-config-path (path/join (os/homedir) ".config"))
(def user-config-path (or (aget process/env "VSCODE_JOYRIDE_USER_CONFIG_PATH")
(path/join (os/homedir) ".config")))
(def user-scripts-path (path/join "joyride" "scripts"))

(defn user-abs-scripts-path
Expand Down
60 changes: 16 additions & 44 deletions src/joyride/getting_started.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,22 @@
(js/console.info "Creating " ^String (.-fsPath dest-uri))
(vscode/workspace.fs.copy source-uri dest-uri)))

(defn user-activate-uri-section-and-subpath []
[(conf/user-abs-scripts-path)
"user"
"user_activate.cljs"])

(defn user-hello-uri-section-and-subpath []
[(conf/user-abs-scripts-path)
"user"
"hello_joyride_user_script.cljs"])

(defn user-my-lib-uri-section-and-subpath []
[(conf/user-abs-scripts-path)
"user"
"my_lib.cljs"])

(defn workspace-activate-uri-section-and-subpath []
[(conf/workspace-abs-scripts-path)
"workspace"
"workspace_activate.cljs"])

(defn workspace-hello-uri-section-and-subpath []
[(conf/workspace-abs-scripts-path)
"workspace"
"hello_joyride_workspace_script.cljs"])

(defn- dest-uri-uri-exists?+ [section-and-subpath]
(p/let [dest-uri (apply destination-uri section-and-subpath)
exists?+ (utils/path-or-uri-exists?+ dest-uri)]
[dest-uri exists?+]))

(defn maybe-create-user-content+ []
(p/let [section-and-subpath (user-activate-uri-section-and-subpath)
[activate-dest-uri activate-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(defn- maybe-create-content+ [section-and-subpath]
(p/let [[activate-dest-uri activate-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(when-not activate-exists?+
(apply (partial create-content-file+ activate-dest-uri) section-and-subpath)))
(p/let [section-and-subpath (user-hello-uri-section-and-subpath)
[hello-dest-uri hello-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(when-not hello-exists?+
(apply (partial create-content-file+ hello-dest-uri) section-and-subpath)))
(p/let [section-and-subpath (user-my-lib-uri-section-and-subpath)
[my-lib-dest-uri my-lib-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(when-not my-lib-exists?+
(apply (partial create-content-file+ my-lib-dest-uri) section-and-subpath))))
(apply (partial create-content-file+ activate-dest-uri) section-and-subpath))))

(defn maybe-create-user-content+ []
(maybe-create-content+ [(conf/user-abs-scripts-path) "user" "user_activate.cljs"])
(maybe-create-content+ [(conf/user-abs-scripts-path) "user" "hello_joyride_user_script.cljs"])
(maybe-create-content+ [(conf/user-abs-scripts-path) "user" "hello_joyride_user_script.js"])
(maybe-create-content+ [(conf/user-abs-src-path) "user" "problem_hover.cljs"])
(maybe-create-content+ [(conf/user-abs-src-path) "user" "my_lib.cljs"]))

(defn create-and-open-content-file+ [content-file-uri section-and-subpath]
(fn []
Expand All @@ -75,14 +48,13 @@
(vscode/window.showTextDocument
#js {:preview false, :preserveFocus false})))))

(defn maybe-create-workspace-activate-fn+ []
(p/let [section-and-subpath (workspace-activate-uri-section-and-subpath)
[activate-dest-uri activate-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(defn maybe-create-and-open-content+ [section-and-subpath]
(p/let [[activate-dest-uri activate-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(when-not activate-exists?+
(create-and-open-content-file+ activate-dest-uri section-and-subpath))))

(defn maybe-create-workspace-activate-fn+ []
(maybe-create-and-open-content+ [(conf/workspace-abs-scripts-path) "workspace" "workspace_activate.cljs"]))

(defn maybe-create-workspace-hello-fn+ []
(p/let [section-and-subpath (workspace-hello-uri-section-and-subpath)
[hello-dest-uri hello-exists?+] (dest-uri-uri-exists?+ section-and-subpath)]
(when-not hello-exists?+
(create-and-open-content-file+ hello-dest-uri section-and-subpath))))
(maybe-create-and-open-content+ [(conf/workspace-abs-scripts-path) "workspace" "hello_joyride_workspace_script.cljs"]))
33 changes: 32 additions & 1 deletion vscode-test-runner/launch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
const path = require("path");
const process = require("process");
const os = require("os");
const fs = require("fs");
const { runTests } = require('@vscode/test-electron');

function init() {
return new Promise((resolve, reject) => {
try {
const USER_CONFIG_PATH_KEY = "VSCODE_JOYRIDE_USER_CONFIG_PATH";
if (!process.env[USER_CONFIG_PATH_KEY]) {
const tmpConfigPath = path.join(
os.tmpdir(),
"vscode-test-runner-joyride",
"user-config"
);
if (fs.existsSync(tmpConfigPath)) {
fs.rmSync(tmpConfigPath, { recursive: true });
}
fs.mkdirSync(tmpConfigPath, { recursive: true });
process.env[USER_CONFIG_PATH_KEY] = tmpConfigPath;
console.info(`USER_CONFIG_PATH: ${process.env[USER_CONFIG_PATH_KEY]}`);
}
resolve();
} catch (error) {
reject(error);
}
});
}

async function main() {
try {
// The folder containing the Extension Manifest package.json
Expand All @@ -27,4 +54,8 @@ async function main() {
}
}

void main();
void init().then(() => main())
.catch((error) => {
console.error('Failed to initialize test running environment:', error);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@
(p/resolve! running true)
(p/reject! running true))))

;; We rely on that the user_activate.cljs script is run before workspace_activate.cljs
(defn- run-when-ws-activated [tries]
(if (:ws-activated? @db/!state)
(do
(println "Runner: Workspace activated, running tests")
(require '[integration-test.user-activate-test])
(require '[integration-test.workspace-activate-test])
(require '[integration-test.ws-scripts-test])
(require '[integration-test.require-js-test])
(require '[integration-test.npm-test])
(cljs.test/run-tests 'integration-test.workspace-activate-test
(cljs.test/run-tests 'integration-test.user-activate-test
'integration-test.workspace-activate-test
'integration-test.ws-scripts-test
'integration-test.require-js-test
'integration-test.npm-test))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns integration-test.user-activate-test
(:require [cljs.test :refer [deftest testing is]]
["path" :as path]))

(deftest user-activate
(testing "User activation script is required"
(is #_{:clj-kondo/ignore [:unresolved-namespace]}
(= #'user-activate/!db
((ns-publics 'user-activate) '!db))))

(testing "my-lib is required"
(is (seq
(ns-publics 'my-lib)))))

0 comments on commit 7584cb2

Please sign in to comment.