Skip to content

Add a script for updating the json files of a service. #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.release;

import java.util.stream.Stream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import software.amazon.awssdk.utils.Logger;

public abstract class Cli {
private final Logger log = Logger.loggerFor(Cli.class);
private final Option[] optionsToAdd;

public Cli(Option... optionsToAdd) {
this.optionsToAdd = optionsToAdd;
}

public final void run(String[] args) {
Options options = new Options();
Stream.of(optionsToAdd).forEach(options::addOption);

CommandLineParser parser = new DefaultParser();
HelpFormatter help = new HelpFormatter();

try {
CommandLine commandLine = parser.parse(options, args);
run(commandLine);
} catch (ParseException e) {
log.error(() -> "Invalid input: " + e.getMessage());
help.printHelp(getClass().getSimpleName(), options);
throw new Error();
} catch (Exception e) {
log.error(() -> "Script execution failed.", e);
throw new Error();
}
}

protected static Option requiredOption(String longCommand, String description) {
Option option = optionalOption(longCommand, description);
option.setRequired(true);
return option;
}

protected static Option optionalOption(String longCommand, String description) {
return new Option(null, longCommand, true, description);
}

protected abstract void run(CommandLine commandLine) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Validate;

/**
* A command line application to create a new, empty service.
Expand All @@ -51,40 +45,22 @@
* --service-protocol json"
* </pre>
*/
public class NewServiceMain {
private static final Logger log = Logger.loggerFor(NewServiceMain.class);

private NewServiceMain() {}
public class NewServiceMain extends Cli {
private NewServiceMain() {
super(requiredOption("service-module-name", "The name of the service module to be created."),
requiredOption("service-id", "The service ID of the service module to be created."),
requiredOption("service-protocol", "The protocol of the service module to be created."),
requiredOption("maven-project-root", "The root directory for the maven project."),
requiredOption("maven-project-version", "The maven version of the service module to be created."));
}

public static void main(String[] args) {
Options options = new Options();

options.addOption(requiredOption("service-module-name", "The name of the service module to be created."));
options.addOption(requiredOption("service-id", "The service ID of the service module to be created."));
options.addOption(requiredOption("service-protocol", "The protocol of the service module to be created."));
options.addOption(requiredOption("maven-project-root", "The root directory for the maven project."));
options.addOption(requiredOption("maven-project-version", "The maven version of the service module to be created."));

CommandLineParser parser = new DefaultParser();
HelpFormatter help = new HelpFormatter();

try {
CommandLine commandLine = parser.parse(options, args);
new NewServiceCreator(commandLine).run();
} catch (ParseException e) {
log.error(() -> "Invalid input: " + e.getMessage());
help.printHelp("NewServiceMain", options);
System.exit(1);
} catch (Exception e) {
log.error(() -> "Script execution failed.", e);
System.exit(2);
}
new NewServiceMain().run(args);
}

private static Option requiredOption(String longCommand, String description) {
Option option = new Option(null, longCommand, true, description);
option.setRequired(true);
return option;
@Override
protected void run(CommandLine commandLine) throws Exception {
new NewServiceCreator(commandLine).run();
}

private static class NewServiceCreator {
Expand All @@ -100,6 +76,8 @@ private NewServiceCreator(CommandLine commandLine) {
this.serviceModuleName = commandLine.getOptionValue("service-module-name").trim();
this.serviceId = commandLine.getOptionValue("service-id").trim();
this.serviceProtocol = transformSpecialProtocols(commandLine.getOptionValue("service-protocol").trim());

Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);
}

private String transformSpecialProtocols(String protocol) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.release;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Validate;

/**
* A command line application to update an existing service.
*
* Example usage:
* <pre>
mvn exec:java -pl :release-scripts \
-Dexec.mainClass="software.amazon.awssdk.release.UpdateServiceMain" \
-Dexec.args="--maven-project-root /path/to/root
--service-module-name service-module-name
--service-json /path/to/service-2.json
[--paginators-json /path/to/paginators-1.json
--waiters-json /path/to/waiters-2.json]"
* </pre>
*/
public class UpdateServiceMain extends Cli {
private static final Logger log = Logger.loggerFor(UpdateServiceMain.class);

private UpdateServiceMain() {
super(requiredOption("service-module-name", "The name of the service module to be created."),
requiredOption("maven-project-root", "The root directory for the maven project."),
requiredOption("service-json", "The service-2.json file for the service."),
optionalOption("paginators-json", "The paginators-1.json file for the service."),
optionalOption("waiters-json", "The waiters-2.json file for the service."));
}

public static void main(String[] args) {
new UpdateServiceMain().run(args);
}

@Override
protected void run(CommandLine commandLine) throws Exception {
new ServiceUpdater(commandLine).run();
}

private static class ServiceUpdater {
private final String serviceModuleName;
private final Path mavenProjectRoot;
private final Path serviceJson;
private final Path paginatorsJson;
private final Path waitersJson;

private ServiceUpdater(CommandLine commandLine) {
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root").trim());
this.serviceModuleName = commandLine.getOptionValue("service-module-name").trim();
this.serviceJson = Paths.get(commandLine.getOptionValue("service-json").trim());
this.paginatorsJson = optionalPath(commandLine.getOptionValue("paginators-json"));
this.waitersJson = optionalPath(commandLine.getOptionValue("waiters-json"));
}

private Path optionalPath(String path) {
path = StringUtils.trimToNull(path);
if (path != null) {
return Paths.get(path);
}
return null;
}

public void run() throws Exception {
Validate.isTrue(Files.isRegularFile(serviceJson), serviceJson + " is not a file.");

Path codegenFileLocation = codegenFileLocation(serviceModuleName);

copyFile(serviceJson, codegenFileLocation.resolve("service-2.json"));
copyFile(paginatorsJson, codegenFileLocation.resolve("paginators-1.json"));
copyFile(waitersJson, codegenFileLocation.resolve("waiters-2.json"));
}

private Path codegenFileLocation(String serviceModuleName) {
String actualServiceModuleName = actualServiceModuleName(serviceModuleName);

Path codegenPath = mavenProjectRoot.resolve("services")
.resolve(actualServiceModuleName)
.resolve("src")
.resolve("main")
.resolve("resources")
.resolve("codegen-resources");

switch (actualServiceModuleName) {
case "waf":
case "dynamodb":
return codegenPath.resolve(serviceModuleName);
default:
return codegenPath;
}
}

private String actualServiceModuleName(String serviceModuleName) {
switch (serviceModuleName) {
case "wafregional":
return "waf";
case "dynamodbstreams":
return "dynamodb";
default:
return serviceModuleName;
}
}

private void copyFile(Path source, Path destination) throws IOException {
if (source != null && Files.isRegularFile(source)) {
log.info(() -> "Copying " + source + " to " + destination);
FileUtils.copyFile(source.toFile(), destination.toFile());
}
}
}
}