Example usage for com.google.common.collect ImmutableMultimap get

List of usage examples for com.google.common.collect ImmutableMultimap get

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultimap get.

Prototype

@Override
public abstract ImmutableCollection<V> get(K key);

Source Link

Document

Returns an immutable collection of the values for the given key.

Usage

From source file:com.facebook.buck.apple.xcode.WorkspaceAndProjectGenerator.java

private void getOrderedTestNodes(TargetGraph targetGraph,
        ImmutableMultimap<BuildTarget, TargetNode<?>> sourceTargetToTestNodes,
        ImmutableSet<TargetNode<?>> orderedTargetNodes, ImmutableSet<TargetNode<?>> extraTestBundleTargets,
        ImmutableSet.Builder<TargetNode<?>> orderedTestTargetNodeBuilder,
        ImmutableSet.Builder<TargetNode<?>> orderedTestBundleTargetNodeBuilder) {
    LOG.debug("Getting ordered test target nodes for %s", orderedTargetNodes);
    final ImmutableSet.Builder<TargetNode<?>> recursiveTestTargetNodesBuilder = ImmutableSet.builder();
    if (!sourceTargetToTestNodes.isEmpty()) {
        for (TargetNode<?> node : orderedTargetNodes) {
            LOG.verbose("Checking if target %s has any tests covering it..", node);
            for (TargetNode<?> testNode : sourceTargetToTestNodes.get(node.getBuildTarget())) {
                AppleTestDescription.Arg testConstructorArg = (AppleTestDescription.Arg) testNode
                        .getConstructorArg();
                addTestNodeAndDependencies(
                        Preconditions.checkNotNull(targetGraph.get(testConstructorArg.testBundle)),
                        recursiveTestTargetNodesBuilder, orderedTestBundleTargetNodeBuilder);
            }/*from   w w w .ja  va2  s .  c  o  m*/
        }
    }

    for (TargetNode<?> testBundleTarget : extraTestBundleTargets) {
        if (!AppleBuildRules.isXcodeTargetTestBundleTargetNode(testBundleTarget)) {
            throw new HumanReadableException("Test target %s must be apple_bundle with a test extension!",
                    testBundleTarget);
        }
        addTestNodeAndDependencies(testBundleTarget, recursiveTestTargetNodesBuilder,
                orderedTestBundleTargetNodeBuilder);
    }

    final Set<TargetNode<?>> includedTestNodes = Sets.difference(recursiveTestTargetNodesBuilder.build(),
            orderedTargetNodes);

    orderedTestTargetNodeBuilder.addAll(TopologicalSort.sort(targetGraph, new Predicate<TargetNode<?>>() {
        @Override
        public boolean apply(TargetNode<?> input) {
            return includedTestNodes.contains(input)
                    && AppleBuildRules.isXcodeTargetBuildRuleType(input.getType());
        }
    }));
}

From source file:com.facebook.buck.android.AndroidModuleConsistencyStep.java

@Override
public StepExecutionResult execute(ExecutionContext context) {
    try {//from w  w w . j ava  2s.c om
        ProguardTranslatorFactory translatorFactory = ProguardTranslatorFactory.create(filesystem,
                proguardFullConfigFile, proguardMappingFile, skipProguard);
        ImmutableMultimap<String, APKModule> classToModuleMap = APKModuleGraph.getAPKModuleToClassesMap(
                apkModuleToJarPathMap, translatorFactory.createObfuscationFunction(), filesystem).inverse();

        ImmutableMap<String, String> classToModuleResult = AppModularityMetadataUtil
                .getClassToModuleMap(filesystem, metadataInput);

        boolean hasError = false;
        StringBuilder errorMessage = new StringBuilder();

        for (Map.Entry<String, String> entry : classToModuleResult.entrySet()) {
            String className = entry.getKey();
            String resultName = entry.getValue();
            if (classToModuleMap.containsKey(className)) {
                // if the new classMap does not contain the old class, that's fine,
                // consistency is only broken when a class changes to a different module.
                APKModule module = classToModuleMap.get(className).iterator().next();
                if (!module.getName().equals(resultName)) {
                    hasError = true;
                    errorMessage.append(className).append(" moved from App Module: ").append(resultName)
                            .append(" to App Module: ").append(module.getName()).append("\n");
                }
            }
        }

        if (hasError) {
            throw new IllegalStateException(errorMessage.toString());
        }

        return StepExecutionResults.SUCCESS;
    } catch (IOException e) {
        context.logError(e, "There was an error running AndroidModuleConsistencyStep.");
        return StepExecutionResults.ERROR;
    } catch (IllegalStateException e) {
        context.logError(e, "There was an error running AndroidModuleConsistencyStep.");
        return StepExecutionResults.ERROR;
    }
}

From source file:com.facebook.buck.features.apple.project.XCodeProjectCommandHelper.java

/** Run xcode specific project generation actions. */
private ExitCode runXcodeProjectGenerator(TargetGraphAndTargets targetGraphAndTargets,
        ImmutableSet<BuildTarget> passedInTargetsSet,
        Optional<ImmutableMap<BuildTarget, TargetNode<?>>> sharedLibraryToBundle)
        throws IOException, InterruptedException {
    ExitCode exitCode = ExitCode.SUCCESS;
    AppleConfig appleConfig = buckConfig.getView(AppleConfig.class);
    ProjectGeneratorOptions options = ProjectGeneratorOptions.builder().setShouldGenerateReadOnlyFiles(readOnly)
            .setShouldIncludeTests(isWithTests(buckConfig))
            .setShouldIncludeDependenciesTests(isWithDependenciesTests(buckConfig))
            .setShouldUseHeaderMaps(appleConfig.shouldUseHeaderMapsInXcodeProject())
            .setShouldUseAbsoluteHeaderMapPaths(absoluteHeaderMapPaths)
            .setShouldMergeHeaderMaps(appleConfig.shouldMergeHeaderMapsInXcodeProject())
            .setShouldAddLinkedLibrariesAsFlags(appleConfig.shouldAddLinkedLibrariesAsFlags())
            .setShouldForceLoadLinkWholeLibraries(appleConfig.shouldAddLinkerFlagsForLinkWholeLibraries())
            .setShouldGenerateHeaderSymlinkTreesOnly(appleConfig.shouldGenerateHeaderSymlinkTreesOnly())
            .setShouldGenerateMissingUmbrellaHeader(appleConfig.shouldGenerateMissingUmbrellaHeaders())
            .setShouldUseShortNamesForTargets(true).setShouldCreateDirectoryStructure(combinedProject)
            .setShouldGenerateProjectSchemes(createProjectSchemes).build();

    LOG.debug("Xcode project generation: Generates workspaces for targets");

    ImmutableSet<BuildTarget> requiredBuildTargets = generateWorkspacesForTargets(buckEventBus, pluginManager,
            cell, buckConfig, ruleKeyConfiguration, executorService, targetGraphAndTargets, passedInTargetsSet,
            options, appleCxxFlavors, getFocusModules(), new HashMap<>(), combinedProject, outputPresenter,
            sharedLibraryToBundle);//from   w  ww  . j  a  va2s  .com
    if (!requiredBuildTargets.isEmpty()) {
        ImmutableMultimap<Path, String> cellPathToCellName = cell.getCellPathResolver().getCellPaths()
                .asMultimap().inverse();
        ImmutableList<String> arguments = RichStream.from(requiredBuildTargets).map(target -> {
            if (!target.getCellPath().equals(cell.getRoot())) {
                Optional<String> cellName = cellPathToCellName.get(target.getCellPath()).stream().findAny();
                if (cellName.isPresent()) {
                    return target.withUnflavoredBuildTarget(ImmutableUnflavoredBuildTarget
                            .of(target.getCellPath(), cellName, target.getBaseName(), target.getShortName()));
                } else {
                    throw new IllegalStateException(
                            "Failed to find cell name for cell path while constructing parameters to "
                                    + "build dependencies for project generation. " + "Build target: " + target
                                    + " cell path: " + target.getCellPath());
                }
            } else {
                return target;
            }
        }).map(Object::toString).toImmutableList();
        exitCode = buildRunner.apply(arguments);
    }
    return exitCode;
}

From source file:com.facebook.buck.apple.WorkspaceAndProjectGenerator.java

public Path generateWorkspaceAndDependentProjects(Map<Path, ProjectGenerator> projectGenerators)
        throws IOException {
    LOG.debug("Generating workspace for target %s", workspaceBuildTarget);

    String workspaceName = XcodeWorkspaceConfigDescription.getWorkspaceNameFromArg(workspaceArguments);
    Path outputDirectory;/*from   w w  w .j a v  a  2s. c  o  m*/
    if (combinedProject) {
        workspaceName += "-Combined";
        outputDirectory = BuildTargets.getGenPath(workspaceBuildTarget, "%s").getParent()
                .resolve(workspaceName + ".xcodeproj");
    } else {
        outputDirectory = workspaceBuildTarget.getBasePath();
    }

    WorkspaceGenerator workspaceGenerator = new WorkspaceGenerator(projectFilesystem,
            combinedProject ? "project" : workspaceName, outputDirectory);

    ImmutableMap.Builder<String, XcodeWorkspaceConfigDescription.Arg> schemeConfigsBuilder = ImmutableMap
            .builder();
    ImmutableSetMultimap.Builder<String, Optional<TargetNode<?>>> schemeNameToSrcTargetNodeBuilder = ImmutableSetMultimap
            .builder();
    ImmutableSetMultimap.Builder<String, TargetNode<?>> buildForTestNodesBuilder = ImmutableSetMultimap
            .builder();
    ImmutableMultimap.Builder<AppleTestBundleParamsKey, TargetNode<AppleTestDescription.Arg>> groupedTestsBuilder = ImmutableMultimap
            .builder();
    ImmutableSetMultimap.Builder<String, TargetNode<AppleTestDescription.Arg>> ungroupedTestsBuilder = ImmutableSetMultimap
            .builder();

    buildWorkspaceSchemes(getTargetToBuildWithBuck(), projectGraph,
            projectGeneratorOptions.contains(ProjectGenerator.Option.INCLUDE_TESTS),
            projectGeneratorOptions.contains(ProjectGenerator.Option.INCLUDE_DEPENDENCIES_TESTS),
            groupableTests, workspaceName, workspaceArguments, schemeConfigsBuilder,
            schemeNameToSrcTargetNodeBuilder, buildForTestNodesBuilder, groupedTestsBuilder,
            ungroupedTestsBuilder);

    ImmutableMap<String, XcodeWorkspaceConfigDescription.Arg> schemeConfigs = schemeConfigsBuilder.build();
    ImmutableSetMultimap<String, Optional<TargetNode<?>>> schemeNameToSrcTargetNode = schemeNameToSrcTargetNodeBuilder
            .build();
    ImmutableSetMultimap<String, TargetNode<?>> buildForTestNodes = buildForTestNodesBuilder.build();
    ImmutableMultimap<AppleTestBundleParamsKey, TargetNode<AppleTestDescription.Arg>> groupedTests = groupedTestsBuilder
            .build();
    ImmutableSetMultimap<String, TargetNode<AppleTestDescription.Arg>> ungroupedTests = ungroupedTestsBuilder
            .build();
    Iterable<PBXTarget> synthesizedCombinedTestTargets = ImmutableList.of();

    ImmutableSet<BuildTarget> targetsInRequiredProjects = FluentIterable
            .from(Optional.presentInstances(schemeNameToSrcTargetNode.values()))
            .append(buildForTestNodes.values()).transform(HasBuildTarget.TO_TARGET).toSet();
    ImmutableMultimap.Builder<BuildTarget, PBXTarget> buildTargetToPbxTargetMapBuilder = ImmutableMultimap
            .builder();
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    Optional<BuildTarget> targetToBuildWithBuck = getTargetToBuildWithBuck();

    if (combinedProject) {
        LOG.debug("Generating a combined project");
        ProjectGenerator generator = new ProjectGenerator(projectGraph, targetsInRequiredProjects,
                projectFilesystem, outputDirectory.getParent(), workspaceName, buildFileName,
                projectGeneratorOptions, targetToBuildWithBuck, buildWithBuckFlags, executableFinder,
                environment, cxxPlatforms, defaultCxxPlatform, sourcePathResolverForNode, buckEventBus,
                attemptToDetermineBestCxxPlatform, halideBuckConfig, cxxBuckConfig)
                        .setAdditionalCombinedTestTargets(groupedTests)
                        .setTestsToGenerateAsStaticLibraries(groupableTests);
        combinedProjectGenerator = Optional.of(generator);
        generator.createXcodeProjects();

        workspaceGenerator.addFilePath(generator.getProjectPath(), Optional.<Path>absent());
        requiredBuildTargetsBuilder.addAll(generator.getRequiredBuildTargets());

        buildTargetToPbxTargetMapBuilder.putAll(generator.getBuildTargetToGeneratedTargetMap());
        for (PBXTarget target : generator.getBuildTargetToGeneratedTargetMap().values()) {
            targetToProjectPathMapBuilder.put(target, generator.getProjectPath());
        }
        synthesizedCombinedTestTargets = generator.getBuildableCombinedTestTargets();
        for (PBXTarget target : synthesizedCombinedTestTargets) {
            targetToProjectPathMapBuilder.put(target, generator.getProjectPath());
        }
    } else {
        ImmutableMultimap.Builder<Path, BuildTarget> projectDirectoryToBuildTargetsBuilder = ImmutableMultimap
                .builder();
        for (TargetNode<?> targetNode : projectGraph.getNodes()) {
            BuildTarget buildTarget = targetNode.getBuildTarget();
            projectDirectoryToBuildTargetsBuilder.put(buildTarget.getBasePath(), buildTarget);
        }
        ImmutableMultimap<Path, BuildTarget> projectDirectoryToBuildTargets = projectDirectoryToBuildTargetsBuilder
                .build();
        for (Path projectDirectory : projectDirectoryToBuildTargets.keySet()) {
            final ImmutableSet<BuildTarget> rules = filterRulesForProjectDirectory(projectGraph,
                    ImmutableSet.copyOf(projectDirectoryToBuildTargets.get(projectDirectory)));
            if (Sets.intersection(targetsInRequiredProjects, rules).isEmpty()) {
                continue;
            }

            ProjectGenerator generator = projectGenerators.get(projectDirectory);
            if (generator == null) {
                LOG.debug("Generating project for directory %s with targets %s", projectDirectory, rules);
                String projectName;
                if (projectDirectory.getNameCount() == 0) {
                    // If we're generating a project in the root directory, use a generic name.
                    projectName = "Project";
                } else {
                    // Otherwise, name the project the same thing as the directory we're in.
                    projectName = projectDirectory.getFileName().toString();
                }
                generator = new ProjectGenerator(projectGraph, rules, projectFilesystem, projectDirectory,
                        projectName, buildFileName, projectGeneratorOptions, Optionals.bind(
                                targetToBuildWithBuck, new Function<BuildTarget, Optional<BuildTarget>>() {
                                    @Override
                                    public Optional<BuildTarget> apply(BuildTarget input) {
                                        return rules.contains(input) ? Optional.of(input)
                                                : Optional.<BuildTarget>absent();
                                    }
                                }),
                        buildWithBuckFlags, executableFinder, environment, cxxPlatforms, defaultCxxPlatform,
                        sourcePathResolverForNode, buckEventBus, attemptToDetermineBestCxxPlatform,
                        halideBuckConfig, cxxBuckConfig).setTestsToGenerateAsStaticLibraries(groupableTests);

                generator.createXcodeProjects();
                requiredBuildTargetsBuilder.addAll(generator.getRequiredBuildTargets());
                projectGenerators.put(projectDirectory, generator);
            } else {
                LOG.debug("Already generated project for target %s, skipping", projectDirectory);
            }

            workspaceGenerator.addFilePath(generator.getProjectPath());

            buildTargetToPbxTargetMapBuilder.putAll(generator.getBuildTargetToGeneratedTargetMap());
            for (PBXTarget target : generator.getBuildTargetToGeneratedTargetMap().values()) {
                targetToProjectPathMapBuilder.put(target, generator.getProjectPath());
            }
        }

        if (!groupedTests.isEmpty()) {
            ProjectGenerator combinedTestsProjectGenerator = new ProjectGenerator(projectGraph,
                    ImmutableSortedSet.<BuildTarget>of(), projectFilesystem,
                    BuildTargets.getGenPath(workspaceBuildTarget, "%s-CombinedTestBundles"),
                    "_CombinedTestBundles", buildFileName, projectGeneratorOptions,
                    Optional.<BuildTarget>absent(), buildWithBuckFlags, executableFinder, environment,
                    cxxPlatforms, defaultCxxPlatform, sourcePathResolverForNode, buckEventBus,
                    attemptToDetermineBestCxxPlatform, halideBuckConfig, cxxBuckConfig);
            combinedTestsProjectGenerator.setAdditionalCombinedTestTargets(groupedTests).createXcodeProjects();
            workspaceGenerator.addFilePath(combinedTestsProjectGenerator.getProjectPath());
            requiredBuildTargetsBuilder.addAll(combinedTestsProjectGenerator.getRequiredBuildTargets());
            for (PBXTarget target : combinedTestsProjectGenerator.getBuildTargetToGeneratedTargetMap()
                    .values()) {
                targetToProjectPathMapBuilder.put(target, combinedTestsProjectGenerator.getProjectPath());
            }
            synthesizedCombinedTestTargets = combinedTestsProjectGenerator.getBuildableCombinedTestTargets();
            for (PBXTarget target : synthesizedCombinedTestTargets) {
                targetToProjectPathMapBuilder.put(target, combinedTestsProjectGenerator.getProjectPath());
            }
            this.combinedTestsProjectGenerator = Optional.of(combinedTestsProjectGenerator);
        }
    }

    Path workspacePath = workspaceGenerator.writeWorkspace();

    final Multimap<BuildTarget, PBXTarget> buildTargetToTarget = buildTargetToPbxTargetMapBuilder.build();
    final Function<BuildTarget, PBXTarget> targetNodeToPBXTargetTransformer = new Function<BuildTarget, PBXTarget>() {
        @Override
        public PBXTarget apply(BuildTarget input) {
            ImmutableList<PBXTarget> targets = ImmutableList.copyOf(buildTargetToTarget.get(input));
            if (targets.size() == 1) {
                return targets.get(0);
            }
            // The only reason why a build target would map to more than one project target is if
            // there are two project targets: one is the usual one, the other is a target that just
            // shells out to Buck.
            Preconditions.checkState(targets.size() == 2);
            PBXTarget first = targets.get(0);
            PBXTarget second = targets.get(1);
            Preconditions.checkState(first.getName().endsWith(ProjectGenerator.BUILD_WITH_BUCK_POSTFIX)
                    ^ second.getName().endsWith(ProjectGenerator.BUILD_WITH_BUCK_POSTFIX));
            PBXTarget buildWithBuckTarget;
            PBXTarget buildWithXcodeTarget;
            if (first.getName().endsWith(ProjectGenerator.BUILD_WITH_BUCK_POSTFIX)) {
                buildWithBuckTarget = first;
                buildWithXcodeTarget = second;
            } else {
                buildWithXcodeTarget = first;
                buildWithBuckTarget = second;
            }
            return buildWithBuck ? buildWithBuckTarget : buildWithXcodeTarget;
        }
    };

    writeWorkspaceSchemes(workspaceName, outputDirectory, schemeConfigs, schemeNameToSrcTargetNode,
            buildForTestNodes, ungroupedTests, targetToProjectPathMapBuilder.build(),
            synthesizedCombinedTestTargets, new Function<TargetNode<?>, Collection<PBXTarget>>() {
                @Override
                public Collection<PBXTarget> apply(TargetNode<?> input) {
                    return buildTargetToTarget.get(input.getBuildTarget());
                }
            }, targetNodeToPBXTargetTransformer);

    return workspacePath;
}

From source file:com.facebook.buck.apple.project_generator.WorkspaceAndProjectGenerator.java

private void generateProject(final Map<Path, ProjectGenerator> projectGenerators,
        ListeningExecutorService listeningExecutorService, WorkspaceGenerator workspaceGenerator,
        ImmutableSet<BuildTarget> targetsInRequiredProjects,
        ImmutableMultimap.Builder<BuildTarget, PBXTarget> buildTargetToPbxTargetMapBuilder,
        ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder,
        final Optional<BuildTarget> targetToBuildWithBuck) throws IOException, InterruptedException {
    ImmutableMultimap.Builder<Cell, BuildTarget> projectCellToBuildTargetsBuilder = ImmutableMultimap.builder();
    for (TargetNode<?, ?> targetNode : projectGraph.getNodes()) {
        BuildTarget buildTarget = targetNode.getBuildTarget();
        projectCellToBuildTargetsBuilder.put(rootCell.getCell(buildTarget), buildTarget);
    }/*w w w . j ava 2  s  .c  o m*/
    ImmutableMultimap<Cell, BuildTarget> projectCellToBuildTargets = projectCellToBuildTargetsBuilder.build();
    List<ListenableFuture<GenerationResult>> projectGeneratorFutures = new ArrayList<>();
    for (final Cell projectCell : projectCellToBuildTargets.keySet()) {
        ImmutableMultimap.Builder<Path, BuildTarget> projectDirectoryToBuildTargetsBuilder = ImmutableMultimap
                .builder();
        final ImmutableSet<BuildTarget> cellRules = ImmutableSet
                .copyOf(projectCellToBuildTargets.get(projectCell));
        for (BuildTarget buildTarget : cellRules) {
            projectDirectoryToBuildTargetsBuilder.put(buildTarget.getBasePath(), buildTarget);
        }
        ImmutableMultimap<Path, BuildTarget> projectDirectoryToBuildTargets = projectDirectoryToBuildTargetsBuilder
                .build();
        final Path relativeTargetCell = rootCell.getRoot().relativize(projectCell.getRoot());
        for (final Path projectDirectory : projectDirectoryToBuildTargets.keySet()) {
            final ImmutableSet<BuildTarget> rules = filterRulesForProjectDirectory(projectGraph,
                    ImmutableSet.copyOf(projectDirectoryToBuildTargets.get(projectDirectory)));
            if (Sets.intersection(targetsInRequiredProjects, rules).isEmpty()) {
                continue;
            }

            projectGeneratorFutures.add(listeningExecutorService.submit(() -> {
                GenerationResult result = generateProjectForDirectory(projectGenerators, targetToBuildWithBuck,
                        projectCell, projectDirectory, rules);
                // convert the projectPath to relative to the target cell here
                result = GenerationResult.of(relativeTargetCell.resolve(result.getProjectPath()),
                        result.isProjectGenerated(), result.getRequiredBuildTargets(),
                        result.getBuildTargetToGeneratedTargetMap());
                return result;
            }));
        }
    }

    List<GenerationResult> generationResults;
    try {
        generationResults = Futures.allAsList(projectGeneratorFutures).get();
    } catch (ExecutionException e) {
        Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
        Throwables.propagateIfPossible(e.getCause());
        throw new IllegalStateException("Unexpected exception: ", e);
    }
    for (GenerationResult result : generationResults) {
        if (!result.isProjectGenerated()) {
            continue;
        }
        workspaceGenerator.addFilePath(result.getProjectPath());
        processGenerationResult(buildTargetToPbxTargetMapBuilder, targetToProjectPathMapBuilder, result);
    }
}

From source file:com.google.gerrit.server.notedb.ChangeRebuilderImpl.java

@Override
public boolean rebuildProject(ReviewDb db, ImmutableMultimap<Project.NameKey, Change.Id> allChanges,
        Project.NameKey project, Repository allUsersRepo)
        throws NoSuchChangeException, IOException, OrmException, ConfigInvalidException {
    checkArgument(allChanges.containsKey(project));
    boolean ok = true;
    ProgressMonitor pm = new TextProgressMonitor(new PrintWriter(System.out));
    pm.beginTask(FormatUtil.elide(project.get(), 50), allChanges.get(project).size());
    try (NoteDbUpdateManager manager = updateManagerFactory.create(project);
            ObjectInserter allUsersInserter = allUsersRepo.newObjectInserter();
            RevWalk allUsersRw = new RevWalk(allUsersInserter.newReader())) {
        manager.setAllUsersRepo(allUsersRepo, allUsersRw, allUsersInserter,
                new ChainedReceiveCommands(allUsersRepo));
        for (Change.Id changeId : allChanges.get(project)) {
            try {
                buildUpdates(manager, ChangeBundle.fromReviewDb(db, changeId));
            } catch (Throwable t) {
                log.error("Failed to rebuild change " + changeId, t);
                ok = false;//w  w w  .  j  a  v  a 2  s  .  c  o  m
            }
            pm.update(1);
        }
        manager.execute();
    } finally {
        pm.endTask();
    }
    return ok;
}

From source file:com.facebook.buck.features.apple.project.WorkspaceAndProjectGenerator.java

private void generateProject(Map<Path, ProjectGenerator> projectGenerators,
        ListeningExecutorService listeningExecutorService, WorkspaceGenerator workspaceGenerator,
        ImmutableSet<BuildTarget> targetsInRequiredProjects,
        ImmutableSetMultimap.Builder<PBXProject, PBXTarget> generatedProjectToPbxTargetsBuilder,
        ImmutableMap.Builder<BuildTarget, PBXTarget> buildTargetToPbxTargetMapBuilder,
        ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder)
        throws IOException, InterruptedException {
    ImmutableMultimap.Builder<Cell, BuildTarget> projectCellToBuildTargetsBuilder = ImmutableMultimap.builder();
    for (TargetNode<?> targetNode : projectGraph.getNodes()) {
        BuildTarget buildTarget = targetNode.getBuildTarget();
        projectCellToBuildTargetsBuilder.put(rootCell.getCell(buildTarget), buildTarget);
    }/* w  w w.ja  v  a  2 s .  com*/
    ImmutableMultimap<Cell, BuildTarget> projectCellToBuildTargets = projectCellToBuildTargetsBuilder.build();
    List<ListenableFuture<GenerationResult>> projectGeneratorFutures = new ArrayList<>();
    for (Cell projectCell : projectCellToBuildTargets.keySet()) {
        ImmutableMultimap.Builder<Path, BuildTarget> projectDirectoryToBuildTargetsBuilder = ImmutableMultimap
                .builder();
        ImmutableSet<BuildTarget> cellRules = ImmutableSet.copyOf(projectCellToBuildTargets.get(projectCell));
        for (BuildTarget buildTarget : cellRules) {
            projectDirectoryToBuildTargetsBuilder.put(buildTarget.getBasePath(), buildTarget);
        }
        ImmutableMultimap<Path, BuildTarget> projectDirectoryToBuildTargets = projectDirectoryToBuildTargetsBuilder
                .build();
        Path relativeTargetCell = rootCell.getRoot().relativize(projectCell.getRoot());
        for (Path projectDirectory : projectDirectoryToBuildTargets.keySet()) {
            ImmutableSet<BuildTarget> rules = filterRulesForProjectDirectory(projectGraph,
                    ImmutableSet.copyOf(projectDirectoryToBuildTargets.get(projectDirectory)));
            if (Sets.intersection(targetsInRequiredProjects, rules).isEmpty()) {
                continue;
            }

            boolean isMainProject = workspaceArguments.getSrcTarget().isPresent()
                    && rules.contains(workspaceArguments.getSrcTarget().get());
            projectGeneratorFutures.add(listeningExecutorService.submit(() -> {
                GenerationResult result = generateProjectForDirectory(projectGenerators, projectCell,
                        projectDirectory, rules, isMainProject, targetsInRequiredProjects);
                // convert the projectPath to relative to the target cell here
                result = GenerationResult.of(relativeTargetCell.resolve(result.getProjectPath()),
                        result.isProjectGenerated(), result.getRequiredBuildTargets(),
                        result.getXcconfigPaths(), result.getFilesToCopyInXcode(),
                        result.getBuildTargetToGeneratedTargetMap(), result.getGeneratedProjectToPbxTargets());
                return result;
            }));
        }
    }

    List<GenerationResult> generationResults;
    try {
        generationResults = Futures.allAsList(projectGeneratorFutures).get();
    } catch (ExecutionException e) {
        Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
        Throwables.throwIfUnchecked(e.getCause());
        throw new IllegalStateException("Unexpected exception: ", e);
    }
    for (GenerationResult result : generationResults) {
        if (!result.isProjectGenerated()) {
            continue;
        }
        workspaceGenerator.addFilePath(result.getProjectPath());
        processGenerationResult(generatedProjectToPbxTargetsBuilder, buildTargetToPbxTargetMapBuilder,
                targetToProjectPathMapBuilder, result);
    }
}

From source file:com.google.devtools.build.lib.analysis.BuildView.java

private OrderedSetMultimap<Attribute, ConfiguredTarget> getPrerequisiteMapForTesting(
        final EventHandler eventHandler, ConfiguredTarget target, BuildConfigurationCollection configurations)
        throws EvalException, InvalidConfigurationException, InterruptedException {
    OrderedSetMultimap<Attribute, Dependency> depNodeNames = getDirectPrerequisiteDependenciesForTesting(
            eventHandler, target, configurations);

    ImmutableMultimap<Dependency, ConfiguredTarget> cts = skyframeExecutor.getConfiguredTargetMap(eventHandler,
            target.getConfiguration(), ImmutableSet.copyOf(depNodeNames.values()), false);

    OrderedSetMultimap<Attribute, ConfiguredTarget> result = OrderedSetMultimap.create();
    for (Map.Entry<Attribute, Dependency> entry : depNodeNames.entries()) {
        result.putAll(entry.getKey(), cts.get(entry.getValue()));
    }/*  ww w  .j a v a2s .com*/
    return result;
}

From source file:org.apache.crunch.impl.mr.plan.MSCRPlanner.java

public MRExecutor plan(Class<?> jarClass, Configuration conf) throws IOException {

    DotfileUtil dotfileUtil = new DotfileUtil(jarClass, conf);

    // Generate the debug lineage dotfiles (if configuration is enabled)
    dotfileUtil.buildLineageDotfile(outputs);

    Map<PCollectionImpl<?>, Set<Target>> targetDeps = Maps.newTreeMap(DEPTH_COMPARATOR);
    for (PCollectionImpl<?> pcollect : outputs.keySet()) {
        targetDeps.put(pcollect, pcollect.getTargetDependencies());
    }//from   www  .j  a  va 2  s  .co m

    Multimap<Target, JobPrototype> assignments = HashMultimap.create();

    while (!targetDeps.isEmpty()) {
        Set<Target> allTargets = Sets.newHashSet();
        for (PCollectionImpl<?> pcollect : targetDeps.keySet()) {
            allTargets.addAll(outputs.get(pcollect));
        }
        GraphBuilder graphBuilder = new GraphBuilder();

        // Walk the current plan tree and build a graph in which the vertices are
        // sources, targets, and GBK operations.
        Set<PCollectionImpl<?>> currentStage = Sets.newHashSet();
        for (PCollectionImpl<?> output : targetDeps.keySet()) {
            Set<Target> deps = Sets.intersection(allTargets, targetDeps.get(output));
            if (deps.isEmpty()) {
                graphBuilder.visitOutput(output);
                currentStage.add(output);
            }
        }

        Graph baseGraph = graphBuilder.getGraph();
        boolean hasInputs = false;
        for (Vertex v : baseGraph) {
            if (v.isInput()) {
                hasInputs = true;
                break;
            }
        }
        if (!hasInputs) {
            LOG.warn("No input sources for pipeline, nothing to do...");
            return new MRExecutor(conf, jarClass, outputs, toMaterialize, appendedTargets, pipelineCallables);
        }

        // Create a new graph that splits up up dependent GBK nodes.
        Graph graph = prepareFinalGraph(baseGraph);

        // Break the graph up into connected components.
        List<List<Vertex>> components = graph.connectedComponents();

        // Generate the debug graph dotfiles (if configuration is enabled)
        dotfileUtil.buildBaseGraphDotfile(outputs, graph);
        dotfileUtil.buildSplitGraphDotfile(outputs, graph, components);

        // For each component, we will create one or more job prototypes,
        // depending on its profile.
        // For dependency handling, we only need to care about which
        // job prototype a particular GBK is assigned to.
        Multimap<Vertex, JobPrototype> newAssignments = HashMultimap.create();
        for (List<Vertex> component : components) {
            newAssignments.putAll(constructJobPrototypes(component));
        }

        // Add in the job dependency information here.
        for (Map.Entry<Vertex, JobPrototype> e : newAssignments.entries()) {
            JobPrototype current = e.getValue();
            for (Vertex parent : graph.getParents(e.getKey())) {
                for (JobPrototype parentJobProto : newAssignments.get(parent)) {
                    current.addDependency(parentJobProto);
                }
            }
        }

        ImmutableMultimap<Target, JobPrototype> previousStages = ImmutableMultimap.copyOf(assignments);
        for (Map.Entry<Vertex, JobPrototype> e : newAssignments.entries()) {
            if (e.getKey().isOutput()) {
                PCollectionImpl<?> pcollect = e.getKey().getPCollection();
                JobPrototype current = e.getValue();

                // Add in implicit dependencies via SourceTargets that are read into memory
                for (Target pt : pcollect.getTargetDependencies()) {
                    for (JobPrototype parentJobProto : assignments.get(pt)) {
                        current.addDependency(parentJobProto);
                    }
                }

                // Add this to the set of output assignments
                for (Target t : outputs.get(pcollect)) {
                    assignments.put(t, e.getValue());
                }
            } else {
                Source source = e.getKey().getSource();
                if (source != null && source instanceof Target) {
                    JobPrototype current = e.getValue();
                    Collection<JobPrototype> parentJobPrototypes = previousStages.get((Target) source);
                    if (parentJobPrototypes != null) {
                        for (JobPrototype parentJobProto : parentJobPrototypes) {
                            current.addDependency(parentJobProto);
                        }
                    }
                }
            }
        }

        // Remove completed outputs and mark materialized output locations
        // for subsequent job processing.
        for (PCollectionImpl<?> output : currentStage) {
            if (toMaterialize.containsKey(output)) {
                MaterializableIterable mi = toMaterialize.get(output);
                if (mi.isSourceTarget()) {
                    output.materializeAt((SourceTarget) mi.getSource());
                }
            }
            targetDeps.remove(output);
        }
    }

    // Finally, construct the jobs from the prototypes and return.
    MRExecutor exec = new MRExecutor(conf, jarClass, outputs, toMaterialize, appendedTargets,
            pipelineCallables);

    // Generate the debug Plan dotfiles
    dotfileUtil.buildPlanDotfile(exec, assignments, pipeline, lastJobID);

    for (JobPrototype proto : Sets.newHashSet(assignments.values())) {
        exec.addJob(proto.getCrunchJob(jarClass, conf, pipeline, lastJobID));
    }

    // Generate the debug RTNode dotfiles (if configuration is enabled)
    dotfileUtil.buildRTNodesDotfile(exec);

    // Attach the dotfiles to the MRExcutor context
    dotfileUtil.addDotfilesToContext(exec);

    return exec;
}

From source file:org.prebake.service.tools.ext.JunitHtmlReportGenerator.java

static void generateHtmlReport(Map<String, ?> jsonReport, Path reportDir) throws IOException {
    // Group tests by packages so we can let users examine the results by
    // logical groupings.
    ImmutableMultimap<String, Map<?, ?>> byPackage;
    List<String> resultTypes;
    {/*  w w  w . ja v  a  2  s  .  co  m*/
        Set<String> resultTypeSet = Sets.newHashSet();
        ImmutableList.Builder<Map<?, ?>> b = ImmutableList.builder();
        // We can't trust the jsonReport to have the same structure as the method
        // above, since a filter could arbitrarily change it.
        Object tests = jsonReport.get(ReportKey.TESTS);
        if (tests instanceof Iterable<?>) {
            for (Object testVal : (Iterable<?>) tests) {
                if (!(testVal instanceof Map<?, ?>)) {
                    continue; // If filter nulls out elements.
                }
                Map<?, ?> test = (Map<?, ?>) testVal;
                b.add(test);
                String result = getIfOfType(test, ReportKey.RESULT, String.class);
                resultTypeSet.add(result);
            }
        }
        byPackage = groupBy(b.build(), new Function<Map<?, ?>, String>() {
            public String apply(Map<?, ?> test) {
                String className = getIfOfType(test, ReportKey.CLASS_NAME, String.class);
                if (className == null) {
                    return null;
                }
                int lastDot = className.lastIndexOf('.');
                return (lastDot >= 0) ? className.substring(0, lastDot) : "";
            }
        });
        String[] resultTypeArr = resultTypeSet.toArray(NO_STRINGS);
        Arrays.sort(resultTypeArr);
        resultTypes = ImmutableList.copyOf(resultTypeArr);
    }
    Map<String, Integer> summary = Maps.newHashMap();
    ImmutableList.Builder<Html> table = ImmutableList.builder();
    // Now, call out to create the package groupings, which in turn,
    // create the class level groupings, which in turn create pages for
    // individual tests.
    // As we descend into the test tree, each recursive call updates summary
    // info.
    Path outFile = reportDir.resolve("index.html");
    mkdirs(outFile.getParent());
    String[] packageNames = byPackage.keySet().toArray(NO_STRINGS);
    Arrays.sort(packageNames);
    for (String packageName : packageNames) {
        Collection<Map<?, ?>> tests = byPackage.get(packageName);
        Map<String, Integer> itemSummary = generateHtmlReportOnePackage(packageName, tests,
                reportDir.resolve("index"), resultTypes);
        bagPutAll(itemSummary, summary);
        table.add(htmlLink("index/" + packageName + PACKAGE_FILE_SUFFIX + ".html", packageName))
                .add(htmlSpan("summary", summaryToHtml(itemSummary, resultTypes)));
    }
    writeReport(outFile, "JUnit", KEY_VAL, table.build(), summary, jsonReport, resultTypes, "index");
    OutputStream out = reportDir.resolve("junit_report.css").newOutputStream(StandardOpenOption.CREATE,
            StandardOpenOption.TRUNCATE_EXISTING);
    try {
        Resources.copy(Resources.getResource(JunitHtmlReportGenerator.class, "junit_report.css"), out);
    } finally {
        out.close();
    }
}