Search in sources :

Example 1 with QueryTarget

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

the class TargetPatternEvaluator method resolveFilePattern.

ImmutableSet<QueryTarget> resolveFilePattern(String pattern) throws IOException {
    ImmutableSet<Path> filePaths = PathArguments.getCanonicalFilesUnderProjectRoot(projectRoot, ImmutableList.of(pattern)).relativePathsUnderProjectRoot;
    ImmutableSet.Builder<QueryTarget> builder = ImmutableSortedSet.naturalOrder();
    for (Path filePath : filePaths) {
        builder.add(QueryFileTarget.of(filePath));
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) QueryTarget(com.facebook.buck.query.QueryTarget) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 2 with QueryTarget

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

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

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

the class BuckQueryEnvironment method getReverseDeps.

@Override
public Set<QueryTarget> getReverseDeps(Iterable<QueryTarget> targets) throws QueryException, InterruptedException {
    Set<QueryTarget> result = new LinkedHashSet<>();
    for (QueryTarget target : targets) {
        TargetNode<?, ?> node = getNode(target);
        result.addAll(getTargetsFromTargetNodes(graph.getIncomingNodesFor(node)));
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryTarget(com.facebook.buck.query.QueryTarget)

Example 5 with QueryTarget

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

the class BuckQueryEnvironment method getBuildFiles.

@Override
public ImmutableSet<QueryTarget> getBuildFiles(Set<QueryTarget> targets) throws QueryException {
    final ProjectFilesystem cellFilesystem = rootCell.getFilesystem();
    final Path rootPath = cellFilesystem.getRootPath();
    Preconditions.checkState(rootPath.isAbsolute());
    ImmutableSet.Builder<QueryTarget> builder = ImmutableSet.builder();
    for (QueryTarget target : targets) {
        Preconditions.checkState(target instanceof QueryBuildTarget);
        BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
        Cell cell = rootCell.getCell(buildTarget);
        if (!buildFileTrees.containsKey(cell)) {
            LOG.info("Creating a new filesystem-backed build file tree for %s", cell.getRoot());
            buildFileTrees.put(cell, new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()));
        }
        BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(cell));
        Optional<Path> path = buildFileTree.getBasePathOfAncestorTarget(buildTarget.getBasePath());
        Preconditions.checkState(path.isPresent());
        Path buildFilePath = MorePaths.relativize(rootPath, cell.getFilesystem().resolve(path.get()).resolve(cell.getBuildFileName()));
        Preconditions.checkState(cellFilesystem.exists(buildFilePath));
        builder.add(QueryFileTarget.of(buildFilePath));
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) QueryTarget(com.facebook.buck.query.QueryTarget) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) ImmutableSet(com.google.common.collect.ImmutableSet) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) BuildFileTree(com.facebook.buck.model.BuildFileTree) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Cell(com.facebook.buck.rules.Cell) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget)

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