-
Notifications
You must be signed in to change notification settings - Fork 119
/
tool.clj
143 lines (124 loc) · 4.49 KB
/
tool.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
(ns cljfmt.tool
(:require [cljfmt.config :as config]
[cljfmt.core :as cljfmt]
[cljfmt.diff :as diff]
[clojure.java.io :as io]
[clojure.stacktrace :as st]))
(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- 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 (.exists 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-no-config
"The same as `check`, but ignores dotfile configuration."
[options]
(let [map* (if (:parallel? options) pmap map)
counts (->> (:paths options)
(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 check
"Checks that the Clojure paths specified by the :paths option are
correctly formatted."
[options]
(let [opts (config/merge-configs (config/load-config) options)]
(check-no-config opts)))
(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-no-config
"The same as `fix`, but ignores dotfile configuration."
[options]
(let [map* (if (:parallel? options) pmap map)]
(->> (:paths options)
(mapcat (partial find-files options))
(map* (partial fix-one options))
(map (partial print-file-status options))
dorun)))
(defn fix
"Fixes the formatting for all files specified by the :paths option."
[options]
(let [opts (config/merge-configs (config/load-config) options)]
(fix-no-config opts)))