-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.clj
87 lines (81 loc) · 3.09 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
(ns cljfmt.main
"Functionality to apply formatting to a given project."
(:require [cljfmt.config :as config]
[cljfmt.tool :as tool]
[clojure.java.io :as io]
[clojure.tools.cli :as cli])
(:gen-class))
(defn- cli-options [defaults]
[[nil "--help"]
[nil "--[no-]parallel"
:id :parallel?
:default (:parallel? defaults)]
[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 config/read-config]
[nil "--alias-map ALIAS_MAP_PATH"
:parse-fn config/read-config]
[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- abort [& msg]
(binding [*out* *err*]
(when (seq msg)
(apply println msg))
(System/exit 1)))
(defn- file-exists? [path]
(.exists (io/as-file path)))
(defn- abort-if-files-missing [paths]
(when-some [missing (some (complement file-exists?) paths)]
(abort "No such file:" (str missing))))
(def ^:dynamic *command*
"clojure -M -m cljfmt.main")
(defn -main [& args]
(let [base-opts (config/load-config)
parsed-opts (cli/parse-opts args (cli-options base-opts))
[cmd & paths] (:arguments parsed-opts)
options (-> (config/merge-configs base-opts (:options parsed-opts))
(update :paths into paths))]
(if (:errors parsed-opts)
(abort (:errors parsed-opts))
(if (or (nil? cmd) (-> parsed-opts :options :help))
(do (println "Usage:")
(println (str \tab *command* " (check | fix) [PATHS...]"))
(println "Options:")
(println (:summary parsed-opts)))
(let [cmdf (case cmd
"check" tool/check-no-config
"fix" tool/fix-no-config
(abort "Unknown cljfmt command:" cmd))]
(abort-if-files-missing paths)
(cmdf options)
(when (:parallel? options)
(shutdown-agents)))))))