Skip to content

Commit

Permalink
More succint output + more fine grained steps
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Jan 22, 2024
1 parent 07ecd90 commit dd05ea6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
## Added

- Inject shadow-cljs deps when needed and not already present

## Fixed
- Support for more fine grained step customization with `start-steps`/`end-steps`/`pre-steps`/`post-steps`

## Changed

- Check deps before injecting extra deps (cider, nrepl, shadow, etc). Declared
deps versions always get precedence.
- More succinct output while starting up
- Show command line invocations that are being run

# 0.22.110-alpha (2024-01-17 / 95c22dc)

Expand Down
95 changes: 67 additions & 28 deletions src/lambdaisland/launchpad.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,42 @@
(defn error [& args] (apply println (java.util.Date.) "[ERROR]" args))

(defn shellquote [a]
(cond
(and (str/includes? a "\"")
(str/includes? a "'"))
(str "'"
(str/replace a "'" "'\"'\"'")
"'")

(str/includes? a "'")
(str "\"" a "\"")

(re-find #"\s|\"" a)
(str "'" a "'")

:else
a))
(let [a (str a)]
(cond
(and (str/includes? a "\"")
(str/includes? a "'"))
(str "'"
(str/replace a "'" "'\"'\"'")
"'")

(str/includes? a "'")
(str "\"" a "\"")

(re-find #"\s|\"" a)
(str "'" a "'")

:else
a)))

(def ansi-fg-color-codes
{:black 30
:red 31
:green 32
:yellow 33
:blue 34
:magenta 35
:cyan 36
:white 37})

(defn ansi-bold [& parts]
(str "\u001b[1m" (str/join " " parts) "\u001b[0m"))

(defn ansi-fg [color & parts]
(str "\u001b[" (if (keyword? color)
(get ansi-fg-color-codes color)
color) "m"
(str/join " " parts)
"\u001b[0m"))

(defn free-port
"Find a free TCP port"
Expand Down Expand Up @@ -396,16 +417,20 @@
ctx)

(defn print-summary [ctx]
(println "Aliases:")
(doseq [a (:aliases ctx)] (println "-" a))
#_(apply println "Java flags: " (:java-args ctx))
(println "\nMiddleware: " )
(doseq [a (:middleware ctx)] (println "-" a))
(print "\nExtra Deps:")
(pprint/print-table (map (fn [[k v]]
{:lib k
:coords v})
(:extra-deps ctx)))
(println (ansi-fg :green "Launching")
(ansi-bold (ansi-fg :green "Clojure"))
(ansi-fg :green "on nREPL port")
(ansi-fg :cyan (:nrepl-port ctx)))
;; (println "Aliases:")
;; (doseq [a (:aliases ctx)] (println "-" a))
;; #_(apply println "Java flags: " (:java-args ctx))
;; (println "\nMiddleware: " )
;; (doseq [a (:middleware ctx)] (println "-" a))
;; (print "\nExtra Deps:")
;; (pprint/print-table (map (fn [[k v]]
;; {:lib k
;; :coords v})
;; (:extra-deps ctx)))
ctx)

(defn pipe-process-output
Expand Down Expand Up @@ -443,9 +468,10 @@
(.directory working-dir))
_ (.putAll (.environment proc-builder) (or env (:env ctx)))
color (mod (hash (or prefix (first cmd))) 8)
prefix (str "\u001b[" (+ 30 color) "m[" (or prefix (first cmd)) "]\u001b[0m ")
prefix (ansi-fg (+ 30 color) (str "[" (or prefix (first cmd)) "] "))
process (pipe-process-output (.start proc-builder) prefix)
ctx (update ctx :processes (fnil conj []) process)]
(apply println (str prefix "$") (map shellquote cmd))
(if background?
ctx
(let [exit (if timeout-ms
Expand Down Expand Up @@ -522,8 +548,21 @@
(defn process-steps [ctx steps]
(reduce #(%2 %1) ctx steps))

(defn main [{:keys [steps] :or {steps default-steps} :as opts}]
(let [ctx (process-steps (initial-context opts) steps)
(defn main [{:keys [steps
start-steps
end-steps
pre-steps
post-steps] :as opts}]
(let [ctx (process-steps (initial-context opts)
(or steps
(concat
start-steps
before-steps
pre-steps
[start-clojure-process]
post-steps
after-steps
end-steps)))
processes (:processes ctx)]
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (run! #(.destroy %) processes))))
Expand Down

0 comments on commit dd05ea6

Please sign in to comment.