我安装了 boot-clj 并希望能够在外部编辑器中编辑 .clj 文件,并单独运行命令行 REPL,我可以从中调用我在 .clj 文件中更改的函数。不需要特殊的重新加载命令。
另一件事是我不想手动键入命令来包含命名空间 - 我只想运行一个脚本,将我带入命名空间,这样我就可以立即调用现有函数。
文件名:
C:\dev\my-project\src\my_project\utils.clj
文件中的一些内容:
(ns my-project.utils
(:require
[clojure.string :as s]))
(defn my-range [start end]
(take (- end start) (iterate inc start)))
我想直接进入 REPL 并去
(my-range 0 3)
看看它是否产生了我想要的结果。这是什么设置?我需要运行的脚本文件是什么样的?
我目前的理解是,答案将如下所示:
(deftask dev-repl
(set-env! …)
(repl))
最佳答案
在命令行
您可以在命令行中在某种程度上实现这一点,而无需创建 build.boot
文件:
在 C:\dev\my_project 中:
boot -r src repl -n my-project.utils
boot -r src
: 从 src
开始启动在“资源路径”上,这是一组可在 JVM 中访问的目录。 repl -n my-project.utils
启动一个 REPL,需要你的命名空间,然后输入它。 当 REPL 正在运行时,并且在您编辑之后
C:\dev\my_project\src\my_project\utils.clj
,你可以像这样在 REPL 重新加载它:my-project.utils=> (require 'my-project.utils :reload)
nil
最小
build.boot
或者,您可以创建文件
C:\dev\my_project\build.boot
有这些内容:(set-env! :resource-paths #{"src"})
(deftask dev
"Run a development REPL"
[]
(repl :init-ns 'my-project.utils))
然后,在
C:\dev\my_project
:boot dev
这也将在您的命名空间中启动 REPL,但需要较少的命令行配置,因为我们已经在
build.boot
中执行了配置, 其中 boot
会自动评估。Note: from a Clojure REPL, regardless of build tool, you can require any namespace (as long as it's on the JVM's class path) with the
require
function and enter it with thein-ns
function.
build.boot 自动重新加载
最后,可以结合 Boot 的功能来实现面向自动重新加载代码的开发工作流程。
在
C:\dev\my_project\build.boot
:(set-env! :resource-paths #{"src"})
(require '[boot.core :as core]
'[boot.pod :as pod])
(deftask load-ns
"Loads the my-project.utils namespace in a fresh pod."
[]
(let [pods (pod/pod-pool (core/get-env))]
(core/with-pre-wrap [fileset]
(pod/with-eval-in (pods :refresh)
;; We require indirectly here so that errors from my-project.utils have
;; proper line and column information.
(require 'my-project.load-impl))
fileset)))
(deftask dev
"Watches source code and loads my-project/utils every time code changes."
[]
(comp (watch)
(load-ns)))
在
C:\dev\my_project\src\my_project\load_impl.clj
:(ns my-project.load-impl)
(require 'my-project.utils)
在
C:\dev\my_project\src\my_project\utils.clj
:(ns my-project.utils
(:require
[clojure.string :as s]))
(defn my-range [start end]
(take (- end start) (iterate inc start)))
(println "In the code!")
(println "(my-range 0 10) = " (my-range 10 20))
回到命令提示符,输入
boot dev
.你应该看到一些 println
输出,每次编辑和保存文件时,您都应该再次看到它,反射(reflect)您所做的任何更改。
关于Clojure:在特定命名空间中启动 repl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33982533/