|
| 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 | +} |
0 commit comments