Search in sources :

Example 1 with Query

use of com.facebook.buck.rules.query.Query in project buck by facebook.

the class QueryMacroExpander method extractTargets.

private Stream<BuildTarget> extractTargets(BuildTarget target, CellPathResolver cellNames, Optional<BuildRuleResolver> resolver, T input) {
    String queryExpression = CharMatcher.anyOf("\"'").trimFrom(input.getQuery().getQuery());
    final GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(resolver, targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
    try {
        QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
        HashSet<String> targetLiterals = new HashSet<>();
        parsedExp.collectTargetPatterns(targetLiterals);
        return targetLiterals.stream().flatMap(pattern -> {
            try {
                return env.getTargetsMatchingPattern(pattern, executorService).stream();
            } catch (Exception e) {
                throw new HumanReadableException(e, "Error parsing target expression %s for target %s", pattern, target);
            }
        }).map(queryTarget -> {
            Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
            return ((QueryBuildTarget) queryTarget).getBuildTarget();
        });
    } catch (QueryException e) {
        throw new HumanReadableException("Error executing query in macro for target %s", target, e);
    }
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ImmutableSet(com.google.common.collect.ImmutableSet) CellPathResolver(com.facebook.buck.rules.CellPathResolver) QueryException(com.facebook.buck.query.QueryException) TargetGraph(com.facebook.buck.rules.TargetGraph) CharMatcher(com.google.common.base.CharMatcher) Set(java.util.Set) Query(com.facebook.buck.rules.query.Query) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Stream(java.util.stream.Stream) QueryExpression(com.facebook.buck.query.QueryExpression) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) QueryTarget(com.facebook.buck.query.QueryTarget) GraphEnhancementQueryEnvironment(com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) GraphEnhancementQueryEnvironment(com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment) QueryException(com.facebook.buck.query.QueryException) HumanReadableException(com.facebook.buck.util.HumanReadableException) QueryExpression(com.facebook.buck.query.QueryExpression) QueryException(com.facebook.buck.query.QueryException) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) HashSet(java.util.HashSet)

Aggregations

BuildTarget (com.facebook.buck.model.BuildTarget)1 MacroException (com.facebook.buck.model.MacroException)1 BuildTargetPatternParser (com.facebook.buck.parser.BuildTargetPatternParser)1 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)1 QueryException (com.facebook.buck.query.QueryException)1 QueryExpression (com.facebook.buck.query.QueryExpression)1 QueryTarget (com.facebook.buck.query.QueryTarget)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 CellPathResolver (com.facebook.buck.rules.CellPathResolver)1 TargetGraph (com.facebook.buck.rules.TargetGraph)1 GraphEnhancementQueryEnvironment (com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment)1 Query (com.facebook.buck.rules.query.Query)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 CharMatcher (com.google.common.base.CharMatcher)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 HashSet (java.util.HashSet)1