-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuck.js
261 lines (226 loc) · 5.56 KB
/
buck.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
'use strict'
const paths = require('path')
const ops = require('./ops')
const mvn = require('./mvn')
class Target {
constructor(path, goal) {
this.path = path
this.goal = goal
}
toString() {
return this.path ? `//${this.path}:${this.goal}` : `:${this.goal}`
}
get basename() {
return this.path ? paths.basename(this.path) : ''
}
get pattern() {
return `${this.path}/...`
}
get isDefault() {
return this.basename === this.goal
}
get isLocal() {
return !this.path
}
get abbr() {
let suffix = this.isDefault ? '' : `:${this.goal}`
return `${this.path}${suffix}`
}
at(path) {
return new Target(paths.join(trimSlashes(path), this.path), this.goal)
}
withGoal(goal) {
return new Target(this.path, goal)
}
resolve(target) {
return (!this.isLocal && target.isLocal)
? this.withGoal(target.goal)
: target
}
}
function trimSlashes(path) {
return path.replace(/^[/]+/, '').replace(/[/]+$/, '')
}
function target(arg) {
if (arg instanceof Target) return arg
let [p, g] = String(arg).split(':')
p = trimSlashes(p)
g = g || paths.basename(p)
if (!p && !g) throw `Wrong target specifier '${string}'`
return new Target(p, g)
}
function flatname(input) {
return String(input).replace(/[-.:]/g, '_')
}
function rulePrebuiltJar(jar, src) {
let n = flatname(jar)
if (src) return `
prebuilt_jar(
name = '${n}',
binary_jar = ':remote_${n}_jar',
source_jar = ':remote_${n}_src',
maven_coords = '${jar}',
)
remote_file(
name = 'remote_${n}_jar',
out = '${jar.filenameJar}',
url = '${jar.remote}${mvn.EXT.jar}',
sha1 = '${jar.checksumJar}',
)
remote_file(
name = 'remote_${n}_src',
out = '${src.filenameSrc}',
url = '${src.remote}${mvn.EXT.src}',
sha1 = '${src.checksumSrc}',
)
`
else return `
prebuilt_jar(
name = '${n}',
binary_jar = ':remote_${n}_jar',
maven_coords = '${jar}',
)
remote_file(
name = 'remote_${n}_jar',
out = '${jar.filenameJar}',
url = '${jar.remote}${mvn.EXT.jar}',
sha1 = '${jar.checksumJar}',
)
`
}
function ruleJavaLibrary(t, jars, options) {
return `
java_library(
name = '${t.goal}',
exported_deps = [${(toBuckDeps(jars, options))}],
visibility = ['PUBLIC'],
)
`
}
function toBuckDeps(jars, options) {
return jars.map(j => `:${flatname(j)}`)
.concat(options.deps || [])
.map(target)
.map(d => `'${d}'`)
.join(', ')
}
function ruleJavaAnnotationProcessor(t, jars, options) {
if (options.processorLibrary) {
return `
java_library(
name = '${options.processorLibrary}',
exported_deps = [${(toBuckDeps(jars, options))}],
visibility = ['PUBLIC'],
)
java_annotation_processor(
name = '${t.goal}',
deps = [':${options.processorLibrary}'],
processor_class = '${options.processor}',
visibility = ['PUBLIC'],
)
`
}
return `
java_annotation_processor(
name = '${t.goal}',
deps = [${toBuckDeps(jars, options)}],
processor_class = '${options.processor}',
visibility = ['PUBLIC'],
)
`
}
function ruleJavaBinary(t, jars, options) {
return `
java_binary(
name = '${t.goal}',
deps = [${toBuckDeps(jars, options)}],
main_class = '${options.main}',
visibility = ['PUBLIC'],
)
`
}
function ruleRemoteFile(target, coords, type, ext, checksumExt) {
return `
remote_file(
name = '${target.goal}',
out = '${coords.filename}${ext}',
url = '${coords.remote}${ext}',
sha1 = '${coords.checksumByExt(checksumExt)}',
type = '${type}',
visibility = ['PUBLIC'],
)
`
}
function rules(target, jars, srcs, options) {
let libRule =
options.processor ? ruleJavaAnnotationProcessor(target, jars, options) :
options.main ? ruleJavaBinary(target, jars, options) :
ruleJavaLibrary(target, jars, options)
let prebuiltJars = jars.map((j, i) => rulePrebuiltJar(j, srcs[i]))
return [libRule, ...prebuiltJars]
}
function query(input, attrs) {
attrs = attrs ? `--output-attributes ${attrs}` : ''
return JSON.parse(ops.exec(`buck query "${input}" --json ${attrs}`))
}
let cachedConf
let cachedInfo = {}
function dropCache() {
cachedInfo = {}
cachedConf = undefined
}
function info(pattern) {
return cachedInfo[pattern]
|| (cachedInfo[pattern] =
JSON.parse(ops.exec(`buck targets "${pattern}" --json --show-output`)))
}
function fetch(pattern) {
pattern = pattern || '//...'
ops.exec(`buck fetch ${pattern}`)
}
function conf() {
return cachedConf
|| (cachedConf = JSON.parse(ops.exec(`buck audit config --json`)))
}
function javaLevel() {
let c = conf()
let norm = v => ({
'8': '1.8'
}[v] || v)
return {
source: norm(c['java.source_level'] || '8'),
target: norm(c['java.target_level'] || '8'),
}
}
const remote = {
// alternative way is to hardcode Buck's internal paths
// `/buck-out/bin/${target.path}/remote_${flatname(j)}_jar/${j.filenameJar}`
jar(target, j) {
return target.withGoal(`remote_${flatname(j)}_jar`)
},
src(target, j) {
return target.withGoal(`remote_${flatname(j)}_src`)
},
}
const attr = {
qname: 'fully_qualified_name',
type: 'buck.type',
path: 'buck.base_path',
name: 'name',
out: 'out',
outputPath: 'buck.outputPath',
generatedSourcePath: 'buck.generatedSourcePath',
directDependencies: 'buck.direct_dependencies',
resourcesRoot: 'resourcesRoot',
annotationProcessors: 'annotationProcessors',
plugins: 'plugins',
labels: 'labels',
deps: 'deps',
exportedDeps: 'exportedDeps',
providedDeps: 'providedDeps',
exportedProvidedDeps: 'exportedProvidedDeps',
mavenCoords: 'mavenCoords',
}
module.exports = {
target, rules, remote, attr, query, info, fetch, dropCache, ruleRemoteFile, conf, javaLevel
}