/
config.clj
179 lines (153 loc) · 5.88 KB
/
config.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
(ns leiningen.cljsbuild.config
"Utilities for parsing the cljsbuild config."
(:require
[clojure.pprint :as pprint]))
(defn in-target-path [target-path subpath]
(str target-path "/cljsbuild-" subpath))
(defn- compiler-output-dir-base [target-path]
(in-target-path target-path "compiler-"))
(def compiler-global-dirs
{:libs "closure-js/libs"
:externs "closure-js/externs"})
(defn- default-global-options [target-path]
{:repl-launch-commands {}
:repl-listen-port 9000
:test-commands {}
:crossover-path (in-target-path target-path "crossover")
:crossover-jar false
:crossovers []})
(defn- default-compiler-options [target-path]
{:output-to (in-target-path target-path "main.js")
:externs []
:libs []})
(defn- default-build-options [target-path]
{:source-paths ["src-cljs"]
:jar false
:notify-command nil
:incremental true
:assert true
:compiler (default-compiler-options target-path)})
(defn convert-builds-map [options]
(update-in options [:builds]
#(if (map? %)
(for [[id build] %]
(assoc build :id (name id)))
%)))
(defn- backwards-compat-builds [options]
(cond
(and (map? options) (some #{:compiler :source-path :source-paths} (keys options)))
{:builds [options]}
(vector? options)
{:builds options}
:else
options))
(defn- backwards-compat-source-path [{:keys [builds] :as options}]
(assoc options :builds
(for [build builds]
(if-let [source-path (:source-path build)]
(dissoc
(assoc build :source-paths
(vec (concat (:source-paths build) [source-path])))
:source-path)
build))))
(defn- backwards-compat-crossovers [{:keys [builds crossovers] :as options}]
(let [all-crossovers (->> builds
(mapcat :crossovers)
(concat crossovers)
(distinct)
(vec))
no-crossovers (assoc options
:builds (vec (map #(dissoc % :crossovers) builds)))]
(if (empty? all-crossovers)
no-crossovers
(assoc no-crossovers
:crossovers all-crossovers))))
(defn backwards-compat [options]
(-> options
backwards-compat-builds
backwards-compat-source-path
backwards-compat-crossovers))
(defn- warn-deprecated [options]
(letfn [(delim [] (println (apply str (take 80 (repeat "-")))))]
(delim)
(println
(str
"WARNING: your :cljsbuild configuration is in a deprecated format. It has been\n"
"automatically converted it to the new format, which will be printed below.\n"
"It is recommended that you update your :cljsbuild configuration ASAP."))
(delim)
(println ":cljsbuild")
(pprint/pprint options)
(delim)
(println
(str
"See https://github.com/emezeske/lein-cljsbuild/blob/master/README.md\n"
"for details on the new format."))
(delim)
options))
(declare deep-merge-item)
(defn- deep-merge [& ms]
(apply merge-with deep-merge-item ms))
(defn- deep-merge-item [a b]
(if (and (map? a) (map? b))
(deep-merge a b)
b))
(defn- set-default-build-options [target-path options]
(deep-merge (default-build-options target-path) options))
(defn- set-default-output-dirs [target-path options]
(let [output-dir-key [:compiler :output-dir]
builds
(for [[build counter] (map vector (:builds options) (range))]
(if (get-in build output-dir-key)
build
(assoc-in build output-dir-key
(str (compiler-output-dir-base target-path) counter))))]
(if (or (empty? builds)
(apply distinct? (map #(get-in % output-dir-key) builds)))
(assoc options :builds builds)
(throw (Exception. (str "All " output-dir-key " options must be distinct."))))))
(defn set-default-options [target-path options]
(set-default-output-dirs target-path
(deep-merge (default-global-options target-path)
(assoc options :builds
(map #(set-default-build-options target-path %) (:builds options))))))
(defn set-build-global-dirs [build]
(reduce (fn [build [k v]] (update-in build [:compiler k] conj v))
build
compiler-global-dirs))
(defn set-compiler-global-dirs [options]
(assoc options :builds
(map set-build-global-dirs (:builds options))))
(defn- normalize-options
"Sets default options and accounts for backwards compatibility."
[target-path options]
(let [options (convert-builds-map options)
compat (backwards-compat options)]
(when (not= options compat)
(warn-deprecated compat))
(->> compat
(set-default-options target-path)
set-compiler-global-dirs)))
(defn parse-shell-command [raw]
(let [[shell tail] (split-with (comp not keyword?) raw)
options (apply hash-map tail)]
(merge {:shell shell} options)))
(defn parse-notify-command [build]
(assoc build :parsed-notify-command
(parse-shell-command (:notify-command build))))
(defn warn-unsupported-warn-on-undeclared [build]
(when (contains? build :warn-on-undeclared)
(println "WARNING: the :warn-on-undeclared option is no longer available.")
(println "Set \":warnings true\" in your :compiler options instead.")))
(defn warn-unsupported-notify-command [build]
(when (or (first (filter #(= "%" %) (:shell (:parsed-notify-command build))))
(:beep (:parsed-notify-command build)))
(println "WARNING: the :notify-command option no longer accepts the \"%\" or :beep options.")
(println "See https://github.com/emezeske/lein-cljsbuild/blob/master/doc/RELEASE-NOTES.md for details.")))
(defn extract-options
"Given a project, returns a seq of cljsbuild option maps."
[project]
(when (nil? (:cljsbuild project))
(println "WARNING: no :cljsbuild entry found in project definition."))
(let [raw-options (:cljsbuild project)]
(normalize-options (:target-path project) raw-options)))