-
Notifications
You must be signed in to change notification settings - Fork 3
/
mods.js
347 lines (292 loc) · 10.9 KB
/
mods.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'use strict'
const paths = require('path')
const buck = require('./buck')
const libs = require('./libs')
const ops = require('./ops')
const UNASSIGNED = '__UNASSIGNED__'
class Mod {
constructor(path) {
Object.assign(this, {
path,
name: UNASSIGNED, // should be assigned unique module name
srcs: {}, // source folders, including test and generated source folders
deps: {}, // collect dependencies
depmods: {}, // collect set of modules as dependencies
deplibs: {}, // collect set of libraries as dependencies
})
}
toString() {
return `//${this.path} [${this.name}] ${Object.keys(this.srcs).join(', ')}`
}
}
function pathDerivedName(m) {
return m.path.replace(/[_/]/g, '.')
}
const mods = {
all: [],
rootname: UNASSIGNED,
addedTargets: [],
add(mod) {
this.all.push(mod)
},
addTargets(pattern) {
this.addedTargets.push(pattern)
},
toString() {
return ['Modules', ...this.all].map(String).join('\n\t')
},
discover() {
if (this.all.length) return // already discovered
// to force rediscovery clear this.all array
let allTargets = buck.info('//...')
for (let a of this.addedTargets) {
allTargets.push(...buck.info(a))
}
let moduleByPath = {} // mapping from path/folder to a originating rule
let moduleByTarget = {} // reverse mapping of which module target falls in
collectModulePaths()
defineSourcesAndDeps()
defineGeneratedSources()
assignModuleNames()
wireDependencies()
for (let p in moduleByPath) {
this.add(moduleByPath[p])
}
// we're done here, hoisted private functions below
function collectModulePaths() {
for (let rule of allTargets) {
let t = buck.target(rule[buck.attr.qname])
if (isModuleOrigin(t, rule)) {
moduleByPath[t.path] = new Mod(t.path)
}
}
}
function defineSourcesAndDeps() {
for (let rule of allTargets) {
let t = buck.target(rule[buck.attr.qname])
let m = moduleByPath[t.path]
if (!m) continue
// dependencies of annotation processor definition are
// not a regular compile dependencies
if (rule[buck.attr.type] === 'java_annotation_processor') continue
let isTest = isTestRule(rule)
if (isPackageModule(rule)) {
m.package = pathDerivedName(m)
}
addSourceFolders(m, rule, isTest)
mergeDeps(m.deps, depsOf(t, rule, isTest))
moduleByTarget[String(t)] = m
}
}
function addSourceFolders(module, rule, isTest) {
let srcs = module.srcs
let folder = rule[buck.attr.resourcesRoot]
if (!folder || (folder != '.' && !ops.exists(`${module.path}/${folder}`))) return
let existing = srcs[folder]
srcs[folder] = {
test: isTest || (existing ? existing.test : false),
path: folder,
res: hasLabel(rule, 'ide_res') ? {test:false}
: hasLabel(rule, 'ide_test_res') ? {test:true}
: false,
gen: false,
package: module.package,
}
}
function defineGeneratedSources() {
// generated folders added in separate loop to precisely
// determine if generated source folder is considered
// "isTestGen" (if one of the contributing rules
// to the base non-gen source folder is test rule)
for (let rule of allTargets) {
let t = buck.target(rule[buck.attr.qname])
let m = moduleByPath[t.path]
if (!m) continue
let isTest = isTestRule(rule)
addGeneratedSourceFolders(m.srcs, rule, isTest)
}
}
function addGeneratedSourceFolders(srcs, rule, isTest) {
let resFolder = rule[buck.attr.resourcesRoot]
let isTestGen = isTest
if (resFolder) {
let existing = srcs[resFolder]
isTestGen = isTest || (existing && existing.test)
}
let genPath = rule[buck.attr.generatedSourcePath]
if (!genPath && isTest && usesCodegen(rule)) {
// this is hardcoded edge-case due to some inconsistency in how Buck
// not returning generatedSourcePath for test rules
// https://github.com/facebook/buck/issues/2235
genPath = `buck-out/annotation/${rule[buck.attr.path]}/__${rule[buck.attr.name]}#testsjar_gen__`
}
if (genPath && usesCodegen(rule) && !hasLabel(rule, 'ide_no_gen_srcs')) {
let alias = resFolder || (isTestGen ? 'test' : 'src')
putInc(srcs, `${alias}-gen`, {
gen: true,
path: genPath,
test: isTestGen
})
}
}
function depsOf(target, rule, isTestRule) {
let plainDeps = rule[buck.attr.deps] || []
let providedDeps = rule[buck.attr.providedDeps] || []
let exportedDeps = rule[buck.attr.exportedDeps] || []
let exportedProvidedDeps = rule[buck.attr.exportedProvidedDeps] || []
let deps = [
...plainDeps,
...providedDeps,
...exportedDeps,
...exportedProvidedDeps]
.map(d => target.resolve(buck.target(d)))
.reduce((ds, d) => (ds[d] = {target:d}, ds), {})
for (let [k, dep] of Object.entries(deps)) {
dep.test = isTestRule
dep.provided = providedDeps.includes(k) || exportedProvidedDeps.includes(k)
dep.exported = exportedDeps.includes(k) || exportedProvidedDeps.includes(k)
}
return deps
}
function mergeDeps(to, from) {
let sharedKeys = Object.assign({}, to, from)
for (let k in sharedKeys) {
let a = to[k]
let b = from[k]
to[k] = (a && b) ? merge(a, b) : (a || b)
}
function merge(a, b) {
let {test, provided, exported, ...rest} = a
return {
test: test && b.test, // if any rule in module use it not for test
provided: provided && b.provided,
exported: exported || b.exported,
...rest
}
}
}
function assignModuleNames() {
mods.rootname = paths.basename(process.cwd())
let bySimpleNames = {}
for (let [p, m] of Object.entries(moduleByPath)) {
let n = paths.basename(m.path)
;(bySimpleNames[n] || (bySimpleNames[n] = [])).push(m)
}
// we prefer simple name, but resort to path-derived name
// in case of conflicts with root or between modules
// in either case we ensure that there will be a unique
// module name across project by using `putInc` routine
// (otherwise there's a chance that name mangling
// will still produce some duplicates)
let uniqueNames = {}
uniqueNames[mods.rootname] = {} // claimed ahead
for (let [n, ms] of Object.entries(bySimpleNames)) {
if (n === mods.rootname || ms.length > 1) {
ms.forEach(m => assignUniqueName(m, pathDerivedName(m)))
} else {
ms.forEach(m => assignUniqueName(m, n))
}
}
function assignUniqueName(m, name) {
m.name = putInc(uniqueNames, name, m)
}
}
function collectLibraryTransitiveDependencies(target) {
if (!(target in libs.byTarget)) throw `Not in libs.byTarget: ${target}`
let results = new Set()
let unprocessed = [target]
do {
let l = unprocessed.shift()
results.add(l)
let deplib = libs.byTarget[l]
for (let d of (deplib.options.deps || [])) {
let t = deplib.target.resolve(buck.target(d)).toString()
if ((t in libs.byTarget) && !results.has(t)) {
unprocessed.push(t)
}
// it occurs to me that under current model, we cannot
// properly propagate library's dependency on a [ide] module,
// only to other external or internal libraries, should be fine though
}
} while (unprocessed.length > 0)
return [...results]
}
function wireDependencies() {
for (let [p, m] of Object.entries(moduleByPath)) {
for (let [t, dep] of Object.entries(m.deps)) {
let resolved = false
// resolve dependency as sibling module in project workspace
if (t in moduleByTarget) {
let mod = moduleByTarget[t]
if (mod !== m) {
// in case of local dependency, we cannot add dependency on self module
// only dependency on local internal libs (to be jar compiled) are supported
// but we mark dependency resolved anyway
m.depmods[mod.path] = Object.assign({}, dep, {mod})
}
resolved = true
}
// and / or as dependency to a jar library (external/3rdparty or prebuilt internal)
// if both module and library dependency are not desired at the same time
// it's better to manage these dependencies, potentially, by
// making module reexport same dependencies instead of using library directly
if (t in libs.byTarget) {
let deplibs = collectLibraryTransitiveDependencies(t)
.reduce((r, depkey) => {
r[depkey] = Object.assign({}, dep, {lib: libs.byTarget[depkey]})
return r
}, {})
mergeDeps(m.deplibs, deplibs)
resolved = true
}
if (!resolved) {
let bt = buck.target(t)
if (bt.isLocal || bt.path == m.path) {
// local dependency should be implicit in IDE module
// or it should have been an internal library
} else ops.err(`${p}: Unresolvable dependency ${t}`)
}
}
}
}
// could only think of convention rule named `*_test`
// obviously would work fine for java_test and kotlin_test etc
// any other ways to distinguish test rules?
function isTestRule(rule) {
return /.+_test$/.test(rule[buck.attr.type] || '')
}
// special streamlined package module type, where the convention is
// - the path to the module withing project root equals package path
// - the sources are directly in the module folder ('.' related to the module path) or 'src'
// - the test sources are in 'test' subfolder
// - within the module, sources do not have additional package path, they use the package
// path folder nesting from withing root for subpackages though.
function isPackageModule(rule) {
return hasLabel(rule, 'ide_mod_package')
}
function hasLabel(rule, label) {
return (rule[buck.attr.labels] || []).includes(label)
}
function isModuleOrigin(target, rule) {
return target.isDefault
&& (rule[buck.attr.resourcesRoot] || hasLabel(rule, 'ide_mod'))
}
function usesCodegen(rule) {
return notEmpty(rule[buck.attr.plugins])
|| notEmpty(rule[buck.attr.annotationProcessors])
}
function putInc(object, field, value) {
for (let s = '';; s = String((+s || 0) + 1)) {
let alias = field + s
if (!(alias in object)) {
object[alias] = value
return alias
}
}
}
function notEmpty(arr) {
return !!arr && arr.length > 0
}
}
}
module.exports = mods