use of com.facebook.buck.query.QueryTarget in project buck by facebook.
the class QueryCommand method runMultipleQuery.
/**
* Evaluate multiple queries in a single `buck query` run. Usage:
* buck query <query format> <input1> <input2> <...> <inputN>
*/
static int runMultipleQuery(CommandRunnerParams params, BuckQueryEnvironment env, ListeningExecutorService executor, String queryFormat, List<String> inputsFormattedAsBuildTargets, boolean generateJsonOutput) throws IOException, InterruptedException, QueryException {
if (inputsFormattedAsBuildTargets.isEmpty()) {
params.getBuckEventBus().post(ConsoleEvent.severe("Specify one or more input targets after the query expression format"));
return 1;
}
// Do an initial pass over the query arguments and parse them into their expressions so we can
// preload all the target patterns from every argument in one go, as doing them one-by-one is
// really inefficient.
Set<String> targetLiterals = new LinkedHashSet<>();
for (String input : inputsFormattedAsBuildTargets) {
String query = queryFormat.replace("%s", input);
QueryExpression expr = QueryExpression.parse(query, env);
expr.collectTargetPatterns(targetLiterals);
}
env.preloadTargetPatterns(targetLiterals, executor);
// Now execute the query on the arguments one-by-one.
TreeMultimap<String, QueryTarget> queryResultMap = TreeMultimap.create();
for (String input : inputsFormattedAsBuildTargets) {
String query = queryFormat.replace("%s", input);
ImmutableSet<QueryTarget> queryResult = env.evaluateQuery(query, executor);
queryResultMap.putAll(input, queryResult);
}
LOG.debug("Printing out the following targets: " + queryResultMap);
if (generateJsonOutput) {
CommandHelper.printJSON(params, queryResultMap);
} else {
CommandHelper.printToConsole(params, queryResultMap);
}
return 0;
}
use of com.facebook.buck.query.QueryTarget in project buck by facebook.
the class QueryCommand method collectAndPrintAttributes.
private void collectAndPrintAttributes(CommandRunnerParams params, BuckQueryEnvironment env, Set<QueryTarget> queryResult) throws QueryException {
PatternsMatcher patternsMatcher = new PatternsMatcher(outputAttributes.get());
SortedMap<String, SortedMap<String, Object>> result = Maps.newTreeMap();
for (QueryTarget target : queryResult) {
if (!(target instanceof QueryBuildTarget)) {
continue;
}
TargetNode<?, ?> node = env.getNode(target);
try {
SortedMap<String, Object> sortedTargetRule = params.getParser().getRawTargetNode(env.getParserState(), params.getCell(), node);
if (sortedTargetRule == null) {
params.getConsole().printErrorText("unable to find rule for target " + node.getBuildTarget().getFullyQualifiedName());
continue;
}
SortedMap<String, Object> attributes = Maps.newTreeMap();
if (patternsMatcher.hasPatterns()) {
for (String key : sortedTargetRule.keySet()) {
String snakeCaseKey = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key);
if (patternsMatcher.matches(snakeCaseKey)) {
attributes.put(snakeCaseKey, sortedTargetRule.get(key));
}
}
}
result.put(node.getBuildTarget().getUnflavoredBuildTarget().getFullyQualifiedName(), attributes);
} catch (BuildFileParseException e) {
params.getConsole().printErrorText("unable to find rule for target " + node.getBuildTarget().getFullyQualifiedName());
continue;
}
}
StringWriter stringWriter = new StringWriter();
try {
params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(stringWriter, result);
} catch (IOException e) {
// Shouldn't be possible while writing to a StringWriter...
throw new RuntimeException(e);
}
String output = stringWriter.getBuffer().toString();
params.getConsole().getStdOut().println(output);
}
use of com.facebook.buck.query.QueryTarget in project buck by facebook.
the class TargetPatternEvaluator method resolveBuildTargetPatterns.
ImmutableMap<String, ImmutableSet<QueryTarget>> resolveBuildTargetPatterns(List<String> patterns, ListeningExecutorService executor) throws InterruptedException, BuildFileParseException, BuildTargetException, IOException {
// Build up an ordered list of patterns and pass them to the parse to get resolved in one go.
// The returned list of nodes maintains the spec list ordering.
List<TargetNodeSpec> specs = new ArrayList<>();
for (String pattern : patterns) {
specs.addAll(targetNodeSpecParser.parse(rootCell.getCellPathResolver(), pattern));
}
ImmutableList<ImmutableSet<BuildTarget>> buildTargets = parser.resolveTargetSpecs(eventBus, rootCell, enableProfiling, executor, specs, SpeculativeParsing.of(false), // because the query engine doesn't handle flavors very well.
ParserConfig.ApplyDefaultFlavorsMode.DISABLED);
LOG.verbose("Resolved target patterns %s -> targets %s", patterns, buildTargets);
// Convert the ordered result into a result map of pattern to set of resolved targets.
ImmutableMap.Builder<String, ImmutableSet<QueryTarget>> queryTargets = ImmutableMap.builder();
for (int index = 0; index < buildTargets.size(); index++) {
ImmutableSet<BuildTarget> targets = buildTargets.get(index);
// Sorting to have predictable results across different java libraries implementations.
ImmutableSet.Builder<QueryTarget> builder = ImmutableSortedSet.naturalOrder();
for (BuildTarget target : targets) {
builder.add(QueryBuildTarget.of(target));
}
queryTargets.put(patterns.get(index), builder.build());
}
return queryTargets.build();
}
use of com.facebook.buck.query.QueryTarget in project buck by facebook.
the class TargetPatternEvaluator method resolveTargetPatterns.
ImmutableMap<String, ImmutableSet<QueryTarget>> resolveTargetPatterns(Iterable<String> patterns, ListeningExecutorService executor) throws InterruptedException, BuildFileParseException, BuildTargetException, IOException {
ImmutableMap.Builder<String, ImmutableSet<QueryTarget>> resolved = ImmutableMap.builder();
Map<String, String> unresolved = new HashMap<>();
for (String pattern : patterns) {
// First check if this pattern was resolved before.
ImmutableSet<QueryTarget> targets = resolvedTargets.get(pattern);
if (targets != null) {
resolved.put(pattern, targets);
continue;
}
// Check if this is an alias.
ImmutableSet<BuildTarget> aliasTargets = buckConfig.getBuildTargetsForAlias(pattern);
if (!aliasTargets.isEmpty()) {
for (BuildTarget alias : aliasTargets) {
unresolved.put(alias.getFullyQualifiedName(), pattern);
}
} else {
// Check if the pattern corresponds to a build target or a path.
if (pattern.contains("//") || pattern.startsWith(":")) {
unresolved.put(pattern, pattern);
} else {
ImmutableSet<QueryTarget> fileTargets = resolveFilePattern(pattern);
resolved.put(pattern, fileTargets);
resolvedTargets.put(pattern, fileTargets);
}
}
}
// Resolve any remaining target patterns using the parser.
ImmutableMap<String, ImmutableSet<QueryTarget>> results = MoreMaps.transformKeys(resolveBuildTargetPatterns(ImmutableList.copyOf(unresolved.keySet()), executor), Functions.forMap(unresolved));
resolved.putAll(results);
resolvedTargets.putAll(results);
return resolved.build();
}
use of com.facebook.buck.query.QueryTarget in project buck by facebook.
the class BuckQueryEnvironment method buildTransitiveClosure.
@Override
public void buildTransitiveClosure(Set<QueryTarget> targets, int maxDepth, ListeningExecutorService executor) throws QueryException, InterruptedException {
// Filter QueryTargets that are build targets and not yet present in the build target graph.
Set<BuildTarget> graphTargets = getTargetsFromNodes(graph.getNodes());
Set<BuildTarget> newBuildTargets = new HashSet<>();
for (QueryTarget target : targets) {
if (target instanceof QueryBuildTarget) {
BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
if (!graphTargets.contains(buildTarget)) {
newBuildTargets.add(buildTarget);
}
}
}
if (!newBuildTargets.isEmpty()) {
buildGraphForBuildTargets(Sets.union(newBuildTargets, graphTargets));
for (BuildTarget buildTarget : getTargetsFromNodes(graph.getNodes())) {
if (!buildTargetToQueryTarget.containsKey(buildTarget)) {
buildTargetToQueryTarget.put(buildTarget, QueryBuildTarget.of(buildTarget));
}
}
}
}
Aggregations