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

标签 clojure boot-clj

我安装了 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 the in-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/

    相关文章:

    firefox-addon-sdk - 使用 Boot 时,将已编译的 Cljs 构建到/移动到任意目录的惯用方法是什么?

    clojure - 类路径冲突 : org. clojure/clojure 1.7.0 版已加载,未加载 1.8.0 版

    Clojure 模块依赖项

    clojure - 通过关键字进行惯用的Clojure map 查找

    clojure - 我如何解决 Clojure 中的整数溢出?

    clojure - 为 Clojure/boot 寻求简单的工作流程设置

    clojure - 在 ClojureScript 中,为什么我无法导航到手动输入的 URL,但如果我单击链接它就可以工作?

    concurrency - Akka作为Clojure的并发模型

    Clojure pedestal.io 代码未在 IntelliJ 中解析

    maven - 将 GPG 加密凭证(或特定环境变量)与 boot-clj 和 s3-wagon-private 一起使用