Search in sources :

Example 41 with ImmutableMap

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

the class GoTestMainStep method getShellCommandInternal.

@Override
protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
    ImmutableList.Builder<String> command = ImmutableList.<String>builder().addAll(generatorCommandPrefix).add("--output", output.toString()).add("--import-path", packageName.toString()).add("--cover-mode", coverageMode);
    for (Map.Entry<Path, ImmutableMap<String, Path>> pkg : coverageVariables.entrySet()) {
        if (pkg.getValue().isEmpty()) {
            continue;
        }
        StringBuilder pkgFlag = new StringBuilder();
        pkgFlag.append(pkg.getKey().toString());
        pkgFlag.append(':');
        boolean first = true;
        for (Map.Entry<String, Path> pkgVars : pkg.getValue().entrySet()) {
            if (!first) {
                pkgFlag.append(',');
            }
            first = false;
            pkgFlag.append(pkgVars.getKey());
            pkgFlag.append('=');
            pkgFlag.append(pkgVars.getValue().toString());
        }
        command.add("--cover-pkgs", pkgFlag.toString());
    }
    for (Path source : testFiles) {
        command.add(source.toString());
    }
    return command.build();
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 42 with ImmutableMap

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

the class DirectToJarOutputSettingsSerializer method serialize.

public static ImmutableMap<String, Object> serialize(DirectToJarOutputSettings settings) {
    ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
    builder.put(OUTPUT_PATH, settings.getDirectToJarOutputPath().toString());
    ImmutableList.Builder<ImmutableMap<String, Object>> serializedPatterns = ImmutableList.builder();
    for (Pattern pattern : settings.getClassesToRemoveFromJar()) {
        serializedPatterns.add(ImmutableMap.<String, Object>of(CLASSES_TO_REMOVE_PATTERN, pattern.pattern(), CLASSES_TO_REMOVE_PATTERN_FLAGS, pattern.flags()));
    }
    builder.put(CLASSES_TO_REMOVE, serializedPatterns.build());
    ImmutableList.Builder<String> serializedEntries = ImmutableList.builder();
    for (Path entry : settings.getEntriesToJar()) {
        serializedEntries.add(entry.toString());
    }
    builder.put(ENTRIES, serializedEntries.build());
    if (settings.getMainClass().isPresent()) {
        builder.put(MAIN_CLASS, settings.getMainClass().get());
    }
    if (settings.getManifestFile().isPresent()) {
        builder.put(MANIFEST_FILE, settings.getManifestFile().get().toString());
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 43 with ImmutableMap

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

the class BaseCompileToJarStepFactory method addPostprocessClassesCommands.

/**
   * Adds a BashStep for each postprocessClasses command that runs the command followed by the
   * outputDirectory of javac outputs.
   *
   * The expectation is that the command will inspect and update the directory by
   * modifying, adding, and deleting the .class files in the directory.
   *
   * The outputDirectory should be a valid java root.  I.e., if outputDirectory
   * is buck-out/bin/java/abc/lib__abc__classes/, then a contained class abc.AbcModule
   * should be at buck-out/bin/java/abc/lib__abc__classes/abc/AbcModule.class
   *
   * @param filesystem the project filesystem.
   * @param postprocessClassesCommands the list of commands to post-process .class files.
   * @param outputDirectory the directory that will contain all the javac output.
   * @param declaredClasspathEntries the list of classpath entries.
   * @param bootClasspath the compilation boot classpath.
   */
@VisibleForTesting
static ImmutableList<Step> addPostprocessClassesCommands(ProjectFilesystem filesystem, List<String> postprocessClassesCommands, Path outputDirectory, ImmutableSortedSet<Path> declaredClasspathEntries, Optional<String> bootClasspath) {
    if (postprocessClassesCommands.isEmpty()) {
        return ImmutableList.of();
    }
    ImmutableList.Builder<Step> commands = new ImmutableList.Builder<Step>();
    ImmutableMap.Builder<String, String> envVarBuilder = ImmutableMap.builder();
    envVarBuilder.put("COMPILATION_CLASSPATH", Joiner.on(':').join(Iterables.transform(declaredClasspathEntries, filesystem::resolve)));
    if (bootClasspath.isPresent()) {
        envVarBuilder.put("COMPILATION_BOOTCLASSPATH", bootClasspath.get());
    }
    ImmutableMap<String, String> envVars = envVarBuilder.build();
    for (final String postprocessClassesCommand : postprocessClassesCommands) {
        BashStep bashStep = new BashStep(filesystem.getRootPath(), postprocessClassesCommand + " " + outputDirectory) {

            @Override
            public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
                return envVars;
            }
        };
        commands.add(bashStep);
    }
    return commands.build();
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) BashStep(com.facebook.buck.shell.BashStep) Step(com.facebook.buck.step.Step) BashStep(com.facebook.buck.shell.BashStep) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 44 with ImmutableMap

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

the class Watchman method build.

@VisibleForTesting
@SuppressWarnings("PMD.PrematureDeclaration")
static Watchman build(ListeningProcessExecutor executor, Function<Path, Optional<WatchmanClient>> watchmanConnector, ImmutableSet<Path> projectWatchList, ImmutableMap<String, String> env, ExecutableFinder exeFinder, Console console, Clock clock, Optional<Long> commandTimeoutMillis) throws InterruptedException {
    LOG.info("Creating for: " + projectWatchList);
    Optional<WatchmanClient> watchmanClient = Optional.empty();
    try {
        Path watchmanPath = exeFinder.getExecutable(WATCHMAN, env).toAbsolutePath();
        Optional<? extends Map<String, ?>> result;
        long timeoutMillis = commandTimeoutMillis.orElse(DEFAULT_COMMAND_TIMEOUT_MILLIS);
        long endTimeNanos = clock.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
        result = execute(executor, console, clock, timeoutMillis, TimeUnit.MILLISECONDS.toNanos(timeoutMillis), watchmanPath, "get-sockname");
        if (!result.isPresent()) {
            return NULL_WATCHMAN;
        }
        String rawSockname = (String) result.get().get("sockname");
        if (rawSockname == null) {
            return NULL_WATCHMAN;
        }
        Path socketPath = Paths.get(rawSockname);
        LOG.info("Connecting to Watchman version %s at %s", result.get().get("version"), socketPath);
        watchmanClient = watchmanConnector.apply(socketPath);
        if (!watchmanClient.isPresent()) {
            LOG.warn("Could not connect to Watchman, disabling.");
            return NULL_WATCHMAN;
        }
        LOG.debug("Connected to Watchman");
        long versionQueryStartTimeNanos = clock.nanoTime();
        result = watchmanClient.get().queryWithTimeout(endTimeNanos - versionQueryStartTimeNanos, "version", ImmutableMap.of("required", REQUIRED_CAPABILITIES, "optional", ALL_CAPABILITIES.keySet()));
        LOG.info("Took %d ms to query capabilities %s", TimeUnit.NANOSECONDS.toMillis(clock.nanoTime() - versionQueryStartTimeNanos), ALL_CAPABILITIES);
        if (!result.isPresent()) {
            LOG.warn("Could not get version response from Watchman, disabling Watchman");
            watchmanClient.get().close();
            return NULL_WATCHMAN;
        }
        ImmutableSet.Builder<Capability> capabilitiesBuilder = ImmutableSet.builder();
        if (!extractCapabilities(result.get(), capabilitiesBuilder)) {
            LOG.warn("Could not extract capabilities, disabling Watchman");
            watchmanClient.get().close();
            return NULL_WATCHMAN;
        }
        ImmutableSet<Capability> capabilities = capabilitiesBuilder.build();
        LOG.debug("Got Watchman capabilities: %s", capabilities);
        ImmutableMap.Builder<Path, ProjectWatch> projectWatchesBuilder = ImmutableMap.builder();
        ImmutableMap.Builder<Path, String> clockIdsBuilder = ImmutableMap.builder();
        for (Path rootPath : projectWatchList) {
            Optional<ProjectWatch> projectWatch = queryWatchProject(watchmanClient.get(), rootPath, clock, endTimeNanos - clock.nanoTime());
            if (!projectWatch.isPresent()) {
                watchmanClient.get().close();
                return NULL_WATCHMAN;
            }
            projectWatchesBuilder.put(rootPath, projectWatch.get());
            Optional<String> clockId = queryClock(watchmanClient.get(), projectWatch.get().getWatchRoot(), capabilities, clock, endTimeNanos - clock.nanoTime());
            if (clockId.isPresent()) {
                clockIdsBuilder.put(rootPath, clockId.get());
            }
        }
        return new Watchman(projectWatchesBuilder.build(), capabilities, clockIdsBuilder.build(), Optional.of(socketPath), watchmanClient);
    } catch (ClassCastException | HumanReadableException | IOException e) {
        LOG.warn(e, "Unable to determine the version of watchman. Going without.");
        if (watchmanClient.isPresent()) {
            try {
                watchmanClient.get().close();
            } catch (IOException ioe) {
                LOG.warn(ioe, "Could not close watchman query client");
            }
        }
        return NULL_WATCHMAN;
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 45 with ImmutableMap

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

the class Resolver method resolve.

public void resolve(Collection<String> artifacts) throws RepositoryException, ExecutionException, InterruptedException, IOException {
    ImmutableList.Builder<RemoteRepository> repoBuilder = ImmutableList.builder();
    ImmutableMap.Builder<String, Dependency> dependencyBuilder = ImmutableMap.builder();
    repoBuilder.addAll(repos);
    for (String artifact : artifacts) {
        if (artifact.endsWith(".pom")) {
            Model model = loadPomModel(Paths.get(artifact));
            repoBuilder.addAll(getReposFromPom(model));
            for (Dependency dep : getDependenciesFromPom(model)) {
                dependencyBuilder.put(buildKey(dep.getArtifact()), dep);
            }
        } else {
            Dependency dep = getDependencyFromString(artifact);
            dependencyBuilder.put(buildKey(dep.getArtifact()), dep);
        }
    }
    repos = repoBuilder.build();
    ImmutableMap<String, Dependency> specifiedDependencies = dependencyBuilder.build();
    ImmutableMap<String, Artifact> knownDeps = getRunTimeTransitiveDeps(specifiedDependencies.values());
    // We now have the complete set of dependencies. Build the graph of dependencies. We'd like
    // aether to do this for us, but it doesn't preserve the complete dependency information we need
    // to accurately construct build files.
    final MutableDirectedGraph<Artifact> graph = buildDependencyGraph(knownDeps);
    // Now we have the graph, grab the sources and jars for each dependency, as well as the relevant
    // checksums (which are download by default. Yay!)
    ImmutableSetMultimap<Path, Prebuilt> downloadedArtifacts = downloadArtifacts(graph, specifiedDependencies);
    createBuckFiles(downloadedArtifacts);
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) STGroupString(org.stringtemplate.v4.STGroupString) Dependency(org.eclipse.aether.graph.Dependency) ImmutableMap(com.google.common.collect.ImmutableMap) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Model(org.apache.maven.model.Model)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)702 Map (java.util.Map)346 Test (org.junit.Test)195 HashMap (java.util.HashMap)144 ImmutableList (com.google.common.collect.ImmutableList)126 Path (java.nio.file.Path)104 List (java.util.List)100 ImmutableSet (com.google.common.collect.ImmutableSet)89 IOException (java.io.IOException)84 ArrayList (java.util.ArrayList)74 Set (java.util.Set)69 Optional (java.util.Optional)61 BuildTarget (com.facebook.buck.model.BuildTarget)57 File (java.io.File)57 Collectors (java.util.stream.Collectors)45 HashSet (java.util.HashSet)44 SourcePath (com.facebook.buck.rules.SourcePath)41 VisibleForTesting (com.google.common.annotations.VisibleForTesting)39 Nullable (javax.annotation.Nullable)39 LinkedHashMap (java.util.LinkedHashMap)36