Search in sources :

Example 1 with BuildFileTree

use of com.facebook.buck.model.BuildFileTree 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)

Example 2 with BuildFileTree

use of com.facebook.buck.model.BuildFileTree in project buck by facebook.

the class DaemonicParserState method invalidateBasedOn.

public void invalidateBasedOn(WatchEvent<?> event) {
    if (!WatchEvents.isPathChangeEvent(event)) {
        // Non-path change event, likely an overflow due to many change events: invalidate everything.
        LOG.debug("Received non-path change event %s, assuming overflow and checking caches.", event);
        if (invalidateAllCaches()) {
            LOG.warn("Invalidated cache on watch event %s.", event);
            cacheInvalidatedByWatchOverflowCounter.inc();
        }
        return;
    }
    filesChangedCounter.inc();
    Path path = (Path) event.context();
    try (AutoCloseableLock readLock = cellStateLock.readLock()) {
        for (DaemonicCellState state : cellPathToDaemonicState.values()) {
            try {
                // rule key change.  For parsing, these are the only events we need to care about.
                if (isPathCreateOrDeleteEvent(event)) {
                    Cell cell = state.getCell();
                    BuildFileTree buildFiles = buildFileTrees.get(cell);
                    if (path.endsWith(cell.getBuildFileName())) {
                        LOG.debug("Build file %s changed, invalidating build file tree for cell %s", path, cell);
                        // If a build file has been added or removed, reconstruct the build file tree.
                        buildFileTrees.invalidate(cell);
                    }
                    // "containing" {@code path} unless its filename matches a temp file pattern.
                    if (!isTempFile(cell, path)) {
                        invalidateContainingBuildFile(cell, buildFiles, path);
                    } else {
                        LOG.debug("Not invalidating the owning build file of %s because it is a temporary file.", state.getCellRoot().resolve(path).toAbsolutePath().toString());
                    }
                }
            } catch (ExecutionException | UncheckedExecutionException e) {
                try {
                    Throwables.throwIfInstanceOf(e, BuildFileParseException.class);
                    Throwables.throwIfUnchecked(e);
                    throw new RuntimeException(e);
                } catch (BuildFileParseException bfpe) {
                    LOG.warn("Unable to parse already parsed build file.", bfpe);
                }
            }
        }
    }
    invalidatePath(path);
}
Also used : Path(java.nio.file.Path) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) BuildFileTree(com.facebook.buck.model.BuildFileTree) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Cell(com.facebook.buck.rules.Cell) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 3 with BuildFileTree

use of com.facebook.buck.model.BuildFileTree 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)

Example 4 with BuildFileTree

use of com.facebook.buck.model.BuildFileTree in project buck by facebook.

the class TargetsCommand method getMatchingNodes.

/**
   * @param graph Graph used to resolve dependencies between targets and find all build files.
   * @param referencedFiles If present, the result will be limited to the nodes that transitively
   *                        depend on at least one of those.
   * @param matchingBuildTargets If present, the result will be limited to the specified targets.
   * @param buildRuleTypes If present, the result will be limited to targets with the specified
   *                       types.
   * @param detectTestChanges If true, tests are considered to be dependencies of the targets they
   *                          are testing.
   * @return A map of target names to target nodes.
   */
@VisibleForTesting
ImmutableSortedMap<String, TargetNode<?, ?>> getMatchingNodes(TargetGraph graph, Optional<ImmutableSet<Path>> referencedFiles, final Optional<ImmutableSet<BuildTarget>> matchingBuildTargets, final Optional<ImmutableSet<Class<? extends Description<?>>>> descriptionClasses, boolean detectTestChanges, String buildFileName) {
    ImmutableSet<TargetNode<?, ?>> directOwners;
    if (referencedFiles.isPresent()) {
        BuildFileTree buildFileTree = new InMemoryBuildFileTree(graph.getNodes().stream().map(TargetNode::getBuildTarget).collect(MoreCollectors.toImmutableSet()));
        directOwners = FluentIterable.from(graph.getNodes()).filter(new DirectOwnerPredicate(buildFileTree, referencedFiles.get(), buildFileName)).toSet();
    } else {
        directOwners = graph.getNodes();
    }
    Iterable<TargetNode<?, ?>> selectedReferrers = FluentIterable.from(getDependentNodes(graph, directOwners, detectTestChanges)).filter(targetNode -> {
        if (matchingBuildTargets.isPresent() && !matchingBuildTargets.get().contains(targetNode.getBuildTarget())) {
            return false;
        }
        if (descriptionClasses.isPresent() && !descriptionClasses.get().contains(targetNode.getDescription().getClass())) {
            return false;
        }
        return true;
    });
    ImmutableSortedMap.Builder<String, TargetNode<?, ?>> matchingNodesBuilder = ImmutableSortedMap.naturalOrder();
    for (TargetNode<?, ?> targetNode : selectedReferrers) {
        matchingNodesBuilder.put(targetNode.getBuildTarget().getFullyQualifiedName(), targetNode);
    }
    return matchingNodesBuilder.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) InMemoryBuildFileTree(com.facebook.buck.model.InMemoryBuildFileTree) BuildFileTree(com.facebook.buck.model.BuildFileTree) InMemoryBuildFileTree(com.facebook.buck.model.InMemoryBuildFileTree) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

BuildFileTree (com.facebook.buck.model.BuildFileTree)4 FilesystemBackedBuildFileTree (com.facebook.buck.model.FilesystemBackedBuildFileTree)3 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)2 Cell (com.facebook.buck.rules.Cell)2 Path (java.nio.file.Path)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 InMemoryBuildFileTree (com.facebook.buck.model.InMemoryBuildFileTree)1 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)1 QueryException (com.facebook.buck.query.QueryException)1 QueryTarget (com.facebook.buck.query.QueryTarget)1 TargetNode (com.facebook.buck.rules.TargetNode)1 AutoCloseableLock (com.facebook.buck.util.concurrent.AutoCloseableLock)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1