-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the default cquery output format to its own callback and standar…
…dize cquery output callback logic PiperOrigin-RevId: 190667120
- Loading branch information
Showing
8 changed files
with
207 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/google/devtools/build/lib/query2/CqueryThreadsafeCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.query2; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.Streams; | ||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback; | ||
import com.google.devtools.build.lib.query2.output.CqueryOptions; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Parents class for cquery output callbacks. Handles names and outputting contents of result list | ||
* that is populated by child classes. | ||
*/ | ||
public abstract class CqueryThreadsafeCallback | ||
extends ThreadSafeOutputFormatterCallback<ConfiguredTarget> { | ||
|
||
protected final CqueryOptions options; | ||
protected PrintStream printStream = null; | ||
|
||
private final List<String> result = new ArrayList<>(); | ||
|
||
CqueryThreadsafeCallback(CqueryOptions options, OutputStream out) { | ||
this.options = options; | ||
if (out != null) { | ||
this.printStream = new PrintStream(out); | ||
} | ||
} | ||
|
||
public abstract String getName(); | ||
|
||
public static String callbackNames(Iterable<CqueryThreadsafeCallback> callbacks) { | ||
return Streams.stream(callbacks).map(CqueryThreadsafeCallback::getName).collect(joining(", ")); | ||
} | ||
|
||
public static CqueryThreadsafeCallback getCallback( | ||
String type, Iterable<CqueryThreadsafeCallback> callbacks) { | ||
for (CqueryThreadsafeCallback callback : callbacks) { | ||
if (callback.getName().equals(type)) { | ||
return callback; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public void addResult(String string) { | ||
result.add(string); | ||
} | ||
|
||
@VisibleForTesting | ||
public List<String> getResult() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public void close(boolean failFast) throws InterruptedException, IOException { | ||
if (!failFast && printStream != null) { | ||
result.forEach(printStream::println); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...va/com/google/devtools/build/lib/query2/LabelAndConfigurationOutputFormatterCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.query2; | ||
|
||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.analysis.config.BuildConfiguration; | ||
import com.google.devtools.build.lib.query2.output.CqueryOptions; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Default Output callback for cquery. Prints a label and configuration pair per | ||
* result. | ||
*/ | ||
public class LabelAndConfigurationOutputFormatterCallback extends CqueryThreadsafeCallback { | ||
|
||
LabelAndConfigurationOutputFormatterCallback(CqueryOptions options, OutputStream out) { | ||
super(options, out); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "label"; | ||
} | ||
|
||
@Override | ||
public void processOutput(Iterable<ConfiguredTarget> partialResult) { | ||
for (ConfiguredTarget configuredTarget : partialResult) { | ||
BuildConfiguration config = configuredTarget.getConfiguration(); | ||
StringBuilder output = | ||
new StringBuilder() | ||
.append(configuredTarget.getLabel()) | ||
.append(" (") | ||
.append(config != null && config.isHostConfiguration() ? "HOST" : config) | ||
.append(")"); | ||
addResult(output.toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.