Skip to content

Generation region/service metadata #759

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

Merged
merged 1 commit into from
Oct 23, 2018
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
66 changes: 66 additions & 0 deletions codegen-lite-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java-pom</artifactId>
<version>2.0.0-preview-13-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>codegen-lite-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>

<name>AWS Java SDK :: Code Generator Lite Maven Plugin</name>
<description>The AWS SDK for Java - Code Generator Lite Maven Plugin module holds a mojo to generate region
and service metadata for the core SDK.
</description>
<url>https://aws.amazon.com/sdkforjava</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jre.version>1.8</jre.version>
</properties>

<!-- The dependencies section in pom.xml is auto generated. No manual changes are allowed -->
<dependencies>
<dependency>
<artifactId>maven-plugin-api</artifactId>
<groupId>org.apache.maven</groupId>
<version>3.5.0</version>
</dependency>
<dependency>
<artifactId>maven-plugin-annotations</artifactId>
<groupId>org.apache.maven.plugin-tools</groupId>
<version>3.5</version>
</dependency>
<dependency>
<artifactId>maven-project</artifactId>
<groupId>org.apache.maven</groupId>
<version>2.2.1</version>
</dependency>
<dependency>
<artifactId>codegen-lite</artifactId>
<groupId>software.amazon.awssdk</groupId>
<version>[${awsjavasdk.version}]</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2010-2018 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.lite.maven.plugin;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import software.amazon.awssdk.codegen.lite.CodeGenerator;
import software.amazon.awssdk.codegen.lite.regions.RegionGenerator;
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataGenerator;
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataLoader;
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataProviderGenerator;
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataGenerator;
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataProviderGenerator;
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;

/**
* The Maven mojo to generate Java client code using software.amazon.awssdk:codegen module.
*/
@Mojo(name = "generate-regions")
public class RegionGenerationMojo extends AbstractMojo {

private static final String SERVICE_METADATA_BASE = "software.amazon.awssdk.regions.servicemetadata";
private static final String REGION_METADATA_BASE = "software.amazon.awssdk.regions.regionmetadata";
private static final String REGION_BASE = "software.amazon.awssdk.regions";

@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
private String outputDirectory;

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

@Parameter(property = "endpoints", defaultValue =
"${basedir}/src/main/resources/software/amazon/awssdk/regions/internal/region/endpoints.json")
private File endpoints;

public void execute() throws MojoExecutionException {
Path baseSourcesDirectory = Paths.get(outputDirectory).resolve("generated-sources").resolve("sdk");
Path testsDirectory = Paths.get(outputDirectory).resolve("generated-test-sources").resolve("sdk-tests");

Partitions partitions = RegionMetadataLoader.build(endpoints);

generateRegionClass(baseSourcesDirectory, partitions);
generateServiceMetadata(baseSourcesDirectory, partitions);
generateRegions(baseSourcesDirectory, partitions);
generateRegionProvider(baseSourcesDirectory, partitions);
generateServiceProvider(baseSourcesDirectory, partitions);

project.addCompileSourceRoot(baseSourcesDirectory.toFile().getAbsolutePath());
project.addTestCompileSourceRoot(testsDirectory.toFile().getAbsolutePath());
}

public void generateRegionClass(Path baseSourcesDirectory, Partitions partitions) {
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
new CodeGenerator(sourcesDirectory.toString(), new RegionGenerator(partitions, REGION_BASE)).generate();
}

public void generateServiceMetadata(Path baseSourcesDirectory, Partitions partitions) {
Path sourcesDirectory = baseSourcesDirectory.resolve(SERVICE_METADATA_BASE.replace(".", "/"));
Set<String> services = new HashSet<>();
partitions.getPartitions().stream().forEach(p -> services.addAll(p.getServices().keySet()));

services.forEach(s -> new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataGenerator(partitions,
s,
SERVICE_METADATA_BASE,
REGION_BASE))
.generate());
}

public void generateRegions(Path baseSourcesDirectory, Partitions partitions) {
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_METADATA_BASE.replace(".", "/"));
partitions.getPartitions()
.stream()
.forEach(p -> p.getRegions().forEach((k, v) ->
new CodeGenerator(sourcesDirectory.toString(),
new RegionMetadataGenerator(p,
k,
v.getDescription(),
REGION_METADATA_BASE,
REGION_BASE))
.generate()));
}

public void generateRegionProvider(Path baseSourcesDirectory, Partitions partitions) {
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
new CodeGenerator(sourcesDirectory.toString(), new RegionMetadataProviderGenerator(partitions,
REGION_METADATA_BASE,
REGION_BASE))
.generate();
}

public void generateServiceProvider(Path baseSourcesDirectory, Partitions partitions) {
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataProviderGenerator(partitions,
SERVICE_METADATA_BASE,
REGION_BASE))
.generate();
}
}
80 changes: 80 additions & 0 deletions codegen-lite/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java-pom</artifactId>
<version>2.0.0-preview-13-SNAPSHOT</version>
</parent>
<artifactId>codegen-lite</artifactId>
<name>AWS Java SDK :: Code Generator Lite</name>
<description>The AWS SDK for Java - Code Generator Lite module holds the classes and templates required to generate the
source files for the core SDK.
</description>
<url>https://aws.amazon.com/sdkforjava</url>

<properties>
<jre.version>1.8</jre.version>
</properties>

<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>${javapoet.verion}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-objects</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>annotations</artifactId>
<version>[${awsjavasdk.version}]</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>utils</artifactId>
<version>[${awsjavasdk.version}]</version>
</dependency>
<dependency>
<artifactId>org.eclipse.jdt.core</artifactId>
<groupId>org.eclipse.jdt</groupId>
<version>${org.eclipse.jdt.version}</version>
</dependency>
<dependency>
<artifactId>org.eclipse.text</artifactId>
<groupId>org.eclipse.text</groupId>
<version>${org.eclipse.text.version}</version>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2010-2018 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.lite;

import static software.amazon.awssdk.codegen.lite.Utils.closeQuietly;

import com.squareup.javapoet.JavaFile;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.time.ZonedDateTime;
import software.amazon.awssdk.codegen.lite.emitters.CodeWriter;
import software.amazon.awssdk.utils.IoUtils;

public final class CodeGenerator {

private final Writer writer;
private final PoetClass poetClass;

public CodeGenerator(String outputDirectory, PoetClass poetClass) {
this.writer = new CodeWriter(outputDirectory, poetClass.className().simpleName());
this.poetClass = poetClass;
}

public void generate() {
try {
writer.write(loadDefaultFileHeader() + "\n");
JavaFile.builder(poetClass.className().packageName(), poetClass.poetClass())
.skipJavaLangImports(true)
.build()
.writeTo(writer);
writer.flush();
} catch (IOException e) {
throw new RuntimeException(String.format("Error creating class %s", poetClass.className().simpleName()), e);
} finally {
closeQuietly(writer);
}
}

private String loadDefaultFileHeader() throws IOException {
try (InputStream inputStream = getClass()
.getResourceAsStream("/software/amazon/awssdk/codegen/lite/DefaultFileHeader.txt")) {
return IoUtils.toUtf8String(inputStream)
.replaceFirst("%COPYRIGHT_DATE_RANGE%", getCopyrightDateRange());
}
}

private String getCopyrightDateRange() {
int currentYear = ZonedDateTime.now().getYear();
int copyrightStartYear = currentYear - 5;
return String.format("%d-%d", copyrightStartYear, currentYear);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
* permissions and limitations under the License.
*/

package software.amazon.awssdk.regions.internal;
package software.amazon.awssdk.codegen.lite;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.regions.ServiceMetadata;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeSpec;

@SdkInternalApi
public interface ServiceMetadataProvider {
ServiceMetadata getServiceMetadata(String service);
public interface PoetClass {

/**
* @return The actual class specification generated from a <code>PoetSpec.builder()...</code> implementation
*/
TypeSpec poetClass();

ClassName className();
}
Loading