forked from boot-clj/boot-new
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
helpers.clj
296 lines (278 loc) · 13.4 KB
/
helpers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
(ns clj-new.helpers
"The top-level logic for the clj-new create/generate entry points."
(:require [clojure.pprint :as pp]
[clojure.stacktrace :as stack]
[clojure.string :as str]
[clojure.tools.cli :as cli]
[clojure.tools.deps.alpha :as deps]
[clojure.tools.deps.alpha.reader :refer [default-deps
read-deps]]
[clojure.tools.deps.alpha.util.session :as session]
;; support boot-template projects:
[boot.new.templates :as bnt]
;; needed for dynamic classloader/add-classpath stuff:
[cemerick.pomegranate :as pom]
;; support clj-template projects:
[clj.new.templates :as cnt]
;; support lein-template projects:
[leiningen.new.templates :as lnt])
(:import java.io.FileNotFoundException))
(def ^:dynamic *debug* nil)
(def ^:dynamic *use-snapshots?* false)
(def ^:dynamic *template-version* nil)
(defn resolve-and-load
"Given a deps map and an extra-deps map, resolve the dependencies, figure
out the classpath, and load everything into our (now dynamic) classloader."
[deps resolve-args]
(session/with-session
(-> (deps/resolve-deps deps resolve-args)
(deps/make-classpath (:paths deps) {})
(str/split (re-pattern java.io.File/pathSeparator))
(->> (run! pom/add-classpath)))))
(def ^:private git-url-sha #"(https?://[^/]+/[^/]+/([^/@]+)).*@([a-fA-Z0-9]+)")
(def ^:private git-path-template #"(https?://[^/]+/[^/]+/[^/]+)/((.*/)?([^/]+))@([a-fA-Z0-9]+)")
(comment
(re-find git-url-sha "https://github.com/org-name/repo-name@abc")
(re-find git-url-sha "https://github.com/org-name/repo-a/to/template-name@abc")
(re-find git-path-template "https://github.com/org-name/repo-name@abc")
(re-find git-path-template "https://github.com/org-name/repo-a/to/template-name@abc"))
(def ^:private local-root #"(.+)::(.+)")
(defn resolve-remote-template
"Given a template name, attempt to resolve it as a clj template first, then
as a Boot template, then as a Leiningen template. Return the type of template
we found and the final, derived template-name."
[template-name]
(let [selected (atom nil)
failure (atom nil)
tmp-version (cond *template-version* *template-version*
*use-snapshots?* "(0.0.0,)"
:else "RELEASE")
[_ git-url git-tmp-name-1 sha] (re-find git-url-sha template-name)
[_ _ git-path _ git-tmp-name-2 _] (re-find git-path-template template-name)
git-tmp-name (or git-tmp-name-2 git-tmp-name-1)
[_ local-root local-tmp-name] (re-find local-root template-name)
clj-only? (or (and git-url git-tmp-name sha)
(and local-root local-tmp-name))
template-name (cond (and git-url git-tmp-name sha)
git-tmp-name
(and local-root local-tmp-name)
local-tmp-name
:else
template-name)
clj-tmp-name (str template-name "/clj-template")
clj-version (cond (and git-url git-tmp-name sha)
(cond-> {:git/url git-url :sha sha}
git-path (assoc :deps/root git-path))
(and local-root local-tmp-name)
{:local/root local-root}
:else
{:mvn/version tmp-version})
boot-tmp-name (str template-name "/boot-template")
lein-tmp-name (str template-name "/lein-template")
all-deps (read-deps (default-deps))
output
(with-out-str
(binding [*err* *out*]
;; need a modifiable classloader to load runtime dependencies:
(.setContextClassLoader (Thread/currentThread)
(clojure.lang.RT/makeClassLoader))
(try
(resolve-and-load
all-deps
{:verbose (and *debug* (> *debug* 1))
:extra-deps
{(symbol clj-tmp-name) clj-version}})
(reset! selected [:clj template-name])
(catch Exception e
(when (and *debug* (> *debug* 2))
(println "Unable to find clj template:")
(stack/print-stack-trace e))
(reset! failure e)
(when-not clj-only?
(try
(resolve-and-load
all-deps
{:verbose (and *debug* (> *debug* 1))
:extra-deps
{(symbol boot-tmp-name) {:mvn/version tmp-version}}})
(reset! selected [:boot template-name])
(catch Exception e
(when (and *debug* (> *debug* 2))
(println "Unable to find Boot template:")
(stack/print-stack-trace e))
(reset! failure e)
(try
(resolve-and-load
all-deps
{:verbose (and *debug* (> *debug* 1))
:extra-deps
{(symbol lein-tmp-name) {:mvn/version tmp-version}
'leiningen-core {:mvn/version "2.7.1"}
'org.sonatype.aether/aether-api {:mvn/version "1.13.1"}
'org.sonatype.aether/aether-impl {:mvn/version "1.13.1"}
'slingshot {:mvn/version "0.10.3"}}})
(reset! selected [:leiningen template-name])
(catch Exception e
(when (and *debug* (> *debug* 1))
(println "Unable to find Leiningen template:")
(stack/print-stack-trace e))
(reset! failure e))))))))))]
(when (and *debug* (pos? *debug*))
(println "Output from locating template:")
(println output))
(if @selected
(let [sym-name (str (name (first @selected)) ".new." (second @selected))]
(try
(require (symbol sym-name))
@selected
(catch Exception e
(when (and *debug* (pos? *debug*))
(println "Unable to require the template symbol:" sym-name)
(stack/print-stack-trace e)
(when (> *debug* 1)
(stack/print-cause-trace e)))
(throw (ex-info (format "Could not load template, require of %s failed with: %s"
sym-name
(.getMessage e)) {})))))
(do
(println output)
(println "Failed with:" (.getMessage @failure))
(throw (ex-info
(format (str "Could not load artifact for template: %s\n"
"\tTried coordinates:\n"
"\t\t[%s \"%s\"]\n"
"\t\t[%s \"%s\"]")
template-name
boot-tmp-name tmp-version
lein-tmp-name tmp-version) {}))))))
(defn resolve-template
"Given a template name, resolve it to a symbol (or exit if not possible)."
[template-name]
(if-let [[type template-name]
(try (require (symbol (str "clj.new." template-name)))
[:clj template-name]
(catch FileNotFoundException _
(resolve-remote-template template-name)))]
(let [the-ns (str (name type) ".new." template-name)
fn-name (str/replace template-name #"^.+\." "")]
(if-let [sym (resolve (symbol the-ns fn-name))]
sym
(throw (ex-info (format (str "Found template %s but could not "
"resolve %s/%s within it.")
template-name
the-ns
fn-name) {}))))
(throw (ex-info (format "Could not find template %s on the classpath."
template-name) {}))))
(defn- valid-project?
"Return true if the project name is 'valid': qualified and/or multi-segment."
[project-name]
(let [project-sym (try (read-string project-name) (catch Exception _))]
(or (qualified-symbol? project-sym)
(and (symbol? project-sym) (re-find #"\." (name project-sym))))))
(defn create*
"Given a template name, a project name and list of template arguments,
perform sanity checking on the project name and, if it's sane, then
generate the project from the template."
[template-name project-name args]
(if (valid-project? project-name)
(apply (resolve-template template-name) project-name args)
(throw (ex-info "Project names must be valid qualified or multi-segment Clojure symbols."
{:project-name project-name}))))
(defn- add-env
"Add a new SYM=VAL variable to the environment."
[m k v]
(let [[sym val] (str/split v #"=")]
(update-in m [k] assoc (keyword sym) val)))
(def ^:private create-cli
"Command line argument spec for create command."
[["-e" "--env SYM=VAL" "Environment variables" :default {} :assoc-fn add-env]
["-f" "--force" "Force overwrite"]
["-h" "--help" "Provide this help"]
["-o" "--output DIR" "Directory prefix for project creation"]
["-?" "--query" "Display information about what will happen"]
["-S" "--snapshot" "Look for -SNAPSHOT version of the template"]
["-v" "--verbose" "Be verbose" :default 0 :update-fn inc]
["-V" "--version VERSION" "Use this version of the template"]])
(defn create
"Exposed to clj-new command-line with simpler signature."
[{:keys [args name template]}]
(let [{:keys [options arguments summary errors]}
(cli/parse-opts args create-cli)]
(cond (or (:help options) errors)
(do
(println "Usage:")
(println summary)
(doseq [err errors]
(println err)))
(:query options)
(if-not (valid-project? name)
(println "Error:" name "is not a qualified symbol or multi-segment name.")
(let [project-sym (try (read-string name) (catch Exception _))]
(println "Will create the folder:"
(or (:output options)
(clojure.core/name project-sym)))
(println "From the template:" template)
(when (seq arguments)
(println "Passing these arguments:"
(str/join " " arguments)))
(println "The following substitutions will be used:")
(binding [cnt/*environment* (:env options)]
(pp/pprint (cnt/project-data name)))))
:else
(let [{:keys [env force snapshot version output verbose]} options]
(binding [*debug* verbose
*use-snapshots?* snapshot
*template-version* version
bnt/*dir* output
bnt/*force?* force
cnt/*dir* output
cnt/*force?* force
cnt/*environment* env
lnt/*dir* output
lnt/*force?* force]
(create* template name arguments))))))
(defn generate-code*
"Given an optional template name, an optional path prefix, a list of
things to generate (type, type=name), and an optional set of arguments
for the generator, resolve the template (if provided), and then resolve
and apply each specified generator."
[template-name prefix generations args]
(when template-name (resolve-template template-name))
(doseq [thing generations]
(let [[gen-type gen-arg] (str/split thing #"=")
_ (try (require (symbol (str "clj.generate." gen-type))) (catch Exception _ (println _)))
generator (resolve (symbol (str "clj.generate." gen-type) "generate"))]
(if generator
(apply generator prefix gen-arg args)
(println (str "Unable to resolve clj.generate."
gen-type
"/generate -- ignoring: "
gen-type
(when gen-arg (str "=\"" gen-arg "\""))))))))
(def ^:private generate-cli
"Command line argument spec for generate command."
[["-f" "--force" "Force overwrite"]
["-h" "--help" "Provide this help"]
["-p" "--prefix DIR" "Directory prefix for generation"]
["-t" "--template NAME" "Override the template name"]
["-S" "--snapshot" "Look for -SNAPSHOT version of the template"]
["-V" "--version VERSION" "Use this version of the template"]])
(defn generate-code
"Exposed to clj new task with simpler signature."
[{:keys [args generate]}]
(let [{:keys [options arguments summary errors]}
(cli/parse-opts args generate-cli)]
(if (or (:help options) errors)
(do
(println "Usage:")
(println summary)
(doseq [err errors]
(println err)))
(let [{:keys [force prefix snapshot template version]} options]
(binding [cnt/*dir* "."
cnt/*force?* force
*use-snapshots?* snapshot
*template-version* version
cnt/*overwrite?* false]
(generate-code* template (or prefix "src") generate arguments))))))