Search in sources :

Example 26 with ImmutableSet

use of com.google.common.collect.ImmutableSet in project buck by facebook.

the class AuditInputCommand method runWithoutHelp.

@Override
public int runWithoutHelp(final CommandRunnerParams params) throws IOException, InterruptedException {
    // Create a TargetGraph that is composed of the transitive closure of all of the dependent
    // TargetNodes for the specified BuildTargets.
    final ImmutableSet<String> fullyQualifiedBuildTargets = ImmutableSet.copyOf(getArgumentsFormattedAsBuildTargets(params.getBuckConfig()));
    if (fullyQualifiedBuildTargets.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Please specify at least one build target."));
        return 1;
    }
    ImmutableSet<BuildTarget> targets = getArgumentsFormattedAsBuildTargets(params.getBuckConfig()).stream().map(input -> BuildTargetParser.INSTANCE.parse(input, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver())).collect(MoreCollectors.toImmutableSet());
    LOG.debug("Getting input for targets: %s", targets);
    TargetGraph graph;
    try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
        graph = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), targets);
    } catch (BuildFileParseException | BuildTargetException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
        return 1;
    }
    if (shouldGenerateJsonOutput()) {
        return printJsonInputs(params, graph);
    }
    return printInputs(params, graph);
}
Also used : SortedSet(java.util.SortedSet) MoreExceptions(com.facebook.buck.util.MoreExceptions) AbstractBottomUpTraversal(com.facebook.buck.graph.AbstractBottomUpTraversal) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) TreeSet(java.util.TreeSet) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) BuildTargetException(com.facebook.buck.model.BuildTargetException) Set(java.util.Set) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Sets(com.google.common.collect.Sets) List(java.util.List) TreeMap(java.util.TreeMap) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SortedMap(java.util.SortedMap) BuildTargetException(com.facebook.buck.model.BuildTargetException) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 27 with ImmutableSet

use of com.google.common.collect.ImmutableSet in project buck by facebook.

the class AuditRulesCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ProjectFilesystem projectFilesystem = params.getCell().getFilesystem();
    try (ProjectBuildFileParser parser = params.getCell().createBuildFileParser(new ConstructorArgMarshaller(new DefaultTypeCoercerFactory(params.getObjectMapper())), params.getConsole(), params.getBuckEventBus(), /* ignoreBuckAutodepsFiles */
    false)) {
        PrintStream out = params.getConsole().getStdOut();
        for (String pathToBuildFile : getArguments()) {
            if (!json) {
                // Print a comment with the path to the build file.
                out.printf("# %s\n\n", pathToBuildFile);
            }
            // Resolve the path specified by the user.
            Path path = Paths.get(pathToBuildFile);
            if (!path.isAbsolute()) {
                Path root = projectFilesystem.getRootPath();
                path = root.resolve(path);
            }
            // Parse the rules from the build file.
            List<Map<String, Object>> rawRules;
            try {
                rawRules = parser.getAll(path);
            } catch (BuildFileParseException e) {
                throw new HumanReadableException(e);
            }
            // Format and print the rules from the raw data, filtered by type.
            final ImmutableSet<String> types = getTypes();
            Predicate<String> includeType = type -> types.isEmpty() || types.contains(type);
            printRulesToStdout(params, rawRules, includeType);
        }
    } catch (BuildFileParseException e) {
        throw new HumanReadableException("Unable to create parser");
    }
    return 0;
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) MoreStrings(com.facebook.buck.util.MoreStrings) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) Escaper(com.facebook.buck.util.Escaper) BuckPyFunction(com.facebook.buck.rules.BuckPyFunction) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) HumanReadableException(com.facebook.buck.util.HumanReadableException) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Predicate(com.google.common.base.Predicate) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PrintStream(java.io.PrintStream) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) HumanReadableException(com.facebook.buck.util.HumanReadableException) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with ImmutableSet

use of com.google.common.collect.ImmutableSet 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 29 with ImmutableSet

use of com.google.common.collect.ImmutableSet in project buck by facebook.

the class AuditFlavorsCommand method printFlavors.

private void printFlavors(ImmutableList<TargetNode<?, ?>> targetNodes, CommandRunnerParams params) {
    DirtyPrintStreamDecorator stdout = params.getConsole().getStdOut();
    for (TargetNode<?, ?> node : targetNodes) {
        Description<?> description = node.getDescription();
        stdout.println(node.getBuildTarget().getFullyQualifiedName());
        if (description instanceof Flavored) {
            Optional<ImmutableSet<FlavorDomain<?>>> flavorDomains = ((Flavored) description).flavorDomains();
            if (flavorDomains.isPresent()) {
                for (FlavorDomain<?> domain : flavorDomains.get()) {
                    ImmutableSet<UserFlavor> userFlavors = RichStream.from(domain.getFlavors().stream()).filter(UserFlavor.class).collect(MoreCollectors.toImmutableSet());
                    if (userFlavors.isEmpty()) {
                        continue;
                    }
                    stdout.printf(" %s\n", domain.getName());
                    for (UserFlavor flavor : userFlavors) {
                        String flavorLine = String.format("  %s", flavor.getName());
                        String flavorDescription = flavor.getDescription();
                        if (flavorDescription.length() > 0) {
                            flavorLine += String.format(" -> %s", flavorDescription);
                        }
                        flavorLine += "\n";
                        stdout.printf(flavorLine);
                    }
                }
            } else {
                stdout.println(" unknown");
            }
        } else {
            stdout.println(" no flavors");
        }
    }
}
Also used : Flavored(com.facebook.buck.model.Flavored) ImmutableSet(com.google.common.collect.ImmutableSet) UserFlavor(com.facebook.buck.model.UserFlavor) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator)

Example 30 with ImmutableSet

use of com.google.common.collect.ImmutableSet in project buck by facebook.

the class AuditFlavorsCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ImmutableSet<BuildTarget> targets = getArgumentsFormattedAsBuildTargets(params.getBuckConfig()).stream().map(input -> BuildTargetParser.INSTANCE.parse(input, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver())).collect(MoreCollectors.toImmutableSet());
    if (targets.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Please specify at least one build target."));
        return 1;
    }
    ImmutableList.Builder<TargetNode<?, ?>> builder = ImmutableList.builder();
    try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
        for (BuildTarget target : targets) {
            TargetNode<?, ?> targetNode = params.getParser().getTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), target);
            builder.add(targetNode);
        }
    } catch (BuildFileParseException | BuildTargetException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
        return 1;
    }
    ImmutableList<TargetNode<?, ?>> targetNodes = builder.build();
    if (shouldGenerateJsonOutput()) {
        printJsonFlavors(targetNodes, params);
    } else {
        printFlavors(targetNodes, params);
    }
    return 0;
}
Also used : MoreExceptions(com.facebook.buck.util.MoreExceptions) RichStream(com.facebook.buck.util.RichStream) Flavored(com.facebook.buck.model.Flavored) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) FlavorDomain(com.facebook.buck.model.FlavorDomain) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSet(com.google.common.collect.ImmutableSet) TargetNode(com.facebook.buck.rules.TargetNode) BuildTargetException(com.facebook.buck.model.BuildTargetException) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) BuildTarget(com.facebook.buck.model.BuildTarget) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator) List(java.util.List) TreeMap(java.util.TreeMap) UserFlavor(com.facebook.buck.model.UserFlavor) Optional(java.util.Optional) SortedMap(java.util.SortedMap) Description(com.facebook.buck.rules.Description) BuildTargetException(com.facebook.buck.model.BuildTargetException) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableList(com.google.common.collect.ImmutableList) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)196 Path (java.nio.file.Path)65 BuildTarget (com.facebook.buck.model.BuildTarget)58 ImmutableMap (com.google.common.collect.ImmutableMap)44 ImmutableList (com.google.common.collect.ImmutableList)43 IOException (java.io.IOException)42 Optional (java.util.Optional)37 Test (org.junit.Test)32 SourcePath (com.facebook.buck.rules.SourcePath)31 TargetNode (com.facebook.buck.rules.TargetNode)28 BuildRule (com.facebook.buck.rules.BuildRule)26 Map (java.util.Map)26 List (java.util.List)25 Set (java.util.Set)25 HumanReadableException (com.facebook.buck.util.HumanReadableException)23 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)22 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)21 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)20 TargetGraph (com.facebook.buck.rules.TargetGraph)19 VisibleForTesting (com.google.common.annotations.VisibleForTesting)19