Skip to content

Commit

Permalink
Handle starting in a different ns
Browse files Browse the repository at this point in the history
Allow either :init-ns and :main. Add tests for reply option
conversions.

refs #432
  • Loading branch information
trptcolin committed Apr 11, 2012
1 parent 6e5dee8 commit b48eec9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
14 changes: 9 additions & 5 deletions sample.project.clj
Expand Up @@ -91,11 +91,15 @@
;; bringing in AOT if you don't need an executable uberjar. ;; bringing in AOT if you don't need an executable uberjar.
:main org.example.sample :main org.example.sample
;; Options to change the way the REPL behaves ;; Options to change the way the REPL behaves
:repl-options {;; These will get passed to clojure.main/repl; see :repl-options {;; Specify the string to print when prompting for input.
;; its docstring for details. ;; defaults to something like (fn [ns] (str *ns* "=> "))
:prompt (fn [ns] (print "your command, master?" ns) (flush)) :prompt (fn [ns] (str "your command for <" ns ">, master? " ))
;; This expression will be run when first opening a REPL. ;; Specify the ns to start the REPL in (overrides :main in
:init (in-ns 'foo.bar) ;; this case only)
:init-ns foo.bar
;; This expression will run when first opening a REPL, in the
;; namespace from :init-ns or :main if specified
:init (println "here we are in" *ns*)
;; Customize the socket the repl task listens on and ;; Customize the socket the repl task listens on and
;; attaches to. ;; attaches to.
:host "0.0.0.0" :host "0.0.0.0"
Expand Down
22 changes: 15 additions & 7 deletions src/leiningen/repl.clj
Expand Up @@ -51,6 +51,20 @@
(-> project :repl-options :ack-port))] (-> project :repl-options :ack-port))]
(Integer. p))) (Integer. p)))


(defn options-for-reply [repl-port project]
(let [repl-options (:repl-options project)]
(clojure.set/rename-keys
(assoc repl-options
:attach (if-let [host (repl-host project)]
(str host ":" repl-port)
(str repl-port))
:init (if-let [init-ns (or (:init-ns repl-options) (:main project))]
`(do (require '~init-ns) (in-ns '~init-ns)
~(:init repl-options))
(:init repl-options)))
{:prompt :custom-prompt
:init :custom-init})))

(defn ^:no-project-needed repl (defn ^:no-project-needed repl
"Start a repl session either with the current project or standalone. "Start a repl session either with the current project or standalone.
Expand Down Expand Up @@ -84,13 +98,7 @@ and port."
:repl-options :repl-options
:timeout) :timeout)
30000))] 30000))]
(reply/launch-nrepl (clojure.set/rename-keys (reply/launch-nrepl (options-for-reply repl-port project))
(assoc (:repl-options project)
:attach (if-let [host (repl-host project)]
(str host ":" repl-port)
(str repl-port)))
{:prompt :custom-prompt
:init :custom-init}))
(println "REPL server launch timed out.")))) (println "REPL server launch timed out."))))
([project flag & opts] ([project flag & opts]
(case flag (case flag
Expand Down
67 changes: 67 additions & 0 deletions test/leiningen/test/repl.clj
@@ -0,0 +1,67 @@
(ns leiningen.test.repl
(:use [clojure.test]
[leiningen.repl]))

(deftest test-options-for-reply-empty
(let [project {}]
(is (= {:attach "9876"
:custom-init nil}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-host
(let [project {:repl-options {:host "127.0.0.1"}}]
(is (= {:attach "127.0.0.1:9876"
:host "127.0.0.1"
:custom-init nil}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-prompt
(let [prompt-fn (fn [ns] "hi ")
project {:repl-options {:prompt prompt-fn}}]
(is (= {:attach "9876"
:custom-prompt prompt-fn
:custom-init nil}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-init
(let [init-form '(println "ohai")
project {:repl-options {:init init-form}}]
(is (= {:attach "9876"
:custom-init init-form}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-init-ns
(let [project {:repl-options {:init-ns 'foo.core}}]
(is (= {:attach "9876"
:init-ns 'foo.core
:custom-init '(do (clojure.core/require 'foo.core)
(clojure.core/in-ns 'foo.core)
nil)}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-init-ns-and-init
(let [project {:repl-options {:init-ns 'foo.core :init '(println "ohai")}}]
(is (= {:attach "9876"
:init-ns 'foo.core
:custom-init '(do (clojure.core/require 'foo.core)
(clojure.core/in-ns 'foo.core)
(println "ohai"))}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-main-ns
(let [project {:main 'foo.core}]
(is (= {:attach "9876"
:custom-init '(do (clojure.core/require 'foo.core)
(clojure.core/in-ns 'foo.core)
nil)}
(options-for-reply 9876 project)))))

(deftest test-options-for-reply-init-ns-beats-main
(let [project {:main 'foo.core :repl-options {:init-ns 'winner.here}}]
(is (= {:attach "9876"
:init-ns 'winner.here
:custom-init '(do (clojure.core/require 'winner.here)
(clojure.core/in-ns 'winner.here)
nil)}
(options-for-reply 9876 project)))))

0 comments on commit b48eec9

Please sign in to comment.