-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.clj
60 lines (51 loc) · 2.11 KB
/
config.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
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns noahtheduke.splint.config
(:require
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as str]))
(set! *warn-on-reflection* true)
(def version (delay (str/trim (slurp (io/resource "SPLINT_VERSION")))))
(defn splint-version [] (str "splint v" @version))
(defn read-default-config []
(edn/read-string (slurp (io/resource "config/default.edn"))))
(def default-config
(delay (read-default-config)))
(defn find-local-config []
(loop [dir (.getParentFile (.getAbsoluteFile (io/file ".")))]
(let [config (io/file dir ".splint.edn")]
(if (.exists config)
{:dir dir
:file (.getAbsoluteFile config)
:local (edn/read-string (slurp config))}
(when-let [parent (.getParentFile dir)]
(recur parent))))))
(defn deep-merge [default & maps]
(letfn [(reconcile-keys [val-in-result val-in-latter]
(if (and (map? val-in-result)
(map? val-in-latter))
(merge-with reconcile-keys val-in-result val-in-latter)
val-in-latter))
(reconcile-maps [result latter]
(merge-with reconcile-keys result latter))]
(reduce reconcile-maps default maps)))
(defn load-config
([options] (load-config (:local (find-local-config)) options))
([local options]
(let [merged-options
(-> (deep-merge @default-config local)
(set/rename-keys {'output :output
'parallel :parallel
'quiet :quiet
'silent :silent})
(merge options))]
;; Defaults are set here because cli options are merged in last and
;; tools.cli defaults can't be distinguished.
(conj {:parallel true :output "full"} merged-options))))
(defn get-config [ctx rule]
(let [full-name (:full-name rule)
init-type (:init-type rule)]
(-> ctx init-type full-name :config)))