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