Support code in var section
This commit is contained in:
parent
016e20dc52
commit
8f16139ab6
8 changed files with 65 additions and 44 deletions
2
deps.edn
2
deps.edn
|
|
@ -4,7 +4,7 @@
|
||||||
:aliases
|
:aliases
|
||||||
{:sci
|
{:sci
|
||||||
{:extra-deps
|
{:extra-deps
|
||||||
{borkdude/sci {:mvn/version "0.0.13-alpha.19"}}}
|
{borkdude/sci {:mvn/version "0.0.13-alpha.22"}}}
|
||||||
:test
|
:test
|
||||||
{:extra-deps
|
{:extra-deps
|
||||||
{test-runner
|
{test-runner
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@
|
||||||
(-> (get m k)
|
(-> (get m k)
|
||||||
bytes->string))
|
bytes->string))
|
||||||
|
|
||||||
|
(defn get-maybe-string [m k]
|
||||||
|
(some-> (get m k)
|
||||||
|
bytes->string))
|
||||||
|
|
||||||
(defn processor [pod]
|
(defn processor [pod]
|
||||||
(let [stdout (:stdout pod)
|
(let [stdout (:stdout pod)
|
||||||
format (:format pod)
|
format (:format pod)
|
||||||
|
|
@ -154,10 +158,13 @@
|
||||||
bytes->string
|
bytes->string
|
||||||
#(Boolean/parseBoolean %))
|
#(Boolean/parseBoolean %))
|
||||||
name-sym (symbol name)
|
name-sym (symbol name)
|
||||||
sym (symbol ns-name-str name)]
|
sym (symbol ns-name-str name)
|
||||||
(assoc m name-sym (fn [& args]
|
code (get-maybe-string var "code")]
|
||||||
(let [res (invoke pod sym args async?)]
|
(assoc m name-sym
|
||||||
res)))))
|
(or code
|
||||||
|
(fn [& args]
|
||||||
|
(let [res (invoke pod sym args async?)]
|
||||||
|
res))))))
|
||||||
{}
|
{}
|
||||||
vars))
|
vars))
|
||||||
pod-namespaces (reduce (fn [namespaces namespace]
|
pod-namespaces (reduce (fn [namespaces namespace]
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,14 @@
|
||||||
(let [pod (impl/load-pod pod-spec _opts)
|
(let [pod (impl/load-pod pod-spec _opts)
|
||||||
namespaces (:namespaces pod)]
|
namespaces (:namespaces pod)]
|
||||||
(doseq [[ns-sym v] namespaces]
|
(doseq [[ns-sym v] namespaces]
|
||||||
(binding [*ns* (create-ns ns-sym)]
|
(binding [*ns* (load-string (format "(ns %s) *ns*" ns-sym))]
|
||||||
(dosync (commute @#'clojure.core/*loaded-libs* conj ns-sym)))
|
(doseq [[var-sym v] v]
|
||||||
(doseq [[var-sym v] v]
|
(cond
|
||||||
(intern ns-sym var-sym v)))
|
(ifn? v)
|
||||||
|
(do
|
||||||
|
(ns-unmap *ns* var-sym)
|
||||||
|
(intern ns-sym var-sym v))
|
||||||
|
(string? v)
|
||||||
|
(load-string v)))))
|
||||||
(future (impl/processor pod))
|
(future (impl/processor pod))
|
||||||
nil)))
|
nil)))
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,14 @@
|
||||||
(impl/load-pod pod-spec _opts))
|
(impl/load-pod pod-spec _opts))
|
||||||
namespaces (:namespaces pod)
|
namespaces (:namespaces pod)
|
||||||
env (:env ctx)]
|
env (:env ctx)]
|
||||||
(swap! env update :namespaces merge namespaces)
|
(doseq [[ns-name vars] namespaces
|
||||||
|
:let [sci-ns (sci/create-ns ns-name)]]
|
||||||
|
(sci/binding [sci/ns sci-ns]
|
||||||
|
(doseq [[var-name var-value] vars]
|
||||||
|
(cond (ifn? var-value)
|
||||||
|
(swap! env assoc-in [:namespaces ns-name var-name] var-value)
|
||||||
|
(string? var-value)
|
||||||
|
(sci/eval-string* ctx var-value)))))
|
||||||
(sci/future (impl/processor pod))
|
(sci/future (impl/processor pod))
|
||||||
nil)))
|
nil)))
|
||||||
{:sci.impl/op :needs-ctx}))
|
{:sci.impl/op :needs-ctx}))
|
||||||
|
|
|
||||||
|
|
@ -45,21 +45,24 @@
|
||||||
op (read-string op)
|
op (read-string op)
|
||||||
op (keyword op)]
|
op (keyword op)]
|
||||||
(case op
|
(case op
|
||||||
:describe (do (write {"format" (if (= format :json)
|
:describe
|
||||||
"json"
|
(do (write {"format" (if (= format :json)
|
||||||
"edn")
|
"json"
|
||||||
"namespaces"
|
"edn")
|
||||||
[{"name" "pod.test-pod"
|
"namespaces"
|
||||||
"vars" [{"name" "add-sync"}
|
[{"name" "pod.test-pod"
|
||||||
{"name" "range-stream"
|
"vars" [{"name" "add-sync"}
|
||||||
"async" "true"}
|
{"name" "range-stream"
|
||||||
{"name" "assoc"}
|
"async" "true"}
|
||||||
{"name" "error"}
|
{"name" "assoc"}
|
||||||
{"name" "print"}
|
{"name" "error"}
|
||||||
{"name" "print-err"}
|
{"name" "print"}
|
||||||
{"name" "return-nil"}]}]
|
{"name" "print-err"}
|
||||||
"ops" {"shutdown" {}}})
|
{"name" "return-nil"}
|
||||||
(recur))
|
{"name" "do-twice"
|
||||||
|
"code" "(defmacro do-twice [x] `(do ~x ~x))"}]}]
|
||||||
|
"ops" {"shutdown" {}}})
|
||||||
|
(recur))
|
||||||
:invoke (let [var (-> (get message "var")
|
:invoke (let [var (-> (get message "var")
|
||||||
read-string
|
read-string
|
||||||
symbol)
|
symbol)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
(ns babashka.pods.jvm-test
|
(ns babashka.pods.jvm-test
|
||||||
(:require [babashka.pods.test-common :refer [test-program]]
|
(:require [babashka.pods.test-common :refer [test-program assertions]]
|
||||||
[clojure.test :refer [deftest is]]))
|
[clojure.test :refer [deftest]]))
|
||||||
|
|
||||||
(deftest jvm-test
|
(deftest jvm-test
|
||||||
(let [out (java.io.StringWriter.)
|
(let [out (java.io.StringWriter.)
|
||||||
|
|
@ -11,10 +11,4 @@
|
||||||
test-program)
|
test-program)
|
||||||
(catch Exception e (prn e))))]
|
(catch Exception e (prn e))))]
|
||||||
|
|
||||||
(is (= '[{:a 1, :b 2}
|
(assertions out err ret)))
|
||||||
6
|
|
||||||
[1 2 3 4 5 6 7 8 9]
|
|
||||||
"Illegal arguments / {:args (1 2 3)}"
|
|
||||||
nil] ret))
|
|
||||||
(is (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out)))
|
|
||||||
(is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err)))))
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
(ns babashka.pods.sci-test
|
(ns babashka.pods.sci-test
|
||||||
(:require [babashka.pods.sci :as pods]
|
(:require [babashka.pods.sci :as pods]
|
||||||
[babashka.pods.test-common :refer [test-program]]
|
[babashka.pods.test-common :refer [test-program assertions]]
|
||||||
[clojure.core.async :as async]
|
[clojure.core.async :as async]
|
||||||
[clojure.test :refer [deftest is]]
|
[clojure.test :refer [deftest]]
|
||||||
[sci.core :as sci]))
|
[sci.core :as sci]))
|
||||||
|
|
||||||
(deftest sci-test
|
(deftest sci-test
|
||||||
|
|
@ -17,10 +17,4 @@
|
||||||
'clojure.core.async
|
'clojure.core.async
|
||||||
{'<!! async/<!!}}}))]
|
{'<!! async/<!!}}}))]
|
||||||
|
|
||||||
(is (= '[{:a 1, :b 2}
|
(assertions out err ret)))
|
||||||
6
|
|
||||||
[1 2 3 4 5 6 7 8 9]
|
|
||||||
"Illegal arguments / {:args (1 2 3)}"
|
|
||||||
nil] ret))
|
|
||||||
(is (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out)))
|
|
||||||
(is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err)))))
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
(ns babashka.pods.test-common)
|
(ns babashka.pods.test-common
|
||||||
|
(:require [clojure.test :refer [is]]))
|
||||||
|
|
||||||
(def test-program "
|
(def test-program "
|
||||||
(require '[babashka.pods :as pods])
|
(require '[babashka.pods :as pods])
|
||||||
|
|
@ -18,8 +19,18 @@
|
||||||
(str (ex-message e) \" / \" (ex-data e)))))
|
(str (ex-message e) \" / \" (ex-data e)))))
|
||||||
(pod.test-pod/print \"hello\" \"print\" \"this\" \"debugging\" \"message\")
|
(pod.test-pod/print \"hello\" \"print\" \"this\" \"debugging\" \"message\")
|
||||||
(pod.test-pod/print-err \"hello\" \"print\" \"this\" \"error\")
|
(pod.test-pod/print-err \"hello\" \"print\" \"this\" \"error\")
|
||||||
|
(pod/do-twice (prn :foo))
|
||||||
[(pod/assoc {:a 1} :b 2)
|
[(pod/assoc {:a 1} :b 2)
|
||||||
(pod.test-pod/add-sync 1 2 3)
|
(pod.test-pod/add-sync 1 2 3)
|
||||||
@stream-results
|
@stream-results
|
||||||
ex-result
|
ex-result
|
||||||
(pod.test-pod/return-nil)]")
|
(pod.test-pod/return-nil)]")
|
||||||
|
|
||||||
|
(defn assertions [out err ret]
|
||||||
|
(is (= '[{:a 1, :b 2}
|
||||||
|
6
|
||||||
|
[1 2 3 4 5 6 7 8 9]
|
||||||
|
"Illegal arguments / {:args (1 2 3)}"
|
||||||
|
nil] ret))
|
||||||
|
(is (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n:foo\n:foo\n" (str out)))
|
||||||
|
(is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue