diff --git a/.idea/libraries/bld.xml b/.idea/libraries/bld.xml index e6f1eb5..0a7a3e9 100644 --- a/.idea/libraries/bld.xml +++ b/.idea/libraries/bld.xml @@ -2,12 +2,12 @@ - + - + diff --git a/lib/bld/bld-wrapper.jar b/lib/bld/bld-wrapper.jar index 66e0105..5e7beae 100644 Binary files a/lib/bld/bld-wrapper.jar and b/lib/bld/bld-wrapper.jar differ diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 1976594..798472e 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -8,4 +8,4 @@ bld.javaOptions= bld.javacOptions= bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES bld.sourceDirectories=core/src/bld/java -bld.version=1.9.2-SNAPSHOT \ No newline at end of file +bld.version=2.0.0-SNAPSHOT \ No newline at end of file diff --git a/src/main/java/rife/bld/BuildExecutor.java b/src/main/java/rife/bld/BuildExecutor.java index 61872c1..60e93f6 100644 --- a/src/main/java/rife/bld/BuildExecutor.java +++ b/src/main/java/rife/bld/BuildExecutor.java @@ -241,49 +241,15 @@ public int execute(String[] arguments) { return exitStatus_; } - var first_command = true; var json_template = TemplateFactory.JSON.get("bld.executor_execute"); - var exception_caught = false; while (!arguments_.isEmpty()) { var command = arguments_.remove(0); try { - 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)); - } - - try { - if (!executeCommand(command)) { - break; - } - } - finally { - if (outputJson()) { - if (first_command) { - json_template.blankValue("separator"); - } - else { - json_template.setValue("separator", ", "); - } - json_template.setValueEncoded("command", command); - json_template.setValueEncoded("out", out.toString(StandardCharsets.UTF_8)); - json_template.setValueEncoded("err", err.toString(StandardCharsets.UTF_8)); - json_template.appendBlock("commands", "command"); - - System.setOut(orig_out); - System.setErr(orig_err); - } - - first_command = false; + if (!executeCommand(json_template, command)) { + break; } } catch (Throwable e) { - exception_caught = true; - exitStatus(1); if (outputJson()) { @@ -335,10 +301,8 @@ public int execute(String[] arguments) { } } - if (!exception_caught) { - if (outputJson()) { - System.out.println(json_template.getContent()); - } + if (outputJson() && exitStatus_ == ExitStatusException.EXIT_SUCCESS) { + System.out.println(json_template.getContent()); } return exitStatus_; @@ -484,6 +448,16 @@ public String getDescription(String topic) { * @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); @@ -532,9 +506,18 @@ public boolean executeCommand(String 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 { - currentCommandName_.set(matched_command); - currentCommandDefinition_.set(definition); definition.execute(); } catch (ExitStatusException e) { exitStatus(e.getExitStatus()); @@ -542,6 +525,22 @@ public boolean executeCommand(String command) } 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 + "'"; diff --git a/src/main/java/rife/bld/operations/HelpOperation.java b/src/main/java/rife/bld/operations/HelpOperation.java index 8da4c3f..1675646 100644 --- a/src/main/java/rife/bld/operations/HelpOperation.java +++ b/src/main/java/rife/bld/operations/HelpOperation.java @@ -100,14 +100,12 @@ private void executePrintOverviewHelp(Exception exception) { t.setValueEncoded("error-message", ExceptionUtils.getExceptionStackTrace(exception)); } - boolean first = true; for (var command : commands.entrySet()) { - if (first) { - first = false; - t.blankValue("separator"); + if (t.isValueSet("commands")) { + t.setValue("separator", ", "); } else { - t.setValue("separator", ", "); + t.blankValue("separator"); } t.setValueEncoded("command", command.getKey()); var build_help = command.getValue().getHelp();