This commit is contained in:
Phillip Webb
2014-11-03 17:20:40 -08:00
parent 9b6cb83be5
commit a973fd41f4
17 changed files with 192 additions and 123 deletions

View File

@@ -29,8 +29,6 @@ import org.springframework.boot.cli.command.options.OptionHelp;
*/
public abstract class AbstractCommand implements Command {
protected static final String NEW_LINE = System.getProperty("line.separator");
private final String name;
private final String description;
@@ -60,11 +58,6 @@ public abstract class AbstractCommand implements Command {
return null;
}
@Override
public String getExamples() {
return null;
}
@Override
public String getHelp() {
return null;
@@ -75,4 +68,9 @@ public abstract class AbstractCommand implements Command {
return Collections.emptyList();
}
@Override
public Collection<HelpExample> getExamples() {
return null;
}
}

View File

@@ -48,13 +48,6 @@ public interface Command {
*/
String getUsageHelp();
/**
* Return some examples for the command. This can be a multi-lined string with
* one example per line, starting with the description and ending with the
* example.
*/
String getExamples();
/**
* Gets full help text for the command, e.g. a longer description and one line per
* option.
@@ -66,6 +59,11 @@ public interface Command {
*/
Collection<OptionHelp> getOptionsHelp();
/**
* Return some examples for the command.
*/
Collection<HelpExample> getExamples();
/**
* Run the command.
* @param args command arguments (this will not include the command itself)

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.springframework.boot.cli.command;
/**
* An example that can be displayed in the help.
*
* @author Phillip Webb
* @since 1.2.0
*/
public class HelpExample {
private final String description;
private final String example;
/**
* @param description The description (in the form "to ....")
* @param example the example
*/
public HelpExample(String description, String example) {
this.description = description;
this.example = example;
}
public String getDescription() {
return this.description;
}
public String getExample() {
return this.example;
}
}

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import org.springframework.boot.cli.command.AbstractCommand;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.CommandRunner;
import org.springframework.boot.cli.command.HelpExample;
import org.springframework.boot.cli.command.NoHelpCommandArgumentsException;
import org.springframework.boot.cli.command.NoSuchCommandException;
import org.springframework.boot.cli.command.options.OptionHelp;
@@ -101,15 +102,20 @@ public class HelpCommand extends AbstractCommand {
+ " " + command.getUsageHelp());
Log.info("");
}
if (command.getExamples() != null) {
Log.info("example(s):");
Log.info("");
Log.info(command.getExamples());
Log.info("");
}
if (command.getHelp() != null) {
Log.info(command.getHelp());
}
Collection<HelpExample> examples = command.getExamples();
if (examples != null) {
Log.info(examples.size() == 1 ? "example:" : "examples:");
Log.info("");
for (HelpExample example : examples) {
Log.info(" " + example.getDescription() + ":");
Log.info(" $ " + example.getExample());
Log.info("");
}
Log.info("");
}
return ExitStatus.OK;
}
}

View File

@@ -19,12 +19,14 @@ package org.springframework.boot.cli.command.init;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.HelpExample;
import org.springframework.boot.cli.command.OptionParsingCommand;
import org.springframework.boot.cli.command.options.OptionHandler;
import org.springframework.boot.cli.command.status.ExitStatus;
@@ -54,13 +56,16 @@ public class InitCommand extends OptionParsingCommand {
}
@Override
public String getExamples() {
StringBuilder sb = new StringBuilder();
sb.append("Lists the capabilities of the service: spring init --list").append(NEW_LINE);
sb.append("Creates a default project: spring init").append(NEW_LINE);
sb.append("Creates a web my-app.zip: spring init -d=web my-app.zip").append(NEW_LINE);
sb.append("Creates a web/data-jpa gradle project unpacked: spring init -d=web,jpa --build=gradle my-dir/");
return sb.toString();
public Collection<HelpExample> getExamples() {
List<HelpExample> examples = new ArrayList<HelpExample>();
examples.add(new HelpExample("To list all the capabilities of the service",
"spring init --list"));
examples.add(new HelpExample("To creates a default project", "spring init"));
examples.add(new HelpExample("To create a web my-app.zip",
"spring init -d=web my-app.zip"));
examples.add(new HelpExample("To create a web/data-jpa gradle project unpacked",
"spring init -d=web,jpa --build=gradle my-dir/"));
return examples;
}
static class InitOptionHandler extends OptionHandler {

View File

@@ -81,18 +81,18 @@ class ProjectGenerationRequest {
if (output != null && output.endsWith("/")) {
this.output = output.substring(0, output.length() - 1);
this.extract = true;
} else {
}
else {
this.output = output;
}
}
/**
* Specify if the project archive should be extract in the output location. If
* the {@link #getOutput() output} ends with "/", the project is extracted
* automatically.
* Specify if the project archive should be extract in the output location. If the
* {@link #getOutput() output} ends with "/", the project is extracted automatically.
*/
public boolean isExtract() {
return extract;
return this.extract;
}
public void setExtract(boolean extract) {
@@ -221,8 +221,7 @@ class ProjectGenerationRequest {
return builder.build();
}
catch (URISyntaxException e) {
throw new ReportableException("Invalid service URL (" + e.getMessage()
+ ")");
throw new ReportableException("Invalid service URL (" + e.getMessage() + ")");
}
}
@@ -230,8 +229,8 @@ class ProjectGenerationRequest {
if (this.type != null) {
ProjectType result = metadata.getProjectTypes().get(this.type);
if (result == null) {
throw new ReportableException(("No project type with id '"
+ this.type + "' - check the service capabilities (--list)"));
throw new ReportableException(
("No project type with id '" + this.type + "' - check the service capabilities (--list)"));
}
return result;
}
@@ -248,8 +247,8 @@ class ProjectGenerationRequest {
return types.values().iterator().next();
}
else if (types.size() == 0) {
throw new ReportableException("No type found with build '"
+ this.build + "' and format '" + this.format
throw new ReportableException("No type found with build '" + this.build
+ "' and format '" + this.format
+ "' check the service capabilities (--list)");
}
else {

View File

@@ -28,10 +28,12 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
/**
* Helper class used to generate the project.
*
* @author Stephane Nicoll
* @since 1.2.0
*/
public class ProjectGenerator {
class ProjectGenerator {
private static final String ZIP_MIME_TYPE = "application/zip";
@@ -41,9 +43,11 @@ public class ProjectGenerator {
this.initializrService = initializrService;
}
public void generateProject(ProjectGenerationRequest request, boolean force) throws IOException {
public void generateProject(ProjectGenerationRequest request, boolean force)
throws IOException {
ProjectGenerationResponse response = this.initializrService.generate(request);
String fileName = (request.getOutput() != null ? request.getOutput() : response.getFileName());
String fileName = (request.getOutput() != null ? request.getOutput() : response
.getFileName());
if (request.isExtract()) {
if (isZipArchive(response)) {
extractProject(response, request.getOutput(), force);
@@ -51,7 +55,8 @@ public class ProjectGenerator {
}
else {
Log.info("Could not extract '" + response.getContentType() + "'");
fileName = response.getFileName(); // Use value from the server since we can't extract it
fileName = response.getFileName(); // Use value from the server since we
// can't extract it
}
}
if (fileName == null) {