forked from google/protobuf-gradle-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_base.gradle
129 lines (112 loc) · 3.81 KB
/
build_base.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
// This build is not a complete project, but is used to generate a project.
// See: ProtobufPluginTestHelper.groovy
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
configurations {
grpcCompile
}
sourceSets {
grpc {
compileClasspath += configurations.grpcCompile
}
test {
compileClasspath += grpc.output
runtimeClasspath += grpc.output
}
}
def protobufDep = 'com.google.protobuf:protobuf-java:3.0.0'
dependencies {
protobuf files("lib/protos.tar.gz")
protobuf files("ext/")
testProtobuf files("lib/protos-test.tar.gz")
compile protobufDep
testCompile 'junit:junit:4.12'
// KotlinFooTest.kt requires reflection utilities
testCompile "org.jetbrains.kotlin:kotlin-reflect:1.2.0"
grpcCompile protobufDep
grpcCompile 'io.grpc:grpc-stub:1.0.0-pre2'
grpcCompile 'io.grpc:grpc-protobuf:1.0.0-pre2'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
}
}
generateProtoTasks {
ofSourceSet('grpc').each { task ->
task.plugins {
grpc {
outputSubDir = 'grpc_output'
}
}
task.generateDescriptorSet = true
}
}
}
// To include all sourceSets in the project jar
jar {
sourceSets.all { sourceSet ->
from sourceSet.output
dependsOn sourceSet.getCompileTaskName('java')
}
}
def assertJavaCompileHasProtoGeneratedDir(String sourceSet, Collection<String> codegenPlugins) {
def compileJavaTask = tasks.getByName(sourceSets.getByName(sourceSet).getCompileTaskName("java"))
assertJavaCompileHasProtoGeneratedDir(project, sourceSet, compileJavaTask, codegenPlugins)
}
def assertFileExists(boolean exists, String path) {
if (exists) {
assert (path as File).exists()
} else {
assert !(path as File).exists()
}
}
test.doLast {
assert ['generateProto', 'generateGrpcProto', 'generateTestProto'] as Set ==
protobuf.generateProtoTasks.all().collect({ it.name }) as Set
assert ['generateProto'] as Set == protobuf.generateProtoTasks.ofSourceSet('main').collect({ it.name }) as Set
assertJavaCompileHasProtoGeneratedDir('main', ['java'])
assertJavaCompileHasProtoGeneratedDir('test', ['java'])
assertJavaCompileHasProtoGeneratedDir('grpc', ['java', 'grpc_output'])
// Check generateDescriptorSet option has been honored
['main', 'test'].each { sourceSet ->
assertFileExists(false, "$buildDir/generated/source/proto/$sourceSet/descriptor_set.desc")
}
assertFileExists(true, "$buildDir/generated/source/proto/grpc/descriptor_set.desc")
}
rootProject.ext {
// Shared test utility. Checks a JavaCompile task for the given sourceSet
// includes the generated source dirs for the given codegenPlugins, and does
// not include any other dirs under the generated code base dir.
assertJavaCompileHasProtoGeneratedDir = {
Project project, String sourceSet, JavaCompile compileJavaTask, Collection<String> codegenPlugins ->
def baseDir = "${project.buildDir}/generated/source/proto/$sourceSet" as File
// The expected direct subdirectories under baseDir
def expectedDirs = codegenPlugins.collect { codegenPlugin ->
"${project.buildDir}/generated/source/proto/$sourceSet/$codegenPlugin" as File
} as Set
def actualDirs = new HashSet()
compileJavaTask.source.visit { fileVisitDetails ->
// If the visited file is or is under a direct subdirectory of baseDir, add
// that subdirectory to actualDirs.
def file = fileVisitDetails.file
while (true) {
if (file.parentFile == baseDir) {
actualDirs.add file
}
if (file.parentFile == null) {
break
}
file = file.parentFile
}
}
assert expectedDirs == actualDirs
}
}