support *in* and *command-line-args* in socket-repl (#46)

This commit is contained in:
Michiel Borkent 2019-09-02 12:50:37 +02:00 committed by GitHub
parent e9e289d355
commit bcd7a5b408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 31 deletions

View file

@ -30,6 +30,10 @@ jobs:
wget -nc https://download.clojure.org/install/linux-install-1.10.1.447.sh wget -nc https://download.clojure.org/install/linux-install-1.10.1.447.sh
chmod +x linux-install-1.10.1.447.sh chmod +x linux-install-1.10.1.447.sh
sudo ./linux-install-1.10.1.447.sh sudo ./linux-install-1.10.1.447.sh
- run:
name: Install lsof
command: |
sudo apt-get install lsof
- run: - run:
name: Download GraalVM name: Download GraalVM
command: | command: |
@ -83,6 +87,10 @@ jobs:
wget https://download.clojure.org/install/linux-install-1.10.1.447.sh wget https://download.clojure.org/install/linux-install-1.10.1.447.sh
chmod +x linux-install-1.10.1.447.sh chmod +x linux-install-1.10.1.447.sh
sudo ./linux-install-1.10.1.447.sh sudo ./linux-install-1.10.1.447.sh
- run:
name: Install lsof
command: |
sudo apt-get install lsof
- run: - run:
name: Install native dev tools name: Install native dev tools
command: | command: |

2
sci

@ -1 +1 @@
Subproject commit ade0d29a4d3d97d9d6c958434368d5fa4359debf Subproject commit 4ab01bd92cc87511f478c861c84b40c6e208eeae

View file

@ -61,7 +61,8 @@
(recur (rest options) (recur (rest options)
(assoc opts-map (assoc opts-map
:socket-repl (first options)))) :socket-repl (first options))))
(if (not (:file opts-map)) (if (not (or (:file opts-map)
(:socket-repl opts-map)))
(assoc opts-map (assoc opts-map
:expression opt :expression opt
:command-line-args (rest options)) :command-line-args (rest options))
@ -177,6 +178,15 @@
(let [s (slurp file)] (let [s (slurp file)]
(sci/eval-string s ctx))) (sci/eval-string s ctx)))
(defn start-socket-repl! [address ctx read-next]
(let [ctx (update ctx :bindings assoc
(with-meta '*in*
{:sci/deref! true})
(read-next))]
(socket-repl/start-repl! address ctx)
;; hang until SIGINT
@(promise)))
(defn main (defn main
[& args] [& args]
(handle-pipe!) (handle-pipe!)
@ -187,17 +197,18 @@
:help? :file :command-line-args :help? :file :command-line-args
:expression :stream? :time? :socket-repl] :as _opts} :expression :stream? :time? :socket-repl] :as _opts}
(parse-opts args) (parse-opts args)
read-next #(if (pipe-signal-received?) read-next (fn [*in*]
::EOF (if (pipe-signal-received?)
(if stream? ::EOF
(if shell-in (or (read-line) ::EOF) (if stream?
(read-edn)) (if shell-in (or (read-line) ::EOF)
(delay (cond shell-in (read-edn))
(shell-seq *in*) (delay (cond shell-in
edn-in (shell-seq *in*)
(edn-seq *in*) edn-in
:else (edn-seq *in*)
(edn/read *in*))))) :else
(edn/read *in*))))))
env (atom {}) env (atom {})
ctx {:bindings (assoc bindings '*command-line-args* command-line-args) ctx {:bindings (assoc bindings '*command-line-args* command-line-args)
:env env} :env env}
@ -212,12 +223,11 @@
[(print-version) 0] [(print-version) 0]
help? help?
[(print-help) 0] [(print-help) 0]
socket-repl [(do (socket-repl/start-repl! socket-repl ctx) socket-repl [(start-socket-repl! socket-repl ctx #(read-next *in*)) 0]
@(promise)) 0]
:else :else
(try (try
(let [expr (if file (read-file file) expression)] (let [expr (if file (read-file file) expression)]
(loop [in (read-next)] (loop [in (read-next *in*)]
(let [ctx (update ctx :bindings assoc (with-meta '*in* (let [ctx (update ctx :bindings assoc (with-meta '*in*
(when-not stream? (when-not stream?
{:sci/deref! true})) in)] {:sci/deref! true})) in)]
@ -235,7 +245,7 @@
(pr-f res)) (pr-f res))
(prn res)))) 0]] (prn res)))) 0]]
(if stream? (if stream?
(recur (read-next)) (recur (read-next *in*))
res)))))) res))))))
(catch Exception e (catch Exception e
(binding [*out* *err*] (binding [*out* *err*]

View file

@ -11,21 +11,48 @@
(str/lower-case (System/getProperty "os.name")) (str/lower-case (System/getProperty "os.name"))
"mac")) "mac"))
(defn socket-command [expr]
(let [ret (sh "bash" "-c"
(format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
(pr-str expr)))]
(:out ret)))
(deftest socket-repl-test (deftest socket-repl-test
(when tu/jvm? (try
(try (if tu/jvm?
(start-repl! "0.0.0.0:1666" {:env (atom {})}) (start-repl! "0.0.0.0:1666" {:bindings {(with-meta '*in*
(is (str/includes? (:out (sh "bash" "-c" {:sci/deref! true})
"echo \"(+ 1 2 3)\n:repl/exit\" | nc 127.0.0.1 1666")) (delay [1 2 3])
"bb=> 6")) '*command-line-args*
(testing "ctrl-d exits normally, doesn't print nil" ["a" "b" "c"]}
(is (str/ends-with? (:out (sh "bash" "-c" :env (atom {})})
(if mac? ;; mac doesn't support -q (future
"echo \"(inc 1336)\" | nc 127.0.0.1 1666" (sh "bash" "-c"
"echo \"(inc 1336)\" | nc -q 1 127.0.0.1 1666"))) "echo '[1 2 3]' | ./bb --socket-repl 0.0.0.0:1666 a b c")))
"1337\nbb=> "))) ;; wait for server to be available
(finally (when tu/native?
(stop-repl!))))) (while (not (zero? (:exit
(sh "bash" "-c"
"lsof -t -i:1666"))))))
(is (str/includes? (socket-command '(+ 1 2 3))
"bb=> 6"))
(testing "ctrl-d exits normally, doesn't print nil"
(is (str/ends-with? (:out (sh "bash" "-c"
(if mac? ;; mac doesn't support -q
"echo \"(inc 1336)\" | nc 127.0.0.1 1666"
"echo \"(inc 1336)\" | nc -q 1 127.0.0.1 1666")))
"1337\nbb=> ")))
(testing "*in*"
(is (str/includes? (socket-command '*in*)
"[1 2 3]")))
(testing "*command-line-args*"
(is (str/includes? (socket-command '*command-line-args*)
"\"a\" \"b\" \"c\"")))
(finally
(if tu/jvm?
(stop-repl!)
(sh "bash" "-c"
"kill -9 $(lsof -t -i:1666)")))))
;;;; Scratch ;;;; Scratch