问题 如何在启动repl时默认加载ns


我正在使用lein2。我想在repl启动时默认加载一些ns。是否可以在project.clj中指定应该加载的ns,何时为该项目执行lein2 repl?


2323
2017-09-08 09:50


起源



答案:


你会发现很多答案 示例项目

;; Options to change the way the REPL behaves
:repl-options {;; Specify the string to print when prompting for input.
               ;; defaults to something like (fn [ns] (str *ns* "=> "))
               :prompt (fn [ns] (str "your command for <" ns ">, master? " ))
               ;; Specify the ns to start the REPL in (overrides :main in
               ;; 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
               ;; 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*)

用一个 project.clj 我很方便:

(defproject test "1.0.0"
  :repl-options { :init-ns test.core 
                  :init (do
                          (use 'clojure.set) 
                          (println (union #{1 2 3} #{3 4 5}))
                          (use 'incanter.core)
                          (println (factorial 5))) }
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [incanter/incanter-core "1.3.0-SNAPSHOT"]])

当我开火 lein repl

$ lein repl
nREPL server started on port 1121
REPL-y 0.1.0-beta10
Clojure 1.4.0
    Exit: Control+D or (exit) or (quit)
Commands: (user/help)
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
          (user/sourcery function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
      (user/clojuredocs name-here)
      (user/clojuredocs "ns-here" "name-here")
#{1 2 3 4 5}
120.0
test.core=>

10
2017-09-08 13:23



谢谢@noahz。我希望能够指定以下内容,其中fn中的3个语句是我想要执行的,我该怎么做 - :repl-options {:init(fn [_](使用'ring.util.serve) (使用'mfaiz.routes)(服务mfaiz.routes / my-app))} - murtaza52
您只是定义一个函数,因此添加第二组括号和所需的参数来调用它。 ((fn [_] (println _) "foo") - noahlz
是这样,这是它给我的错误 - CompilerException java.lang.RuntimeException:无法解析符号:在此上下文中提供,编译:(NO_SOURCE_PATH:1) - murtaza52
更新了我的答案,证明我提供的解决方案有效。似乎你已经转移到一个新问题。也许打开一个新问题并发布更多你的project.clj? - noahlz
你可以赞成我的答案和我的一些评论:) - noahlz


答案:


你会发现很多答案 示例项目

;; Options to change the way the REPL behaves
:repl-options {;; Specify the string to print when prompting for input.
               ;; defaults to something like (fn [ns] (str *ns* "=> "))
               :prompt (fn [ns] (str "your command for <" ns ">, master? " ))
               ;; Specify the ns to start the REPL in (overrides :main in
               ;; 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
               ;; 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*)

用一个 project.clj 我很方便:

(defproject test "1.0.0"
  :repl-options { :init-ns test.core 
                  :init (do
                          (use 'clojure.set) 
                          (println (union #{1 2 3} #{3 4 5}))
                          (use 'incanter.core)
                          (println (factorial 5))) }
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [incanter/incanter-core "1.3.0-SNAPSHOT"]])

当我开火 lein repl

$ lein repl
nREPL server started on port 1121
REPL-y 0.1.0-beta10
Clojure 1.4.0
    Exit: Control+D or (exit) or (quit)
Commands: (user/help)
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
          (user/sourcery function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
      (user/clojuredocs name-here)
      (user/clojuredocs "ns-here" "name-here")
#{1 2 3 4 5}
120.0
test.core=>

10
2017-09-08 13:23



谢谢@noahz。我希望能够指定以下内容,其中fn中的3个语句是我想要执行的,我该怎么做 - :repl-options {:init(fn [_](使用'ring.util.serve) (使用'mfaiz.routes)(服务mfaiz.routes / my-app))} - murtaza52
您只是定义一个函数,因此添加第二组括号和所需的参数来调用它。 ((fn [_] (println _) "foo") - noahlz
是这样,这是它给我的错误 - CompilerException java.lang.RuntimeException:无法解析符号:在此上下文中提供,编译:(NO_SOURCE_PATH:1) - murtaza52
更新了我的答案,证明我提供的解决方案有效。似乎你已经转移到一个新问题。也许打开一个新问题并发布更多你的project.clj? - noahlz
你可以赞成我的答案和我的一些评论:) - noahlz


我有时会使用 :injections 选项 project.clj 加载名称空间。以下示例将加载 foo.bar 命名空间时 lein2 命令执行:

(defproject org.example/sample "0.1.0-SNAPSHOT"
  :description "A sample project"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :injections [(use 'foo.bar)])

3
2017-09-08 13:31



那是什么 :repl-options { :init expr } 是为了 - noahlz