Skip to content

Commit

Permalink
bazel syntax: break Printer -> Label dependency
Browse files Browse the repository at this point in the history
The only use of Label is in WorkspacePrinter, which can be moved near one of its callers.
The other caller can be trivially simplified.

PiperOrigin-RevId: 270262707
  • Loading branch information
Googler authored and copybara-github committed Sep 20, 2019
1 parent 94e6cf6 commit 40fa815
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.devtools.build.lib.analysis.NoBuildRequestFinishedEvent;
import com.google.devtools.build.lib.bazel.repository.RepositoryOrderEvent;
import com.google.devtools.build.lib.bazel.repository.skylark.SkylarkRepositoryFunction;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelConstants;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
Expand Down Expand Up @@ -243,12 +244,12 @@ private static boolean shouldSync(Rule rule, boolean configure) {

private static ResolvedEvent resolveBind(Rule rule) {
String name = rule.getName();
Object actual = rule.getAttributeContainer().getAttr("actual");
Label actual = (Label) rule.getAttributeContainer().getAttr("actual");
String nativeCommand =
"bind(name = "
+ Printer.getPrinter().repr(name)
+ ", actual = "
+ Printer.getWorkspacePrettyPrinter().repr(actual)
+ Printer.getPrinter().repr(actual.getCanonicalForm())
+ ")";

return new ResolvedEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.eventbus.Subscribe;
import com.google.common.io.Files;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.ExtendedEventHandler.ResolvedEvent;
import com.google.devtools.build.lib.runtime.BlazeModule;
import com.google.devtools.build.lib.runtime.Command;
Expand Down Expand Up @@ -79,9 +80,7 @@ public void afterCommand() {
}
try (Writer writer = Files.newWriter(new File(resolvedFile), StandardCharsets.UTF_8)) {
writer.write(
EXPORTED_NAME
+ " = "
+ Printer.getWorkspacePrettyPrinter().repr(resultBuilder.build()));
EXPORTED_NAME + " = " + getWorkspacePrettyPrinter().repr(resultBuilder.build()));
writer.close();
} catch (IOException e) {
logger.warning("IO Error writing to file " + resolvedFile + ": " + e);
Expand All @@ -91,6 +90,26 @@ public void afterCommand() {
this.resolvedValues = null;
}

/**
* Returns a pretty printer that represents values in a form usable in WORKSPACE files.
*
* <p>In WORKSPACE files, the Label constructor is not available. Fortunately, in all places where
* a label is needed, we can pass the canonical string associated with this label.
*/
private static Printer.PrettyPrinter getWorkspacePrettyPrinter() {
return new Printer.PrettyPrinter(new StringBuilder()) {
@Override
public Printer.BasePrinter repr(Object o) {
if (o instanceof Label) {
this.repr(((Label) o).getCanonicalForm());
} else {
super.repr(o);
}
return this;
}
};
}

@Subscribe
public void repositoryOrderEvent(RepositoryOrderEvent event) {
orderedNames = event.getOrderedNames();
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/google/devtools/build/lib/syntax/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrintable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
Expand Down Expand Up @@ -81,15 +80,6 @@ public static PrettyPrinter getPrettyPrinter() {
return new PrettyPrinter(new StringBuilder());
}

/**
* Creates an instance of {@link WorkspacePrettyPrinter} with an empty buffer.
*
* @return new {@link WorkspacePrettyPrinter}
*/
public static WorkspacePrettyPrinter getWorkspacePrettyPrinter() {
return new WorkspacePrettyPrinter(new StringBuilder());
}

/**
* Creates an instance of {@link BasePrinter} with an empty buffer and whose format strings allow
* only %s and %%.
Expand Down Expand Up @@ -713,29 +703,6 @@ public BasePrinter printList(
}
}

/**
* A pretty printer that represents values in a form usable in WORKSPACE files.
*
* <p>In WORKSPACE files, the Label constructor is not available. Fortunately, in all places where
* a label is needed, we can pass the canonical string associated with this label.
*/
public static class WorkspacePrettyPrinter extends PrettyPrinter {

protected WorkspacePrettyPrinter(Appendable buffer) {
super(buffer);
}

@Override
public BasePrinter repr(Object o) {
if (o instanceof Label) {
this.repr(((Label) o).getCanonicalForm());
} else {
super.repr(o);
}
return this;
}
}

/** A version of {@code BasePrinter} that is able to print abbreviated lists. */
public static final class LengthLimitedPrinter extends BasePrinter {

Expand Down

0 comments on commit 40fa815

Please sign in to comment.