Build.java
package org.docascode.cli;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Target;
import org.docascode.api.BuildCommand;
import org.docascode.api.DocAsCode;
import org.docascode.api.core.errors.DocAsCodeException;
import org.fusesource.jansi.Ansi;
import picocli.CommandLine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.fusesource.jansi.Ansi.Color.RED;
@CommandLine.Command(name = "build",
description = "Performs the build defined in delivery.xml.")
public class Build extends Command implements Runnable {
@CommandLine.Option(names = {"-t", "--target"}, arity = "0..*", description = "The Targets to run.")
private List<String> targets = new ArrayList<>();
@CommandLine.Option(names = {"-p", "--property"}, description = "Define properties to inject in your build.")
private Map<String, String> properties = new HashMap<>();
@CommandLine.Option(names = {"-l", "--list"}, description = "List the targets defined in delivery.xml.")
private Boolean list = false;
@CommandLine.Option(names = {"--log-level"}, description = "Set the output log-level. Valid values: ${COMPLETION-CANDIDATES}")
private BuildCommand.LogLevel logLevel = BuildCommand.LogLevel.INFO;
@Override
public void run() {
try (DocAsCode docascode = DocAsCode.open()) {
if (list){
Map<String, Target> listTargets = docascode.build()
.listTargets();
for ( Map.Entry<String, Target> entry : listTargets.entrySet()){
success(
String.format("%s (defined in %s)%n",
entry.getKey(),
entry.getValue().getLocation().getFileName())
);
info(
String.format(" %s%n",
entry.getValue().getDescription() == null ? "No Description" : entry.getValue().getDescription())
);
}
} else {
docascode.build()
.setTargets(targets)
.setLogLevel(logLevel)
.setProperties(properties)
.call();
success("BUILD SUCCESSFULL\n");
System.exit(0);
}
} catch (DocAsCodeException e) {
error(e);
} catch (BuildException e){
logger.println(
Ansi.ansi().fg(RED)
.a("BUILD FAILED\n"));
logger.println(
Ansi.ansi().fg(RED)
.a(e.getMessage()).reset());
for (StackTraceElement s : e.getStackTrace()){
logger.println(
Ansi.ansi().fg(RED)
.a(s.toString()).reset());
}
System.exit(1);
}
}
}