-
Notifications
You must be signed in to change notification settings - Fork 7
/
core.clj
94 lines (78 loc) · 3.76 KB
/
core.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
(ns boot-fmt.core
"Reformat Clojure(script) source files"
{:boot/export-tasks true}
(:require [clojure.set]
[clojure.string]
[clojure.java.shell]
[boot.core :as bc]
[boot.util :as bu]))
(def pod-deps
'[[zprint "0.4.6"] [clojure-future-spec "1.9.0-alpha17"]
[com.google.guava/guava "18.0"]])
(defn find-files-git
[]
(let [{:keys [exit out err]} (clojure.java.shell/sh "git"
"ls-files" "-z"
"*.clj" "*.cljs"
"*.cljc" "*.cljx"
"*.cljs.hl" "*.boot")]
(when (not= exit 0) (throw (ex-info "git ls-files failed" {:err err})))
(-> out
(clojure.string/split #"\000")
set)))
(defn find-files-source
[]
(clojure.set/union (bc/get-env :source-paths) (bc/get-env :resource-paths)))
;!zprint {:format :skip}
(bc/deftask fmt
"Reformat Clojure(script) source files, like gofmt
Print reformatted source code to standard output. Parameters specified using
`-f` can be files or directories. Directories are scanned recursively for
Clojure(Script) source files.
Specify the operation using the --mode parameter:
--mode print (default)
Print reformatted code to standard output
--mode diff
When reformatted code is different from original, print diff to standard output.
--mode list
Where reformatted code is different from original, print filename standard output.
--mode overwrite
Overwrite files with reformatted code. As this is a potentially dangerous
operation, you need to specify the --really flag in addition to setting
the --mode parameter"
[m mode MODE kw "Mode of operation, i.e. print, list, diff or overwrite. Defaults to print"
r really bool "In overwrite mode, files are overwritten only if the --really flag is set as well"
f files VAL #{str} "The list of files or directories to format"
s source bool "Automatically scan for files in boot source-paths and resource-paths"
g git bool "Automatically scan for files in current git repository"
o options OPTS edn "zprint options"]
(let [mode (or mode :print)
files (cond-> files
source (clojure.set/union (find-files-source))
git (clojure.set/union (find-files-git)))]
(assert (seq files) "At least one filename needs to be provided.")
(assert (#{:print :list :diff :overwrite} mode) "Invalid mode")
(assert (or (not= :overwrite mode) really)
"In overwrite mode, add the --really flag")
(let [pod (boot.pod/make-pod (update (bc/get-env) :dependencies concat pod-deps))]
(bc/with-pre-wrap
fileset
(let [files* (some->> files
(map clojure.java.io/file)
(mapcat (fn [f]
(if (.isDirectory f) (file-seq f) [f])))
(filter (fn [f]
(and (.exists f)
(.isFile f)
(not (.isHidden f))
(->> (.. f getName toLowerCase)
(re-find #"\.(clj|cljs|cljc|cljx|cljs\.hl|boot)$")))))
(map #(.getPath %))
set
sort)]
(boot.pod/with-eval-in pod (require 'boot-fmt.impl))
(boot.pod/with-call-in pod
(boot-fmt.impl/process-many-file-names {:mode ~mode
:zprint-options ~options}
~files*))
fileset)))))