Fix preloads

This commit is contained in:
Michiel Borkent 2020-02-04 11:09:29 +01:00
parent 1d3eaae943
commit ba3eac83b1

View file

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