mirror of
https://github.com/ethauvin/bld.git
synced 2025-04-25 16:27:11 -07:00
Removed generic json output support and only add it for the help operation
This commit is contained in:
parent
48c43a05ed
commit
8e02a3ac7e
6 changed files with 54 additions and 132 deletions
Binary file not shown.
|
@ -39,7 +39,6 @@ public class BuildExecutor {
|
||||||
private static final String ARG_HELP3 = "-?";
|
private static final String ARG_HELP3 = "-?";
|
||||||
private static final String ARG_STACKTRACE1 = "--stacktrace";
|
private static final String ARG_STACKTRACE1 = "--stacktrace";
|
||||||
private static final String ARG_STACKTRACE2 = "-s";
|
private static final String ARG_STACKTRACE2 = "-s";
|
||||||
private static final String ARG_JSON = "--json";
|
|
||||||
|
|
||||||
private final HierarchicalProperties properties_;
|
private final HierarchicalProperties properties_;
|
||||||
private List<String> arguments_ = Collections.emptyList();
|
private List<String> arguments_ = Collections.emptyList();
|
||||||
|
@ -48,7 +47,6 @@ public class BuildExecutor {
|
||||||
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>();
|
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>();
|
||||||
private final AtomicReference<CommandDefinition> currentCommandDefinition_ = new AtomicReference<>();
|
private final AtomicReference<CommandDefinition> currentCommandDefinition_ = new AtomicReference<>();
|
||||||
private int exitStatus_ = 0;
|
private int exitStatus_ = 0;
|
||||||
private boolean outputJson_ = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show the full Java stacktrace when exceptions occur, as opposed
|
* Show the full Java stacktrace when exceptions occur, as opposed
|
||||||
|
@ -127,17 +125,6 @@ public class BuildExecutor {
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether output should be in the JSON format.
|
|
||||||
*
|
|
||||||
* @return {@code true} if JSON output is enabled;
|
|
||||||
* or {@code false} otherwise
|
|
||||||
* @since 2.0
|
|
||||||
*/
|
|
||||||
public boolean outputJson() {
|
|
||||||
return outputJson_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the properties uses by this conversation.
|
* Returns the properties uses by this conversation.
|
||||||
*
|
*
|
||||||
|
@ -227,10 +214,6 @@ public class BuildExecutor {
|
||||||
public int execute(String[] arguments) {
|
public int execute(String[] arguments) {
|
||||||
arguments_ = new ArrayList<>(Arrays.asList(arguments));
|
arguments_ = new ArrayList<>(Arrays.asList(arguments));
|
||||||
|
|
||||||
if (!arguments_.isEmpty() && arguments_.get(0).equals(ARG_JSON)) {
|
|
||||||
outputJson_ = true;
|
|
||||||
arguments_.remove(0);
|
|
||||||
}
|
|
||||||
var show_help = false;
|
var show_help = false;
|
||||||
show_help |= arguments_.removeAll(List.of(ARG_HELP1, ARG_HELP2, ARG_HELP3));
|
show_help |= arguments_.removeAll(List.of(ARG_HELP1, ARG_HELP2, ARG_HELP3));
|
||||||
showStacktrace = arguments_.removeAll(List.of(ARG_STACKTRACE1, ARG_STACKTRACE2));
|
showStacktrace = arguments_.removeAll(List.of(ARG_STACKTRACE1, ARG_STACKTRACE2));
|
||||||
|
@ -241,12 +224,11 @@ public class BuildExecutor {
|
||||||
return exitStatus_;
|
return exitStatus_;
|
||||||
}
|
}
|
||||||
|
|
||||||
var json_template = TemplateFactory.JSON.get("bld.executor_execute");
|
|
||||||
while (!arguments_.isEmpty()) {
|
while (!arguments_.isEmpty()) {
|
||||||
var command = arguments_.remove(0);
|
var command = arguments_.remove(0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!executeCommand(json_template, command)) {
|
if (!executeCommand(command)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
@ -256,58 +238,30 @@ public class BuildExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) {
|
|
||||||
System.out.println(json_template.getContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
return exitStatus_;
|
return exitStatus_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void outputCommandExecutionException(Throwable e) {
|
private void outputCommandExecutionException(Throwable e) {
|
||||||
if (outputJson()) {
|
System.err.println();
|
||||||
var t = TemplateFactory.JSON.get("bld.executor_error");
|
|
||||||
if (showStacktrace) {
|
if (showStacktrace) {
|
||||||
t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e));
|
System.err.println(ExceptionUtils.getExceptionStackTrace(e));
|
||||||
}
|
} else {
|
||||||
else {
|
boolean first_exception = true;
|
||||||
boolean first_exception = true;
|
var e2 = e;
|
||||||
var e2 = e;
|
while (e2 != null) {
|
||||||
while (e2 != null) {
|
if (e2.getMessage() != null) {
|
||||||
if (e2.getMessage() != null) {
|
if (!first_exception) {
|
||||||
t.setValueEncoded("error-message", e2.getMessage());
|
System.err.print("> ");
|
||||||
first_exception = false;
|
|
||||||
}
|
}
|
||||||
e2 = e2.getCause();
|
System.err.println(e2.getMessage());
|
||||||
}
|
first_exception = false;
|
||||||
|
|
||||||
if (first_exception) {
|
|
||||||
t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e));
|
|
||||||
}
|
}
|
||||||
|
e2 = e2.getCause();
|
||||||
}
|
}
|
||||||
System.out.println(t.getContent());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
System.err.println();
|
|
||||||
|
|
||||||
if (showStacktrace) {
|
if (first_exception) {
|
||||||
System.err.println(ExceptionUtils.getExceptionStackTrace(e));
|
System.err.println(ExceptionUtils.getExceptionStackTrace(e));
|
||||||
} else {
|
|
||||||
boolean first_exception = true;
|
|
||||||
var e2 = e;
|
|
||||||
while (e2 != null) {
|
|
||||||
if (e2.getMessage() != null) {
|
|
||||||
if (!first_exception) {
|
|
||||||
System.err.print("> ");
|
|
||||||
}
|
|
||||||
System.err.println(e2.getMessage());
|
|
||||||
first_exception = false;
|
|
||||||
}
|
|
||||||
e2 = e2.getCause();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first_exception) {
|
|
||||||
System.err.println(ExceptionUtils.getExceptionStackTrace(e));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -452,16 +406,6 @@ public class BuildExecutor {
|
||||||
* @since 1.5
|
* @since 1.5
|
||||||
*/
|
*/
|
||||||
public boolean executeCommand(String command)
|
public boolean executeCommand(String command)
|
||||||
throws Throwable {
|
|
||||||
var json_template = TemplateFactory.JSON.get("bld.executor_execute");
|
|
||||||
var result = executeCommand(json_template, command);
|
|
||||||
if (result && outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) {
|
|
||||||
System.out.println(json_template.getContent());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean executeCommand(Template jsonTemplate, String command)
|
|
||||||
throws Throwable {
|
throws Throwable {
|
||||||
var matched_command = command;
|
var matched_command = command;
|
||||||
var definition = buildCommands().get(command);
|
var definition = buildCommands().get(command);
|
||||||
|
@ -501,24 +445,13 @@ public class BuildExecutor {
|
||||||
// only proceed if exactly one match was found
|
// only proceed if exactly one match was found
|
||||||
if (matches.size() == 1) {
|
if (matches.size() == 1) {
|
||||||
matched_command = matches.get(0);
|
matched_command = matches.get(0);
|
||||||
if (!outputJson()) {
|
System.out.println("Executing matched command: " + matched_command);
|
||||||
System.out.println("Executing matched command: " + matched_command);
|
|
||||||
}
|
|
||||||
definition = buildCommands().get(matched_command);
|
definition = buildCommands().get(matched_command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute the command if we found one
|
// execute the command if we found one
|
||||||
if (definition != null) {
|
if (definition != null) {
|
||||||
var orig_out = System.out;
|
|
||||||
var orig_err = System.err;
|
|
||||||
var out = new ByteArrayOutputStream();
|
|
||||||
var err = new ByteArrayOutputStream();
|
|
||||||
if (outputJson()) {
|
|
||||||
System.setOut(new PrintStream(out, true, StandardCharsets.UTF_8));
|
|
||||||
System.setErr(new PrintStream(err, true, StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
|
|
||||||
currentCommandName_.set(matched_command);
|
currentCommandName_.set(matched_command);
|
||||||
currentCommandDefinition_.set(definition);
|
currentCommandDefinition_.set(definition);
|
||||||
try {
|
try {
|
||||||
|
@ -529,35 +462,12 @@ public class BuildExecutor {
|
||||||
} finally {
|
} finally {
|
||||||
currentCommandDefinition_.set(null);
|
currentCommandDefinition_.set(null);
|
||||||
currentCommandName_.set(null);
|
currentCommandName_.set(null);
|
||||||
|
|
||||||
if (outputJson()) {
|
|
||||||
if (jsonTemplate.isValueSet("commands")) {
|
|
||||||
jsonTemplate.setValue("separator", ", ");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
jsonTemplate.blankValue("separator");
|
|
||||||
}
|
|
||||||
jsonTemplate.setValueEncoded("command", matched_command);
|
|
||||||
jsonTemplate.setValueEncoded("out", out.toString(StandardCharsets.UTF_8));
|
|
||||||
jsonTemplate.setValueEncoded("err", err.toString(StandardCharsets.UTF_8));
|
|
||||||
jsonTemplate.appendBlock("commands", "command");
|
|
||||||
|
|
||||||
System.setOut(orig_out);
|
|
||||||
System.setErr(orig_err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var message = "Unknown command '" + command + "'";
|
var message = "Unknown command '" + command + "'";
|
||||||
if (outputJson()) {
|
new HelpOperation(this, arguments()).executePrintOverviewHelp();
|
||||||
var t = TemplateFactory.JSON.get("bld.executor_error");
|
System.err.println();
|
||||||
t.setValueEncoded("error-message", message);
|
System.err.println("ERROR: " + message);
|
||||||
System.out.println(t.getContent());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
new HelpOperation(this, arguments()).executePrintOverviewHelp();
|
|
||||||
System.err.println();
|
|
||||||
System.err.println("ERROR: " + message);
|
|
||||||
}
|
|
||||||
exitStatus(ExitStatusException.EXIT_FAILURE);
|
exitStatus(ExitStatusException.EXIT_FAILURE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,11 @@ import static java.util.Comparator.comparingInt;
|
||||||
* @since 1.5
|
* @since 1.5
|
||||||
*/
|
*/
|
||||||
public class HelpOperation {
|
public class HelpOperation {
|
||||||
|
private static final String JSON_ARGUMENT = "--json";
|
||||||
|
|
||||||
private final BuildExecutor executor_;
|
private final BuildExecutor executor_;
|
||||||
private final List<String> arguments_;
|
private final List<String> arguments_;
|
||||||
|
private boolean outputJson_ = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new help operation.
|
* Creates a new help operation.
|
||||||
|
@ -43,15 +46,25 @@ public class HelpOperation {
|
||||||
public void execute() {
|
public void execute() {
|
||||||
var topic = "";
|
var topic = "";
|
||||||
if (!arguments_.isEmpty()) {
|
if (!arguments_.isEmpty()) {
|
||||||
topic = arguments_.remove(0);
|
if (arguments_.get(0).equals(JSON_ARGUMENT)) {
|
||||||
|
arguments_.remove(0);
|
||||||
|
outputJson_ = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
topic = arguments_.remove(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!arguments_.isEmpty() && arguments_.get(0).equals(JSON_ARGUMENT)) {
|
||||||
|
arguments_.remove(0);
|
||||||
|
outputJson_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!executor_.outputJson()) {
|
if (!outputJson_) {
|
||||||
System.err.println("Welcome to bld " + BldVersion.getVersion() + ".");
|
System.err.println("Welcome to bld " + BldVersion.getVersion() + ".");
|
||||||
System.err.println();
|
System.err.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean print_full_help = true;
|
var print_full_help = true;
|
||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
try {
|
try {
|
||||||
var commands = executor_.buildCommands();
|
var commands = executor_.buildCommands();
|
||||||
|
@ -59,7 +72,7 @@ public class HelpOperation {
|
||||||
var command = commands.get(topic);
|
var command = commands.get(topic);
|
||||||
var help = command.getHelp().getDescription(topic);
|
var help = command.getHelp().getDescription(topic);
|
||||||
if (!help.isEmpty()) {
|
if (!help.isEmpty()) {
|
||||||
if (executor_.outputJson()) {
|
if (outputJson_) {
|
||||||
var t = TemplateFactory.JSON.get("bld.help_description");
|
var t = TemplateFactory.JSON.get("bld.help_description");
|
||||||
t.setValueEncoded("command", topic);
|
t.setValueEncoded("command", topic);
|
||||||
t.setValueEncoded("description", help);
|
t.setValueEncoded("description", help);
|
||||||
|
@ -93,7 +106,7 @@ public class HelpOperation {
|
||||||
private void executePrintOverviewHelp(Exception exception) {
|
private void executePrintOverviewHelp(Exception exception) {
|
||||||
var commands = executor_.buildCommands();
|
var commands = executor_.buildCommands();
|
||||||
|
|
||||||
if (executor_.outputJson()) {
|
if (outputJson_) {
|
||||||
var t = TemplateFactory.JSON.get("bld.help_commands");
|
var t = TemplateFactory.JSON.get("bld.help_commands");
|
||||||
|
|
||||||
if (exception != null) {
|
if (exception != null) {
|
||||||
|
@ -125,8 +138,9 @@ public class HelpOperation {
|
||||||
perform specific tasks. The help command provides more information about
|
perform specific tasks. The help command provides more information about
|
||||||
the other commands.
|
the other commands.
|
||||||
|
|
||||||
Usage: help [command]
|
Usage: help [command] [""" + JSON_ARGUMENT + "]");
|
||||||
|
System.err.println("""
|
||||||
|
|
||||||
The following commands are supported.
|
The following commands are supported.
|
||||||
""");
|
""");
|
||||||
|
|
||||||
|
@ -141,10 +155,10 @@ public class HelpOperation {
|
||||||
|
|
||||||
System.err.println("""
|
System.err.println("""
|
||||||
|
|
||||||
|
--json Output help in JSON format
|
||||||
-?, -h, --help Shows this help message
|
-?, -h, --help Shows this help message
|
||||||
-D<name>=<value> Set a JVM system property
|
-D<name>=<value> Set a JVM system property
|
||||||
-s, --stacktrace Print out the stacktrace for exceptions
|
-s, --stacktrace Print out the stacktrace for exceptions
|
||||||
--json Output in JSON format (only as first argument)
|
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ public class Wrapper {
|
||||||
|
|
||||||
public static final String BUILD_ARGUMENT = "--build";
|
public static final String BUILD_ARGUMENT = "--build";
|
||||||
public static final String JSON_ARGUMENT = "--json";
|
public static final String JSON_ARGUMENT = "--json";
|
||||||
|
public static final String HELP_COMMAND = "help";
|
||||||
|
|
||||||
static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/";
|
static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/";
|
||||||
static final String SONATYPE_SNAPSHOTS = "https://s01.oss.sonatype.org/content/repositories/snapshots/";
|
static final String SONATYPE_SNAPSHOTS = "https://s01.oss.sonatype.org/content/repositories/snapshots/";
|
||||||
|
@ -375,7 +376,7 @@ public class Wrapper {
|
||||||
|
|
||||||
private int installAndLaunch(List<String> arguments) {
|
private int installAndLaunch(List<String> arguments) {
|
||||||
if (!arguments.isEmpty()) {
|
if (!arguments.isEmpty()) {
|
||||||
File current_file = null;
|
File current_file;
|
||||||
try {
|
try {
|
||||||
current_file = new File(arguments.remove(0)).getCanonicalFile();
|
current_file = new File(arguments.remove(0)).getCanonicalFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -386,9 +387,17 @@ public class Wrapper {
|
||||||
if (BUILD_ARGUMENT.equals(arguments.get(0))) {
|
if (BUILD_ARGUMENT.equals(arguments.get(0))) {
|
||||||
launchMode_ = LaunchMode.Build;
|
launchMode_ = LaunchMode.Build;
|
||||||
arguments.remove(0);
|
arguments.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (arguments.size() >= 2 && JSON_ARGUMENT.equals(arguments.get(1))) {
|
var help_index = arguments.indexOf(HELP_COMMAND);
|
||||||
launchMode_ = LaunchMode.Json;
|
if (help_index != -1) {
|
||||||
|
try {
|
||||||
|
if (arguments.get(help_index + 1).equals(JSON_ARGUMENT) ||
|
||||||
|
arguments.get(help_index + 2).equals(JSON_ARGUMENT)) {
|
||||||
|
launchMode_ = LaunchMode.Json;
|
||||||
|
}
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// no-op, there are no additional arguments, so definitely no json option
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"error": "{{v error-message/}}"
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
{
|
|
||||||
"commands": {{{v commands}}{{/v}}{{b command}}{{v separator/}}
|
|
||||||
"{{v command/}}": {
|
|
||||||
"out": "{{v out/}}",
|
|
||||||
"err": "{{v err/}}"
|
|
||||||
}{{/b}}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue