Skip to content

Commit

Permalink
Improvements to json operation.
Browse files Browse the repository at this point in the history
Upgraded bld to 2.0.0-SNAPSHOT.
  • Loading branch information
gbevin committed Jun 25, 2024
1 parent 793efb2 commit c912e43
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .idea/libraries/bld.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified lib/bld/bld-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/bld/bld-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
bld.version=2.0.0-SNAPSHOT
83 changes: 41 additions & 42 deletions src/main/java/rife/bld/BuildExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -532,16 +506,41 @@ 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());
return e.getExitStatus() == ExitStatusException.EXIT_SUCCESS;
} 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 + "'";
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/rife/bld/operations/HelpOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c912e43

Please sign in to comment.