Skip to content

Commit

Permalink
$ blaze config: add --output=json
Browse files Browse the repository at this point in the history
This also generally refactors ConfigCommand to make a clean separation between
structural logic and output formatting logic.

ConfigCommand is responsible for taking a raw BuildConfiguration and translating it
into appropriate data structures that represent what it wants to output: what data
is output, how data relates to other data, and how results are ordered.

An output formatter then consumes these data structures to style the output
accordingly.

This also moves the logic that handled both out of BuildConfiguration & FragmentOptions.

It's neat how simple this makes JsonOutputFormatter. :)

Serves #10613.

PiperOrigin-RevId: 293152292
  • Loading branch information
gregestren authored and copybara-github committed Feb 4, 2020
1 parent 43ce0d4 commit 271b8ce
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 192 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ java_library(
"//src/main/protobuf:test_status_java_proto",
"//third_party:auto_value",
"//third_party:flogger",
"//third_party:gson",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/protobuf:protobuf_java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package com.google.devtools.build.lib.analysis.config;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Suppliers;
Expand Down Expand Up @@ -208,34 +206,6 @@ private int computeHashCode() {
return Objects.hash(fragments, buildOptions.getNativeOptions());
}

public void describe(StringBuilder sb) {
sb.append("BuildConfiguration ").append(checksum()).append(":\n");
// Fragments.
sb.append(" fragments: ")
.append(
getFragmentsMap().keySet().stream()
.sorted(comparing(Class::getName))
.map(Class::getName)
.collect(joining(",")))
.append("\n");
// Options.
getOptions().getFragmentClasses().stream()
.sorted(comparing(Class::getName))
.map(optionsClass -> getOptions().get(optionsClass))
.forEach(options -> options.describe(sb));
// User-defined options.
sb.append("Fragment user-defined {\n");
buildOptions.getStarlarkOptions().entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.forEach(
entry ->
sb.append(" ")
.append(entry.getKey().toString())
.append(": ")
.append(entry.getValue()));
sb.append("\n}");
}

@Override
public int hashCode() {
return hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.devtools.build.lib.analysis.config;

import static java.util.Map.Entry.comparingByKey;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.common.options.OptionDefinition;
Expand Down Expand Up @@ -136,14 +135,4 @@ public String getErrorMessage() {
public Map<OptionDefinition, SelectRestriction> getSelectRestrictions() {
return ImmutableMap.of();
}

public void describe(StringBuilder sb) {
sb.append("Fragment ").append(getClass().getName()).append(" {\n");
Map<String, Object> options = asMap();
options.entrySet().stream()
.sorted(comparingByKey())
.forEach(
e -> sb.append(" ").append(e.getKey()).append(": ").append(e.getValue()).append("\n"));
sb.append("}\n");
}
}
Loading

0 comments on commit 271b8ce

Please sign in to comment.