Clojure:在特定命名空间中的boot repl [英] Clojure: boot repl in a particular namespace

查看:276
本文介绍了Clojure:在特定命名空间中的boot repl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了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)

我想直接进入REPL并且去(my-range 0 3) / code>,看看它是否产生我想要的结果。



这是什么设置?我需要运行的脚本文件是什么样子?



我目前的理解是,答案会是这样:

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


解决方案

在命令行中



在某种程度上在命令行,不创建 build.boot 文件:



\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



minimal build.boot



或者,您可以创建文件 C:\dev\my_project\ build-boot 包含以下内容:

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

(deftask dev
运行开发REPL
[]
(repl:init-ns'my-project.utils))

然后,在 C:\dev\my_project

  boot dev 

这也将在你的命名空间中启动一个REPL,但是需要更少的命令行配置,因为我们在 build.boot boot 将自动计算。


注意:从Clojure REPL,构建工具,可以使用 require 函数来请求任何命名空间(只要它位于JVM的类路径上),并使用 in-ns




具有自动重新载入功能的build.boot



最后,可以结合Boot的功能来实现面向自动重新加载代码的开发工作流程。



C: \dev\my_project\build.boot

  env! :resource-paths#{src})

(require'[boot.core:as core]
'[boot.pod:as pod])

(deftask load-ns
在一个新的pod中加载my-project.utils命名空间
[]
(let [pods(pod / pod-pool -env))]
(core / with-pre-wrap [fileset]
(pod / with-eval-in(pods:refresh)
;;来自my-project.utils有
;;正确的行和列信息。
(require'my-project.load-impl))
fileset)))
$ b b(deftask dev
每次代码更改时监视源代码并加载my-project / utils。
[]
(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)))

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



在命令提示符下,键入 boot dev 。您应该会看到一些 println 输出,并且每次编辑和保存文件时,您都应该再次看到它,反映您所做的任何更改。


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. 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.

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:

In 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.
  • repl -n my-project.utils starts a REPL, requires your namespace, and enters it.

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:

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

minimal build.boot

Alternatively, you could create the file C:\dev\my_project\build.boot with these contents:

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

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

Then, in 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.

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 with automatic reloading

Finally, it's possible to combine features of Boot to achieve a development workflow oriented around automatically reloading code.

In 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:

(ns my-project.load-impl)

(require 'my-project.utils)

In 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. 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.

这篇关于Clojure:在特定命名空间中的boot repl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆