|
| 1 | +(ns lint |
| 2 | + (:require [babashka.classpath :as bbcp] |
| 3 | + [babashka.cli :as cli] |
| 4 | + [babashka.fs :as fs] |
| 5 | + [babashka.tasks :as t] |
| 6 | + [clojure.string :as string] |
| 7 | + [lread.status-line :as status])) |
| 8 | + |
| 9 | +(def clj-kondo-cache ".clj-kondo/.cache") |
| 10 | + |
| 11 | +(defn- cache-exists? [] |
| 12 | + (fs/exists? clj-kondo-cache)) |
| 13 | + |
| 14 | +(defn- delete-cache [] |
| 15 | + (when (cache-exists?) |
| 16 | + (fs/delete-tree clj-kondo-cache))) |
| 17 | + |
| 18 | +(defn- build-cache [] |
| 19 | + (when (cache-exists?) |
| 20 | + (delete-cache)) |
| 21 | + (let [clj-cp (-> (t/clojure {:out :string} |
| 22 | + "-Spath -M:test") |
| 23 | + with-out-str |
| 24 | + string/trim) |
| 25 | + bb-cp (bbcp/get-classpath)] |
| 26 | + (status/line :detail "- copying configs") |
| 27 | + (t/clojure "-M:clj-kondo --skip-lint --copy-configs --lint" clj-cp bb-cp) |
| 28 | + (status/line :detail "- creating cache") |
| 29 | + (t/clojure "-M:clj-kondo --dependencies --lint" clj-cp bb-cp))) |
| 30 | + |
| 31 | +(defn- check-cache [{:keys [rebuild]}] |
| 32 | + (status/line :head "clj-kondo: cache check") |
| 33 | + (if-let [rebuild-reason (cond |
| 34 | + rebuild |
| 35 | + "Rebuild requested" |
| 36 | + |
| 37 | + (not (cache-exists?)) |
| 38 | + "Cache not found" |
| 39 | + |
| 40 | + :else |
| 41 | + (let [updated-dep-files (fs/modified-since clj-kondo-cache ["deps.edn" "bb.edn"])] |
| 42 | + (when (seq updated-dep-files) |
| 43 | + (format "Found deps files newer than lint cache: %s" (mapv str updated-dep-files)))))] |
| 44 | + (do (status/line :detail rebuild-reason) |
| 45 | + (build-cache)) |
| 46 | + (status/line :detail "Using existing cache"))) |
| 47 | + |
| 48 | +(defn- lint [opts] |
| 49 | + (check-cache opts) |
| 50 | + (status/line :head "clj-kondo: linting") |
| 51 | + (let [{:keys [exit]} |
| 52 | + (t/clojure {:continue true} |
| 53 | + "-M:clj-kondo --lint src test bb deps.edn bb.edn")] |
| 54 | + (cond |
| 55 | + (= 2 exit) (status/die exit "clj-kondo found one or more lint errors") |
| 56 | + (= 3 exit) (status/die exit "clj-kondo found one or more lint warnings") |
| 57 | + (> exit 0) (status/die exit "clj-kondo returned unexpected exit code")))) |
| 58 | + |
| 59 | +(defn -main [& args] |
| 60 | + (when-let [opts (cli/parse-opts args)] |
| 61 | + (lint opts))) |
| 62 | + |
| 63 | +(when (= *file* (System/getProperty "babashka.file")) |
| 64 | + (apply -main *command-line-args*)) |
0 commit comments