简体   繁体   English

Clojure:在特定命名空间中启动repl

[英]Clojure: boot repl in a particular namespace

I have boot-clj installed and want to be able to edit a .clj file in an external editor and separately have a command line REPL running from which I can call the functions that I change in the .clj file. 我安装了boot-clj并希望能够在外部编辑器中编辑.clj文件,并且单独运行命令行REPL,我可以从中调用我在.clj文件中更改的函数。 No special reloading commands should be required. 不需要特殊的重新加载命令。

Another thing is I don't want to have to manually type commands to include namespaces - I would like to just run a script that brings me into the namespace, so I can call existing functions right away. 另一件事是我不想手动输入命令来包含命名空间 - 我想只运行一个脚本将我带入命名空间,所以我可以立即调用现有的函数。

Name of the file: 文件名:

C:\dev\my-project\src\my_project\utils.clj

Something of what is inside the file: 文件内部的东西:

(ns my-project.utils
  (:require
    [clojure.string :as s]))

(defn my-range [start end]
  (take (- end start) (iterate inc start)))

I would like to go straight into a REPL and go (my-range 0 3) and see if it produces the result I want. 我想直接进入REPL并去(my-range 0 3) ,看看它是否产生我想要的结果。

What's the setup for this? 这是什么设置? What would the script file I need to run look like? 我需要运行的脚本文件是什么样的?

My current understanding is that the answer will look something like this: 我目前的理解是答案看起来像这样:

(deftask dev-repl
  (set-env! …)
  (repl))

at the command line 在命令行

You can achieve this to some degree at the command line, without creating a build.boot file: 您可以在命令行中在某种程度上实现此目的,而无需创建build.boot文件:

In C:\\dev\\my_project : 在C:\\ dev \\ my_project中

boot -r src repl -n my-project.utils
  • boot -r src : starts boot with src on the "resource paths", which is the set of directories that will be accessible within the JVM. boot -r src :在“资源路径”上使用src启动引导,这是可在JVM中访问的目录集。
  • repl -n my-project.utils starts a REPL, requires your namespace, and enters it. repl -n my-project.utils启动一个REPL,需要你的命名空间,然后输入它。

While the REPL is running, and after you have edited C:\\dev\\my_project\\src\\my_project\\utils.clj , you can reload it at the REPL like this: 当REPL正在运行时,在您编辑C:\\dev\\my_project\\src\\my_project\\utils.clj ,您可以在REPL上重新加载它,如下所示:

my-project.utils=> (require 'my-project.utils :reload)
nil

minimal build.boot 最小的build.boot

Alternatively, you could create the file C:\\dev\\my_project\\build.boot with these contents: 或者,您可以使用以下内容创建文件C:\\dev\\my_project\\build.boot

(set-env! :resource-paths #{"src"})

(deftask dev 
  "Run a development REPL"
  []
  (repl :init-ns 'my-project.utils))

Then, in C:\\dev\\my_project : 然后,在C:\\dev\\my_project

boot dev

Which will also start a REPL in your namespace, but requires less command-line configuration as we've performed the configuration in build.boot , which boot will automatically evaluate. 这也将在您的命名空间中启动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 the in-ns function. 注:从Clojure的REPL,无论构建工具,你可以要求任何命名空间(只要它是JVM的类路径上)与require的功能,并与输入它in-ns功能。

build.boot with automatic reloading build.boot自动重新加载

Finally, it's possible to combine features of Boot to achieve a development workflow oriented around automatically reloading code. 最后,可以结合Boot的功能来实现围绕自动重新加载代码的开发工作流程。

In C:\\dev\\my_project\\build.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)))

In C:\\dev\\my_project\\src\\my_project\\load_impl.clj : C:\\dev\\my_project\\src\\my_project\\load_impl.clj

(ns my-project.load-impl)

(require 'my-project.utils)

In C:\\dev\\my_project\\src\\my_project\\utils.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)))

(println "In the code!")
(println "(my-range 0 10) = " (my-range 10 20))

Back at the command prompt, type boot dev . 返回命令提示符,键入boot dev You should see some println output, and every time you edit and save the file you should see it again, reflecting any changes you made. 您应该看到一些println输出,每次编辑和保存文件时,您应该再次看到它,反映您所做的任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM