forked from dropbox/dropbox-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstone.gradle
217 lines (190 loc) · 7.85 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
apply plugin: 'java'
class StoneConfig {
String packageName = 'com.dropbox.stone'
String globalRouteFilter = null
boolean dataTypesOnly = false
ClientSpec client = null
String routeWhitelistFilter = null
}
class ClientSpec {
String name = null
String javadoc = null
String routeFilter = null
String requestsClassnamePrefix = null
String unusedClassesToGenerate = null
}
def runStoneGenerator(List<StoneConfig> configs,
File stoneDir,
File generatorFile,
Collection<File> specFiles,
File outputDir
) {
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.")
}
project.exec {
standardOutput = new FileOutputStream(logFile, append)
commandLine "python", "-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 "
project.exec {
standardOutput = new FileOutputStream(logFile, append)
commandLine "python", "-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"
task "${taskName}" {
description "Generate Stone Java source files for ${sourceSet.name}."
def specDirPropName = "com.dropbox.api.${sourceSet.name}.specDir".toString()
def routeWhitelistFilterPropName = "com.dropbox.api.${sourceSet.name}.routeWhitelistFilter".toString()
ext {
configs = []
generatorDir = 'generator'
stoneDir = 'stone'
specDir = project.properties.get(specDirPropName, "src/${sourceSet.name}/stone")
outputDir = "${project.buildDir}/generated/source/stone/${sourceSet.name}"
routeWhitelistFilter = project.properties.get(routeWhitelistFilterPropName, null)
}
def getSpecFiles = { fileTree(dir: specDir, include: '**/*.stone') }
inputs.dir { project.fileTree(dir: generatorDir, exclude: '**/*.pyc') }
inputs.dir(getSpecFiles).skipWhenEmpty = true
inputs.property "configs", { new groovy.json.JsonBuilder(configs).toString() }
outputs.dir { outputDir }
doLast {
def generatorFile = fileTree(dir: generatorDir, include: '**/*stoneg.py').getSingleFile()
def specFiles = getSpecFiles().getFiles()
for (StoneConfig config in configs) {
config.routeWhitelistFilter = routeWhitelistFilter
}
runStoneGenerator configs, file(stoneDir), generatorFile, specFiles, file(outputDir)
}
}
sourceSet.java.srcDir project.tasks."${taskName}".outputDir + "/src"
Task compile = project.tasks.getByName(sourceSet.getCompileTaskName("java"))
compile.dependsOn project.tasks."${taskName}"
}
generateStone {
String unusedClassesToGenerate = 'PathRoot, PathRootError, AccessError, RateLimitError'
String packageName = 'com.dropbox.core.v2'
String globalRouteFilter = 'alpha_group=null and beta_group=null'
configs = [
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"',
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"',
)
),
]
}
generateTestStone {
String packageName = 'com.dropbox.core.stone'
configs = [
new StoneConfig(
packageName: packageName,
dataTypesOnly: true,
),
new StoneConfig(
packageName: packageName,
client: new ClientSpec(
name: 'DbxClientV2Base',
javadoc: 'TestClass.',
requestsClassnamePrefix: "DbxTest",
)
),
]
}