-
Notifications
You must be signed in to change notification settings - Fork 3
/
buck.js
201 lines (172 loc) · 4.37 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
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 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)
}
}
function trimSlashes(path) {
return path.replace(/^[/]+/, '').replace(/[/]+$/, '')
}
function target(string) {
let [p, g] = string.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) {
let deps = jars.map(j => `':${flatname(j)}'`)
.concat((options.deps || []).map(target).map(d => `'${d}'`))
return `
java_library(
name = '${t.goal}',
exported_deps = [${deps.join(', ')}],
visibility = ['PUBLIC'],
)
`
}
function ruleJavaAnnotationProcessor(t, jars, options) {
let deps = jars.map(j => `':${flatname(j)}'`)
.concat((options.deps || []).map(target).map(d => `'${d}'`))
if (options.processorLibrary) {
return `
java_library(
name = '${options.processorLibrary}',
exported_deps = [${deps.join(', ')}],
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 = [${deps.join(', ')}],
processor_class = '${options.processor}',
visibility = ['PUBLIC'],
)
`
}
function rules(target, jars, srcs, options) {
let mainRule = options.processor
? ruleJavaAnnotationProcessor(target, jars, options)
: ruleJavaLibrary(target, jars, options)
let prebuiltJars = jars.map((j, i) => rulePrebuiltJar(j, srcs[i]))
return [mainRule, ...prebuiltJars]
}
function query(input, attrs) {
attrs = attrs ? `--output-attributes ${attrs}` : ''
return JSON.parse(ops.exec(`buck query "${input}" --json ${attrs}`))
}
let cachedInfo = {}
function dropCache() {
cachedTargets = {}
}
function info(pattern) {
return cachedInfo[pattern]
|| (cachedInfo[pattern] =
JSON.parse(ops.exec(`buck targets "${pattern}" --json --show-output`)))
}
function fetchAll() {
ops.exec('buck fetch //...')
}
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',
}
module.exports = {
target, rules, remote, attr, query, info, fetchAll, dropCache
}