Skip to content

Commit bec341e

Browse files
committed
Refactor lib / cli reporting to use events
1 parent f2017f8 commit bec341e

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

cljfmt/src/cljfmt/tool.clj

+64-44
Original file line numberDiff line numberDiff line change
@@ -115,53 +115,59 @@
115115
(and file diff) (assoc-in [:incorrect path] diff)
116116
(and file exception) (assoc-in [:error path] exception))))
117117

118-
(defmulti check-report-file (fn [& args] (first args)))
119-
(defmulti check-report-final (fn [& args] (first args)))
118+
(defmulti report-cli (fn [event & _] event))
119+
(defmulti report-lib (fn [event & _] event))
120120

121-
(defmethod check-report-file :cli
122-
[_ options status]
123-
(print-file-status options status)
124-
(:counts status))
121+
(defmethod report-cli :check/initial
122+
[_ _ _]
123+
nil)
125124

126-
(defmethod check-report-final :cli
127-
[_ results]
128-
(let [counts (reduce merge-counts results)]
125+
(defmethod report-cli :check/file
126+
[_ context data]
127+
(print-file-status context data)
128+
(:counts data))
129+
130+
(defmethod report-cli :check/summary
131+
[_ summary _]
132+
(let [counts (reduce merge-counts summary)]
129133
(print-final-count counts)
130134
(exit counts)))
131135

132-
(defmethod check-report-file :lib
133-
[_ _ status]
134-
status)
136+
(defmethod report-lib :check/initial
137+
[_ _ _]
138+
nil)
139+
140+
(defmethod report-lib :check/file
141+
[_ _ data]
142+
data)
135143

136-
(defmethod check-report-final :lib
137-
[_ results]
138-
(reduce merge-statuses {} results))
144+
(defmethod report-lib :check/summary
145+
[_ context _]
146+
(reduce merge-statuses {} context))
139147

140148
(defn check-no-config
141149
"The same as `check`, but ignores dotfile configuration."
142-
[{:keys [parallel? paths report/file report/final] :as options}]
143-
(let [map* (if parallel? pmap map)
144-
report-file (partial file options)
145-
results (->> paths
146-
(mapcat (partial find-files options))
147-
(map* (partial check-one options))
148-
(map report-file))]
149-
(final results)))
150+
[{:keys [parallel? paths report] :as options}]
151+
(report :check/initial options nil)
152+
(let [map* (if parallel? pmap map)
153+
summary (->> paths
154+
(mapcat (partial find-files options))
155+
(map* (partial check-one options))
156+
(reduce #(report :check/file %1 %2) options))]
157+
(report :check/summary summary nil)))
150158

151159
(defn check
152160
"Checks that the Clojure paths specified by the :paths option are
153161
correctly formatted."
154162
[options]
155-
(let [opts (merge {:report/file #(check-report-file :cli %1 %2)
156-
:report/final #(check-report-final :cli %)}
163+
(let [opts (merge {:report report-cli}
157164
(config/load-config) options)]
158165
(check-no-config opts)))
159166

160167
(defn check-paths
161168
"Runs a check on the provided paths and returns a map detailing the results."
162169
[paths & [options]]
163-
(let [opts (merge {:report/file #(check-report-file :lib %1 %2)
164-
:report/final #(check-report-final :lib %)}
170+
(let [opts (merge {:report report-lib}
165171
(config/load-config) options
166172
{:paths paths})]
167173
(check-no-config opts)))
@@ -182,38 +188,52 @@
182188
(defn- recursively-find-files [{:keys [paths] :as options}]
183189
(mapcat #(find-files options %) (set paths)))
184190

185-
(defmulti fix-report (fn [& args] (first args)))
191+
(defmethod report-cli :fix/initial
192+
[_ _ _]
193+
nil)
194+
195+
(defmethod report-cli :fix/file
196+
[_ context data]
197+
(print-file-status context data))
198+
199+
(defmethod report-cli :fix/summary
200+
[_ _ _]
201+
nil)
202+
203+
(defmethod report-lib :fix/initial
204+
[_ _ _]
205+
nil)
186206

187-
(defmethod fix-report :cli
188-
[_ options status]
189-
(print-file-status options status))
207+
(defmethod report-lib :fix/file
208+
[_ _ data]
209+
data)
190210

191-
(defmethod fix-report :lib
192-
[_ _ status]
193-
status)
211+
(defmethod report-lib :fix/summary
212+
[_ summary _]
213+
summary)
194214

195215
(defn fix-no-config
196216
"The same as `fix`, but ignores dotfile configuration."
197-
[{:keys [parallel? report/fix] :as options}]
198-
(let [files (recursively-find-files options)
199-
map* (if parallel? pmap map)
200-
fix-report (partial fix options)]
201-
(->> files
202-
(map* (partial fix-one options))
203-
(map fix-report)
204-
dorun)))
217+
[{:keys [parallel? report] :as options}]
218+
(report :fix/initial options nil)
219+
(let [files (recursively-find-files options)
220+
map* (if parallel? pmap map)
221+
summary (->> files
222+
(map* (partial fix-one options))
223+
(reduce #(report :fix/file %1 %2) options))]
224+
(report :fix/summary summary nil)))
205225

206226
(defn fix
207227
"Fixes the formatting for all files specified by the :paths option."
208228
[options]
209-
(let [opts (merge {:report/fix #(fix-report :cli %1 %2)}
229+
(let [opts (merge {:report report-cli}
210230
(config/load-config) options)]
211231
(fix-no-config opts)))
212232

213233
(defn fix-paths
214234
"Fixes code in the provided paths."
215235
[paths & [options]]
216-
(let [opts (merge {:report/fix #(fix-report :lib %1 %2)}
236+
(let [opts (merge {:report report-lib}
217237
(config/load-config) options
218238
{:paths paths})]
219239
(fix-no-config opts)))

0 commit comments

Comments
 (0)