Skip to content

Commit dc5672f

Browse files
committed
Add CreateNewServiceModuleMain and FinalizeNewServiceModuleMain release scripts, which are just a split up version of the NewServiceMain release script.
1 parent c5d7778 commit dc5672f

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 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 java.util.stream.Collectors;
28+
import java.util.stream.Stream;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.io.FileUtils;
31+
import org.apache.commons.lang3.StringUtils;
32+
import software.amazon.awssdk.utils.Validate;
33+
import software.amazon.awssdk.utils.internal.CodegenNamingUtils;
34+
35+
/**
36+
* A command line application to create a new, empty service. This *does not* add the new service to the shared pom.xmls, that
37+
* should be done via {@link FinalizeNewServiceModuleMain}.
38+
*
39+
* Example usage:
40+
* <pre>
41+
* mvn exec:java -pl :release-scripts \
42+
* -Dexec.mainClass="software.amazon.awssdk.release.CreateNewServiceModuleMain" \
43+
* -Dexec.args="--maven-project-root /path/to/root
44+
* --maven-project-version 2.1.4-SNAPSHOT
45+
* --service-id 'Service Id'
46+
* --service-module-name service-module-name
47+
* --service-protocol json"
48+
* </pre>
49+
*/
50+
public class CreateNewServiceModuleMain extends Cli {
51+
private CreateNewServiceModuleMain() {
52+
super(requiredOption("service-module-name", "The name of the service module to be created."),
53+
requiredOption("service-id", "The service ID of the service module to be created."),
54+
requiredOption("service-protocol", "The protocol of the service module to be created."),
55+
requiredOption("maven-project-root", "The root directory for the maven project."),
56+
requiredOption("maven-project-version", "The maven version of the service module to be created."));
57+
}
58+
59+
public static void main(String[] args) {
60+
new CreateNewServiceModuleMain().run(args);
61+
}
62+
63+
@Override
64+
protected void run(CommandLine commandLine) throws Exception {
65+
new NewServiceCreator(commandLine).run();
66+
}
67+
68+
private static class NewServiceCreator {
69+
private final Path mavenProjectRoot;
70+
private final String mavenProjectVersion;
71+
private final String serviceModuleName;
72+
private final String serviceId;
73+
private final String serviceProtocol;
74+
75+
private NewServiceCreator(CommandLine commandLine) {
76+
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root").trim());
77+
this.mavenProjectVersion = commandLine.getOptionValue("maven-project-version").trim();
78+
this.serviceModuleName = commandLine.getOptionValue("service-module-name").trim();
79+
this.serviceId = commandLine.getOptionValue("service-id").trim();
80+
this.serviceProtocol = transformSpecialProtocols(commandLine.getOptionValue("service-protocol").trim());
81+
82+
Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);
83+
}
84+
85+
private String transformSpecialProtocols(String protocol) {
86+
switch (protocol) {
87+
case "ec2": return "query";
88+
case "rest-xml": return "xml";
89+
case "rest-json": return "json";
90+
default: return protocol;
91+
}
92+
}
93+
94+
public void run() throws Exception {
95+
Path servicesRoot = mavenProjectRoot.resolve("services");
96+
Path templateModulePath = servicesRoot.resolve("new-service-template");
97+
Path newServiceModulePath = servicesRoot.resolve(serviceModuleName);
98+
99+
createNewModuleFromTemplate(templateModulePath, newServiceModulePath);
100+
replaceTemplatePlaceholders(newServiceModulePath);
101+
}
102+
103+
private void createNewModuleFromTemplate(Path templateModulePath, Path newServiceModule) throws IOException {
104+
FileUtils.copyDirectory(templateModulePath.toFile(), newServiceModule.toFile());
105+
}
106+
107+
private void replaceTemplatePlaceholders(Path newServiceModule) throws IOException {
108+
Files.walkFileTree(newServiceModule, new SimpleFileVisitor<Path>() {
109+
@Override
110+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
111+
replacePlaceholdersInFile(file);
112+
return FileVisitResult.CONTINUE;
113+
}
114+
});
115+
}
116+
117+
private void replacePlaceholdersInFile(Path file) throws IOException {
118+
String fileContents = new String(Files.readAllBytes(file), UTF_8);
119+
String newFileContents = replacePlaceholders(fileContents);
120+
Files.write(file, newFileContents.getBytes(UTF_8));
121+
}
122+
123+
private String replacePlaceholders(String line) {
124+
String[] searchList = {
125+
"{{MVN_ARTIFACT_ID}}",
126+
"{{MVN_NAME}}",
127+
"{{MVN_VERSION}}",
128+
"{{PROTOCOL}}"
129+
};
130+
String[] replaceList = {
131+
serviceModuleName,
132+
mavenName(serviceId),
133+
mavenProjectVersion,
134+
serviceProtocol
135+
};
136+
return StringUtils.replaceEach(line, searchList, replaceList);
137+
}
138+
139+
private String mavenName(String serviceId) {
140+
return Stream.of(CodegenNamingUtils.splitOnWordBoundaries(serviceId))
141+
.map(StringUtils::capitalize)
142+
.collect(Collectors.joining(" "));
143+
}
144+
}
145+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 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 java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.Stream;
24+
import org.apache.commons.cli.CommandLine;
25+
import org.w3c.dom.Document;
26+
import org.w3c.dom.Node;
27+
import software.amazon.awssdk.utils.Validate;
28+
29+
/**
30+
* A command line application to add new services to the shared pom.xml files.
31+
*
32+
* Example usage:
33+
* <pre>
34+
* mvn exec:java -pl :release-scripts \
35+
* -Dexec.mainClass="software.amazon.awssdk.release.FinalizeNewServiceModuleMain" \
36+
* -Dexec.args="--maven-project-root /path/to/root
37+
* --service-module-names service-module-name-1,service-module-name-2"
38+
* </pre>
39+
*/
40+
public class FinalizeNewServiceModuleMain extends Cli {
41+
private FinalizeNewServiceModuleMain() {
42+
super(requiredOption("service-module-names",
43+
"A comma-separated list containing the name of the service modules to be created."),
44+
requiredOption("maven-project-root", "The root directory for the maven project."));
45+
}
46+
47+
public static void main(String[] args) {
48+
new FinalizeNewServiceModuleMain().run(args);
49+
}
50+
51+
@Override
52+
protected void run(CommandLine commandLine) throws Exception {
53+
new NewServiceCreator(commandLine).run();
54+
}
55+
56+
private static class NewServiceCreator {
57+
private final Path mavenProjectRoot;
58+
private final List<String> serviceModuleNames;
59+
60+
private NewServiceCreator(CommandLine commandLine) {
61+
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root").trim());
62+
this.serviceModuleNames = Stream.of(commandLine.getOptionValue("service-module-names").split(","))
63+
.map(String::trim)
64+
.collect(Collectors.toList());
65+
66+
Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);
67+
}
68+
69+
public void run() throws Exception {
70+
for (String serviceModuleName : serviceModuleNames) {
71+
Path servicesPomPath = mavenProjectRoot.resolve("services").resolve("pom.xml");
72+
Path aggregatePomPath = mavenProjectRoot.resolve("aws-sdk-java").resolve("pom.xml");
73+
Path bomPomPath = mavenProjectRoot.resolve("bom").resolve("pom.xml");
74+
75+
new AddSubmoduleTransformer(serviceModuleName).transform(servicesPomPath);
76+
new AddDependencyTransformer(serviceModuleName).transform(aggregatePomPath);
77+
new AddDependencyManagementDependencyTransformer(serviceModuleName).transform(bomPomPath);
78+
}
79+
}
80+
81+
private static class AddSubmoduleTransformer extends PomTransformer {
82+
private final String serviceModuleName;
83+
84+
private AddSubmoduleTransformer(String serviceModuleName) {
85+
this.serviceModuleName = serviceModuleName;
86+
}
87+
88+
@Override
89+
protected void updateDocument(Document doc) {
90+
Node project = findChild(doc, "project");
91+
Node modules = findChild(project, "modules");
92+
93+
modules.appendChild(textElement(doc, "module", serviceModuleName));
94+
}
95+
}
96+
97+
private static class AddDependencyTransformer extends PomTransformer {
98+
private final String serviceModuleName;
99+
100+
private AddDependencyTransformer(String serviceModuleName) {
101+
this.serviceModuleName = serviceModuleName;
102+
}
103+
104+
@Override
105+
protected void updateDocument(Document doc) {
106+
Node project = findChild(doc, "project");
107+
Node dependencies = findChild(project, "dependencies");
108+
109+
dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
110+
}
111+
}
112+
113+
private static class AddDependencyManagementDependencyTransformer extends PomTransformer {
114+
private final String serviceModuleName;
115+
116+
private AddDependencyManagementDependencyTransformer(String serviceModuleName) {
117+
this.serviceModuleName = serviceModuleName;
118+
}
119+
120+
@Override
121+
protected void updateDocument(Document doc) {
122+
Node project = findChild(doc, "project");
123+
Node dependencyManagement = findChild(project, "dependencyManagement");
124+
Node dependencies = findChild(dependencyManagement, "dependencies");
125+
126+
dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)