/
cli.clj
110 lines (93 loc) · 3 KB
/
cli.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(ns cambada.cli
(:require [clojure.java.io :as io]
[clojure.string :as string]
[clojure.tools.cli :as cli]
[clojure.tools.deps.alpha :as tool-deps]
[clojure.tools.deps.alpha.script.make-classpath2 :as make-classpath]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Console out and operating functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:dynamic *debug* (System/getenv "DEBUG"))
(defn debug
"Print if *debug* (from DEBUG environment variable) is truthy."
[& args]
(when *debug* (apply println args)))
(def ^:dynamic *info* (not (System/getenv "LEIN_SILENT")))
(defn info
"Print if *info* (from LEIN_SILENT environment variable) is truthy."
[& args]
(when *info* (apply println args)))
(defn warn
"Print to stderr if *info* is truthy."
[& args]
(when *info*
(binding [*out* *err*]
(apply println args))))
(defn abort
"Print msg to standard err and exit with a value of 1."
[& msg]
(let [msg-out (concat ["ERROR!"] msg)]
(binding [*out* *err*]
(when (seq msg-out)
(apply println msg-out))
(System/exit 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers for cli use
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def base-cli-options
[["-d" "--deps FILE_PATH" "Location of deps.edn file"
:default "deps.edn"]
["-o" "--out PATH" "Output directory"
:default "target"]
["-h" "--help" "Shows this help"]])
(defn ^:private args->parsed-opts
[args cli-options]
(cli/parse-opts args cli-options))
(defn ^:private parsed-opts->task
[{{:keys [deps main aot] :as options} :options
:keys [summary errors]}]
(try
(let [install-deps (tool-deps/root-deps)
project-deps (make-classpath/read-deps deps)
deps-map (merge install-deps project-deps)
opts (cond-> options
;; if main is not nil, it needs to be added to aot
;; unless user chose all or main has been added
;; manually to aot
(and (not (nil? main))
(and (not= (first aot) 'all)
(not= (some #(= main %) aot))))
(assoc :aot (conj (or aot []) (symbol main))))]
(-> {:parser {:summary summary
:errors errors}}
(merge opts)
(assoc :deps-map deps-map)))
(catch Exception e
(abort (->> ["Error reading your deps file. Make sure"
deps
"is existent and correct."]
(string/join " "))))))
(defn args->task
[args cli-options]
(-> args
(args->parsed-opts cli-options)
parsed-opts->task))
(defn usage
[main description task]
(->>
[description
""
(str "Usage: clj -m " main " [options]")
""
"Options:"
(-> task :parser :summary)]
(string/join \newline)
info))
(defn runner
[{:keys [help? task apply-fn
entrypoint-main entrypoint-description]}]
(if help?
(usage entrypoint-main entrypoint-description task)
(do (apply-fn task)
(info "Done!")
(System/exit 0))))