Compare commits

...

5 Commits

Author SHA1 Message Date
Bryan Maass
38883ff7cd add docs for --watch flag on :dev-start alias 2025-05-13 11:11:34 -06:00
Bryan Maass
5cbba5d817 stop leaking watchers 2025-05-09 15:28:34 -06:00
Bryan Maass
ff4a57e933 improve watcher ergonomics 2025-05-09 13:25:24 -06:00
Bryan Maass
93515ebe31 docs 2025-05-09 13:18:32 -06:00
Bryan Maass
d53a7a680b fix watcher alias
- A path object cannot be printed via warnf and %s.
- Some paths from all-paths-to-watch were causing beholder to choke
2025-05-09 12:47:18 -06:00
4 changed files with 47 additions and 53 deletions

View File

@@ -247,11 +247,13 @@
com.clojure-goes-fast/clj-memory-meter
{:mvn/version "0.4.0"} ; Enables easy memory measurement
com.gfredericks/test.chuck {:mvn/version "0.2.15"} ; generating strings from regexes (useful with malli)
com.nextjournal/beholder {:mvn/version "1.0.2"} ; watcher
criterium/criterium {:mvn/version "0.4.6"} ; benchmarking library
dev.weavejester/hashp {:mvn/version "0.4.0"} ; debugging/spying utility
djblue/portal {:mvn/version "0.59.0"} ; ui for inspecting values
io.github.camsaul/humane-are {:mvn/version "1.0.2"}
io.github.metabase/hawk {:mvn/version "1.0.7"}
io.github.camsaul/humane-are {:mvn/version "1.0.2"} ; cleaner test output
io.github.metabase/hawk {:mvn/version "1.0.7"} ; test runner
io.github.tonsky/clj-reload {:mvn/version "0.9.5"} ; reloader
jonase/eastwood {:mvn/version "1.4.3" ; inspects namespaces and reports possible problems using tools.analyzer
:exclusions
[org.ow2.asm/asm-all]}
@@ -306,6 +308,9 @@
;; 1. Runs (dev) (start!) for you
;; 2. This starts the repl too: just like the :nrepl alias below. (see [[user/-main]])
;; clj -M:dev:dev-start:drivers:drivers-dev:ee:ee-dev
;;
;; To start it with hot code reloading, use:
;; clj -M:dev:dev-start:drivers:drivers-dev:ee:ee-dev --watch
:dev-start {:main-opts ["-m" "user"]
:jvm-opts ["-Dmb.run.mode=dev"
"-Dmacaw.run.mode=dev"
@@ -868,13 +873,6 @@
:test/kondo
{:exec-args {:only [".clj-kondo/test"]}}
;; watch and reload clojure namespaces
:watch {:extra-deps
{com.nextjournal/beholder {:mvn/version "1.0.2"} ;; watcher
io.github.tonsky/clj-reload {:mvn/version "0.9.5"}} ;; reloader
:extra-paths "dev/src/watch"
:main-opts ["-m" "watch.watcher"]}
;; DB configs -- start DBs with:
;; ./bin/mage start-db postgres oldest

View File

@@ -51,11 +51,19 @@
(in-ns 'dev)
:loaded)
(defn watch? []
(contains? (set *command-line-args*) "--watch"))
(defn -main
"This is called by the `:dev-start` cli alias.
Try it out: `clj -M:dev:dev-start:drivers:drivers-dev:ee:ee-dev`"
Try it out: `clj -M:dev:dev-start:drivers:drivers-dev:ee:ee-dev`
Command line options:
- `--watch` - start the hot-reload clojure code watcher"
[& _args]
(future (nrepl.cmdline/-main "-p" "50605" "-b" "0.0.0.0"))
(when (watch?)
(future (requiring-resolve 'watch.watcher/watcher)))
((requiring-resolve 'dev/start!))
(deref (promise)))

View File

@@ -1,51 +1,29 @@
(ns watch.watcher
(:require [clj-reload.core :as reload]
[clojure.edn :as edn]
[clojure.string :as str]
[dev :as dev]
[metabase.util.log :as log]
[nextjournal.beholder :as beholder]
[user :as user])
(ns ^:clj-reload/no-unload
^:clj-reload/no-reload
watch.watcher
(:require
[clj-reload.core :as reload]
[dev :as dev]
[metabase.util.log :as log]
[nextjournal.beholder :as beholder]
[user :as user])
(:gen-class))
(set! *warn-on-reflection* true)
(def all-paths-to-watch
"Returns all paths that contain code. These are the paths we should watch."
(memoize
(fn all-paths-to-watch* []
(->> (str (System/getProperty "user.dir") "/deps.edn")
slurp
edn/read-string
:aliases
vals
(keep :extra-paths)
(mapv #(if (coll? %) % [%]))
(apply concat)
(map (comp first #(str/split % #"/")))
(concat ["dev" "src" "test"])
distinct
vec))))
(defn watch-fn
"Reloads the system when a file changes."
[{:keys [type path]}]
(log/warnf "type: %s | path: %s" type path)
(let [before (System/currentTimeMillis)
_ (log/warn
(-> (reload/reload {:throw false
:no-reload '#{watch.watcher user dev}})
(update :unloaded count)
(update :loaded count)))
after (System/currentTimeMillis)
_ (log/warn (str "Reloaded in " (- after before) " ms"))]))
[change]
(log/warn "Change detected. Reloading system. " (pr-str change) " ...")
(reload/reload {:throw :false}))
(def ^:private paths
["src" "test" "enterprise/backend" "modules"])
#_:clj-kondo/ignore
(defonce watcher
(do
(log/warn "Watching paths:" (all-paths-to-watch))
(reload/init {:dirs (all-paths-to-watch)})
(apply (partial beholder/watch watch-fn) (all-paths-to-watch))))
(log/warn "Watching paths:" paths)
(apply beholder/watch watch-fn paths)))
(defn -main "Watcher main function"
[& _args]
@@ -54,6 +32,6 @@
(comment
(beholder/stop watcher)
(reload/reload)
)
(beholder/stop watcher))

View File

@@ -45,7 +45,12 @@
"Evaluate Clojure code in a running nREPL server. With one arg, reads port from .nrepl-port file.
With two args, uses the provided port number. Returns and formats the evaluation results."
([nns code]
(nrepl-eval (or nns "user") code (slurp ".nrepl-port")))
(nrepl-eval (or nns "user") code (try (slurp ".nrepl-port")
(catch Exception _
(throw (ex-info
(str
"Unable to find .nrepl-port, is the server's repl running?"
" See: the :dev-start alias in deps.edn") {}))))))
([nns code port]
(try (let [port (safe-parse-int port)
s (java.net.Socket. "localhost" port)
@@ -94,6 +99,11 @@
(nrepl-eval "metabase.logger-test" "(do (println :!! hi) hi)" 59498)
(nrepl-eval "metabase.logger-test" "*ns*" 59498)
(nrepl-eval "metabase.logger-test" "*ns*")
(nrepl-eval "dev.migrate" "(do (rollback! :count 1) ::rollback-done)")
(nrepl-eval "dev.migrate" "(do (migrate! :up) ::migrate-up-done)")
(nrepl-eval "")
(println code-str))