forked from dropbox/dropbox-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stone.gradle
190 lines (163 loc) · 6.7 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
apply plugin: 'java'
class StoneConfig {
String packageName = 'com.dropbox.stone'
String globalRouteFilter = null
boolean dataTypesOnly = false
List<ClientSpec> clients = []
String routeWhitelistFilter = null
}
class ClientSpec {
String name = null
String javadoc = null
String routeFilter = null
String requestsClassnamePrefix = null
String unusedClassesToGenerate = null
}
def runStoneGenerator(StoneConfig config,
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()
if (config.dataTypesOnly && config.clients) {
throw new GradleException("Cannot specify dataTypesOnly and clients for Stone generation.")
}
// generate only data types. This is a much simpler call
if (config.dataTypesOnly) {
project.exec {
standardOutput = new FileOutputStream(logFile)
commandLine "python", "-m", "stone.cli"
environment PYTHONPATH: stoneDir.absolutePath
args "--clean-build"
if (config.routeWhitelistFilter) {
args "--route-whitelist-filter", config.routeWhitelistFilter
}
args generatorFile.absolutePath
args srcOutputDir.absolutePath
args specFiles.collect({ f -> f.absolutePath })
args "--"
args "--package", config.packageName
args "--data-types-only"
}
return
}
for (int i = 0; i < config.clients.size; ++i) {
def client = config.clients[i]
def isFirst = i == 0
def append = !isFirst
def routeFilters = [config.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 (config.routeWhitelistFilter) {
args "--route-whitelist-filter", config.routeWhitelistFilter
}
args generatorFile.absolutePath
args srcOutputDir.absolutePath
args specFiles.collect({ f -> f.absolutePath })
args "--"
args "--package", config.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
}
}
}
}
// add generateStone task for all source sets (e.g. generateTestStone, etc)
project.sourceSets.all { SourceSet sourceSet ->
def taskName = "main".equals(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 {
config = new StoneConfig()
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 "config", { new groovy.json.JsonBuilder(config).toString() }
outputs.dir { outputDir }
doLast {
def generatorFile = fileTree(dir: generatorDir, include: '**/*stoneg.py').getSingleFile()
def specFiles = getSpecFiles().getFiles()
config.routeWhitelistFilter = routeWhitelistFilter
runStoneGenerator config, 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'
config = new StoneConfig(
packageName: 'com.dropbox.core.v2',
globalRouteFilter: 'alpha_group=null and beta_group=null',
clients: [
new ClientSpec(
name: 'DbxClientV2Base',
javadoc: 'Base class for user auth clients.',
requestsClassnamePrefix: "DbxUser",
routeFilter: 'auth="user" or auth="noauth"',
unusedClassesToGenerate: unusedClassesToGenerate,
),
new ClientSpec(
name: 'DbxTeamClientV2Base',
javadoc: 'Base class for team auth clients.',
requestsClassnamePrefix: 'DbxTeam',
routeFilter: 'auth="team"',
),
new ClientSpec(
name: 'DbxAppClientV2Base',
javadoc: 'Base class for app auth clients.',
requestsClassnamePrefix: "DbxApp",
routeFilter: 'auth="app"',
)
],
)
}
generateTestStone {
config = new StoneConfig(
packageName: 'com.dropbox.core.stone',
dataTypesOnly: true,
)
}