user
Namespace.While doing interactive development, it's important to have a place to put code that is useful for that purpose, but maybe isn't appropriate for the software you ultimately ship.
dev/user.clj
Most REPLs will automatically load in any user
namespace.
This makes it the easiest place to put "development only" code.
(ns user)
:dev
alias to your deps.edn
.
You want to have the dev
folder included as an extra
path.
Because user.clj
is treated specially, you can run into
wacky behavior during distribution
if you include it in your normal build. So the alias serves to make sure dev/user.clj
is only on the path during development.
{:paths ["src"]
:deps {...}
:aliases {...
:dev {:extra-paths ["dev"]}}}
dev
folder to your :nREPL
alias as
well.
Seeing as you will only be using nREPL
during
development, this is needed. We can merge :dev
and
:nREPL
eventually if they become too much to keep in
sync.
{:paths ["src"]
:deps {...}
:aliases {:nREPL
{:extra-paths ["dev"]
:extra-deps
{nrepl/nrepl {:mvn/version "1.2.0"}}}
:dev {:extra-paths ["dev"]}}}
def
at the top assigning system
to
nil
.
(ns user)
(def system nil)
start-system!
function in user
.
This should call start-system
from
example.system
and set the value of the
system
var to the return value.
If a system is already started - which you can tell by checking is
system
is nil
- you can print a message and
exit early.
(ns user
(:require [example.system :as system]))
(def system nil)
(defn start-system!
[]
(if system
(println "Already Started")
(alter-var-root #'system (constantly (system/start-system)))))
stop-system!
function in user
.If no system has been started this should be a noop.
(ns user
(:require [example.system :as system]))
(def system nil)
(defn start-system!
[]
(if system
(println "Already Started")
(alter-var-root #'system (constantly (system/start-system)))))
(defn stop-system!
[]
(when system
(system/stop-system system)
(alter-var-root #'system (constantly nil))))
restart-system
function in user
When you want a quick way to turn your system off and back on again, it's nice to have such a function available.
(ns user
(:require [example.system :as system]))
(def system nil)
(defn start-system!
[]
(if system
(println "Already Started")
(alter-var-root #'system (constantly (system/start-system)))))
(defn stop-system!
[]
(when system
(system/stop-system system)
(alter-var-root #'system (constantly nil))))
(defn restart-system!
[]
(stop-system!)
(start-system!))
(defn server
[]
(::system/server system))
server
function in user
.
The purpose of this will be to let code access the running
server
.
(ns user
(:require [example.system :as system]))
(def system nil)
(defn start-system!
[]
(if system
(println "Already Started")
(alter-var-root #'system (constantly (system/start-system)))))
(defn stop-system!
[]
(when system
(system/stop-system system)))
(defn server
[]
(::system/server system))
Now that we have a :dev
alias which includes your development only code,
you need to make sure your editor uses that alias when you are developing.
Again I will defer to the official documentation of those tools. For both Calva and Cursive its simply a button to click in the UI.