Clojure: boot repl in a specific namespace

I have boot-clj installed and you want to be able to edit the .clj file in an external editor and separately have a REPL command from which I can call the functions that I modify in the .clj file. No special reboot commands are required.

Another thing is that I don’t want to manually enter commands to include namespaces - I would just like to run a script that introduced me to the namespace, so I can immediately call the existing functions.

File name:

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

Something from what's 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 directly to REPL and go (my-range 0 3) and see if it creates the result I want.

What is the setting for this? What would the script file I need to run look like?

My real understanding is that the answer will look something like this:

 (deftask dev-repl (set-env! …) (repl)) 
+7
clojure boot-clj
source share
1 answer

on the command line

You can achieve this to some extent on the command line without creating the build.boot file:

In C: \ dev \ my_project:

 boot -r src repl -n my-project.utils 
  • boot -r src : boot starts with src on the "resource path", which is the set of directories that will be available in the JVM.
  • repl -n my-project.utils launches REPL, requires your namespace and enters it.

While REPL is running, and after editing C:\dev\my_project\src\my_project\utils.clj you can reload it in REPL, like this:

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

minimum build.boot

Alternatively, you can create a file C:\dev\my_project\build.boot with this content:

 (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 run REPL in your namespace, but requires less command line configuration, since we performed the configuration in build.boot , which boot will be automatically evaluated.

Note: from Clojure REPL, regardless of the build tool, you may need any namespace (as long as it is in the path of the JVM class) using the require function and enter it using the in-ns function.

build.boot with automatic reboot

Finally, you can combine the Boot functions to achieve a development workflow focused on 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)) 

At the command prompt, enter boot dev . You should see println output, and every time you edit and save the file, you should see it again, reflecting any changes you made.

+9
source share

All Articles