Skip to content

Add service version info #6197

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-382b343.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Adding constant to each SDK module to represent its version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask;
import software.amazon.awssdk.codegen.poet.client.EnvironmentTokenSystemSettingsClass;
import software.amazon.awssdk.codegen.poet.client.SdkClientOptions;
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
import software.amazon.awssdk.codegen.poet.common.UserAgentUtilsSpec;

public class CommonInternalGeneratorTasks extends BaseGeneratorTasks {
Expand All @@ -40,6 +41,7 @@ protected List<GeneratorTask> createTasks() throws Exception {
if (params.getModel().getCustomizationConfig().isEnableEnvironmentBearerToken()) {
tasks.add(createEnvironmentTokenSystemSettingTask());
}
tasks.add(createServiceVersionInfoTask());
return tasks;
}

Expand All @@ -58,6 +60,11 @@ private GeneratorTask createEnvironmentTokenSystemSettingTask() {
new EnvironmentTokenSystemSettingsClass(params.getModel()));
}

private GeneratorTask createServiceVersionInfoTask() {
return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(),
new ServiceVersionInfoSpec(params.getModel()));
}

private String clientOptionsDir() {
return params.getPathProvider().getClientInternalDirectory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public ClassName getUserAgentClass() {
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "UserAgentUtils");
}

public ClassName getServiceVersionInfoClass() {
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "ServiceVersionInfo");
}

public ClassName getEnvironmentTokenSystemSettingsClass() {
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "EnvironmentTokenSystemSettings");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.poet.client.specs;

import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;

public class ServiceVersionInfoSpec implements ClassSpec {
private final PoetExtension poetExtension;

public ServiceVersionInfoSpec(IntermediateModel model) {
this.poetExtension = new PoetExtension(model);
}

@Override
public TypeSpec poetSpec() {
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addAnnotation(PoetUtils.generatedAnnotation())
.addField(FieldSpec.builder(
String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", SDK_VERSION)
.addJavadoc("Returns the current version for the AWS SDK in which"
+ " this class is running.")
.build())
.addMethod(privateConstructor());

return builder.build();
}

protected MethodSpec privateConstructor() {
return MethodSpec.constructorBuilder()
.addModifiers(Modifier.PRIVATE)
.build();
}

@Override
public ClassName className() {
return poetExtension.getServiceVersionInfoClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.poet.client;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import java.io.InputStream;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.ClientTestModels;
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
import software.amazon.awssdk.core.util.VersionInfo;

public class ServiceVersionInfoSpecTest {

// Fixture test that compares generated ServiceVersionInfo class against expected output.
// The fixture file uses {{VERSION}} as a placeholder which gets replaced with the current
// SDK version at test time, since the generated code injects the actual version at build time.
@Test
void testServiceVersionInfoClass() {
String currVersion = VersionInfo.SDK_VERSION;
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels());

String expectedContent = loadFixtureFile("test-service-version-info-class.java");
expectedContent = expectedContent.replace("{{VERSION}}", currVersion);

String actualContent = generateContent(serviceVersionInfoSpec);

assertThat(actualContent).isEqualToIgnoringWhitespace(expectedContent);
}

private String loadFixtureFile(String filename) {
InputStream is = getClass().getResourceAsStream("specs/" + filename);
return new Scanner(is).useDelimiter("\\A").next();
}

private String generateContent(ClassSpec spec) {
TypeSpec typeSpec = spec.poetSpec();
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build();
return javaFile.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package software.amazon.awssdk.services.json.internal;

import java.lang.String;
import software.amazon.awssdk.annotations.Generated;

@Generated("software.amazon.awssdk:codegen")
public final class ServiceVersionInfo {
/**
* Returns the current version for the AWS SDK in which this class is running.
*/
public static final String VERSION = "{{VERSION}}";

private ServiceVersionInfo() {
}
}
Loading