forked from dropbox/dropbox-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stone.gradle
290 lines (248 loc) · 10.5 KB
/
stone.gradle
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
apply plugin: 'java'
class StoneConfig implements Serializable {
String packageName = 'com.dropbox.stone'
String globalRouteFilter = null
boolean dataTypesOnly = false
ClientSpec client = null
String routeWhitelistFilter = null
}
class ClientSpec implements Serializable {
String name = null
String javadoc = null
String routeFilter = null
String requestsClassnamePrefix = null
String unusedClassesToGenerate = null
}
abstract class StoneTask extends DefaultTask {
@Input
protected abstract ListProperty<StoneConfig> getStoneConfigs();
@Input
protected abstract Property<String> getGeneratorDir()
@Input
protected abstract Property<String> getSpecDir()
@Optional
@Input
protected abstract Property<String> getRouteWhitelistFilter()
@Input
protected abstract Property<String> getStoneDir()
@Input
protected abstract Property<String> getPythonCommand()
@OutputDirectory
protected abstract DirectoryProperty getOutputDir();
@Inject
protected abstract ObjectFactory getObjectFactory();
@Inject
protected abstract ExecOperations getExecOperations();
public void config(List<StoneConfig> configs) {
getStoneConfigs().set(configs)
}
public void generatorDir(String generatorDir) {
getGeneratorDir().set(generatorDir)
}
public void routeWhitelistFilter(String routeWhitelistFilter) {
getRouteWhitelistFilter().set(routeWhitelistFilter)
}
public void stoneDir(String stoneDir) {
getStoneDir().set(stoneDir)
}
public void pythonCommand(String pythonCommand) {
getPythonCommand().set(pythonCommand)
}
public void specDir(String specDir) {
getSpecDir().set(specDir)
}
public void outputDir(String outputDir) {
getOutputDir().set(new File(outputDir))
}
@TaskAction
public void processStone() {
def generatorFileTree = getObjectFactory().fileTree().from(getGeneratorDir().get())
generatorFileTree.include('**/*stoneg.py')
def generatorFile = generatorFileTree.getSingleFile()
def specFiles = getSpecFiles(getObjectFactory(), getSpecDir().get()).getFiles()
for (StoneConfig config in getStoneConfigs().get()) {
config.routeWhitelistFilter = getRouteWhitelistFilter().getOrNull()
}
runStoneGenerator(
getStoneConfigs().get(),
getObjectFactory().fileTree().from(getStoneDir().get()).getDir(),
generatorFile,
specFiles,
getOutputDir().getAsFile().get(),
getPythonCommand().get()
)
}
static FileTree getSpecFiles(ObjectFactory objectFactory, String specDir) {
def fileTree = objectFactory.fileTree().from(specDir)
fileTree.include('**/*.stone')
return fileTree
}
def runStoneGenerator(
List<StoneConfig> configs,
File stoneDir,
File generatorFile,
Collection<File> specFiles,
File outputDir,
String pythonCommand
) {
def srcOutputDir = new File(outputDir, "src")
def refsFile = new File(outputDir, "refs/javadoc-refs.json")
def logFile = new File(outputDir, "log/stone.log")
// delete output dir for a clean build
if (outputDir.exists()) {
if (!outputDir.deleteDir()) {
throw new GradleException("Failed to delete output directory: ${outputDir.absolutePath}")
}
}
srcOutputDir.mkdirs()
logFile.parentFile.mkdirs()
refsFile.parentFile.mkdirs()
boolean isFirst = true
for (StoneConfig c : configs) {
boolean append = !isFirst
if (c.dataTypesOnly) {
// generate only data types. This is a much simpler call
if (c.client) {
throw new GradleException("Cannot specify dataTypesOnly and clients for Stone generation.")
}
getExecOperations().exec {
standardOutput = new FileOutputStream(logFile, append)
commandLine pythonCommand, "-m", "stone.cli"
environment PYTHONPATH: stoneDir.absolutePath
if (isFirst) {
args "--clean-build"
}
if (c.routeWhitelistFilter) {
args "--route-whitelist-filter", config.routeWhitelistFilter
}
args generatorFile.absolutePath
args srcOutputDir.absolutePath
args specFiles.collect({ f -> f.absolutePath })
args "--"
args "--package", c.packageName
args "--data-types-only"
}
} else {
def client = c.client
def routeFilters = [c.globalRouteFilter, client.routeFilter]
def routeFilter = routeFilters\
.findAll { filter -> filter != null }\
.collect { filter -> "(${filter})" }\
.join " and "
getExecOperations().exec {
standardOutput = new FileOutputStream(logFile, append)
commandLine pythonCommand, "-m", "stone.cli"
environment PYTHONPATH: stoneDir.absolutePath
if (isFirst) {
args "--clean-build"
}
args "--attribute", ":all"
if (routeFilter) {
args "--filter-by-route-attr", routeFilter
}
if (c.routeWhitelistFilter) {
args "--route-whitelist-filter", c.routeWhitelistFilter
}
args generatorFile.absolutePath
args srcOutputDir.absolutePath
args specFiles.collect({ f -> f.absolutePath })
args "--"
args "--package", c.packageName
args "--javadoc-refs", refsFile.absolutePath
if (client.name) {
args "--client-class", client.name
}
if (client.javadoc != null) {
args "--client-javadoc", client.javadoc
}
if (client.requestsClassnamePrefix != null) {
args "--requests-classname-prefix", client.requestsClassnamePrefix
}
if (client.unusedClassesToGenerate != null) {
args "--unused-classes-to-generate", client.unusedClassesToGenerate
}
}
}
isFirst = false
}
}
}
// add generateStone task for all source sets (e.g. generateTestStone, etc)
project.sourceSets.all { SourceSet sourceSet ->
def taskName = "main" == sourceSet.name ? "generateStone" : "generate${sourceSet.name.capitalize()}Stone"
tasks.register(taskName, StoneTask) {
description "Generate Stone Java source files for ${sourceSet.name}."
def specDirPropName = "com.dropbox.api.${sourceSet.name}.specDir".toString()
def mySpecDir = project.properties.get(specDirPropName, "src/${sourceSet.name}/stone")
specDir mySpecDir
def routeWhitelistFilterPropName = "com.dropbox.api.${sourceSet.name}.routeWhitelistFilter".toString()
generatorDir 'generator'
stoneDir 'stone'
routeWhitelistFilter project.properties.get(routeWhitelistFilterPropName, null)
pythonCommand 'python'
outputDir "${project.buildDir}/generated/source/stone/${sourceSet.name}"
inputs.dir { project.fileTree(dir: generatorDir, exclude: '**/*.pyc') }.withPropertyName("stone").withPathSensitivity(PathSensitivity.RELATIVE)
inputs.dir(getSpecFiles(objects, getSpecDir().get())).withPathSensitivity(PathSensitivity.RELATIVE).withPropertyName("stoneSpec").skipWhenEmpty(true)
inputs.property "configs", { new groovy.json.JsonBuilder(getStoneConfigs().get()).toString() }
outputs.dir { getOutputDir().get() }.withPropertyName("generatedStone")
outputs.cacheIf { true }
}
sourceSet.java.srcDir project.tasks."${taskName}".getOutputDir().get().toString() + "/src"
Task compile = project.tasks.getByName(sourceSet.getCompileTaskName("java"))
compile.dependsOn project.tasks."${taskName}"
}
generateStone {
String unusedClassesToGenerate = 'AuthError, PathRoot, PathRootError, AccessError, RateLimitError'
String packageName = 'com.dropbox.core.v2'
String globalRouteFilter = 'alpha_group=null and beta_group=null'
config [
new StoneConfig(
packageName: packageName,
globalRouteFilter: globalRouteFilter,
client: new ClientSpec(
name: 'DbxClientV2Base',
javadoc: 'Base class for user auth clients.',
requestsClassnamePrefix: "DbxUser",
routeFilter: 'auth="user" or auth="noauth" or auth="app, user"',
unusedClassesToGenerate: unusedClassesToGenerate,
),
),
new StoneConfig(
packageName: packageName,
globalRouteFilter: globalRouteFilter,
client: new ClientSpec(
name: 'DbxTeamClientV2Base',
javadoc: 'Base class for team auth clients.',
requestsClassnamePrefix: 'DbxTeam',
routeFilter: 'auth="team"',
),
),
new StoneConfig(
packageName: packageName,
globalRouteFilter: globalRouteFilter,
client: new ClientSpec(
name: 'DbxAppClientV2Base',
javadoc: 'Base class for app auth clients.',
requestsClassnamePrefix: "DbxApp",
routeFilter: 'auth="app" or auth="app, user"',
)
),
]
}
generateTestStone {
String packageName = 'com.dropbox.core.stone'
config [
new StoneConfig(
packageName: packageName,
dataTypesOnly: true,
),
new StoneConfig(
packageName: packageName,
client: new ClientSpec(
name: 'DbxClientV2Base',
javadoc: 'TestClass.',
requestsClassnamePrefix: "DbxTest",
)
),
]
}