Skip to content

Commit

Permalink
[ggj][codegen] feat: add package-info.java codegen (#414)
Browse files Browse the repository at this point in the history
* fix: refactor requestBuilder into separate method in ServiceClientClassComposer

* feat: add varargs to AnonClass and ref setter methods

* feat: add HTTP annotation parsing/validation

* feat: Generate RequestParamsExtractor in GrpcServiceStub

* feat: add GrpcPublisherStub test to exercise HTTP subfields

* fix: add ByteString to DefaultValueComposer

* fix: Use repeated field name for paged RPC unit tests

* fix: refactor exception field, use paged repeated field name, add pubsub client test

* fix: ensure all testgen methods throw Exceptions

* fix: Fix resname helper method names for of* and format*

* fix: use only generated resnames in codegen

* fix: propagate of*Name changes to resname codegen

* fix: fix method arg resname mappings, add logging test

* fix: ensure paged tests use the right repeated resp. type

* feat: add PackageInfoDefinition AST node

* feat: add package-info.java codegen
  • Loading branch information
miraleung authored Oct 25, 2020
1 parent 7cbe2cd commit 9923680
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
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

0 comments on commit 9923680

Please sign in to comment.