Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -14,7 +14,6 @@

package com.google.api.generator.spring;

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;
Expand All @@ -28,7 +27,7 @@ public class SpringGenerator {
public static CodeGeneratorResponse generateSpring(CodeGeneratorRequest request) {
GapicContext context = Parser.parse(request);
List<GapicClass> clazzes = SpringComposer.composeServiceAutoConfigClasses(context);
GapicPackageInfo packageInfo = Composer.composePackageInfo(context);
GapicPackageInfo packageInfo = SpringComposer.composePackageInfo(context);
String outputFilename = "temp-codegen-spring.srcjar";
return SpringWriter.write(context, clazzes, packageInfo, outputFilename);
}
Expand Down
25 changes: 3 additions & 22 deletions src/main/java/com/google/api/generator/spring/SpringWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.api.generator.spring.utils.Utils;
import com.google.protobuf.ByteString;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand Down Expand Up @@ -61,8 +60,8 @@ public static CodeGeneratorResponse write(
writeAutoConfigRegistration(context, jos);
writeSpringAdditionalMetadataJson(context, jos);

// TODO: metadata and package info not custimized for Spring
writeMetadataFile(context, writePackageInfo(gapicPackageInfo, codeWriter, jos), jos);
// write package-info.java
writePackageInfo(gapicPackageInfo, codeWriter, jos);

try {
jos.finish();
Expand Down Expand Up @@ -111,8 +110,7 @@ private static String writePackageInfo(
String code = codeWriter.write();
codeWriter.clear();

String packagePath =
"src/main/java/" + packageInfo.pakkage().replaceAll("\\.", "/") + "/spring";
String packagePath = "src/main/java/" + packageInfo.pakkage().replaceAll("\\.", "/");
JarEntry jarEntry = new JarEntry(String.format("%s/package-info.java", packagePath));
try {
jos.putNextEntry(jarEntry);
Expand All @@ -123,19 +121,6 @@ private static String writePackageInfo(
return packagePath;
}

private static void writeMetadataFile(GapicContext context, String path, JarOutputStream jos) {
if (context.gapicMetadataEnabled()) {
JarEntry jarEntry = new JarEntry(String.format("%s/gapic_metadata.json", path));
try {
jos.putNextEntry(jarEntry);
jos.write(
JsonFormat.printer().print(context.gapicMetadata()).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new GapicWriterException("Could not write gapic_metadata.json", e);
}
}
}

private static void writeAutoConfigRegistration(GapicContext context, JarOutputStream jos) {
String path = "src/main/resources/META-INF/spring";
JarEntry jarEntry =
Expand Down Expand Up @@ -202,8 +187,4 @@ private static String getPath(String pakkage, String className) {
}
return path;
}

private static String getSamplePackage(GapicClass gapicClazz) {
return gapicClazz.classDefinition().packageString().concat(".samples");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.gapic.composer.comment.CommentComposer;
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.model.Transport;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,6 +31,10 @@ public static List<GapicClass> composeServiceAutoConfigClasses(GapicContext cont
return addApacheLicense(clazzes);
}

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

protected static List<GapicClass> generatePerServiceClasses(GapicContext context) {
List<GapicClass> clazzes = new ArrayList<>();
context
Expand Down Expand Up @@ -59,4 +64,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,55 @@
// Copyright 2022 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.spring.composer;

import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.JavaDocComment;
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.api.generator.spring.utils.Utils;
import com.google.common.base.Preconditions;
import javax.annotation.Generated;

public class SpringPackageInfoComposer {
private static final String PACKAGE_INFO_TITLE_PATTERN =
"Spring Boot auto-configurations for %s.";

public static GapicPackageInfo generatePackageInfo(GapicContext context) {
Preconditions.checkState(!context.services().isEmpty(), "No services found to generate");
PackageInfoDefinition packageInfo =
PackageInfoDefinition.builder()
.setPakkage(Utils.getSpringPackageName(Utils.getPackageName(context)))
.setHeaderCommentStatements(createPackageInfoJavadoc(context))
.setAnnotations(
AnnotationNode.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Generated.class)))
.setDescription("by gapic-generator-java")
.build())
.build();
return GapicPackageInfo.with(packageInfo);
}

private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder();
javaDocCommentBuilder =
javaDocCommentBuilder.addComment(
String.format(PACKAGE_INFO_TITLE_PATTERN, Utils.getLibName(context)));
return CommentStatement.withComment(javaDocCommentBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

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

import com.google.api.generator.engine.writer.JavaWriterVisitor;
import com.google.api.generator.gapic.composer.common.TestProtoLoader;
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.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -34,11 +39,20 @@ public void setUp() {
public void spring_composer_test() {

List<GapicClass> gapicClasses = SpringComposer.composeServiceAutoConfigClasses(context);
GapicPackageInfo packageInfo = SpringComposer.composePackageInfo(context);

// write to verify result for now
for (GapicClass gapicClazz : gapicClasses) {
String fileName = gapicClazz.classDefinition().classIdentifier() + "Full.golden";
Assert.assertGoldenClass(this.getClass(), gapicClazz, fileName);
}

String packageInfoFileName = "SpringPackageInfoFull.golden";
JavaWriterVisitor visitor = new JavaWriterVisitor();
packageInfo.packageInfo().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), packageInfoFileName, visitor.write());
Path packageInfoGoldenFilePath =
Paths.get(Utils.getGoldenDir(this.getClass()), packageInfoFileName);
Assert.assertCodeEquals(packageInfoGoldenFilePath, visitor.write());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 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.spring.composer;

import com.google.api.generator.engine.writer.JavaWriterVisitor;
import com.google.api.generator.gapic.composer.common.TestProtoLoader;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Test;

public class SpringPackageInfoComposerTest {
private GapicContext context;

@Before
public void setUp() {
this.context = TestProtoLoader.instance().parseShowcaseEcho();
}

@Test
public void generateSpringPackageInfoTest() {
GapicPackageInfo packageInfo = SpringPackageInfoComposer.generatePackageInfo(this.context);
JavaWriterVisitor visitor = new JavaWriterVisitor();
packageInfo.packageInfo().accept(visitor);
String fileName = "SpringPackageInfo.golden";

Utils.saveCodegenToFile(this.getClass(), fileName, visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), fileName);
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Spring Boot auto-configurations for showcase. */
@Generated("by gapic-generator-java")
package com.google.showcase.v1beta1.spring;

import javax.annotation.Generated;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 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
*
* https://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.
*/

/** Spring Boot auto-configurations for showcase. */
@Generated("by gapic-generator-java")
package com.google.showcase.v1beta1.spring;

import javax.annotation.Generated;