Skip to content

Commit aa98f76

Browse files
committed
Generating region and service metadata
1 parent 895a263 commit aa98f76

File tree

46 files changed

+5201
-488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5201
-488
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,119 @@
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.HashSet;
22+
import java.util.Set;
23+
import org.apache.maven.plugin.AbstractMojo;
24+
import org.apache.maven.plugin.MojoExecutionException;
25+
import org.apache.maven.plugins.annotations.Mojo;
26+
import org.apache.maven.plugins.annotations.Parameter;
27+
import org.apache.maven.project.MavenProject;
28+
import software.amazon.awssdk.codegen.lite.CodeGenerator;
29+
import software.amazon.awssdk.codegen.lite.regions.RegionGenerator;
30+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataGenerator;
31+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataLoader;
32+
import software.amazon.awssdk.codegen.lite.regions.RegionMetadataProviderGenerator;
33+
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataGenerator;
34+
import software.amazon.awssdk.codegen.lite.regions.ServiceMetadataProviderGenerator;
35+
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;
36+
37+
/**
38+
* The Maven mojo to generate Java client code using software.amazon.awssdk:codegen module.
39+
*/
40+
@Mojo(name = "generate-regions")
41+
public class RegionGenerationMojo extends AbstractMojo {
42+
43+
private static final String SERVICE_METADATA_BASE = "software.amazon.awssdk.regions.servicemetadata";
44+
private static final String REGION_METADATA_BASE = "software.amazon.awssdk.regions.regionmetadata";
45+
private static final String REGION_BASE = "software.amazon.awssdk.regions";
46+
47+
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
48+
private String outputDirectory;
49+
50+
@Parameter(defaultValue = "${project}", readonly = true)
51+
private MavenProject project;
52+
53+
@Parameter(property = "endpoints", defaultValue =
54+
"${basedir}/src/main/resources/software/amazon/awssdk/regions/internal/region/endpoints.json")
55+
private File endpoints;
56+
57+
public void execute() throws MojoExecutionException {
58+
Path baseSourcesDirectory = Paths.get(outputDirectory).resolve("generated-sources").resolve("sdk");
59+
Path testsDirectory = Paths.get(outputDirectory).resolve("generated-test-sources").resolve("sdk-tests");
60+
61+
Partitions partitions = RegionMetadataLoader.build(endpoints);
62+
63+
generateRegionClass(baseSourcesDirectory, partitions);
64+
generateServiceMetadata(baseSourcesDirectory, partitions);
65+
generateRegions(baseSourcesDirectory, partitions);
66+
generateRegionProvider(baseSourcesDirectory, partitions);
67+
generateServiceProvider(baseSourcesDirectory, partitions);
68+
69+
project.addCompileSourceRoot(baseSourcesDirectory.toFile().getAbsolutePath());
70+
project.addTestCompileSourceRoot(testsDirectory.toFile().getAbsolutePath());
71+
}
72+
73+
public void generateRegionClass(Path baseSourcesDirectory, Partitions partitions) {
74+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
75+
new CodeGenerator(sourcesDirectory.toString(), new RegionGenerator(partitions, REGION_BASE)).generate();
76+
}
77+
78+
public void generateServiceMetadata(Path baseSourcesDirectory, Partitions partitions) {
79+
Path sourcesDirectory = baseSourcesDirectory.resolve(SERVICE_METADATA_BASE.replace(".", "/"));
80+
Set<String> services = new HashSet<>();
81+
partitions.getPartitions().stream().forEach(p -> services.addAll(p.getServices().keySet()));
82+
83+
services.forEach(s -> new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataGenerator(partitions,
84+
s,
85+
SERVICE_METADATA_BASE,
86+
REGION_BASE))
87+
.generate());
88+
}
89+
90+
public void generateRegions(Path baseSourcesDirectory, Partitions partitions) {
91+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_METADATA_BASE.replace(".", "/"));
92+
partitions.getPartitions()
93+
.stream()
94+
.forEach(p -> p.getRegions().forEach((k, v) ->
95+
new CodeGenerator(sourcesDirectory.toString(),
96+
new RegionMetadataGenerator(p,
97+
k,
98+
v.getDescription(),
99+
REGION_METADATA_BASE,
100+
REGION_BASE))
101+
.generate()));
102+
}
103+
104+
public void generateRegionProvider(Path baseSourcesDirectory, Partitions partitions) {
105+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
106+
new CodeGenerator(sourcesDirectory.toString(), new RegionMetadataProviderGenerator(partitions,
107+
REGION_METADATA_BASE,
108+
REGION_BASE))
109+
.generate();
110+
}
111+
112+
public void generateServiceProvider(Path baseSourcesDirectory, Partitions partitions) {
113+
Path sourcesDirectory = baseSourcesDirectory.resolve(REGION_BASE.replace(".", "/"));
114+
new CodeGenerator(sourcesDirectory.toString(), new ServiceMetadataProviderGenerator(partitions,
115+
SERVICE_METADATA_BASE,
116+
REGION_BASE))
117+
.generate();
118+
}
119+
}

codegen-lite/pom.xml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>software.amazon.awssdk</groupId>
59+
<artifactId>annotations</artifactId>
60+
<version>[${awsjavasdk.version}]</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>software.amazon.awssdk</groupId>
64+
<artifactId>utils</artifactId>
65+
<version>[${awsjavasdk.version}]</version>
66+
</dependency>
67+
<dependency>
68+
<artifactId>org.eclipse.jdt.core</artifactId>
69+
<groupId>org.eclipse.jdt</groupId>
70+
<version>${org.eclipse.jdt.version}</version>
71+
</dependency>
72+
<dependency>
73+
<artifactId>org.eclipse.text</artifactId>
74+
<groupId>org.eclipse.text</groupId>
75+
<version>${org.eclipse.text.version}</version>
76+
</dependency>
77+
</dependencies>
78+
79+
80+
</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 final 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(loadDefaultFileHeader() + "\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/lite/DefaultFileHeader.txt")) {
56+
return IoUtils.toUtf8String(inputStream)
57+
.replaceFirst("%COPYRIGHT_DATE_RANGE%", getCopyrightDateRange());
58+
}
59+
}
60+
61+
private String getCopyrightDateRange() {
62+
int currentYear = ZonedDateTime.now().getYear();
63+
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)