-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.clj
251 lines (221 loc) · 8.16 KB
/
main.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
(ns cljfmt.main
"Functionality to apply formatting to a given project."
(:require [cljfmt.core :as cljfmt]
[cljfmt.diff :as diff]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.stacktrace :as st]
[clojure.string :as str]
[clojure.tools.cli :as cli])
(:gen-class))
(defn- abort [& msg]
(binding [*out* *err*]
(when (seq msg)
(apply println msg))
(System/exit 1)))
(defn- warn [& args]
(binding [*out* *err*]
(apply println args)))
(defn- relative-path [^java.io.File dir ^java.io.File file]
(-> (.toAbsolutePath (.toPath dir))
(.relativize (.toAbsolutePath (.toPath file)))
(.toString)))
(defn- filename-ext [filename]
(subs filename (inc (str/last-index-of filename "."))))
(defn- grep [re dir]
(filter #(re-find re (relative-path dir %)) (file-seq (io/file dir))))
(defn- find-files [{:keys [file-pattern]} f]
(let [f (io/file f)]
(when-not (.exists f) (abort "No such file:" (str f)))
(if (.isDirectory f)
(grep file-pattern f)
[f])))
(defn- reformat-string [options s]
((cljfmt/wrap-normalize-newlines #(cljfmt/reformat-string % options)) s))
(defn- project-path [{:keys [project-root]} file]
(-> project-root (or ".") io/file (relative-path (io/file file))))
(defn- format-diff
([options file]
(let [original (slurp (io/file file))]
(format-diff options file original (reformat-string options original))))
([options file original revised]
(let [filename (project-path options file)
diff (diff/unified-diff filename original revised)]
(if (:ansi? options)
(diff/colorize-diff diff)
diff))))
(def ^:private zero-counts {:okay 0, :incorrect 0, :error 0})
(defn- check-one [options file]
(let [original (slurp file)
status {:counts zero-counts :file file}]
(try
(let [revised (reformat-string options original)]
(if (not= original revised)
(-> status
(assoc-in [:counts :incorrect] 1)
(assoc :diff (format-diff options file original revised)))
(assoc-in status [:counts :okay] 1)))
(catch Exception ex
(-> status
(assoc-in [:counts :error] 1)
(assoc :exception ex))))))
(defn- print-stack-trace [ex]
(binding [*out* *err*]
(st/print-stack-trace ex)))
(defn- print-file-status [options status]
(let [path (project-path options (:file status))]
(when-let [ex (:exception status)]
(warn "Failed to format file:" path)
(print-stack-trace ex))
(when (:reformatted status)
(println "Reformatting" path))
(when-let [diff (:diff status)]
(warn path "has incorrect formatting")
(warn diff))))
(defn- exit [counts]
(when-not (zero? (:error counts 0))
(System/exit 2))
(when-not (zero? (:incorrect counts 0))
(System/exit 1)))
(defn- print-final-count [counts]
(let [error (:error counts 0)
incorrect (:incorrect counts 0)]
(when-not (zero? error)
(warn error "file(s) could not be parsed for formatting"))
(when-not (zero? incorrect)
(warn incorrect "file(s) formatted incorrectly"))
(when (and (zero? incorrect) (zero? error))
(println "All source files formatted correctly"))))
(defn- merge-counts
([] zero-counts)
([a] a)
([a b] (merge-with + a b)))
(defn check
"Checks that the Clojure files contained in `paths` follow the formatting
(as per `options`)."
([paths]
(check paths {}))
([paths options]
(let [map* (if (:parallel? options) pmap map)
counts (->> paths
(mapcat (partial find-files options))
(map* (partial check-one options))
(map (fn [status]
(print-file-status options status)
(:counts status)))
(reduce merge-counts))]
(print-final-count counts)
(exit counts))))
(defn- fix-one [options file]
(let [original (slurp file)]
(try
(let [revised (reformat-string options original)]
(if (not= original revised)
(do (spit file revised)
{:file file :reformatted true})
{:file file}))
(catch Exception e
{:file file :exception e}))))
(defn fix
"Applies the formatting (as per `options`) to the Clojure files
contained in `paths`."
([paths]
(fix paths {}))
([paths options]
(let [map* (if (:parallel? options) pmap map)]
(->> paths
(mapcat (partial find-files options))
(map* (partial fix-one options))
(map (partial print-file-status options))
dorun))))
(defn- cli-file-reader [filepath]
(let [contents (slurp filepath)]
(if (= (filename-ext filepath) "clj")
(read-string contents)
(edn/read-string contents))))
(defn- parent-dirs [^String root]
(->> (.getAbsoluteFile (io/file root))
(iterate #(.getParentFile ^java.io.File %))
(take-while some?)))
(defn- find-file-in-dir ^java.io.File [^java.io.File dir ^String name]
(let [f (io/file dir name)]
(when (.exists f) f)))
(defn- find-config-file-in-dir ^java.io.File [^java.io.File dir]
(or (find-file-in-dir dir ".cljfmt.edn")
(find-file-in-dir dir ".cljfmt.clj")))
(defn- load-configuration []
(some->> (parent-dirs "")
(some find-config-file-in-dir)
(#(.getPath ^java.io.File %))
cli-file-reader))
(defn- load-default-options []
(merge cljfmt/default-options
{:project-root "."
:file-pattern #"\.clj[csx]?$"
:ansi? true
:parallel? false}
(load-configuration)))
(defn merge-default-options [defaults options]
(-> (merge defaults options)
(assoc :indents (merge (:indents defaults)
(:indents options {})))))
(def default-paths ["src" "test" "project.clj"])
(defn- cli-options [defaults]
[[nil "--help"]
[nil "--parallel"
:id :parallel?]
[nil "--project-root PROJECT_ROOT"
:default (:project-root defaults)]
[nil "--file-pattern FILE_PATTERN"
:default (:file-pattern defaults)
:parse-fn re-pattern]
[nil "--indents INDENTS_PATH"
:parse-fn cli-file-reader]
[nil "--alias-map ALIAS_MAP_PATH"
:parse-fn cli-file-reader]
[nil "--[no-]ansi"
:default (:ansi? defaults)
:id :ansi?]
[nil "--[no-]indentation"
:default (:indentation? defaults)
:id :indentation?]
[nil "--[no-]remove-multiple-non-indenting-spaces"
:default (:remove-multiple-non-indenting-spaces? defaults)
:id :remove-multiple-non-indenting-spaces?]
[nil "--[no-]remove-surrounding-whitespace"
:default (:remove-surrounding-whitespace? defaults)
:id :remove-surrounding-whitespace?]
[nil "--[no-]remove-trailing-whitespace"
:default (:remove-trailing-whitespace? defaults)
:id :remove-trailing-whitespace?]
[nil "--[no-]insert-missing-whitespace"
:default (:insert-missing-whitespace? defaults)
:id :insert-missing-whitespace?]
[nil "--[no-]remove-consecutive-blank-lines"
:default (:remove-consecutive-blank-lines? defaults)
:id :remove-consecutive-blank-lines?]
[nil "--[no-]split-keypairs-over-multiple-lines"
:default (:split-keypairs-over-multiple-lines? defaults)
:id :split-keypairs-over-multiple-lines?]
[nil "--[no-]sort-ns-references"
:default (:sort-ns-references? defaults)
:id :sort-ns-references?]])
(defn- file-exists? [path]
(.exists (io/as-file path)))
(defn -main [& args]
(let [defaults (load-default-options)
parsed-opts (cli/parse-opts args (cli-options defaults))
[cmd & paths] (:arguments parsed-opts)
options (merge-default-options defaults (:options parsed-opts))
paths (or (seq paths) (filter file-exists? default-paths))]
(if (:errors parsed-opts)
(abort (:errors parsed-opts))
(if (or (nil? cmd) (:help options))
(do (println "cljfmt [OPTIONS] COMMAND [PATHS ...]")
(println (:summary parsed-opts)))
(do (case cmd
"check" (check paths options)
"fix" (fix paths options)
(abort "Unknown cljfmt command:" cmd))
(when (:parallel? options)
(shutdown-agents)))))))