-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.clj
105 lines (95 loc) · 3.41 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
(ns cljfmt.main
"Functionality to apply formatting to a given project."
(:require [cljfmt.config :as config]
[cljfmt.io :as io]
[cljfmt.tool :as tool]
[clojure.tools.cli :as cli])
(:gen-class))
(def ^:const VERSION "0.10.4")
(defn- cli-options [defaults]
[["-h" "--help"]
[nil "--version"]
["-q" "--quiet"
:id :quiet?]
["-v" "--verbose"
:id :verbose?]
[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-missing? [path]
(not (io/exists? (io/file-entity path))))
(defn- abort-if-files-missing [paths]
(when-some [missing (first (filter file-missing? paths))]
(abort "No such file:" (str missing))))
(def ^:dynamic *command* "cljfmt")
(defn- print-help [summary]
(println "Usage:")
(println (str \tab *command* " (check | fix) [PATHS...]"))
(println "Options:")
(println summary))
(defn -main [& args]
(let [base-opts (config/load-config)
parsed-opts (cli/parse-opts args (cli-options base-opts))
[cmd & paths] (:arguments parsed-opts)
flags (:options parsed-opts)
options (-> (config/merge-configs base-opts flags)
(update :paths #(or (seq paths) %)))]
(when (:errors parsed-opts)
(abort (:errors parsed-opts)))
(cond
(:version flags)
(println "cljfmt" VERSION)
(or (nil? cmd) (:help flags))
(print-help (:summary parsed-opts))
:else
(let [cmdf (case cmd
"check" tool/check-no-config
"fix" tool/fix-no-config
(abort "Unknown cljfmt command:" cmd))]
(abort-if-files-missing paths)
(binding [tool/*no-output* (:quiet? options)
tool/*verbose* (:verbose? options)]
(cmdf options))
(when (:parallel? options)
(shutdown-agents))))))