forked from weavejester/cljfmt
/
main.clj
207 lines (183 loc) · 6.74 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
(ns cljfmt.main
"Functionality to apply formatting to a given project."
(:require [cljfmt.core :as cljfmt]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.stacktrace :as st]
[clojure.tools.cli :as cli]
[cljfmt.diff :as diff]))
(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 [dir file]
(-> (.toAbsolutePath (.toPath dir))
(.relativize (.toAbsolutePath (.toPath file)))
(.toString)))
(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-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 [counts (transduce
(comp (mapcat (partial find-files options))
(map (partial check-one options))
(map (fn [status]
(print-file-status options status)
(:counts status))))
(completing merge-counts)
paths)]
(print-final-count counts)
(exit counts))))
(defn fix
"Applies the formatting (as per `options`) to the Clojure files
contained in `paths`."
([paths]
(fix paths {}))
([paths options]
(let [files (mapcat (partial find-files options) paths)]
(doseq [^java.io.File f files]
(let [original (slurp f)]
(try
(let [revised (reformat-string options original)]
(when (not= original revised)
(println "Reformatting" (project-path options f))
(spit f revised)))
(catch Exception e
(warn "Failed to format file:" (project-path options f))
(print-stack-trace e))))))))
(def ^:private cli-file-reader
(comp edn/read-string slurp diff/to-absolute-path))
(def default-options
{:project-root "."
:file-pattern #"\.clj[csx]?$"
:ansi? true
:indentation? true
:insert-missing-whitespace? true
:remove-surrounding-whitespace? true
:remove-trailing-whitespace? true
:remove-consecutive-blank-lines? true
:indents cljfmt/default-indents
:alias-map {}})
(defn merge-default-options [options]
(-> (merge default-options options)
(assoc :indents (merge (:indents default-options)
(:indents options {})))))
(def default-paths ["src" "test" "project.clj"])
(def ^:private cli-options
[[nil "--help"]
[nil "--file-pattern FILE_PATTERN"
:default (:file-pattern default-options)
: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 "--project-root PROJECT_ROOT"
:default (:project-root default-options)]
[nil "--[no-]ansi"
:default (:ansi? default-options)
:id :ansi?]
[nil "--[no-]indentation"
:default (:indentation? default-options)
:id :indentation?]
[nil "--[no-]remove-surrounding-whitespace"
:default (:remove-surrounding-whitespace? default-options)
:id :remove-surrounding-whitespace?]
[nil "--[no-]remove-trailing-whitespace"
:default (:remove-trailing-whitespace? default-options)
:id :remove-trailing-whitespace?]
[nil "--[no-]insert-missing-whitespace"
:default (:insert-missing-whitespace? default-options)
:id :insert-missing-whitespace?]
[nil "--[no-]remove-consecutive-blank-lines"
:default (:remove-consecutive-blank-lines? default-options)
:id :remove-consecutive-blank-lines?]])
(defn- command-name []
(or (System/getProperty "sun.java.command") "cljfmt"))
(defn -main [& args]
(let [parsed-opts (cli/parse-opts args cli-options)
[cmd & paths] (:arguments parsed-opts)
options (merge-default-options (:options parsed-opts))
paths (or (seq paths) 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)))
(case cmd
"check" (check paths options)
"fix" (fix paths options)
(abort "Unknown cljfmt command:" cmd))))))