2
0
Fork 0
mirror of https://github.com/ethauvin/bld.git synced 2025-04-25 08:17:11 -07:00

Removed generic json output support and only add it for the help operation

This commit is contained in:
Geert Bevin 2024-07-14 15:22:45 -04:00
parent 48c43a05ed
commit 8e02a3ac7e
6 changed files with 54 additions and 132 deletions

Binary file not shown.

View file

@ -39,7 +39,6 @@ public class BuildExecutor {
private static final String ARG_HELP3 = "-?";
private static final String ARG_STACKTRACE1 = "--stacktrace";
private static final String ARG_STACKTRACE2 = "-s";
private static final String ARG_JSON = "--json";
private final HierarchicalProperties properties_;
private List<String> arguments_ = Collections.emptyList();
@ -48,7 +47,6 @@ public class BuildExecutor {
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>();
private final AtomicReference<CommandDefinition> currentCommandDefinition_ = new AtomicReference<>();
private int exitStatus_ = 0;
private boolean outputJson_ = false;
/**
* Show the full Java stacktrace when exceptions occur, as opposed
@ -127,17 +125,6 @@ public class BuildExecutor {
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.
*
@ -227,10 +214,6 @@ public class BuildExecutor {
public int execute(String[] 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;
show_help |= arguments_.removeAll(List.of(ARG_HELP1, ARG_HELP2, ARG_HELP3));
showStacktrace = arguments_.removeAll(List.of(ARG_STACKTRACE1, ARG_STACKTRACE2));
@ -241,12 +224,11 @@ public class BuildExecutor {
return exitStatus_;
}
var json_template = TemplateFactory.JSON.get("bld.executor_execute");
while (!arguments_.isEmpty()) {
var command = arguments_.remove(0);
try {
if (!executeCommand(json_template, command)) {
if (!executeCommand(command)) {
break;
}
} catch (Throwable e) {
@ -256,58 +238,30 @@ public class BuildExecutor {
}
}
if (outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) {
System.out.println(json_template.getContent());
}
return exitStatus_;
}
private void outputCommandExecutionException(Throwable e) {
if (outputJson()) {
var t = TemplateFactory.JSON.get("bld.executor_error");
if (showStacktrace) {
t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e));
}
else {
boolean first_exception = true;
var e2 = e;
while (e2 != null) {
if (e2.getMessage() != null) {
t.setValueEncoded("error-message", e2.getMessage());
first_exception = false;
System.err.println();
if (showStacktrace) {
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("> ");
}
e2 = e2.getCause();
}
if (first_exception) {
t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(e));
System.err.println(e2.getMessage());
first_exception = false;
}
e2 = e2.getCause();
}
System.out.println(t.getContent());
}
else {
System.err.println();
if (showStacktrace) {
if (first_exception) {
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
*/
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 {
var matched_command = command;
var definition = buildCommands().get(command);
@ -501,24 +445,13 @@ public class BuildExecutor {
// only proceed if exactly one match was found
if (matches.size() == 1) {
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);
}
}
// execute the command if we found one
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);
currentCommandDefinition_.set(definition);
try {
@ -529,35 +462,12 @@ public class BuildExecutor {
} finally {
currentCommandDefinition_.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 {
var message = "Unknown command '" + command + "'";
if (outputJson()) {
var t = TemplateFactory.JSON.get("bld.executor_error");
t.setValueEncoded("error-message", message);
System.out.println(t.getContent());
}
else {
new HelpOperation(this, arguments()).executePrintOverviewHelp();
System.err.println();
System.err.println("ERROR: " + message);
}
new HelpOperation(this, arguments()).executePrintOverviewHelp();
System.err.println();
System.err.println("ERROR: " + message);
exitStatus(ExitStatusException.EXIT_FAILURE);
return false;
}

View file

@ -20,8 +20,11 @@ import static java.util.Comparator.comparingInt;
* @since 1.5
*/
public class HelpOperation {
private static final String JSON_ARGUMENT = "--json";
private final BuildExecutor executor_;
private final List<String> arguments_;
private boolean outputJson_ = false;
/**
* Creates a new help operation.
@ -43,15 +46,25 @@ public class HelpOperation {
public void execute() {
var topic = "";
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();
}
boolean print_full_help = true;
var print_full_help = true;
Exception exception = null;
try {
var commands = executor_.buildCommands();
@ -59,7 +72,7 @@ public class HelpOperation {
var command = commands.get(topic);
var help = command.getHelp().getDescription(topic);
if (!help.isEmpty()) {
if (executor_.outputJson()) {
if (outputJson_) {
var t = TemplateFactory.JSON.get("bld.help_description");
t.setValueEncoded("command", topic);
t.setValueEncoded("description", help);
@ -93,7 +106,7 @@ public class HelpOperation {
private void executePrintOverviewHelp(Exception exception) {
var commands = executor_.buildCommands();
if (executor_.outputJson()) {
if (outputJson_) {
var t = TemplateFactory.JSON.get("bld.help_commands");
if (exception != null) {
@ -125,8 +138,9 @@ public class HelpOperation {
perform specific tasks. The help command provides more information about
the other commands.
Usage: help [command]
Usage: help [command] [""" + JSON_ARGUMENT + "]");
System.err.println("""
The following commands are supported.
""");
@ -141,10 +155,10 @@ public class HelpOperation {
System.err.println("""
--json Output help in JSON format
-?, -h, --help Shows this help message
-D<name>=<value> Set a JVM system property
-s, --stacktrace Print out the stacktrace for exceptions
--json Output in JSON format (only as first argument)
""");
}
}

View file

@ -41,6 +41,7 @@ public class Wrapper {
public static final String BUILD_ARGUMENT = "--build";
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 SONATYPE_SNAPSHOTS = "https://s01.oss.sonatype.org/content/repositories/snapshots/";
@ -375,7 +376,7 @@ public class Wrapper {
private int installAndLaunch(List<String> arguments) {
if (!arguments.isEmpty()) {
File current_file = null;
File current_file;
try {
current_file = new File(arguments.remove(0)).getCanonicalFile();
} catch (IOException e) {
@ -386,9 +387,17 @@ public class Wrapper {
if (BUILD_ARGUMENT.equals(arguments.get(0))) {
launchMode_ = LaunchMode.Build;
arguments.remove(0);
}
if (arguments.size() >= 2 && JSON_ARGUMENT.equals(arguments.get(1))) {
launchMode_ = LaunchMode.Json;
var help_index = arguments.indexOf(HELP_COMMAND);
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
}
}
}

View file

@ -1,3 +0,0 @@
{
"error": "{{v error-message/}}"
}

View file

@ -1,8 +0,0 @@
{
"commands": {{{v commands}}{{/v}}{{b command}}{{v separator/}}
"{{v command/}}": {
"out": "{{v out/}}",
"err": "{{v err/}}"
}{{/b}}
}
}