-
Notifications
You must be signed in to change notification settings - Fork 982
Expand file tree
/
Copy pathjava-rpc-thrift.gradle
More file actions
189 lines (163 loc) · 6.38 KB
/
java-rpc-thrift.gradle
File metadata and controls
189 lines (163 loc) · 6.38 KB
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
def libDir = buildscript.sourceFile.parentFile
configure(projectsWithFlags('java')) {
def thriftJsonEnabled = true
def fullCamel = false
def annotationsAsMetadata = false
ext {
thriftVersion = null
thriftPath = null
enableThriftFullCamel = { fullCamel = true }
disableThriftJson = { thriftJsonEnabled = false }
enableAnnotationsAsMetadata = {annotationsAsMetadata = true}
}
// TODO(anuraaga): Consider replacing with https://github.com/yodle/griddle
project.sourceSets.all { sourceSet ->
def scope = sourceSet.name
// Create the task assuming the source directory exists
// so that subprojects can refer to the task.
def task = project.tasks.register(
(sourceSet.getTaskName('compile', 'thrift')) as String,
CompileThriftTask,
)
def sourcesJarTask = project.tasks.findByName(sourceSet.getTaskName('sources', 'jar'))
if (sourcesJarTask) {
sourcesJarTask.dependsOn(task)
}
// Use afterEvaluate to give subprojects a chance to override the properties.
project.afterEvaluate {
def srcDirs = project.findProperty(sourceSet.getTaskName('', 'thriftSrcDirs'))
if (srcDirs == null) {
def defaultSrcDir = "${projectDir}/src/${scope}/thrift"
if (!project.file(defaultSrcDir).isDirectory()) {
// Disable the compile*Thrift task which turned out to be unnecessary.
task.configure { enabled = false }
return
}
srcDirs = [defaultSrcDir]
}
if (!(srcDirs instanceof Iterable) || srcDirs instanceof CharSequence) {
srcDirs = [srcDirs]
}
srcDirs = project.files(srcDirs)
def includeDirs = project.findProperty(sourceSet.getTaskName('', 'thriftIncludeDirs')) ?: []
if (!(includeDirs instanceof Iterable) || includeDirs instanceof CharSequence) {
includeDirs = [includeDirs]
}
includeDirs = project.files(includeDirs)
def javaOutputDir = project.file("${project.ext.genSrcDir}/${scope}/javaThrift")
if (!sourceSet.java.srcDirs.contains(javaOutputDir)) {
sourceSet.java.srcDir javaOutputDir
}
def jsonOutputDir = project.file("${project.ext.genSrcDir}/${scope}/resources")
task.configure {
// configure inputs and outputs
it.libDir = libDir
it.srcDirs.addAll(srcDirs)
it.includeDirs.addAll(includeDirs)
it.thriftJsonEnabled = thriftJsonEnabled
it.fullCamel = fullCamel
it.annotationsAsMetadata = annotationsAsMetadata
it.thriftBinary = project.ext.thriftPath != null ? project.file(project.ext.thriftPath) : null
it.thriftVersion = project.ext.thriftVersion
it.javaOutputDir = javaOutputDir
it.jsonOutputDir = jsonOutputDir
}
def processResourcesTask = tasks.findByName(sourceSet.getTaskName('process', 'resources'))
if (processResourcesTask != null) {
processResourcesTask.dependsOn(task)
}
def compileTask = tasks.findByName(sourceSet.getCompileTaskName('java'))
if (compileTask != null) {
compileTask.dependsOn(task)
}
project.ext.getGenerateSourcesTask().dependsOn(task)
}
}
}
@CacheableTask
abstract class CompileThriftTask extends DefaultTask {
@javax.inject.Inject
protected abstract ExecOperations getExecOperations()
@Override
String getGroup() {
return 'build'
}
@Override
String getDescription() {
return "Compiles the ${name} .thrift files."
}
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
def srcDirs = new ArrayList<File>()
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
def includeDirs = new ArrayList<File>()
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
File libDir
@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
@Optional
File thriftBinary
@Input
@Optional
String thriftVersion
@Input
Boolean thriftJsonEnabled
@Input
Boolean fullCamel
@Input
Boolean annotationsAsMetadata
@OutputDirectory
File javaOutputDir
@OutputDirectory
File jsonOutputDir
@TaskAction
def execute() {
def actualThriftPath
if (thriftBinary != null) {
actualThriftPath = thriftBinary.path
} else {
actualThriftPath =
"${libDir}/thrift" +
"/${thriftVersion?: '0.18'}" +
"/thrift.${project.rootProject.osdetector.classifier}"
}
srcDirs.each { srcDir ->
project.fileTree(srcDir) {
include '**/*.thrift'
}.each { sourceFile ->
logger.info("Using ${actualThriftPath} to generate Java sources from ${sourceFile}")
project.mkdir(javaOutputDir)
def javaGenerator = 'java'
if (fullCamel) {
javaGenerator += ':fullcamel'
}
if (annotationsAsMetadata) {
javaGenerator += ':annotations_as_metadata'
}
execOperations.exec {
commandLine actualThriftPath
args '-gen', javaGenerator, '-out', javaOutputDir
includeDirs.each {
args '-I', it
}
args sourceFile.absolutePath
}
if (thriftJsonEnabled) {
logger.info("Using ${actualThriftPath} to generate JSON from ${sourceFile}")
def outDir = "${jsonOutputDir}/META-INF/armeria/thrift"
project.mkdir(outDir)
execOperations.exec {
commandLine actualThriftPath
args '-gen', 'json', '-out', outDir
includeDirs.each {
args '-I', it
}
args sourceFile.absolutePath
}
}
}
}
}
}