|
1 | 1 | #!/usr/bin/env bb
|
2 | 2 |
|
3 | 3 | (ns lint
|
4 |
| - (:require [clojure.java.io :as io] |
| 4 | + (:require [babashka.classpath :as bbcp] |
| 5 | + [babashka.fs :as fs] |
5 | 6 | [clojure.string :as string]
|
| 7 | + [docopt.core :as docopt] |
| 8 | + [docopt.match :as docopt-match] |
6 | 9 | [helper.env :as env]
|
7 | 10 | [helper.shell :as shell]
|
8 | 11 | [lread.status-line :as status]))
|
9 | 12 |
|
10 |
| -(defn cache-exists? [] |
11 |
| - (.exists (io/file ".clj-kondo/.cache"))) |
| 13 | +(defn- cache-exists? [] |
| 14 | + (fs/exists? ".clj-kondo/.cache")) |
12 | 15 |
|
13 |
| -(defn -main[] |
| 16 | +(defn- delete-cache [] |
| 17 | + (when (cache-exists?) |
| 18 | + (fs/delete-tree ".clj-kondo/.cache"))) |
| 19 | + |
| 20 | +(defn- build-cache [] |
| 21 | + (status/line :head "clj-kondo: building cache") |
| 22 | + (let [clj-cp (-> (shell/command ["clojure" "-A:test" "-Spath"] {:out :string}) :out string/trim) |
| 23 | + bb-cp (bbcp/get-classpath)] |
| 24 | + (shell/command ["clojure" "-M:clj-kondo" |
| 25 | + "--dependencies" "--copy-configs" |
| 26 | + "--lint" clj-cp bb-cp]))) |
| 27 | + |
| 28 | +(defn- lint [] |
| 29 | + (when (not (cache-exists?)) |
| 30 | + (build-cache)) |
| 31 | + (status/line :head "clj-kondo: linting") |
| 32 | + (let [{:keys [exit]} |
| 33 | + (shell/command-no-exit ["clojure" "-M:clj-kondo" |
| 34 | + "--lint" "src" "test" "script" "deps.edn"])] |
| 35 | + (if (not (some #{exit} '(0 2 3))) |
| 36 | + (status/die exit "clj-kondo existed with unexpected exit code: %d" exit) |
| 37 | + (System/exit exit)))) |
| 38 | + |
| 39 | +(def docopt-usage "Usage: lint.clj [options] |
| 40 | +
|
| 41 | +Options: |
| 42 | + --help Show this screen. |
| 43 | + --rebuild-cache Force rebuild of lint cache.") |
| 44 | + |
| 45 | +(defn -main [& args] |
14 | 46 | (env/assert-min-versions)
|
15 |
| - (if (not (cache-exists?)) |
16 |
| - (status/line :head "linting and building cache") |
17 |
| - (status/line :head "linting")) |
18 |
| - |
19 |
| - (let [lint-args (if (not (cache-exists?)) |
20 |
| - [(-> (shell/command ["clojure" "-A:test-common:script" "-Spath"] {:out :string}) |
21 |
| - :out |
22 |
| - string/trim) |
23 |
| - "deps.edn"] |
24 |
| - ["src" "test" "script" "deps.edn"]) |
25 |
| - {:keys [:exit]} (shell/command-no-exit |
26 |
| - (concat ["clojure" "-M:clj-kondo" |
27 |
| - "--lint"] |
28 |
| - lint-args |
29 |
| - ["--config" ".clj-kondo/ci-config.edn"]))] |
30 |
| - (when (not (some #{exit} '(0 2 3))) |
31 |
| - (status/die exit |
32 |
| - "clj-kondo existed with unexpected exit code: %d" exit)) |
33 |
| - (System/exit exit))) |
| 47 | + (if-let [opts (-> docopt-usage docopt/parse (docopt-match/match-argv args))] |
| 48 | + (cond |
| 49 | + (get opts "--help") |
| 50 | + (println docopt-usage) |
| 51 | + |
| 52 | + (get opts "--rebuild-cache") |
| 53 | + (do (delete-cache) (lint)) |
| 54 | + |
| 55 | + :else |
| 56 | + (lint)) |
| 57 | + (status/die 1 docopt-usage))) |
34 | 58 |
|
35 | 59 | (env/when-invoked-as-script
|
36 |
| - (-main)) |
| 60 | + (apply -main *command-line-args*)) |
0 commit comments