Skip to content

Commit 28accef

Browse files
committed
Added a script that simplifies generating a new service from the service template.
Removed copyrights from transformed pom.xml files because they aren't needed and they get mangled by the new service addition process.
1 parent 3bd2865 commit 28accef

File tree

9 files changed

+388
-44
lines changed

9 files changed

+388
-44
lines changed

aws-sdk-java/pom.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
<?xml version="1.0"?>
2-
<!--
3-
~ Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
~
5-
~ Licensed under the Apache License, Version 2.0 (the "License").
6-
~ You may not use this file except in compliance with the License.
7-
~ A copy of the License is located at
8-
~
9-
~ http://aws.amazon.com/apache2.0
10-
~
11-
~ or in the "license" file accompanying this file. This file is distributed
12-
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
~ express or implied. See the License for the specific language governing
14-
~ permissions and limitations under the License.
15-
-->
162
<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">
173
<modelVersion>4.0.0</modelVersion>
184
<parent>

bom/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0"?>
2-
<!--
3-
~ Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
~
5-
~ Licensed under the Apache License, Version 2.0 (the "License").
6-
~ You may not use this file except in compliance with the License.
7-
~ A copy of the License is located at
8-
~
9-
~ http://aws.amazon.com/apache2.0
10-
~
11-
~ or in the "license" file accompanying this file. This file is distributed
12-
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
~ express or implied. See the License for the specific language governing
14-
~ permissions and limitations under the License.
15-
-->
16-
172
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
183
xmlns="http://maven.apache.org/POM/4.0.0"
194
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

build-tools/src/main/resources/software/amazon/awssdk/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<!-- ignore missing annotation checks under test/codegen directory -->
3131
<suppress checks="MissingSdkAnnotationCheck"
32-
files=".(codegen|test)[\\/].+\.java$"/>
32+
files=".(codegen|test|release)[\\/].+\.java$"/>
3333

3434
<!-- TODO want to contribute this back to Netty -->
3535
<suppress checks=".*"

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<module>codegen-maven-plugin</module>
6262
<module>bundle</module>
6363
<module>build-tools</module>
64+
<module>release-scripts</module>
6465
<module>test/dynamodbdocument-v1</module>
6566
<module>test/dynamodbmapper-v1</module>
6667
<module>test/http-client-tests</module>

release-scripts/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<parent>
23+
<groupId>software.amazon.awssdk</groupId>
24+
<artifactId>aws-sdk-java-pom</artifactId>
25+
<version>2.1.4-SNAPSHOT</version>
26+
<relativePath>../pom.xml</relativePath>
27+
</parent>
28+
<artifactId>release-scripts</artifactId>
29+
30+
<name>AWS Java SDK :: New Service Maven Plugin</name>
31+
<description>This plugin can add new services to the SDK.</description>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<jre.version>1.8</jre.version>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.apache.commons</groupId>
41+
<artifactId>commons-lang3</artifactId>
42+
<version>3.7</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>commons-io</groupId>
46+
<artifactId>commons-io</artifactId>
47+
<version>2.6</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>commons-cli</groupId>
51+
<artifactId>commons-cli</artifactId>
52+
<version>1.4</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>software.amazon.awssdk</groupId>
56+
<artifactId>utils</artifactId>
57+
<version>${awsjavasdk.version}</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>log4j</groupId>
61+
<artifactId>log4j</artifactId>
62+
<version>${log4j.version}</version>
63+
<scope>runtime</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.slf4j</groupId>
67+
<artifactId>slf4j-log4j12</artifactId>
68+
<version>${slf4j.version}</version>
69+
<scope>runtime</scope>
70+
</dependency>
71+
</dependencies>
72+
</project>
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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.release;
17+
18+
import static java.nio.charset.StandardCharsets.UTF_8;
19+
20+
import java.io.IOException;
21+
import java.nio.file.FileVisitResult;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.nio.file.SimpleFileVisitor;
26+
import java.nio.file.attribute.BasicFileAttributes;
27+
import org.apache.commons.cli.CommandLine;
28+
import org.apache.commons.cli.CommandLineParser;
29+
import org.apache.commons.cli.DefaultParser;
30+
import org.apache.commons.cli.HelpFormatter;
31+
import org.apache.commons.cli.Option;
32+
import org.apache.commons.cli.Options;
33+
import org.apache.commons.cli.ParseException;
34+
import org.apache.commons.io.FileUtils;
35+
import org.apache.commons.lang3.StringUtils;
36+
import org.w3c.dom.Document;
37+
import org.w3c.dom.Node;
38+
import software.amazon.awssdk.utils.Logger;
39+
import software.amazon.awssdk.utils.Validate;
40+
41+
/**
42+
* A command line application to create a new, empty service.
43+
*
44+
* Example usage:
45+
* <pre>
46+
* mvn exec:java -pl :release-scripts \
47+
* -Dexec.mainClass="software.amazon.awssdk.release.NewServiceMain" \
48+
* -Dexec.args="--maven-project-root /path/to/root
49+
* --maven-project-version 2.1.4-SNAPSHOT
50+
* --service-id 'Service Id'
51+
* --service-module-name service-module-name
52+
* --service-protocol json"
53+
* </pre>
54+
*/
55+
public class NewServiceMain {
56+
private static final Logger log = Logger.loggerFor(NewServiceMain.class);
57+
58+
private NewServiceMain() {}
59+
60+
public static void main(String[] args) {
61+
Options options = new Options();
62+
63+
options.addOption(requiredOption("service-module-name", "The name of the service module to be created."));
64+
options.addOption(requiredOption("service-id", "The service ID of the service module to be created."));
65+
options.addOption(requiredOption("service-protocol", "The protocol of the service module to be created."));
66+
options.addOption(requiredOption("maven-project-root", "The root directory for the maven project."));
67+
options.addOption(requiredOption("maven-project-version", "The maven version of the service module to be created."));
68+
69+
CommandLineParser parser = new DefaultParser();
70+
HelpFormatter help = new HelpFormatter();
71+
72+
try {
73+
CommandLine commandLine = parser.parse(options, args);
74+
new NewServiceCreator(commandLine).run();
75+
} catch (ParseException e) {
76+
log.error(() -> "Invalid input: " + e.getMessage());
77+
help.printHelp("NewServiceMain", options);
78+
System.exit(1);
79+
} catch (Exception e) {
80+
log.error(() -> "Script execution failed.", e);
81+
System.exit(2);
82+
}
83+
}
84+
85+
private static Option requiredOption(String longCommand, String description) {
86+
Option option = new Option(null, longCommand, true, description);
87+
option.setRequired(true);
88+
return option;
89+
}
90+
91+
private static class NewServiceCreator {
92+
private final Path mavenProjectRoot;
93+
private final String mavenProjectVersion;
94+
private final String serviceModuleName;
95+
private final String serviceId;
96+
private final String serviceProtocol;
97+
98+
private NewServiceCreator(CommandLine commandLine) {
99+
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root"));
100+
this.mavenProjectVersion = commandLine.getOptionValue("maven-project-version");
101+
this.serviceModuleName = commandLine.getOptionValue("service-module-name");
102+
this.serviceId = commandLine.getOptionValue("service-id");
103+
this.serviceProtocol = transformSpecialProtocols(commandLine.getOptionValue("service-protocol"));
104+
}
105+
106+
private String transformSpecialProtocols(String protocol) {
107+
switch (protocol) {
108+
case "ec2": return "query";
109+
case "rest-xml": return "xml";
110+
case "rest-json": return "json";
111+
default: return protocol;
112+
}
113+
}
114+
115+
public void run() throws Exception {
116+
Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);
117+
118+
Path servicesRoot = mavenProjectRoot.resolve("services");
119+
Path templateModulePath = servicesRoot.resolve("new-service-template");
120+
Path newServiceModulePath = servicesRoot.resolve(serviceModuleName);
121+
122+
createNewModuleFromTemplate(templateModulePath, newServiceModulePath);
123+
replaceTemplatePlaceholders(newServiceModulePath);
124+
125+
Path servicesPomPath = mavenProjectRoot.resolve("services").resolve("pom.xml");
126+
Path aggregatePomPath = mavenProjectRoot.resolve("aws-sdk-java").resolve("pom.xml");
127+
Path bomPomPath = mavenProjectRoot.resolve("bom").resolve("pom.xml");
128+
129+
new AddSubmoduleTransformer().transform(servicesPomPath);
130+
new AddDependencyTransformer().transform(aggregatePomPath);
131+
new AddDependencyManagementDependencyTransformer().transform(bomPomPath);
132+
}
133+
134+
private void createNewModuleFromTemplate(Path templateModulePath, Path newServiceModule) throws IOException {
135+
FileUtils.copyDirectory(templateModulePath.toFile(), newServiceModule.toFile());
136+
}
137+
138+
private void replaceTemplatePlaceholders(Path newServiceModule) throws IOException {
139+
Files.walkFileTree(newServiceModule, new SimpleFileVisitor<Path>() {
140+
@Override
141+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
142+
replacePlaceholdersInFile(file);
143+
return FileVisitResult.CONTINUE;
144+
}
145+
});
146+
}
147+
148+
private void replacePlaceholdersInFile(Path file) throws IOException {
149+
String fileContents = new String(Files.readAllBytes(file), UTF_8);
150+
String newFileContents = replacePlaceholders(fileContents);
151+
Files.write(file, newFileContents.getBytes(UTF_8));
152+
}
153+
154+
private String replacePlaceholders(String line) {
155+
String[] searchList = {
156+
"{{MVN_ARTIFACT_ID}}",
157+
"{{MVN_NAME}}",
158+
"{{MVN_VERSION}}",
159+
"{{PROTOCOL}}"
160+
};
161+
String[] replaceList = {
162+
serviceModuleName,
163+
serviceId,
164+
mavenProjectVersion,
165+
serviceProtocol
166+
};
167+
return StringUtils.replaceEach(line, searchList, replaceList);
168+
}
169+
170+
private class AddSubmoduleTransformer extends PomTransformer {
171+
@Override
172+
protected void updateDocument(Document doc) {
173+
Node project = findChild(doc, "project");
174+
Node modules = findChild(project, "modules");
175+
176+
modules.appendChild(textElement(doc, "module", serviceModuleName));
177+
}
178+
}
179+
180+
private class AddDependencyTransformer extends PomTransformer {
181+
@Override
182+
protected void updateDocument(Document doc) {
183+
Node project = findChild(doc, "project");
184+
Node dependencies = findChild(project, "dependencies");
185+
186+
dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
187+
}
188+
}
189+
190+
private class AddDependencyManagementDependencyTransformer extends PomTransformer {
191+
@Override
192+
protected void updateDocument(Document doc) {
193+
Node project = findChild(doc, "project");
194+
Node dependencyManagement = findChild(project, "dependencyManagement");
195+
Node dependencies = findChild(dependencyManagement, "dependencies");
196+
197+
dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
198+
}
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)