Skip to content

Commit 72c5a5c

Browse files
committed
Generation region/service metadata
1 parent 28a5f48 commit 72c5a5c

31 files changed

+1474
-423
lines changed

codegen-lite-maven-plugin/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>software.amazon.awssdk</groupId>
9+
<artifactId>aws-sdk-java-pom</artifactId>
10+
<version>2.0.0-preview-13-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
<artifactId>codegen-lite-maven-plugin</artifactId>
14+
<packaging>maven-plugin</packaging>
15+
16+
<name>AWS Java SDK :: Code Generator Lite Maven Plugin</name>
17+
<description>The AWS SDK for Java - Code Generator Lite Maven Plugin module holds a mojo to generate region
18+
and service metadata for the core SDK.
19+
</description>
20+
<url>https://aws.amazon.com/sdkforjava</url>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<jre.version>1.8</jre.version>
25+
</properties>
26+
27+
<!-- The dependencies section in pom.xml is auto generated. No manual changes are allowed -->
28+
<dependencies>
29+
<dependency>
30+
<artifactId>maven-plugin-api</artifactId>
31+
<groupId>org.apache.maven</groupId>
32+
<version>3.5.0</version>
33+
</dependency>
34+
<dependency>
35+
<artifactId>maven-plugin-annotations</artifactId>
36+
<groupId>org.apache.maven.plugin-tools</groupId>
37+
<version>3.5</version>
38+
</dependency>
39+
<dependency>
40+
<artifactId>maven-project</artifactId>
41+
<groupId>org.apache.maven</groupId>
42+
<version>2.2.1</version>
43+
</dependency>
44+
<dependency>
45+
<artifactId>codegen-lite</artifactId>
46+
<groupId>software.amazon.awssdk</groupId>
47+
<version>[${awsjavasdk.version}]</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-plugin-plugin</artifactId>
56+
<version>3.5</version>
57+
<executions>
58+
<execution>
59+
<id>default-descriptor</id>
60+
<phase>process-classes</phase>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.lite.maven.plugin;
17+
18+
import java.io.File;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.HashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
import java.util.stream.Collectors;
27+
import org.apache.maven.plugin.AbstractMojo;
28+
import org.apache.maven.plugin.MojoExecutionException;
29+
import org.apache.maven.plugins.annotations.Mojo;
30+
import org.apache.maven.plugins.annotations.Parameter;
31+
import org.apache.maven.project.MavenProject;
32+
import software.amazon.awssdk.codegen.lite.CodeGenerator;
33+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataGenerator;
34+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataLoader;
35+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataProviderGenerator;
36+
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataGenerator;
37+
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataProviderGenerator;
38+
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;
39+
/**
40+
* The Maven mojo to generate Java client code using software.amazon.awssdk:codegen module.
41+
*/
42+
@Mojo(name = "generate-regions")
43+
public class RegionGenerationMojo extends AbstractMojo {
44+
45+
private static final String SERVICE_METADATA_BASE = "software.amazon.awssdk.regions.servicemetadata";
46+
private static final String REGION_METADATA_BASE = "software.amazon.awssdk.regions.regionmetadata";
47+
private static final String REGION_BASE = "software.amazon.awssdk.regions";
48+
49+
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
50+
private String outputDirectory;
51+
52+
@Parameter(defaultValue = "${project}", readonly = true)
53+
private MavenProject project;
54+
55+
@Parameter(property = "endpoints", defaultValue = "${basedir}/src/main/resources/software/amazon/awssdk/regions/internal/region/endpoints.json")
56+
private File endpoints;
57+
58+
public void execute() throws MojoExecutionException {
59+
Path baseSourcesDirectory = Paths.get(outputDirectory).resolve("generated-sources").resolve("sdk");
60+
61+
Partitions partitions = RegionMetadataLoader.build(endpoints);
62+
63+
generateServiceMetadata(baseSourcesDirectory, partitions);
64+
generateRegions(baseSourcesDirectory, partitions);
65+
generateRegionProvider(baseSourcesDirectory, partitions);
66+
generateServiceProvider(baseSourcesDirectory, partitions);
67+
68+
project.addCompileSourceRoot(baseSourcesDirectory.toFile().getAbsolutePath());
69+
}
70+
71+
public void generateServiceMetadata(Path baseSourcesDirectory, Partitions partitions) {
72+
Path sourcesDirectory = baseSourcesDirectory.resolve(SERVICE_METADATA_BASE.replace(".", "/"));
73+
Set<String> services = new HashSet<>();
74+
partitions.getPartitions().stream().forEach(p -> services.addAll(p.getServices().keySet()));
75+
76+
services.forEach(s -> new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataGenerator(partitions, s, SERVICE_METADATA_BASE, REGION_BASE)).generate());
77+
}
78+
79+
public void generateRegions(Path baseSourcesDirectory, Partitions partitions) {
80+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_METADATA_BASE.replace(".", "/"));
81+
partitions.getPartitions()
82+
.stream()
83+
.forEach(p -> p.getRegions().forEach((k, v) ->
84+
new CodeGenerator(sourcesDirectory.toString(),
85+
new RegionMetadataGenerator(p,
86+
k,
87+
v.getDescription(),
88+
REGION_METADATA_BASE,
89+
REGION_BASE))
90+
.generate()));
91+
}
92+
93+
public void generateRegionProvider(Path baseSourcesDirectory, Partitions partitions) {
94+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_METADATA_BASE.replace(".", "/"));
95+
new CodeGenerator(sourcesDirectory.toString(), new RegionMetadataProviderGenerator(partitions, REGION_METADATA_BASE, REGION_BASE))
96+
.generate();
97+
}
98+
99+
public void generateServiceProvider(Path baseSourcesDirectory, Partitions partitions) {
100+
Path sourcesDirectory = baseSourcesDirectory.resolve(SERVICE_METADATA_BASE.replace(".", "/"));
101+
new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataProviderGenerator(partitions, SERVICE_METADATA_BASE, REGION_BASE))
102+
.generate();
103+
}
104+
}

codegen-lite/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>software.amazon.awssdk</groupId>
8+
<artifactId>aws-sdk-java-pom</artifactId>
9+
<version>2.0.0-preview-13-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>codegen-lite</artifactId>
12+
<name>AWS Java SDK :: Code Generator Lite</name>
13+
<description>The AWS SDK for Java - Code Generator Lite module holds the classes and templates required to generate the
14+
source files for the core SDK.
15+
</description>
16+
<url>https://aws.amazon.com/sdkforjava</url>
17+
18+
<properties>
19+
<jre.version>1.8</jre.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.squareup</groupId>
25+
<artifactId>javapoet</artifactId>
26+
<version>${javapoet.verion}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-annotations</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.jr</groupId>
34+
<artifactId>jackson-jr-objects</artifactId>
35+
<version>${jackson.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.hamcrest</groupId>
39+
<artifactId>hamcrest-all</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-core</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-core</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>software.amazon.awssdk</groupId>
54+
<artifactId>annotations</artifactId>
55+
<version>[${awsjavasdk.version}]</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>software.amazon.awssdk</groupId>
59+
<artifactId>utils</artifactId>
60+
<version>[${awsjavasdk.version}]</version>
61+
</dependency>
62+
<dependency>
63+
<artifactId>org.eclipse.jdt.core</artifactId>
64+
<groupId>org.eclipse.jdt</groupId>
65+
<version>${org.eclipse.jdt.version}</version>
66+
</dependency>
67+
<dependency>
68+
<artifactId>org.eclipse.text</artifactId>
69+
<groupId>org.eclipse.text</groupId>
70+
<version>${org.eclipse.text.version}</version>
71+
</dependency>
72+
</dependencies>
73+
74+
75+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.lite;
17+
18+
import static software.amazon.awssdk.codegen.lite.Utils.closeQuietly;
19+
20+
import com.squareup.javapoet.JavaFile;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.Writer;
24+
import java.time.ZonedDateTime;
25+
import software.amazon.awssdk.codegen.lite.emitters.CodeWriter;
26+
import software.amazon.awssdk.utils.IoUtils;
27+
28+
public class CodeGenerator {
29+
30+
private final Writer writer;
31+
private final PoetClass poetClass;
32+
33+
public CodeGenerator(String outputDirectory, PoetClass poetClass) {
34+
this.writer = new CodeWriter(outputDirectory, poetClass.className().simpleName());
35+
this.poetClass = poetClass;
36+
}
37+
38+
public void generate() {
39+
try {
40+
writer.write("" + "\n");
41+
JavaFile.builder(poetClass.className().packageName(), poetClass.poetClass())
42+
.skipJavaLangImports(true)
43+
.build()
44+
.writeTo(writer);
45+
writer.flush();
46+
} catch (IOException e) {
47+
throw new RuntimeException(String.format("Error creating class %s", poetClass.className().simpleName()), e);
48+
} finally {
49+
closeQuietly(writer);
50+
}
51+
}
52+
53+
// private String loadDefaultFileHeader() throws IOException {
54+
// try (InputStream inputStream = getClass()
55+
// .getResourceAsStream("/software/amazon/awssdk/codegen/DefaultFileHeader.txt")) {
56+
// return IoUtils.toUtf8String(inputStream)
57+
// .replaceFirst("%COPYRIGHT_DATE_RANGE%", getCopyrightDateRange());
58+
// }
59+
// }
60+
//
61+
// private String getCopyrightDateRange() {
62+
// final int currentYear = ZonedDateTime.now().getYear();
63+
// final int copyrightStartYear = currentYear - 5;
64+
// return String.format("%d-%d", copyrightStartYear, currentYear);
65+
// }
66+
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
package software.amazon.awssdk.regions.internal;
16+
package software.amazon.awssdk.codegen.lite;
1717

18-
import software.amazon.awssdk.annotations.SdkInternalApi;
19-
import software.amazon.awssdk.regions.ServiceMetadata;
18+
import com.squareup.javapoet.ClassName;
19+
import com.squareup.javapoet.TypeSpec;
2020

21-
@SdkInternalApi
22-
public interface ServiceMetadataProvider {
23-
ServiceMetadata getServiceMetadata(String service);
21+
public interface PoetClass {
22+
23+
/**
24+
* @return The actual class specification generated from a <code>PoetSpec.builder()...</code> implementation
25+
*/
26+
TypeSpec poetClass();
27+
28+
ClassName className();
2429
}

0 commit comments

Comments
 (0)