|
| 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