Skip to content

Commit 5549559

Browse files
authored
Initial commit for the probuf compiler plugin. (#2364)
* Initial commit for the probuf compiler plugin. It consists of a fat jar can be used instead or in addition to standard codegen. The jar can be consumed via an absolute path to it(as is done in the `tests` project) or as a maven artifact if available in one of the configured repositories. * Copyright fix. * Address review comments. * Review comments.
1 parent 4b57862 commit 5549559

File tree

7 files changed

+173
-1
lines changed

7 files changed

+173
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ buildscript {
2929
}
3030

3131
dependencies {
32-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
32+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
3333
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:1.3.0'
3434
classpath 'org.jsoup:jsoup:1.13.1'
3535
classpath 'gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.9'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id "org.jetbrains.kotlin.jvm"
17+
id "java-library"
18+
id "com.github.johnrengelman.shadow" version "5.2.0"
19+
}
20+
21+
jar {
22+
manifest.attributes "Main-Class": "com.google.firebase.encoders.proto.codegen.MainKt"
23+
}
24+
25+
dependencies {
26+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72"
27+
28+
implementation "com.google.protobuf:protobuf-java:3.11.4"
29+
implementation 'com.squareup:javapoet:1.13.0'
30+
implementation 'com.google.guava:guava:30.0-jre'
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.encoders.proto.codegen
16+
17+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest
18+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse
19+
20+
class CodeGenerator(val request: CodeGeneratorRequest) {
21+
fun generate(): CodeGeneratorResponse = CodeGeneratorResponse.getDefaultInstance()
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.encoders.proto.codegen
16+
17+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest
18+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse
19+
import java.io.PrintWriter
20+
import java.io.StringWriter
21+
22+
/**
23+
* Main entry point to the executable jar.
24+
*
25+
* According to protoc plugin spec, this function is invoked with no arguments and receives a
26+
* serialized [CodeGeneratorRequest] via `stdin` and must return a [CodeGeneratorResponse] via
27+
* `stdout`.
28+
*/
29+
fun main(args: Array<String>) {
30+
val request = CodeGeneratorRequest.parseFrom(System.`in`)
31+
runCatching {
32+
CodeGenerator(request).generate().writeTo(System.out)
33+
}.onFailure {
34+
val stringWriter = StringWriter()
35+
it.printStackTrace(PrintWriter(stringWriter))
36+
CodeGeneratorResponse.newBuilder()
37+
.setError(stringWriter.toString())
38+
.build()
39+
.writeTo(System.out)
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2021 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
syntax = "proto3";
17+
18+
package com.firebase.testing;
19+
20+
message TestMessage {
21+
int32 foo = 1;
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id "java-library"
17+
id 'com.google.protobuf'
18+
}
19+
20+
21+
// add a dependency on the protoc plugin's fat jar to make it available to protobuf below.
22+
configurations.create("protobuild")
23+
dependencies {
24+
protobuild project(path: ":encoders:protoc-gen-firebase-encoders", configuration: "shadow")
25+
}
26+
27+
protobuf {
28+
protoc {
29+
artifact = "com.google.protobuf:protoc:$protocVersion"
30+
}
31+
plugins {
32+
firebaseEncoders {
33+
path = configurations.protobuild.asPath
34+
}
35+
}
36+
generateProtoTasks {
37+
all().each { task ->
38+
task.builtins {
39+
remove java
40+
}
41+
task.dependsOn configurations.protobuild
42+
task.plugins {
43+
firebaseEncoders {}
44+
}
45+
}
46+
}
47+
}
48+
49+
dependencies {
50+
51+
implementation project(":encoders:firebase-encoders")
52+
implementation project(":encoders:firebase-encoders-proto")
53+
annotationProcessor project(":encoders:firebase-encoders-processor")
54+
}

subprojects.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ encoders:firebase-encoders-processor
4949
encoders:firebase-encoders-proto
5050
encoders:firebase-encoders-reflective
5151
encoders:firebase-decoders-json
52+
encoders:protoc-gen-firebase-encoders
53+
encoders:protoc-gen-firebase-encoders:tests
5254

5355
integ-testing
5456

0 commit comments

Comments
 (0)