Fix preloads
This commit is contained in:
parent
1d3eaae943
commit
ba3eac83b1
1 changed files with 148 additions and 147 deletions
|
|
@ -282,153 +282,154 @@ Everything after that is bound to *command-line-args*."))
|
|||
(handle-pipe!)
|
||||
#_(binding [*out* *err*]
|
||||
(prn "M" (meta (get bindings 'future))))
|
||||
(let [t0 (System/currentTimeMillis)
|
||||
{:keys [:version :shell-in :edn-in :shell-out :edn-out
|
||||
:help? :file :command-line-args
|
||||
:expressions :stream? :time?
|
||||
:repl :socket-repl
|
||||
:verbose? :classpath
|
||||
:main :uberscript] :as _opts}
|
||||
(parse-opts args)
|
||||
read-next (fn [*in*]
|
||||
(if (pipe-signal-received?)
|
||||
::EOF
|
||||
(if stream?
|
||||
(if shell-in (or (read-line) ::EOF)
|
||||
(read-edn))
|
||||
(delay (cond shell-in
|
||||
(shell-seq *in*)
|
||||
edn-in
|
||||
(edn-seq *in*)
|
||||
:else
|
||||
(edn/read *in*))))))
|
||||
uberscript-sources (atom ())
|
||||
env (atom {})
|
||||
classpath (or classpath
|
||||
(System/getenv "BABASHKA_CLASSPATH"))
|
||||
_ (when classpath
|
||||
(add-classpath* classpath))
|
||||
load-fn (fn [{:keys [:namespace]}]
|
||||
(when-let [{:keys [:loader]} @cp-state]
|
||||
(let [res (cp/source-for-namespace loader namespace nil)]
|
||||
(when uberscript (swap! uberscript-sources conj (:source res)))
|
||||
res)))
|
||||
_ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file))))
|
||||
ctx {:aliases aliases
|
||||
:namespaces (-> namespaces
|
||||
(assoc 'clojure.core
|
||||
(assoc core-extras
|
||||
'*command-line-args*
|
||||
(sci/new-dynamic-var '*command-line-args* command-line-args)
|
||||
'*warn-on-reflection* reflection-var))
|
||||
(assoc-in ['clojure.java.io 'resource]
|
||||
#(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true}))))
|
||||
:bindings bindings
|
||||
:env env
|
||||
:features #{:bb}
|
||||
:classes classes/class-map
|
||||
:imports '{ArithmeticException java.lang.ArithmeticException
|
||||
AssertionError java.lang.AssertionError
|
||||
Boolean java.lang.Boolean
|
||||
Class java.lang.Class
|
||||
Double java.lang.Double
|
||||
Exception java.lang.Exception
|
||||
IllegalArgumentException java.lang.IllegalArgumentException
|
||||
Integer java.lang.Integer
|
||||
File java.io.File
|
||||
Math java.lang.Math
|
||||
Object java.lang.Object
|
||||
ProcessBuilder java.lang.ProcessBuilder
|
||||
String java.lang.String
|
||||
StringBuilder java.lang.StringBuilder
|
||||
System java.lang.System
|
||||
Thread java.lang.Thread
|
||||
Throwable java.lang.Throwable}
|
||||
:load-fn load-fn
|
||||
:dry-run uberscript}
|
||||
ctx (addons/future ctx)
|
||||
sci-ctx (sci-opts/init ctx)
|
||||
_ (vreset! common/ctx sci-ctx)
|
||||
_ (swap! (:env sci-ctx)
|
||||
(fn [env]
|
||||
(update-in env [:namespaces 'clojure.core] assoc
|
||||
'eval #(eval* sci-ctx %)
|
||||
'load-file #(load-file* sci-ctx %))))
|
||||
_ (swap! (:env sci-ctx)
|
||||
(fn [env]
|
||||
(assoc-in env [:namespaces 'clojure.main 'repl]
|
||||
(fn [& opts]
|
||||
(let [opts (apply hash-map opts)]
|
||||
(repl/start-repl! sci-ctx opts))))))
|
||||
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
|
||||
[expressions exit-code]
|
||||
(cond expressions [expressions nil]
|
||||
main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)"
|
||||
main)] nil]
|
||||
file (try [[(read-file file)] nil]
|
||||
(catch Exception e
|
||||
(error-handler* e verbose?))))
|
||||
expression (str/join " " expressions) ;; this might mess with the locations...
|
||||
exit-code
|
||||
;; handle preloads
|
||||
(if exit-code exit-code
|
||||
(do (when preloads (try (eval-string* sci-ctx preloads)
|
||||
(catch Throwable e
|
||||
(error-handler* e verbose?))))
|
||||
nil))
|
||||
exit-code
|
||||
(or exit-code
|
||||
(sci/with-bindings {reflection-var false
|
||||
vars/current-ns (vars/->SciNamespace 'user)}
|
||||
(or
|
||||
(second
|
||||
(cond version
|
||||
[(print-version) 0]
|
||||
help?
|
||||
[(print-help) 0]
|
||||
repl [(repl/start-repl! sci-ctx) 0]
|
||||
socket-repl [(start-socket-repl! socket-repl sci-ctx) 0]
|
||||
(not (str/blank? expression))
|
||||
(try
|
||||
(loop [in (read-next *in*)]
|
||||
(let [_ (swap! env update-in [:namespaces 'user]
|
||||
assoc (with-meta '*input*
|
||||
(when-not stream?
|
||||
{:sci.impl/deref! true}))
|
||||
(sci/new-dynamic-var '*input* in))]
|
||||
(if (identical? ::EOF in)
|
||||
[nil 0] ;; done streaming
|
||||
(let [res [(let [res (eval-string* sci-ctx expression)]
|
||||
(when (some? res)
|
||||
(if-let [pr-f (cond shell-out println
|
||||
edn-out prn)]
|
||||
(if (coll? res)
|
||||
(doseq [l res
|
||||
:while (not (pipe-signal-received?))]
|
||||
(pr-f l))
|
||||
(pr-f res))
|
||||
(prn res)))) 0]]
|
||||
(if stream?
|
||||
(recur (read-next *in*))
|
||||
res)))))
|
||||
(catch Throwable e
|
||||
(error-handler* e verbose?)))
|
||||
uberscript [nil 0]
|
||||
:else [(repl/start-repl! sci-ctx) 0]))
|
||||
1)))
|
||||
t1 (System/currentTimeMillis)]
|
||||
(flush)
|
||||
(when uberscript
|
||||
uberscript
|
||||
(let [uberscript-out uberscript]
|
||||
(spit uberscript-out "") ;; reset file
|
||||
(doseq [s @uberscript-sources]
|
||||
(spit uberscript-out s :append true))
|
||||
(spit uberscript-out preloads :append true)
|
||||
(spit uberscript-out expression :append true)))
|
||||
(when time? (binding [*out* *err*]
|
||||
(println "bb took" (str (- t1 t0) "ms."))))
|
||||
exit-code))
|
||||
(sci/with-bindings {reflection-var false
|
||||
vars/current-ns (vars/->SciNamespace 'user)}
|
||||
(let [t0 (System/currentTimeMillis)
|
||||
{:keys [:version :shell-in :edn-in :shell-out :edn-out
|
||||
:help? :file :command-line-args
|
||||
:expressions :stream? :time?
|
||||
:repl :socket-repl
|
||||
:verbose? :classpath
|
||||
:main :uberscript] :as _opts}
|
||||
(parse-opts args)
|
||||
read-next (fn [*in*]
|
||||
(if (pipe-signal-received?)
|
||||
::EOF
|
||||
(if stream?
|
||||
(if shell-in (or (read-line) ::EOF)
|
||||
(read-edn))
|
||||
(delay (cond shell-in
|
||||
(shell-seq *in*)
|
||||
edn-in
|
||||
(edn-seq *in*)
|
||||
:else
|
||||
(edn/read *in*))))))
|
||||
uberscript-sources (atom ())
|
||||
env (atom {})
|
||||
classpath (or classpath
|
||||
(System/getenv "BABASHKA_CLASSPATH"))
|
||||
_ (when classpath
|
||||
(add-classpath* classpath))
|
||||
load-fn (fn [{:keys [:namespace]}]
|
||||
(when-let [{:keys [:loader]} @cp-state]
|
||||
(let [res (cp/source-for-namespace loader namespace nil)]
|
||||
(when uberscript (swap! uberscript-sources conj (:source res)))
|
||||
res)))
|
||||
_ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file))))
|
||||
ctx {:aliases aliases
|
||||
:namespaces (-> namespaces
|
||||
(assoc 'clojure.core
|
||||
(assoc core-extras
|
||||
'*command-line-args*
|
||||
(sci/new-dynamic-var '*command-line-args* command-line-args)
|
||||
'*warn-on-reflection* reflection-var))
|
||||
(assoc-in ['clojure.java.io 'resource]
|
||||
#(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true}))))
|
||||
:bindings bindings
|
||||
:env env
|
||||
:features #{:bb}
|
||||
:classes classes/class-map
|
||||
:imports '{ArithmeticException java.lang.ArithmeticException
|
||||
AssertionError java.lang.AssertionError
|
||||
Boolean java.lang.Boolean
|
||||
Class java.lang.Class
|
||||
Double java.lang.Double
|
||||
Exception java.lang.Exception
|
||||
IllegalArgumentException java.lang.IllegalArgumentException
|
||||
Integer java.lang.Integer
|
||||
File java.io.File
|
||||
Math java.lang.Math
|
||||
Object java.lang.Object
|
||||
ProcessBuilder java.lang.ProcessBuilder
|
||||
String java.lang.String
|
||||
StringBuilder java.lang.StringBuilder
|
||||
System java.lang.System
|
||||
Thread java.lang.Thread
|
||||
Throwable java.lang.Throwable}
|
||||
:load-fn load-fn
|
||||
:dry-run uberscript}
|
||||
ctx (addons/future ctx)
|
||||
sci-ctx (sci-opts/init ctx)
|
||||
_ (vreset! common/ctx sci-ctx)
|
||||
_ (swap! (:env sci-ctx)
|
||||
(fn [env]
|
||||
(update-in env [:namespaces 'clojure.core] assoc
|
||||
'eval #(eval* sci-ctx %)
|
||||
'load-file #(load-file* sci-ctx %))))
|
||||
_ (swap! (:env sci-ctx)
|
||||
(fn [env]
|
||||
(assoc-in env [:namespaces 'clojure.main 'repl]
|
||||
(fn [& opts]
|
||||
(let [opts (apply hash-map opts)]
|
||||
(repl/start-repl! sci-ctx opts))))))
|
||||
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
|
||||
[expressions exit-code]
|
||||
(cond expressions [expressions nil]
|
||||
main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)"
|
||||
main)] nil]
|
||||
file (try [[(read-file file)] nil]
|
||||
(catch Exception e
|
||||
(error-handler* e verbose?))))
|
||||
expression (str/join " " expressions) ;; this might mess with the locations...
|
||||
exit-code
|
||||
;; handle preloads
|
||||
(if exit-code exit-code
|
||||
(do (when preloads
|
||||
(try
|
||||
(eval-string* sci-ctx preloads)
|
||||
(catch Throwable e
|
||||
(error-handler* e verbose?))))
|
||||
nil))
|
||||
exit-code
|
||||
(or exit-code
|
||||
(second
|
||||
(cond version
|
||||
[(print-version) 0]
|
||||
help?
|
||||
[(print-help) 0]
|
||||
repl [(repl/start-repl! sci-ctx) 0]
|
||||
socket-repl [(start-socket-repl! socket-repl sci-ctx) 0]
|
||||
(not (str/blank? expression))
|
||||
(try
|
||||
(loop [in (read-next *in*)]
|
||||
(let [_ (swap! env update-in [:namespaces 'user]
|
||||
assoc (with-meta '*input*
|
||||
(when-not stream?
|
||||
{:sci.impl/deref! true}))
|
||||
(sci/new-dynamic-var '*input* in))]
|
||||
(if (identical? ::EOF in)
|
||||
[nil 0] ;; done streaming
|
||||
(let [res [(let [res (eval-string* sci-ctx expression)]
|
||||
(when (some? res)
|
||||
(if-let [pr-f (cond shell-out println
|
||||
edn-out prn)]
|
||||
(if (coll? res)
|
||||
(doseq [l res
|
||||
:while (not (pipe-signal-received?))]
|
||||
(pr-f l))
|
||||
(pr-f res))
|
||||
(prn res)))) 0]]
|
||||
(if stream?
|
||||
(recur (read-next *in*))
|
||||
res)))))
|
||||
(catch Throwable e
|
||||
(error-handler* e verbose?)))
|
||||
uberscript [nil 0]
|
||||
:else [(repl/start-repl! sci-ctx) 0]))
|
||||
1)
|
||||
t1 (System/currentTimeMillis)]
|
||||
(flush)
|
||||
(when uberscript
|
||||
uberscript
|
||||
(let [uberscript-out uberscript]
|
||||
(spit uberscript-out "") ;; reset file
|
||||
(doseq [s @uberscript-sources]
|
||||
(spit uberscript-out s :append true))
|
||||
(spit uberscript-out preloads :append true)
|
||||
(spit uberscript-out expression :append true)))
|
||||
(when time? (binding [*out* *err*]
|
||||
(println "bb took" (str (- t1 t0) "ms."))))
|
||||
exit-code)))
|
||||
|
||||
(defn -main
|
||||
[& args]
|
||||
|
|
|
|||
Loading…
Reference in a new issue