Skip to content

Added a script that simplifies generating a new service from the service template. #919

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 1 commit into from
Dec 5, 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
14 changes: 0 additions & 14 deletions aws-sdk-java/pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
<?xml version="1.0"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand Down
15 changes: 0 additions & 15 deletions bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<?xml version="1.0"?>
<!--
~ 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.
-->

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<!-- ignore missing annotation checks under test/codegen directory -->
<suppress checks="MissingSdkAnnotationCheck"
files=".(codegen|test)[\\/].+\.java$"/>
files=".(codegen|test|release)[\\/].+\.java$"/>

<!-- TODO want to contribute this back to Netty -->
<suppress checks=".*"
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>codegen-maven-plugin</module>
<module>bundle</module>
<module>build-tools</module>
<module>release-scripts</module>
<module>test/dynamodbdocument-v1</module>
<module>test/dynamodbmapper-v1</module>
<module>test/http-client-tests</module>
Expand Down
72 changes: 72 additions & 0 deletions release-scripts/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0"?>
<!--
~ 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.
-->

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java-pom</artifactId>
<version>2.1.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>release-scripts</artifactId>

<name>AWS Java SDK :: New Service Maven Plugin</name>
<description>This plugin can add new services to the SDK.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jre.version>1.8</jre.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>utils</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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 static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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.
*
* Example usage:
* <pre>
* mvn exec:java -pl :release-scripts \
* -Dexec.mainClass="software.amazon.awssdk.release.NewServiceMain" \
* -Dexec.args="--maven-project-root /path/to/root
* --maven-project-version 2.1.4-SNAPSHOT
* --service-id 'Service Id'
* --service-module-name service-module-name
* --service-protocol json"
* </pre>
*/
public class NewServiceMain {
private static final Logger log = Logger.loggerFor(NewServiceMain.class);

private NewServiceMain() {}

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);
}
}

private static Option requiredOption(String longCommand, String description) {
Option option = new Option(null, longCommand, true, description);
option.setRequired(true);
return option;
}

private static class NewServiceCreator {
private final Path mavenProjectRoot;
private final String mavenProjectVersion;
private final String serviceModuleName;
private final String serviceId;
private final String serviceProtocol;

private NewServiceCreator(CommandLine commandLine) {
this.mavenProjectRoot = Paths.get(commandLine.getOptionValue("maven-project-root"));
this.mavenProjectVersion = commandLine.getOptionValue("maven-project-version");
this.serviceModuleName = commandLine.getOptionValue("service-module-name");
this.serviceId = commandLine.getOptionValue("service-id");
this.serviceProtocol = transformSpecialProtocols(commandLine.getOptionValue("service-protocol"));
}

private String transformSpecialProtocols(String protocol) {
switch (protocol) {
case "ec2": return "query";
case "rest-xml": return "xml";
case "rest-json": return "json";
default: return protocol;
}
}

public void run() throws Exception {
Validate.isTrue(Files.exists(mavenProjectRoot), "Project root does not exist: " + mavenProjectRoot);

Path servicesRoot = mavenProjectRoot.resolve("services");
Path templateModulePath = servicesRoot.resolve("new-service-template");
Path newServiceModulePath = servicesRoot.resolve(serviceModuleName);

createNewModuleFromTemplate(templateModulePath, newServiceModulePath);
replaceTemplatePlaceholders(newServiceModulePath);

Path servicesPomPath = mavenProjectRoot.resolve("services").resolve("pom.xml");
Path aggregatePomPath = mavenProjectRoot.resolve("aws-sdk-java").resolve("pom.xml");
Path bomPomPath = mavenProjectRoot.resolve("bom").resolve("pom.xml");

new AddSubmoduleTransformer().transform(servicesPomPath);
new AddDependencyTransformer().transform(aggregatePomPath);
new AddDependencyManagementDependencyTransformer().transform(bomPomPath);
}

private void createNewModuleFromTemplate(Path templateModulePath, Path newServiceModule) throws IOException {
FileUtils.copyDirectory(templateModulePath.toFile(), newServiceModule.toFile());
}

private void replaceTemplatePlaceholders(Path newServiceModule) throws IOException {
Files.walkFileTree(newServiceModule, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
replacePlaceholdersInFile(file);
return FileVisitResult.CONTINUE;
}
});
}

private void replacePlaceholdersInFile(Path file) throws IOException {
String fileContents = new String(Files.readAllBytes(file), UTF_8);
String newFileContents = replacePlaceholders(fileContents);
Files.write(file, newFileContents.getBytes(UTF_8));
}

private String replacePlaceholders(String line) {
String[] searchList = {
"{{MVN_ARTIFACT_ID}}",
"{{MVN_NAME}}",
"{{MVN_VERSION}}",
"{{PROTOCOL}}"
};
String[] replaceList = {
serviceModuleName,
serviceId,
mavenProjectVersion,
serviceProtocol
};
return StringUtils.replaceEach(line, searchList, replaceList);
}

private class AddSubmoduleTransformer extends PomTransformer {
@Override
protected void updateDocument(Document doc) {
Node project = findChild(doc, "project");
Node modules = findChild(project, "modules");

modules.appendChild(textElement(doc, "module", serviceModuleName));
}
}

private class AddDependencyTransformer extends PomTransformer {
@Override
protected void updateDocument(Document doc) {
Node project = findChild(doc, "project");
Node dependencies = findChild(project, "dependencies");

dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
}
}

private class AddDependencyManagementDependencyTransformer extends PomTransformer {
@Override
protected void updateDocument(Document doc) {
Node project = findChild(doc, "project");
Node dependencyManagement = findChild(project, "dependencyManagement");
Node dependencies = findChild(dependencyManagement, "dependencies");

dependencies.appendChild(sdkDependencyElement(doc, serviceModuleName));
}
}
}
}
Loading