Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nbb.core/invoked-script fn #317

Merged
merged 16 commits into from
Mar 31, 2023
18 changes: 18 additions & 0 deletions script/nbb_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@
(is (thrown? Exception
(nbb {:dir "test-scripts/paths-test"} "src/project_dir_not_on_classpath.cljs")))))

(deftest invoked-file-test
(testing "calling as a script"
(is (= :invoked
(nbb
{:dir "test-scripts/invoked-file-test"}
"src/script.cljs"))))
(testing "calling with -m"
(is (= :invoked
(nbb {:dir "test-scripts/invoked-file-test"}
"-m" "core"))))
(testing "calling with -e"
(is (= :not-invoked
(nbb {:dir "test-scripts/invoked-file-test"}
"-e" "(require [nbb.core :refer [*file* invoked-file])
(if (= *file* (invoked-file))
borkdude marked this conversation as resolved.
Show resolved Hide resolved
:invoked
:not-invoked)")))))

(deftest medley-test
(let [deps '{medley/medley {:git/url "https://github.com/weavejester/medley"
:git/tag "1.4.0"
Expand Down
17 changes: 17 additions & 0 deletions src/nbb/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

(def initialized? (atom false))

(defn get-file-path-from-ns [some-ns]
(loop [cp-entries (cp/split-classpath (cp/get-classpath))]
(let [cp-entry (first cp-entries)
filename (str cp-entry "/" some-ns ".cljs")]
(when cp-entry
(if (fs/existsSync filename)
(path/resolve filename)
(recur (rest cp-entries)))))))

(defn resolve-nbb-edn
"Finds a local nbb.edn file and reads it. Returns nil if none found."
[path]
Expand Down Expand Up @@ -74,6 +83,7 @@

(defn loadFile [script]
(let [script-path (path/resolve script)]
(reset! nbb/-invoked-file script-path)
(-> (initialize script-path nil)
(.then #(nbb/load-file script-path)))))

Expand All @@ -82,6 +92,13 @@
(.then
#(nbb/load-string expr))))

(defn loadMain [main-ns]
(let [script-path (get-file-path-from-ns main-ns)]
(reset! nbb/-invoked-file script-path)
(-> (initialize script-path nil)
(.then
#(nbb/load-main main-ns script-path)))))

(defn addClassPath [cp]
(cp/add-classpath cp))

Expand Down
18 changes: 17 additions & 1 deletion src/nbb/common.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
(ns nbb.common
(:require [sci.core :as sci]))
(:require
[sci.core :as sci]
[clojure.string :as str]))

(def core-ns (sci/create-ns 'clojure.core nil))

(defn main-expr [main-fn]
(let [main-fn (symbol main-fn)
main-fn (if (simple-symbol? main-fn)
(symbol (str main-fn) "-main")
main-fn)
ns (namespace main-fn)
expr (str/replace "(require '$1) (apply $2 *command-line-args*)"
#"\$(\d)"
(fn [match]
(case (second match)
"1" ns
"2" main-fn)))]
expr))
13 changes: 12 additions & 1 deletion src/nbb/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[edamame.core]
[goog.object :as gobj]
[nbb.classpath :as cp]
[nbb.common :refer [core-ns]]
[nbb.common :refer [core-ns main-expr]]
[nbb.error :as nbb.error]
[sci.core :as sci]
[sci.ctx-store :as store]
Expand Down Expand Up @@ -479,6 +479,10 @@
#_(.finally (fn []
(prn :finally (str @sci/ns)))))))

(defn load-main [main-ns path]
(let [sci-ns @sci/ns]
(eval-string* (main-expr main-ns) {:ns sci-ns :file path})))

(defn register-plugin! [_plug-in-name sci-opts]
(store/swap-ctx! sci/merge-opts sci-opts))

Expand Down Expand Up @@ -575,6 +579,12 @@
([e opts]
(nbb.error/print-error-report e opts)))

(def -invoked-file (atom nil))
(defn invoked-file
"Return an absolute path for the file where nbb was invoked"
[]
@-invoked-file)

(def sns
(sci/create-ns 'sci.core nil))

Expand Down Expand Up @@ -619,6 +629,7 @@
'alter-var-root (sci/copy-var sci/alter-var-root nbb-ns)
'slurp (sci/copy-var slurp nbb-ns)
'*file* sci/file
'invoked-file invoked-file
'version (sci/copy-var version nbb-ns)
'await (sci/copy-var await nbb-ns)
'time (sci/copy-var time* nbb-ns)}
Expand Down
21 changes: 5 additions & 16 deletions src/nbb/impl/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
[sci.ctx-store :as store]
[shadow.esm :as esm]))

(defn main-expr [main-fn]
(let [main-fn (symbol main-fn)
main-fn (if (simple-symbol? main-fn)
(symbol (str main-fn) "-main")
main-fn)
ns (namespace main-fn)
expr (str/replace "(require '$1) (apply $2 *command-line-args*)"
#"\$(\d)"
(fn [match]
(case (second match)
"1" ns
"2" main-fn)))]
expr))

(defn exec-expr
[exec-fn]
(let [exec-fn (symbol exec-fn)
Expand Down Expand Up @@ -62,7 +48,7 @@
(next nargs))
("-m" "--main")
(assoc opts
:expr (main-expr (first nargs))
:main (first nargs)
:args (next nargs))
("-x" "--exec")
(assoc opts
Expand Down Expand Up @@ -136,6 +122,7 @@ Tooling:
_ (reset! common/opts opts)
script-file (:script opts)
expr (:expr opts)
main (:main opts)
classpath (:classpath opts)
_ (when classpath
(cp/add-classpath classpath))
Expand All @@ -151,7 +138,7 @@ Tooling:
(when (:version opts)
(println (str (nbb/cli-name) " v" (nbb/version)))
(js/process.exit 0))
(if (or script-file expr nrepl-server repl? bundle-opts)
(if (or script-file main expr nrepl-server repl? bundle-opts)
(do (sci/alter-var-root nbb/command-line-args (constantly (:args opts)))
(->
(js/Promise.resolve
Expand All @@ -162,6 +149,8 @@ Tooling:
(fn []
(-> (cond script-file
(api/loadFile script-file)
main
(api/loadMain main)
expr
(api/loadString expr)
(:nrepl-server opts)
Expand Down
1 change: 1 addition & 0 deletions test-scripts/invoked-file-test/nbb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["src"]}
7 changes: 7 additions & 0 deletions test-scripts/invoked-file-test/src/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns core
(:require [nbb.core :refer [invoked-file *file*]]))

(defn -main []
(prn (if (= *file* (invoked-file))
:invoked
:not-invoked)))
7 changes: 7 additions & 0 deletions test-scripts/invoked-file-test/src/script.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns script
(:require [nbb.core :refer [invoked-file *file*]]))

(prn
(if (= *file* (invoked-file))
:invoked
:not-invoked))
2 changes: 1 addition & 1 deletion test/nbb/main_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
(is (= {:script "foo.cljs", :args nil} (main/parse-args ["foo.cljs"])))
(is (= {:script "foo.cljs", :args '("1" "2" "3")} (main/parse-args ["foo.cljs" "1" "2" "3"])))
(is (= {:classpath "src", :script "foo.cljs", :args nil} (main/parse-args ["--classpath" "src" "foo.cljs"])))
(is (= {:expr "(require 'foo) (apply foo/-main *command-line-args*)", :args '("1" "2" "3")}
(is (= {:main "foo", :args '("1" "2" "3")}
(main/parse-args ["-m" "foo" "1" "2" "3"])))
(is (= {:nrepl-server true, :port "0.0.0.0"}
(main/parse-args ["nrepl-server" "--port" "0.0.0.0" ])))
Expand Down