Search in sources :

Example 6 with QueryTarget

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryTarget(com.facebook.buck.query.QueryTarget) QueryExpression(com.facebook.buck.query.QueryExpression)

Example 7 with QueryTarget

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);
}
Also used : PatternsMatcher(com.facebook.buck.util.PatternsMatcher) IOException(java.io.IOException) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) QueryTarget(com.facebook.buck.query.QueryTarget) StringWriter(java.io.StringWriter) SortedMap(java.util.SortedMap) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget)

Example 8 with QueryTarget

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();
}
Also used : ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) QueryTarget(com.facebook.buck.query.QueryTarget) ImmutableSet(com.google.common.collect.ImmutableSet) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) TargetNodeSpec(com.facebook.buck.parser.TargetNodeSpec)

Example 9 with QueryTarget

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();
}
Also used : QueryTarget(com.facebook.buck.query.QueryTarget) ImmutableSet(com.google.common.collect.ImmutableSet) HashMap(java.util.HashMap) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with QueryTarget

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));
            }
        }
    }
}
Also used : QueryTarget(com.facebook.buck.query.QueryTarget) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

QueryTarget (com.facebook.buck.query.QueryTarget)14 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 BuildTarget (com.facebook.buck.model.BuildTarget)7 QueryExpression (com.facebook.buck.query.QueryExpression)5 QueryException (com.facebook.buck.query.QueryException)4 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)3 LinkedHashSet (java.util.LinkedHashSet)3 MacroException (com.facebook.buck.model.MacroException)2 BuildTargetPatternParser (com.facebook.buck.parser.BuildTargetPatternParser)2 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)2 CellPathResolver (com.facebook.buck.rules.CellPathResolver)2 TargetGraph (com.facebook.buck.rules.TargetGraph)2 GraphEnhancementQueryEnvironment (com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2