Search in sources :

Example 1 with QueryExpression

use of com.facebook.buck.query.QueryExpression 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)

Example 2 with QueryExpression

use of com.facebook.buck.query.QueryExpression in project buck by facebook.

the class QueryMacroExpander method resolveQuery.

Stream<QueryTarget> resolveQuery(BuildTarget target, CellPathResolver cellNames, final BuildRuleResolver resolver, String queryExpression) throws MacroException {
    GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.of(resolver), targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
    try {
        QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
        Set<QueryTarget> queryTargets = parsedExp.eval(env, executorService);
        return queryTargets.stream();
    } catch (QueryException e) {
        throw new MacroException("Error parsing/executing query from macro", e);
    } catch (InterruptedException e) {
        throw new MacroException("Error executing query", e);
    }
}
Also used : QueryTarget(com.facebook.buck.query.QueryTarget) GraphEnhancementQueryEnvironment(com.facebook.buck.rules.query.GraphEnhancementQueryEnvironment) QueryException(com.facebook.buck.query.QueryException) QueryExpression(com.facebook.buck.query.QueryExpression) MacroException(com.facebook.buck.model.MacroException)

Example 3 with QueryExpression

use of com.facebook.buck.query.QueryExpression 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 4 with QueryExpression

use of com.facebook.buck.query.QueryExpression in project buck by facebook.

the class QueryUtils method resolveDepQuery.

public static Stream<BuildRule> resolveDepQuery(BuildRuleParams params, Query query, BuildRuleResolver resolver, TargetGraph targetGraph) {
    BuildTarget target = params.getBuildTarget();
    Set<BuildTarget> declaredDeps = params.getDeclaredDeps().get().stream().map(BuildRule::getBuildTarget).collect(Collectors.toSet());
    GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.of(resolver), Optional.of(targetGraph), params.getCellRoots(), BuildTargetPatternParser.forBaseName(target.getBaseName()), declaredDeps);
    ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService();
    try {
        QueryExpression parsedExp = QueryExpression.parse(query.getQuery(), env);
        Set<QueryTarget> queryTargets = parsedExp.eval(env, executorService);
        return queryTargets.stream().map(queryTarget -> {
            Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
            return resolver.getRule(((QueryBuildTarget) queryTarget).getBuildTarget());
        });
    } catch (QueryException e) {
        throw new RuntimeException("Error parsing/executing query from deps for " + target, e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Error executing query from deps for " + target, e);
    }
}
Also used : QueryTarget(com.facebook.buck.query.QueryTarget) QueryException(com.facebook.buck.query.QueryException) BuildTarget(com.facebook.buck.model.BuildTarget) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) QueryExpression(com.facebook.buck.query.QueryExpression) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget)

Example 5 with QueryExpression

use of com.facebook.buck.query.QueryExpression in project buck by facebook.

the class QueryUtils method extractBuildTargets.

public static Stream<BuildTarget> extractBuildTargets(CellPathResolver cellPathResolver, BuildTargetPatternParser<BuildTargetPattern> parserPattern, Query query) throws QueryException {
    GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.empty(), Optional.empty(), cellPathResolver, parserPattern, ImmutableSet.of());
    ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService();
    QueryExpression parsedExp = QueryExpression.parse(query.getQuery(), env);
    List<String> targetLiterals = new ArrayList<>();
    parsedExp.collectTargetPatterns(targetLiterals);
    return targetLiterals.stream().flatMap(pattern -> {
        try {
            return env.getTargetsMatchingPattern(pattern, executorService).stream();
        } catch (Exception e) {
            throw new RuntimeException("Error parsing target expression", e);
        }
    }).map(queryTarget -> {
        Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
        return ((QueryBuildTarget) queryTarget).getBuildTarget();
    });
}
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) Set(java.util.Set) BuildTarget(com.facebook.buck.model.BuildTarget) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) BuildRule(com.facebook.buck.rules.BuildRule) List(java.util.List) BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Stream(java.util.stream.Stream) QueryExpression(com.facebook.buck.query.QueryExpression) 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) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ArrayList(java.util.ArrayList) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) QueryExpression(com.facebook.buck.query.QueryExpression) QueryException(com.facebook.buck.query.QueryException) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget)

Aggregations

QueryExpression (com.facebook.buck.query.QueryExpression)5 QueryTarget (com.facebook.buck.query.QueryTarget)5 QueryException (com.facebook.buck.query.QueryException)4 BuildTarget (com.facebook.buck.model.BuildTarget)3 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)3 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)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 ImmutableSet (com.google.common.collect.ImmutableSet)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 BuildTargetPattern (com.facebook.buck.model.BuildTargetPattern)1