Skip to content

Commit 7fbc693

Browse files
committed
Add a script for updating the json files of a service. This takes waf and dynamo into account as special cases.
1 parent 20d2fba commit 7fbc693

File tree

7 files changed

+214
-37
lines changed

7 files changed

+214
-37
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 java.util.stream.Stream;
19+
import org.apache.commons.cli.CommandLine;
20+
import org.apache.commons.cli.CommandLineParser;
21+
import org.apache.commons.cli.DefaultParser;
22+
import org.apache.commons.cli.HelpFormatter;
23+
import org.apache.commons.cli.Option;
24+
import org.apache.commons.cli.Options;
25+
import org.apache.commons.cli.ParseException;
26+
import software.amazon.awssdk.utils.Logger;
27+
28+
public abstract class Cli {
29+
private final Logger log = Logger.loggerFor(Cli.class);
30+
private final Option[] optionsToAdd;
31+
32+
public Cli(Option... optionsToAdd) {
33+
this.optionsToAdd = optionsToAdd;
34+
}
35+
36+
public final void run(String[] args) {
37+
Options options = new Options();
38+
Stream.of(optionsToAdd).forEach(options::addOption);
39+
40+
CommandLineParser parser = new DefaultParser();
41+
HelpFormatter help = new HelpFormatter();
42+
43+
try {
44+
CommandLine commandLine = parser.parse(options, args);
45+
run(commandLine);
46+
} catch (ParseException e) {
47+
log.error(() -> "Invalid input: " + e.getMessage());
48+
help.printHelp(getClass().getSimpleName(), options);
49+
throw new Error();
50+
} catch (Exception e) {
51+
log.error(() -> "Script execution failed.", e);
52+
throw new Error();
53+
}
54+
}
55+
56+
protected static Option requiredOption(String longCommand, String description) {
57+
Option option = optionalOption(longCommand, description);
58+
option.setRequired(true);
59+
return option;
60+
}
61+
62+
protected static Option optionalOption(String longCommand, String description) {
63+
return new Option(null, longCommand, true, description);
64+
}
65+
66+
protected abstract void run(CommandLine commandLine) throws Exception;
67+
}

release-scripts/src/main/java/software/amazon/awssdk/release/NewServiceMain.java

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@
2525
import java.nio.file.SimpleFileVisitor;
2626
import java.nio.file.attribute.BasicFileAttributes;
2727
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;
3428
import org.apache.commons.io.FileUtils;
3529
import org.apache.commons.lang3.StringUtils;
3630
import org.w3c.dom.Document;
3731
import org.w3c.dom.Node;
38-
import software.amazon.awssdk.utils.Logger;
32+
import software.amazon.awssdk.utils.Validate;
3933

4034
/**
4135
* A command line application to create a new, empty service.
@@ -51,40 +45,22 @@
5145
* --service-protocol json"
5246
* </pre>
5347
*/
54-
public class NewServiceMain {
55-
private static final Logger log = Logger.loggerFor(NewServiceMain.class);
56-
57-
private NewServiceMain() {}
48+
public class NewServiceMain extends Cli {
49+
private NewServiceMain() {
50+
super(requiredOption("service-module-name", "The name of the service module to be created."),
51+
requiredOption("service-id", "The service ID of the service module to be created."),
52+
requiredOption("service-protocol", "The protocol of the service module to be created."),
53+
requiredOption("maven-project-root", "The root directory for the maven project."),
54+
requiredOption("maven-project-version", "The maven version of the service module to be created."));
55+
}
5856

5957
public static void main(String[] args) {
60-
Options options = new Options();
61-
62-
options.addOption(requiredOption("service-module-name", "The name of the service module to be created."));
63-
options.addOption(requiredOption("service-id", "The service ID of the service module to be created."));
64-
options.addOption(requiredOption("service-protocol", "The protocol of the service module to be created."));
65-
options.addOption(requiredOption("maven-project-root", "The root directory for the maven project."));
66-
options.addOption(requiredOption("maven-project-version", "The maven version of the service module to be created."));
67-
68-
CommandLineParser parser = new DefaultParser();
69-
HelpFormatter help = new HelpFormatter();
70-
71-
try {
72-
CommandLine commandLine = parser.parse(options, args);
73-
new NewServiceCreator(commandLine).run();
74-
} catch (ParseException e) {
75-
log.error(() -> "Invalid input: " + e.getMessage());
76-
help.printHelp("NewServiceMain", options);
77-
System.exit(1);
78-
} catch (Exception e) {
79-
log.error(() -> "Script execution failed.", e);
80-
System.exit(2);
81-
}
58+
new NewServiceMain().run(args);
8259
}
8360

84-
private static Option requiredOption(String longCommand, String description) {
85-
Option option = new Option(null, longCommand, true, description);
86-
option.setRequired(true);
87-
return option;
61+
@Override
62+
protected void run(CommandLine commandLine) throws Exception {
63+
new NewServiceCreator(commandLine).run();
8864
}
8965

9066
private static class NewServiceCreator {
@@ -100,6 +76,8 @@ private NewServiceCreator(CommandLine commandLine) {
10076
this.serviceModuleName = commandLine.getOptionValue("service-module-name").trim();
10177
this.serviceId = commandLine.getOptionValue("service-id").trim();
10278
this.serviceProtocol = transformSpecialProtocols(commandLine.getOptionValue("service-protocol").trim());
79+
80+
Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);
10381
}
10482

10583
private String transformSpecialProtocols(String protocol) {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 java.io.IOException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.nio.file.Paths;
22+
import org.apache.commons.cli.CommandLine;
23+
import org.apache.commons.io.FileUtils;
24+
import org.apache.commons.lang3.StringUtils;
25+
import software.amazon.awssdk.utils.Logger;
26+
import software.amazon.awssdk.utils.Validate;
27+
28+
/**
29+
* A command line application to update an existing service.
30+
*
31+
* Example usage:
32+
* <pre>
33+
mvn exec:java -pl :release-scripts \
34+
-Dexec.mainClass="software.amazon.awssdk.release.UpdateServiceMain" \
35+
-Dexec.args="--maven-project-root /path/to/root
36+
--service-module-name service-module-name
37+
--service-json /path/to/service-2.json
38+
[--paginators-json /path/to/paginators-1.json
39+
--waiters-json /path/to/waiters-2.json]"
40+
* </pre>
41+
*/
42+
public class UpdateServiceMain extends Cli {
43+
private static final Logger log = Logger.loggerFor(UpdateServiceMain.class);
44+
45+
private UpdateServiceMain() {
46+
super(requiredOption("service-module-name", "The name of the service module to be created."),
47+
requiredOption("maven-project-root", "The root directory for the maven project."),
48+
requiredOption("service-json", "The service-2.json file for the service."),
49+
optionalOption("paginators-json", "The paginators-1.json file for the service."),
50+
optionalOption("waiters-json", "The waiters-2.json file for the service."));
51+
}
52+
53+
public static void main(String[] args) {
54+
new UpdateServiceMain().run(args);
55+
}
56+
57+
@Override
58+
protected void run(CommandLine commandLine) throws Exception {
59+
new ServiceUpdater(commandLine).run();
60+
}
61+
62+
private static class ServiceUpdater {
63+
private final String serviceModuleName;
64+
private final Path mavenProjectRoot;
65+
private final Path serviceJson;
66+
private final Path paginatorsJson;
67+
private final Path waitersJson;
68+
69+
private ServiceUpdater(CommandLine commandLine) {
70+
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root").trim());
71+
this.serviceModuleName = commandLine.getOptionValue("service-module-name").trim();
72+
this.serviceJson = Paths.get(commandLine.getOptionValue("service-json").trim());
73+
this.paginatorsJson = optionalPath(commandLine.getOptionValue("paginators-json"));
74+
this.waitersJson = optionalPath(commandLine.getOptionValue("waiters-json"));
75+
}
76+
77+
private Path optionalPath(String path) {
78+
path = StringUtils.trimToNull(path);
79+
if (path != null) {
80+
return Paths.get(path);
81+
}
82+
return null;
83+
}
84+
85+
public void run() throws Exception {
86+
Validate.isTrue(Files.isRegularFile(serviceJson), serviceJson + " is not a file.");
87+
88+
Path codegenFileLocation = codegenFileLocation(serviceModuleName);
89+
90+
copyFile(serviceJson, codegenFileLocation.resolve("service-2.json"));
91+
copyFile(paginatorsJson, codegenFileLocation.resolve("paginators-1.json"));
92+
copyFile(waitersJson, codegenFileLocation.resolve("waiters-2.json"));
93+
}
94+
95+
private Path codegenFileLocation(String serviceModuleName) {
96+
String actualServiceModuleName = actualServiceModuleName(serviceModuleName);
97+
98+
Path codegenPath = mavenProjectRoot.resolve("services")
99+
.resolve(actualServiceModuleName)
100+
.resolve("src")
101+
.resolve("main")
102+
.resolve("resources")
103+
.resolve("codegen-resources");
104+
105+
switch (actualServiceModuleName) {
106+
case "waf":
107+
case "dynamodb":
108+
return codegenPath.resolve(serviceModuleName);
109+
default:
110+
return codegenPath;
111+
}
112+
}
113+
114+
private String actualServiceModuleName(String serviceModuleName) {
115+
switch (serviceModuleName) {
116+
case "wafregional":
117+
return "waf";
118+
case "dynamodbstreams":
119+
return "dynamodb";
120+
default:
121+
return serviceModuleName;
122+
}
123+
}
124+
125+
private void copyFile(Path source, Path destination) throws IOException {
126+
if (source != null && Files.isRegularFile(source)) {
127+
log.info(() -> "Copying " + source + " to " + destination);
128+
FileUtils.copyFile(source.toFile(), destination.toFile());
129+
}
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)