move things to compile time
This commit is contained in:
parent
c2059b4a1d
commit
d12b503b8a
4 changed files with 97 additions and 83 deletions
2
deps.edn
2
deps.edn
|
|
@ -63,7 +63,7 @@
|
||||||
:lib-tests
|
:lib-tests
|
||||||
{:extra-paths ["process/src" "process/test" "test-resources/lib_tests"]
|
{:extra-paths ["process/src" "process/test" "test-resources/lib_tests"]
|
||||||
:extra-deps {org.clj-commons/clj-http-lite {:mvn/version "0.4.392"}
|
:extra-deps {org.clj-commons/clj-http-lite {:mvn/version "0.4.392"}
|
||||||
org.babashka/spec.alpha {:git/url "https://github.com/babashka/spec.alpha"
|
#_#_org.babashka/spec.alpha {:git/url "https://github.com/babashka/spec.alpha"
|
||||||
:sha "0dec1f88cbde74a0470b454396f09a03adb4ae39"}
|
:sha "0dec1f88cbde74a0470b454396f09a03adb4ae39"}
|
||||||
lambdaisland/regal {:mvn/version "0.0.143"}
|
lambdaisland/regal {:mvn/version "0.0.143"}
|
||||||
cprop/cprop {:mvn/version "0.1.16"}
|
cprop/cprop {:mvn/version "0.1.16"}
|
||||||
|
|
|
||||||
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit b4c51fd5a0cedf19f1804c9bef1301ffb4e73bf0
|
Subproject commit 3d0a6e0ba050c288c5e5985423b77735eef9cf05
|
||||||
|
|
@ -89,6 +89,7 @@
|
||||||
|
|
||||||
(def signal-ns {'pipe-signal-received? (sci/copy-var pipe-signal-received? (sci/create-ns 'babashka.signal nil))})
|
(def signal-ns {'pipe-signal-received? (sci/copy-var pipe-signal-received? (sci/create-ns 'babashka.signal nil))})
|
||||||
|
|
||||||
|
(sci/enable-unrestricted-access!)
|
||||||
(sci/alter-var-root sci/in (constantly *in*))
|
(sci/alter-var-root sci/in (constantly *in*))
|
||||||
(sci/alter-var-root sci/out (constantly *out*))
|
(sci/alter-var-root sci/out (constantly *out*))
|
||||||
(sci/alter-var-root sci/err (constantly *err*))
|
(sci/alter-var-root sci/err (constantly *err*))
|
||||||
|
|
@ -744,8 +745,6 @@ Use bb run --help to show this help output.
|
||||||
:else
|
:else
|
||||||
(parse-args options opts-map))))))
|
(parse-args options opts-map))))))
|
||||||
|
|
||||||
(def env (atom {}))
|
|
||||||
|
|
||||||
(def pod-namespaces (volatile! {}))
|
(def pod-namespaces (volatile! {}))
|
||||||
|
|
||||||
(defn download-only?
|
(defn download-only?
|
||||||
|
|
@ -767,6 +766,79 @@ Use bb run --help to show this help output.
|
||||||
env-os-name-present? (not= env-os-name sys-os-name)
|
env-os-name-present? (not= env-os-name sys-os-name)
|
||||||
env-os-arch-present? (not= env-os-arch sys-os-arch))))
|
env-os-arch-present? (not= env-os-arch sys-os-arch))))
|
||||||
|
|
||||||
|
(def !uberscript (volatile! nil))
|
||||||
|
(def !uberscript-sources (volatile! nil))
|
||||||
|
|
||||||
|
(defn load-fn [{:keys [namespace reload]}]
|
||||||
|
(let [{:keys [loader]}
|
||||||
|
@cp/cp-state
|
||||||
|
uberscript @!uberscript
|
||||||
|
uberscript-sources @!uberscript-sources]
|
||||||
|
(or
|
||||||
|
(when ;; ignore built-in namespaces when uberscripting, unless with :reload
|
||||||
|
(and uberscript
|
||||||
|
(not reload)
|
||||||
|
(or (contains? namespaces namespace)
|
||||||
|
(contains? sci-namespaces/namespaces namespace)))
|
||||||
|
"")
|
||||||
|
;; pod namespaces go before namespaces from source,
|
||||||
|
;; unless reload is used
|
||||||
|
(when-not reload
|
||||||
|
(when-let [pod (get @pod-namespaces namespace)]
|
||||||
|
(if uberscript
|
||||||
|
(do
|
||||||
|
(swap! uberscript-sources conj
|
||||||
|
(format
|
||||||
|
"(babashka.pods/load-pod '%s \"%s\" '%s)\n"
|
||||||
|
(:pod-spec pod) (:version (:opts pod))
|
||||||
|
(dissoc (:opts pod)
|
||||||
|
:version :metadata)))
|
||||||
|
{})
|
||||||
|
(pods/load-pod (:pod-spec pod) (:opts pod)))))
|
||||||
|
(when loader
|
||||||
|
(when-let [res (cp/source-for-namespace loader namespace nil)]
|
||||||
|
(if uberscript
|
||||||
|
(do (swap! uberscript-sources conj (:source res))
|
||||||
|
(uberscript/uberscript {:ctx (common/ctx)
|
||||||
|
:expressions [(:source res)]})
|
||||||
|
{})
|
||||||
|
res)))
|
||||||
|
(let [rps (cp/resource-paths namespace)
|
||||||
|
rps (mapv #(str "src/babashka/" %) rps)]
|
||||||
|
(when-let [url (some #(io/resource %) rps)]
|
||||||
|
(let [source (slurp url)]
|
||||||
|
{:file (str url)
|
||||||
|
:source source})))
|
||||||
|
(case namespace
|
||||||
|
clojure.spec.alpha
|
||||||
|
(binding [*out* *err*]
|
||||||
|
(println "[babashka] WARNING: Use the babashka-compatible version of clojure.spec.alpha, available here: https://github.com/babashka/spec.alpha"))
|
||||||
|
clojure.core.specs.alpha
|
||||||
|
(binding [*out* *err*]
|
||||||
|
(println "[babashka] WARNING: clojure.core.specs.alpha is removed from the classpath, unless you explicitly add the dependency."))
|
||||||
|
nil))))
|
||||||
|
|
||||||
|
(def opts (-> {:aliases aliases
|
||||||
|
:namespaces (-> namespaces
|
||||||
|
(assoc 'clojure.core
|
||||||
|
(assoc core-extras
|
||||||
|
'load-file (sci-copy-vars/new-var 'load-file load-file*))))
|
||||||
|
:features #{:bb :clj}
|
||||||
|
:classes @classes/class-map
|
||||||
|
:imports classes/imports
|
||||||
|
:load-fn load-fn
|
||||||
|
;; :readers core/data-readers
|
||||||
|
:reify-fn reify-fn
|
||||||
|
:proxy-fn proxy-fn}
|
||||||
|
addons/future))
|
||||||
|
|
||||||
|
(defn new-sci-ctx []
|
||||||
|
(let [ctx (sci/init opts)]
|
||||||
|
(ctx-store/reset-ctx! ctx)
|
||||||
|
ctx))
|
||||||
|
|
||||||
|
(def sci-ctx (new-sci-ctx))
|
||||||
|
|
||||||
(defn exec [cli-opts]
|
(defn exec [cli-opts]
|
||||||
(binding [*unrestricted* true]
|
(binding [*unrestricted* true]
|
||||||
(sci/binding [core/warn-on-reflection @core/warn-on-reflection
|
(sci/binding [core/warn-on-reflection @core/warn-on-reflection
|
||||||
|
|
@ -775,15 +847,15 @@ Use bb run --help to show this help output.
|
||||||
sci/ns @sci/ns
|
sci/ns @sci/ns
|
||||||
sci/print-length @sci/print-length]
|
sci/print-length @sci/print-length]
|
||||||
(let [{version-opt :version
|
(let [{version-opt :version
|
||||||
:keys [:shell-in :edn-in :shell-out :edn-out
|
:keys [shell-in edn-in shell-out edn-out
|
||||||
:help :file :command-line-args
|
help file command-line-args
|
||||||
:expressions :stream? :init
|
expressions stream? init
|
||||||
:repl :socket-repl :nrepl
|
repl socket-repl nrepl
|
||||||
:debug :classpath :force?
|
debug classpath force?
|
||||||
:main :uberscript :describe?
|
main uberscript describe?
|
||||||
:jar :uberjar :clojure
|
jar uberjar clojure
|
||||||
:doc :run :list-tasks
|
doc run list-tasks
|
||||||
:print-deps :prepare]
|
print-deps prepare]
|
||||||
exec-fn :exec}
|
exec-fn :exec}
|
||||||
cli-opts
|
cli-opts
|
||||||
_ (when debug (vreset! common/debug true))
|
_ (when debug (vreset! common/debug true))
|
||||||
|
|
@ -820,79 +892,15 @@ Use bb run --help to show this help output.
|
||||||
abs-path))
|
abs-path))
|
||||||
_ (when jar
|
_ (when jar
|
||||||
(cp/add-classpath jar))
|
(cp/add-classpath jar))
|
||||||
load-fn (fn [{:keys [:namespace :reload]}]
|
|
||||||
(let [{:keys [loader]}
|
|
||||||
@cp/cp-state]
|
|
||||||
(or
|
|
||||||
(when ;; ignore built-in namespaces when uberscripting, unless with :reload
|
|
||||||
(and uberscript
|
|
||||||
(not reload)
|
|
||||||
(or (contains? namespaces namespace)
|
|
||||||
(contains? sci-namespaces/namespaces namespace)))
|
|
||||||
"")
|
|
||||||
;; pod namespaces go before namespaces from source,
|
|
||||||
;; unless reload is used
|
|
||||||
(when-not reload
|
|
||||||
(when-let [pod (get @pod-namespaces namespace)]
|
|
||||||
(if uberscript
|
|
||||||
(do
|
|
||||||
(swap! uberscript-sources conj
|
|
||||||
(format
|
|
||||||
"(babashka.pods/load-pod '%s \"%s\" '%s)\n"
|
|
||||||
(:pod-spec pod) (:version (:opts pod))
|
|
||||||
(dissoc (:opts pod)
|
|
||||||
:version :metadata)))
|
|
||||||
{})
|
|
||||||
(pods/load-pod (:pod-spec pod) (:opts pod)))))
|
|
||||||
(when loader
|
|
||||||
(when-let [res (cp/source-for-namespace loader namespace nil)]
|
|
||||||
(if uberscript
|
|
||||||
(do (swap! uberscript-sources conj (:source res))
|
|
||||||
(uberscript/uberscript {:ctx (common/ctx)
|
|
||||||
:expressions [(:source res)]})
|
|
||||||
{})
|
|
||||||
res)))
|
|
||||||
(let [rps (cp/resource-paths namespace)
|
|
||||||
rps (mapv #(str "src/babashka/" %) rps)]
|
|
||||||
(when-let [url (some #(io/resource %) rps)]
|
|
||||||
(let [source (slurp url)]
|
|
||||||
{:file (str url)
|
|
||||||
:source source})))
|
|
||||||
(case namespace
|
|
||||||
clojure.spec.alpha
|
|
||||||
(binding [*out* *err*]
|
|
||||||
(println "[babashka] WARNING: Use the babashka-compatible version of clojure.spec.alpha, available here: https://github.com/babashka/spec.alpha"))
|
|
||||||
clojure.core.specs.alpha
|
|
||||||
(binding [*out* *err*]
|
|
||||||
(println "[babashka] WARNING: clojure.core.specs.alpha is removed from the classpath, unless you explicitly add the dependency."))
|
|
||||||
nil))))
|
|
||||||
main (if (and jar (not main))
|
main (if (and jar (not main))
|
||||||
(when-let [res (cp/getResource
|
(when-let [res (cp/getResource
|
||||||
(cp/loader jar)
|
(cp/loader jar)
|
||||||
["META-INF/MANIFEST.MF"] {:url? true})]
|
["META-INF/MANIFEST.MF"] {:url? true})]
|
||||||
(cp/main-ns res))
|
(cp/main-ns res))
|
||||||
main)
|
main)
|
||||||
;; TODO: pull more of these values to compile time
|
|
||||||
opts {:aliases aliases
|
|
||||||
:namespaces (-> namespaces
|
|
||||||
(assoc 'clojure.core
|
|
||||||
(assoc core-extras
|
|
||||||
'load-file (sci-copy-vars/new-var 'load-file load-file*))))
|
|
||||||
:env env
|
|
||||||
:features #{:bb :clj}
|
|
||||||
:classes @classes/class-map
|
|
||||||
:imports classes/imports
|
|
||||||
:load-fn load-fn
|
|
||||||
:uberscript uberscript
|
|
||||||
;; :readers core/data-readers
|
|
||||||
:reify-fn reify-fn
|
|
||||||
:proxy-fn proxy-fn}
|
|
||||||
opts (addons/future opts)
|
|
||||||
sci-ctx (sci/init opts)
|
|
||||||
_ (ctx-store/reset-ctx! sci-ctx)
|
|
||||||
_ (when-let [pods (:pods @common/bb-edn)]
|
_ (when-let [pods (:pods @common/bb-edn)]
|
||||||
(when-let [pod-metadata (pods/load-pods-metadata
|
(when-let [pod-metadata (pods/load-pods-metadata
|
||||||
pods {:download-only (download-only?)})]
|
pods {:download-only (download-only?)})]
|
||||||
(vreset! pod-namespaces pod-metadata)))
|
(vreset! pod-namespaces pod-metadata)))
|
||||||
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
|
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
|
||||||
[expressions exit-code]
|
[expressions exit-code]
|
||||||
|
|
@ -970,9 +978,14 @@ Use bb run --help to show this help output.
|
||||||
print-deps [(print-deps/print-deps (:print-deps-format cli-opts)) 0]
|
print-deps [(print-deps/print-deps (:print-deps-format cli-opts)) 0]
|
||||||
prepare [nil 0]
|
prepare [nil 0]
|
||||||
uberscript
|
uberscript
|
||||||
[nil (do (uberscript/uberscript {:ctx sci-ctx
|
[nil (do
|
||||||
:expressions expressions})
|
(vreset! !uberscript true)
|
||||||
0)]
|
(vreset! !uberscript-sources uberscript-sources)
|
||||||
|
(uberscript/uberscript {:ctx sci-ctx
|
||||||
|
:expressions expressions})
|
||||||
|
(vreset! !uberscript nil)
|
||||||
|
(vreset! !uberscript-sources nil)
|
||||||
|
0)]
|
||||||
expressions
|
expressions
|
||||||
;; execute code
|
;; execute code
|
||||||
(sci/binding [sci/file abs-path]
|
(sci/binding [sci/file abs-path]
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@
|
||||||
[clojure.test :as test :refer [*report-counters*]]
|
[clojure.test :as test :refer [*report-counters*]]
|
||||||
[clojure.tools.reader.reader-types :as r]
|
[clojure.tools.reader.reader-types :as r]
|
||||||
[sci.core :as sci]
|
[sci.core :as sci]
|
||||||
[sci.impl.vars :as vars]))
|
[sci.impl.vars :as vars]
|
||||||
|
[sci.ctx-store :as ctx-store]))
|
||||||
|
|
||||||
(set! *warn-on-reflection* true)
|
(set! *warn-on-reflection* true)
|
||||||
|
|
||||||
|
|
@ -46,7 +47,7 @@
|
||||||
|
|
||||||
(defn bb-jvm [input-or-opts & args]
|
(defn bb-jvm [input-or-opts & args]
|
||||||
(reset! cp/cp-state nil)
|
(reset! cp/cp-state nil)
|
||||||
(reset! main/env {})
|
(alter-var-root #'main/sci-ctx (constantly (main/new-sci-ctx)))
|
||||||
(vreset! common/bb-edn nil)
|
(vreset! common/bb-edn nil)
|
||||||
(System/clearProperty "babashka.config")
|
(System/clearProperty "babashka.config")
|
||||||
(let [args (cond-> args *bb-edn-path*
|
(let [args (cond-> args *bb-edn-path*
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue