File: lint.clj

package info (click to toggle)
pomegranate-clojure 1.2.24-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: xml: 135; sh: 37; makefile: 17
file content (64 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
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
(ns lint
  (:require [babashka.classpath :as bbcp]
            [babashka.cli :as cli]
            [babashka.fs :as fs]
            [babashka.tasks :as t]
            [clojure.string :as string]
            [lread.status-line :as status]))

(def clj-kondo-cache ".clj-kondo/.cache")

(defn- cache-exists? []
  (fs/exists? clj-kondo-cache))

(defn- delete-cache []
  (when (cache-exists?)
    (fs/delete-tree clj-kondo-cache)))

(defn- build-cache []
  (when (cache-exists?)
    (delete-cache))
  (let [clj-cp (-> (t/clojure {:out :string}
                              "-Spath -M:test:isolated")
                   with-out-str
                   string/trim)
        bb-cp (bbcp/get-classpath)]
    (status/line :detail "- copying configs")
    (t/clojure "-M:clj-kondo --skip-lint --copy-configs --lint" clj-cp bb-cp)
    (status/line :detail "- creating cache")
    (t/clojure "-M:clj-kondo --dependencies --lint" clj-cp bb-cp)))

(defn- check-cache [{:keys [rebuild]}]
  (status/line :head "clj-kondo: cache check")
  (if-let [rebuild-reason (cond
                            rebuild
                            "Rebuild requested"

                            (not (cache-exists?))
                            "Cache not found"

                            :else
                            (let [updated-dep-files (fs/modified-since clj-kondo-cache ["deps.edn" "bb.edn" "nvd_check_helper_project/deps.edn"])]
                              (when (seq updated-dep-files)
                                (format "Found deps files newer than lint cache: %s" (mapv str updated-dep-files)))))]
    (do (status/line :detail rebuild-reason)
        (build-cache))
    (status/line :detail "Using existing cache")))

(defn- lint [opts]
  (check-cache opts)
  (status/line :head "clj-kondo: linting")
  (let [{:keys [exit]}
        (t/clojure {:continue true}
                   "-M:clj-kondo --lint src script deps.edn bb.edn nvd_check_helper_project/deps.edn")]
    (cond
      (= 2 exit) (status/die exit "clj-kondo found one or more lint errors")
      (= 3 exit) (status/die exit "clj-kondo found one or more lint warnings")
      (> exit 0) (status/die exit "clj-kondo returned unexpected exit code"))))

(defn -main [& args]
  (when-let [opts (cli/parse-opts args)]
    (lint opts)))

(when (= *file* (System/getProperty "babashka.file"))
  (apply -main *command-line-args*))