|
80 | 80 | (when-let [ex (:exception status)]
|
81 | 81 | (warn "Failed to format file:" path)
|
82 | 82 | (print-stack-trace ex))
|
| 83 | + (when (:reformatted status) |
| 84 | + (println "Reformatting" path)) |
83 | 85 | (when-let [diff (:diff status)]
|
84 | 86 | (warn path "has incorrect formatting")
|
85 | 87 | (warn diff))))
|
|
111 | 113 | ([paths]
|
112 | 114 | (check paths {}))
|
113 | 115 | ([paths options]
|
114 |
| - (let [counts (transduce |
115 |
| - (comp (mapcat (partial find-files options)) |
116 |
| - (map (partial check-one options)) |
117 |
| - (map (fn [status] |
118 |
| - (print-file-status options status) |
119 |
| - (:counts status)))) |
120 |
| - (completing merge-counts) |
121 |
| - paths)] |
| 116 | + (let [map* (if (:parallel? options) pmap map) |
| 117 | + counts (->> paths |
| 118 | + (mapcat (partial find-files options)) |
| 119 | + (map* (partial check-one options)) |
| 120 | + (map (fn [status] |
| 121 | + (print-file-status options status) |
| 122 | + (:counts status))) |
| 123 | + (reduce merge-counts))] |
122 | 124 | (print-final-count counts)
|
123 | 125 | (exit counts))))
|
124 | 126 |
|
| 127 | +(defn- fix-one [options file] |
| 128 | + (let [original (slurp file)] |
| 129 | + (try |
| 130 | + (let [revised (reformat-string options original)] |
| 131 | + (if (not= original revised) |
| 132 | + (do (spit file revised) |
| 133 | + {:file file :reformatted true}) |
| 134 | + {:file file})) |
| 135 | + (catch Exception e |
| 136 | + {:file file :exception e})))) |
| 137 | + |
125 | 138 | (defn fix
|
126 | 139 | "Applies the formatting (as per `options`) to the Clojure files
|
127 | 140 | contained in `paths`."
|
128 | 141 | ([paths]
|
129 | 142 | (fix paths {}))
|
130 | 143 | ([paths options]
|
131 |
| - (let [files (mapcat (partial find-files options) paths)] |
132 |
| - (doseq [^java.io.File f files] |
133 |
| - (let [original (slurp f)] |
134 |
| - (try |
135 |
| - (let [revised (reformat-string options original)] |
136 |
| - (when (not= original revised) |
137 |
| - (println "Reformatting" (project-path options f)) |
138 |
| - (spit f revised))) |
139 |
| - (catch Exception e |
140 |
| - (warn "Failed to format file:" (project-path options f)) |
141 |
| - (print-stack-trace e)))))))) |
| 144 | + (let [map* (if (:parallel? options) pmap map)] |
| 145 | + (->> paths |
| 146 | + (mapcat (partial find-files options)) |
| 147 | + (map* (partial fix-one options)) |
| 148 | + (map (partial print-file-status options)) |
| 149 | + dorun)))) |
142 | 150 |
|
143 | 151 | (defn- cli-file-reader [filepath]
|
144 | 152 | (let [contents (slurp filepath)]
|
|
149 | 157 | (def default-options
|
150 | 158 | {:project-root "."
|
151 | 159 | :file-pattern #"\.clj[csx]?$"
|
152 |
| - :ansi? true}) |
| 160 | + :ansi? true |
| 161 | + :parallel? false}) |
153 | 162 |
|
154 | 163 | (defn merge-default-options [options]
|
155 | 164 | (-> (merge default-options options)
|
|
160 | 169 |
|
161 | 170 | (def ^:private cli-options
|
162 | 171 | [[nil "--help"]
|
| 172 | + [nil "--parallel" |
| 173 | + :id :parallel?] |
163 | 174 | [nil "--file-pattern FILE_PATTERN"
|
164 | 175 | :default (:file-pattern default-options)
|
165 | 176 | :parse-fn re-pattern]
|
|
207 | 218 | (if (or (nil? cmd) (:help options))
|
208 | 219 | (do (println "cljfmt [OPTIONS] COMMAND [PATHS ...]")
|
209 | 220 | (println (:summary parsed-opts)))
|
210 |
| - (case cmd |
211 |
| - "check" (check paths options) |
212 |
| - "fix" (fix paths options) |
213 |
| - (abort "Unknown cljfmt command:" cmd)))))) |
| 221 | + (do (case cmd |
| 222 | + "check" (check paths options) |
| 223 | + "fix" (fix paths options) |
| 224 | + (abort "Unknown cljfmt command:" cmd)) |
| 225 | + (when (:parallel? options) |
| 226 | + (shutdown-agents))))))) |
0 commit comments