Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][codegen] feat: add package-info.java codegen #414

Merged
merged 19 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ceaaddb
fix: refactor requestBuilder into separate method in ServiceClientCla…
miraleung Oct 7, 2020
fba69cb
Merge branch 'master' of github.com:googleapis/gapic-generator-java i…
miraleung Oct 8, 2020
32672f2
fix: merge master
miraleung Oct 21, 2020
bdcb62e
feat: add varargs to AnonClass and ref setter methods
miraleung Oct 21, 2020
e15e115
feat: add HTTP annotation parsing/validation
miraleung Oct 21, 2020
28d3b3c
feat: Generate RequestParamsExtractor in GrpcServiceStub
miraleung Oct 21, 2020
5293551
feat: add GrpcPublisherStub test to exercise HTTP subfields
miraleung Oct 21, 2020
2a50453
fix: add ByteString to DefaultValueComposer
miraleung Oct 21, 2020
e4684f3
fix: Use repeated field name for paged RPC unit tests
miraleung Oct 21, 2020
7829850
fix: refactor exception field, use paged repeated field name, add pub…
miraleung Oct 22, 2020
11c38bd
fix: ensure all testgen methods throw Exceptions
miraleung Oct 22, 2020
d3ec180
fix: Fix resname helper method names for of* and format*
miraleung Oct 22, 2020
6416984
fix: use only generated resnames in codegen
miraleung Oct 22, 2020
96046a6
fix: propagate of*Name changes to resname codegen
miraleung Oct 22, 2020
297f64a
fix: fix method arg resname mappings, add logging test
miraleung Oct 23, 2020
2691d8c
fix: ensure paged tests use the right repeated resp. type
miraleung Oct 23, 2020
01b8051
feat: add PackageInfoDefinition AST node
miraleung Oct 23, 2020
0d83a14
feat: add package-info.java codegen
miraleung Oct 23, 2020
aa08128
Merge branch 'master' into gp/g14
miraleung Oct 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public abstract class PackageInfoDefinition implements AstNode {

public abstract ImmutableList<AnnotationNode> annotations();

public abstract Builder toBuilder();

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/google/api/generator/gapic/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.api.generator.gapic.composer.Composer;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.gapic.protowriter.Writer;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
Expand All @@ -27,8 +28,9 @@ public class Generator {
public static CodeGeneratorResponse generateGapic(CodeGeneratorRequest request) {
GapicContext context = Parser.parse(request);
List<GapicClass> clazzes = Composer.composeServiceClasses(context);
GapicPackageInfo packageInfo = Composer.composePackageInfo(context);
String outputFilename = "temp-codegen.srcjar";
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilename);
CodeGeneratorResponse response = Writer.writeCode(clazzes, packageInfo, outputFilename);
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.composer;

import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.PackageInfoDefinition;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.common.base.Preconditions;
import javax.annotation.Generated;

public class ClientLibraryPackageInfoComposer {
public static GapicPackageInfo generatePackageInfo(GapicContext context) {
Preconditions.checkState(!context.services().isEmpty(), "No services found to generate");
// Pick some service's package, as we assume they are all the same.
String libraryPakkage = context.services().get(0).pakkage();

PackageInfoDefinition packageInfo =
PackageInfoDefinition.builder()
.setPakkage(libraryPakkage)
.setAnnotations(
AnnotationNode.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Generated.class)))
.setDescription("by gapic-generator-java")
.build())
.build();
return GapicPackageInfo.with(packageInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.ResourceName;
Expand Down Expand Up @@ -49,6 +50,10 @@ public static List<GapicClass> composeServiceClasses(GapicContext context) {
return addApacheLicense(clazzes);
}

public static GapicPackageInfo composePackageInfo(GapicContext context) {
return addApacheLicense(ClientLibraryPackageInfoComposer.generatePackageInfo(context));
}

public static List<GapicClass> generateServiceClasses(
@Nonnull Service service,
@Nullable GapicServiceConfig serviceConfig,
Expand Down Expand Up @@ -131,4 +136,13 @@ protected static List<GapicClass> addApacheLicense(List<GapicClass> gapicClassLi
})
.collect(Collectors.toList());
}

private static GapicPackageInfo addApacheLicense(GapicPackageInfo gapicPackageInfo) {
return GapicPackageInfo.with(
gapicPackageInfo
.packageInfo()
.toBuilder()
.setFileHeader(CommentComposer.APACHE_LICENSE_COMMENT)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.model;

import com.google.api.generator.engine.ast.PackageInfoDefinition;
import com.google.auto.value.AutoValue;

/** A thin wrapper around PackageInfoDefinition to maintain a clean separation of concerns. */
@AutoValue
public abstract class GapicPackageInfo {
public abstract PackageInfoDefinition packageInfo();

public static GapicPackageInfo with(PackageInfoDefinition packageInfo) {
return builder().setPackageInfo(packageInfo).build();
}

static Builder builder() {
return new AutoValue_GapicPackageInfo.Builder();
}

@AutoValue.Builder
abstract static class Builder {
abstract Builder setPackageInfo(PackageInfoDefinition packageInfo);

abstract GapicPackageInfo build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package com.google.api.generator.gapic.protowriter;

import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.PackageInfoDefinition;
import com.google.api.generator.engine.writer.JavaWriterVisitor;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.protobuf.ByteString;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import java.io.IOException;
Expand All @@ -31,7 +33,8 @@ public GapicWriterException(String errorMessage) {
}
}

public static CodeGeneratorResponse writeCode(List<GapicClass> clazzes, String outputFilePath) {
public static CodeGeneratorResponse writeCode(
List<GapicClass> clazzes, GapicPackageInfo gapicPackageInfo, String outputFilePath) {
ByteString.Output output = ByteString.newOutput();
JavaWriterVisitor codeWriter = new JavaWriterVisitor();
JarOutputStream jos = null;
Expand Down Expand Up @@ -62,6 +65,21 @@ public static CodeGeneratorResponse writeCode(List<GapicClass> clazzes, String o
}
}

// Write the package info.
PackageInfoDefinition packageInfo = gapicPackageInfo.packageInfo();
packageInfo.accept(codeWriter);
String code = codeWriter.write();
codeWriter.clear();

String path = "src/main/java/" + packageInfo.pakkage().replaceAll("\\.", "/");
JarEntry jarEntry = new JarEntry(String.format("%s/package-info.java", path));
try {
jos.putNextEntry(jarEntry);
jos.write(code.getBytes());
} catch (IOException e) {
throw new GapicWriterException("Could not write code for package-info.java");
}

try {
jos.finish();
jos.flush();
Expand Down