-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
source_paths.clj
215 lines (195 loc) · 9.58 KB
/
source_paths.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(ns clojure-lsp.source-paths
(:require
[clojure-lsp.config :as config]
[clojure-lsp.parser :as parser]
[clojure-lsp.shared :as shared]
[clojure.java.io :as io]
[clojure.set :as set]
[lsp4clj.protocols.logger :as logger]
[rewrite-clj.zip :as z])
(:import
[java.io File]))
(set! *warn-on-reflection* true)
(def default-source-paths #{"src" "test"})
(def default-lein-source-paths ["src" "src/main/clojure"])
(def default-lein-test-paths ["test" "src/test/clojure"])
(def default-bb-source-paths #{"src" "test" "script" "scripts"})
(def default-source-aliases #{:dev :test})
(defn ^:private extract-source-paths [paths extra-paths aliases]
(->> (concat paths extra-paths)
(map (fn [path]
(if (keyword? path)
(when (or (vector? (get aliases path))
(set? (get aliases path)))
(get aliases path))
path)))
flatten
(remove nil?)
set))
(defn ^:private extract-local-roots [deps]
(when (map? deps)
(->> (vec deps)
(map #(some-> % second :local/root))
(remove nil?))))
(defn ^:private deps-file->source-paths
[deps-file settings]
(let [{:keys [paths extra-paths aliases]} (config/read-edn-file deps-file)
source-aliases (or (:source-aliases settings) default-source-aliases)
root-source-paths (extract-source-paths paths extra-paths aliases)]
(->> source-aliases
(map #(get aliases % nil))
(mapcat #(extract-source-paths (:paths %) (:extra-paths %) nil))
(remove nil?)
set
(set/union root-source-paths))))
(defn ^:private relative-to-deps-file [file ^File deps-file]
(if (.isAbsolute (io/file file))
file
(shared/normalize-file (io/file (.getParentFile deps-file) file))))
(defn deps-file->local-roots
[deps-file settings]
(let [{:keys [deps extra-deps aliases]} (config/read-edn-file deps-file)
source-aliases (or (:source-aliases settings) default-source-aliases)
deps-local-roots (extract-local-roots deps)
extra-deps-local-roots (extract-local-roots extra-deps)]
(->> source-aliases
(map #(get aliases % nil))
(mapcat (fn [{:keys [deps extra-deps]}]
(concat (extract-local-roots deps)
(extract-local-roots extra-deps))))
(concat deps-local-roots extra-deps-local-roots)
(map #(relative-to-deps-file % deps-file))
(remove nil?))))
(defn ^:private resolve-deps-source-paths
[deps-file root-path settings]
(loop [deps-files-by-root [[nil deps-file]]
accum-source-paths nil
recur-level 1]
(if (>= recur-level 500)
(logger/warn "Max deps source-paths resolve level found" recur-level ", maybe a cyclic dependency?")
(let [source-paths (->> deps-files-by-root
(map (fn [[local-root deps-file]]
(->> (deps-file->source-paths deps-file settings)
(map #(if local-root (str (io/file local-root %)) %))
set)))
(reduce set/union))
local-roots (->> deps-files-by-root
(map second)
(map #(deps-file->local-roots % settings))
flatten
(remove nil?))
deps-files-by-local-root (->> local-roots
(map (fn [local-root]
(let [local-root (shared/relativize-filepath local-root root-path)
sub-deps-file (io/file root-path local-root "deps.edn")]
(when (shared/file-exists? sub-deps-file)
[local-root sub-deps-file]))))
(remove nil?))
final-source-paths (set/union source-paths accum-source-paths)]
(if (seq deps-files-by-local-root)
(recur deps-files-by-local-root final-source-paths (inc recur-level))
final-source-paths)))))
(defn ^:private valid-path-config? [config]
(and config
(or (vector? config)
(and (list? config)
(string? (first config)))
(set? config))))
(defn ^:private resolve-lein-source-paths
[{:keys [source-paths test-paths profiles] :as project}
settings]
(when project
(let [source-aliases (or (:source-aliases settings) default-source-aliases)
source-paths (if (valid-path-config? source-paths)
source-paths
default-lein-source-paths)
test-paths (if (valid-path-config? test-paths)
test-paths
default-lein-test-paths)
root-source-paths (extract-source-paths source-paths test-paths profiles)]
(->> source-aliases
(map #(get profiles % nil))
(mapcat #(extract-source-paths (:source-paths %) (:test-paths %) nil))
(remove nil?)
set
(set/union root-source-paths)))))
(defn ^:private resolve-bb-source-paths
[{:keys [paths]}]
(set (map str paths)))
(defn ^:private manual-source-paths-calculation [root-path settings]
(let [deps-file (shared/to-file root-path "deps.edn")
lein-file (shared/to-file root-path "project.clj")
bb-file (shared/to-file root-path "bb.edn")
file-source-paths
(cond-> []
(shared/file-exists? deps-file)
(conj
(let [deps-source-paths (resolve-deps-source-paths deps-file (str root-path) settings)]
(if (seq deps-source-paths)
{:deps-source-paths deps-source-paths
:source-paths deps-source-paths
:origin :deps-edn}
{:origin :empty-deps-edn
:source-paths default-source-paths})))
(shared/file-exists? lein-file)
(conj
(let [lein-edn (parser/lein-zloc->edn (z/of-file lein-file))
lein-source-paths (resolve-lein-source-paths lein-edn settings)]
(if (seq lein-source-paths)
{:lein-source-paths lein-source-paths
:source-paths lein-source-paths
:origin :leiningen}
{:origin :empty-leiningen
:source-paths default-source-paths})))
(shared/file-exists? bb-file)
(conj
(let [bb-edn (config/read-edn-file bb-file)
bb-paths (resolve-bb-source-paths bb-edn)]
(if (seq bb-paths)
{:bb-source-paths bb-paths
:source-paths bb-paths
:origin :bb}
{:origin :empty-bb
:source-paths default-bb-source-paths}))))]
(when (seq file-source-paths)
(reduce
(fn [a b]
(-> (merge a b)
(dissoc :origin)
(assoc :origins (set (conj (:origins a) (:origin b)))
:source-paths (set/union (set (:source-paths a))
(set (:source-paths b))))))
{}
file-source-paths))))
(defn ^:private classpath->source-paths [^java.nio.file.Path root-path classpath]
(let [source-paths (->> classpath
(remove shared/jar-file?)
(map (comp #(.getAbsoluteFile ^File %) io/file))
(filter (fn [^File file]
(.startsWith (.toPath file) root-path)))
(mapv #(.getCanonicalPath ^File %)))]
(when (seq source-paths)
{:origins #{:classpath}
:source-paths source-paths
:classpath-paths source-paths})))
(defn ^:private resolve-source-paths [root-path classpath given-source-paths settings]
(if given-source-paths
{:source-paths given-source-paths
:origins #{:settings}}
(or (if (get settings :use-source-paths-from-classpath true)
(classpath->source-paths root-path classpath)
(manual-source-paths-calculation root-path settings))
{:source-paths default-source-paths
:origins #{:default}})))
(defn process-source-paths [root-path classpath settings given-source-paths]
(let [{:keys [origins source-paths classpath-paths deps-source-paths lein-source-paths bb-source-paths]} (resolve-source-paths root-path classpath given-source-paths settings)]
(when (contains? origins :settings) (logger/info "Using given source-paths:" given-source-paths))
(when (contains? origins :classpath) (logger/info "Using source-paths from classpath:" classpath-paths))
(when (contains? origins :deps-edn) (logger/info "Manually resolved source-paths from deps.edn:" deps-source-paths))
(when (contains? origins :leiningen) (logger/info "Manually resolved source-paths from project.clj:" lein-source-paths))
(when (contains? origins :bb) (logger/info "Manually resolved source-paths from bb.edn:" bb-source-paths))
(when (contains? origins :empty-deps-edn) (logger/info "Empty deps.edn source-paths, using default source-paths:" default-source-paths))
(when (contains? origins :empty-leiningen) (logger/info "Empty project.clj source-paths, using default source-paths:" default-source-paths))
(when (contains? origins :empty-bb) (logger/info "Empty bb.edn paths, using default source-paths:" default-source-paths))
(when (contains? origins :default) (logger/info "Using default source-paths:" default-source-paths))
(mapv #(->> % (shared/to-file root-path) .getCanonicalPath str) source-paths)))