Search in sources :

Example 1 with QueryException

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

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

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

the class QueryTargetTranslator method translateTargets.

@Override
public Optional<Query> translateTargets(CellPathResolver cellPathResolver, BuildTargetPatternParser<BuildTargetPattern> pattern, TargetNodeTranslator translator, Query query) {
    // Extract all build targets from the original query string.
    ImmutableList<BuildTarget> targets;
    try {
        targets = QueryUtils.extractBuildTargets(cellPathResolver, pattern, query).collect(MoreCollectors.toImmutableList());
    } catch (QueryException e) {
        throw new RuntimeException("Error parsing/executing query from deps", e);
    }
    // If there's no targets, bail early.
    if (targets.isEmpty()) {
        return Optional.empty();
    }
    // A pattern matching all of the build targets in the query string.
    Pattern targetsPattern = Pattern.compile(targets.stream().map(Object::toString).map(Pattern::quote).collect(Collectors.joining("|")));
    // Build a new query string from the original by translating all build targets.
    String queryString = query.getQuery();
    Matcher matcher = targetsPattern.matcher(queryString);
    StringBuilder builder = new StringBuilder();
    int lastEnd = 0;
    while (matcher.find()) {
        builder.append(queryString.substring(lastEnd, matcher.start()));
        BuildTarget target = BuildTargetParser.INSTANCE.parse(matcher.group(), pattern, cellPathResolver);
        Optional<BuildTarget> translated = translator.translate(cellPathResolver, pattern, target);
        builder.append(translated.orElse(target).getFullyQualifiedName());
        lastEnd = matcher.end();
    }
    builder.append(queryString.substring(lastEnd, queryString.length()));
    String newQuery = builder.toString();
    return queryString.equals(newQuery) ? Optional.empty() : Optional.of(Query.of(newQuery));
}
Also used : BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Pattern(java.util.regex.Pattern) QueryException(com.facebook.buck.query.QueryException) Matcher(java.util.regex.Matcher) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 4 with QueryException

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

the class QueryCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    if (arguments.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Must specify at least the query expression"));
        return 1;
    }
    try (CommandThreadManager pool = new CommandThreadManager("Query", getConcurrencyLimit(params.getBuckConfig()));
        PerBuildState parserState = new PerBuildState(params.getParser(), params.getBuckEventBus(), pool.getExecutor(), params.getCell(), getEnableParserProfiling(), SpeculativeParsing.of(true), /* ignoreBuckAutodepsFiles */
        false)) {
        BuckQueryEnvironment env = BuckQueryEnvironment.from(params, parserState, getEnableParserProfiling());
        ListeningExecutorService executor = pool.getExecutor();
        return formatAndRunQuery(params, env, executor);
    } catch (QueryException | BuildFileParseException e) {
        throw new HumanReadableException(e);
    }
}
Also used : QueryException(com.facebook.buck.query.QueryException) HumanReadableException(com.facebook.buck.util.HumanReadableException) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) PerBuildState(com.facebook.buck.parser.PerBuildState)

Example 5 with QueryException

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

the class BuckQueryEnvironment method getFileOwners.

@Override
public ImmutableSet<QueryTarget> getFileOwners(ImmutableList<String> files, ListeningExecutorService executor) throws InterruptedException, QueryException {
    try {
        BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(rootCell));
        OwnersReport report = ownersReportBuilder.build(buildFileTree, executor, files);
        return getTargetsFromTargetNodes(report.owners.keySet());
    } catch (BuildFileParseException | IOException e) {
        throw new QueryException(e, "Could not parse build targets.\n%s", e.getMessage());
    }
}
Also used : QueryException(com.facebook.buck.query.QueryException) BuildFileTree(com.facebook.buck.model.BuildFileTree) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) IOException(java.io.IOException) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Aggregations

QueryException (com.facebook.buck.query.QueryException)7 BuildTarget (com.facebook.buck.model.BuildTarget)4 QueryExpression (com.facebook.buck.query.QueryExpression)4 QueryTarget (com.facebook.buck.query.QueryTarget)4 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)4 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)3 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)2 BuildTargetPattern (com.facebook.buck.model.BuildTargetPattern)2 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 HumanReadableException (com.facebook.buck.util.HumanReadableException)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