Search in sources :

Example 1 with Optional

use of java.util.Optional in project buck by facebook.

the class AndroidBinary method addAccumulateClassNamesStep.

public Supplier<ImmutableMap<String, HashCode>> addAccumulateClassNamesStep(final ImmutableSet<Path> classPathEntriesToDex, ImmutableList.Builder<Step> steps) {
    final ImmutableMap.Builder<String, HashCode> builder = ImmutableMap.builder();
    steps.add(new AbstractExecutionStep("collect_all_class_names") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            for (Path path : classPathEntriesToDex) {
                Optional<ImmutableSortedMap<String, HashCode>> hashes = AccumulateClassNamesStep.calculateClassHashes(context, getProjectFilesystem(), path);
                if (!hashes.isPresent()) {
                    return StepExecutionResult.ERROR;
                }
                builder.putAll(hashes.get());
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    return Suppliers.memoize(builder::build);
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) HashCode(com.google.common.hash.HashCode) ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Optional(java.util.Optional) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with Optional

use of java.util.Optional in project buck by facebook.

the class DefaultAndroidDirectoryResolver method findBuildTools.

private Optional<Path> findBuildTools() {
    if (!sdk.isPresent()) {
        buildToolsErrorMessage = Optional.of(TOOLS_NEED_SDK_MESSAGE);
        return Optional.empty();
    }
    final Path sdkDir = sdk.get();
    final Path toolsDir = sdkDir.resolve("build-tools");
    if (toolsDir.toFile().isDirectory()) {
        // In older versions of the ADT that have been upgraded via the SDK manager, the build-tools
        // directory appears to contain subfolders of the form "17.0.0". However, newer versions of
        // the ADT that are downloaded directly from http://developer.android.com/ appear to have
        // subfolders of the form android-4.2.2. There also appear to be cases where subfolders
        // are named build-tools-18.0.0. We need to support all of these scenarios.
        File[] directories;
        try {
            directories = toolsDir.toFile().listFiles(pathname -> {
                if (!pathname.isDirectory()) {
                    return false;
                }
                String version = stripBuildToolsPrefix(pathname.getName());
                if (!VersionStringComparator.isValidVersionString(version)) {
                    throw new HumanReadableException("%s in %s is not a valid build tools directory.%n" + "Build tools directories should be follow the naming scheme: " + "android-<VERSION>, build-tools-<VERSION>, or <VERSION>. Please remove " + "directory %s.", pathname.getName(), buildTools, pathname.getName());
                }
                if (targetBuildToolsVersion.isPresent()) {
                    return targetBuildToolsVersion.get().equals(pathname.getName());
                }
                return true;
            });
        } catch (HumanReadableException e) {
            buildToolsErrorMessage = Optional.of(e.getHumanReadableErrorMessage());
            return Optional.empty();
        }
        if (targetBuildToolsVersion.isPresent()) {
            if (directories.length == 0) {
                buildToolsErrorMessage = unableToFindTargetBuildTools();
                return Optional.empty();
            } else {
                return Optional.of(directories[0].toPath());
            }
        }
        // We aren't looking for a specific version, so we pick the newest version
        final VersionStringComparator comparator = new VersionStringComparator();
        File newestBuildDir = null;
        String newestBuildDirVersion = null;
        for (File directory : directories) {
            String currentDirVersion = stripBuildToolsPrefix(directory.getName());
            if (newestBuildDir == null || newestBuildDirVersion == null || comparator.compare(newestBuildDirVersion, currentDirVersion) < 0) {
                newestBuildDir = directory;
                newestBuildDirVersion = currentDirVersion;
            }
        }
        if (newestBuildDir == null) {
            buildToolsErrorMessage = Optional.of(buildTools + " was empty, but should have " + "contained a subdirectory with build tools. Install them using the Android " + "SDK Manager (" + toolsDir.getParent().resolve("tools").resolve("android") + ").");
            return Optional.empty();
        }
        return Optional.of(newestBuildDir.toPath());
    }
    if (targetBuildToolsVersion.isPresent()) {
        // We were looking for a specific version, but we aren't going to find it at this point since
        // nothing under platform-tools was versioned.
        buildToolsErrorMessage = unableToFindTargetBuildTools();
        return Optional.empty();
    }
    // Build tools used to exist inside of platform-tools, so fallback to that.
    return Optional.of(sdkDir.resolve("platform-tools"));
}
Also used : Path(java.nio.file.Path) Charsets(com.google.common.base.Charsets) ImmutableSet(com.google.common.collect.ImmutableSet) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HumanReadableException(com.facebook.buck.util.HumanReadableException) FileSystem(java.nio.file.FileSystem) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Strings(com.google.common.base.Strings) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Escaper(com.facebook.buck.util.Escaper) Optional(java.util.Optional) Pair(com.facebook.buck.model.Pair) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BufferedReader(java.io.BufferedReader) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) Path(java.nio.file.Path) HumanReadableException(com.facebook.buck.util.HumanReadableException) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) File(java.io.File)

Example 3 with Optional

use of java.util.Optional in project buck by facebook.

the class AndroidResourceDescription method createSymlinkTree.

private SymlinkTree createSymlinkTree(SourcePathRuleFinder ruleFinder, BuildRuleParams params, Optional<Either<SourcePath, ImmutableSortedMap<String, SourcePath>>> symlinkAttribute, String outputDirName) {
    ImmutableMap<Path, SourcePath> links = ImmutableMap.of();
    if (symlinkAttribute.isPresent()) {
        if (symlinkAttribute.get().isLeft()) {
            // If our resources are coming from a `PathSourcePath`, we collect only the inputs we care
            // about and pass those in separately, so that that `AndroidResource` rule knows to only
            // hash these into it's rule key.
            // TODO(k21): This is deprecated and should be disabled or removed.
            // Accessing the filesystem during rule creation is problematic because the accesses are
            // not cached or tracked in any way.
            Preconditions.checkArgument(symlinkAttribute.get().getLeft() instanceof PathSourcePath, "Resource or asset symlink tree can only be built for a PathSourcePath");
            PathSourcePath path = (PathSourcePath) symlinkAttribute.get().getLeft();
            links = collectInputFiles(path.getFilesystem(), path.getRelativePath());
        } else {
            links = RichStream.from(symlinkAttribute.get().getRight().entrySet()).map(e -> new AbstractMap.SimpleEntry<>(Paths.get(e.getKey()), e.getValue())).filter(e -> isPossibleResourcePath(e.getKey())).collect(MoreCollectors.toImmutableMap(e -> e.getKey(), e -> e.getValue()));
        }
    }
    Path symlinkTreeRoot = BuildTargets.getGenPath(params.getProjectFilesystem(), params.getBuildTarget(), "%s").resolve(outputDirName);
    return new SymlinkTree(params.getBuildTarget(), params.getProjectFilesystem(), symlinkTreeRoot, links, ruleFinder);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) RichStream(com.facebook.buck.util.RichStream) Either(com.facebook.buck.model.Either) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavored(com.facebook.buck.model.Flavored) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BuildRule(com.facebook.buck.rules.BuildRule) SymlinkTree(com.facebook.buck.rules.SymlinkTree) Files(com.google.common.io.Files) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) Suppliers(com.google.common.base.Suppliers) Pair(com.facebook.buck.model.Pair) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) MoreCollectors(com.facebook.buck.util.MoreCollectors) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) FileVisitor(java.nio.file.FileVisitor) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) MorePaths(com.facebook.buck.io.MorePaths) FileVisitResult(java.nio.file.FileVisitResult) AbstractMap(java.util.AbstractMap) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Paths(java.nio.file.Paths) MiniAapt(com.facebook.buck.android.aapt.MiniAapt) Hint(com.facebook.buck.rules.Hint) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BuildTargets(com.facebook.buck.model.BuildTargets) Description(com.facebook.buck.rules.Description) SymlinkTree(com.facebook.buck.rules.SymlinkTree) PathSourcePath(com.facebook.buck.rules.PathSourcePath)

Example 4 with Optional

use of java.util.Optional in project buck by facebook.

the class ApplePackageDescription method getApplePackageConfig.

/**
   * Get the correct package configuration based on the platform flavors of this build target.
   *
   * Validates that all named platforms yields the identical package config.
   *
   * @return If found, a package config for this target.
   * @throws HumanReadableException if there are multiple possible package configs.
   */
private Optional<ApplePackageConfigAndPlatformInfo> getApplePackageConfig(BuildTarget target, Function<String, com.facebook.buck.rules.args.Arg> macroExpander) {
    Set<Flavor> platformFlavors = getPlatformFlavorsOrDefault(target);
    // Ensure that different platforms generate the same config.
    // The value of this map is just for error reporting.
    Multimap<Optional<ApplePackageConfigAndPlatformInfo>, Flavor> packageConfigs = MultimapBuilder.hashKeys().arrayListValues().build();
    for (Flavor flavor : platformFlavors) {
        AppleCxxPlatform platform = appleCxxPlatformFlavorDomain.getValue(flavor);
        Optional<ApplePackageConfig> packageConfig = config.getPackageConfigForPlatform(platform.getAppleSdk().getApplePlatform());
        packageConfigs.put(packageConfig.isPresent() ? Optional.of(ApplePackageConfigAndPlatformInfo.of(packageConfig.get(), macroExpander, platform)) : Optional.empty(), flavor);
    }
    if (packageConfigs.isEmpty()) {
        return Optional.empty();
    } else if (packageConfigs.keySet().size() == 1) {
        return Iterables.getOnlyElement(packageConfigs.keySet());
    } else {
        throw new HumanReadableException("In target %s: Multi-architecture package has different package configs for targets: %s", target.getFullyQualifiedName(), packageConfigs.asMap().values());
    }
}
Also used : Optional(java.util.Optional) HumanReadableException(com.facebook.buck.util.HumanReadableException) Flavor(com.facebook.buck.model.Flavor)

Example 5 with Optional

use of java.util.Optional in project buck by facebook.

the class AppleTest method getTestCommand.

public Pair<ImmutableList<Step>, ExternalTestRunnerTestSpec> getTestCommand(ExecutionContext context, TestRunningOptions options, SourcePathResolver pathResolver, TestRule.TestReportingCallback testReportingCallback) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    ExternalTestRunnerTestSpec.Builder externalSpec = ExternalTestRunnerTestSpec.builder().setTarget(getBuildTarget()).setLabels(getLabels()).setContacts(getContacts());
    Path resolvedTestBundleDirectory = pathResolver.getAbsolutePath(Preconditions.checkNotNull(testBundle.getSourcePathToOutput()));
    Path pathToTestOutput = getProjectFilesystem().resolve(getPathToTestOutputDirectory());
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTestOutput));
    Path resolvedTestLogsPath = getProjectFilesystem().resolve(testLogsPath);
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), resolvedTestLogsPath));
    Path resolvedTestOutputPath = getProjectFilesystem().resolve(testOutputPath);
    Optional<Path> testHostAppPath = Optional.empty();
    if (testHostApp.isPresent()) {
        Path resolvedTestHostAppDirectory = pathResolver.getAbsolutePath(Preconditions.checkNotNull(testHostApp.get().getSourcePathToOutput()));
        testHostAppPath = Optional.of(resolvedTestHostAppDirectory.resolve(testHostApp.get().getUnzippedOutputFilePathToBinary()));
    }
    if (!useXctest) {
        if (!xctool.isPresent()) {
            throw new HumanReadableException("Set xctool_path = /path/to/xctool or xctool_zip_target = //path/to:xctool-zip " + "in the [apple] section of .buckconfig to run this test");
        }
        ImmutableSet.Builder<Path> logicTestPathsBuilder = ImmutableSet.builder();
        ImmutableMap.Builder<Path, Path> appTestPathsToHostAppsBuilder = ImmutableMap.builder();
        if (testHostAppPath.isPresent()) {
            appTestPathsToHostAppsBuilder.put(resolvedTestBundleDirectory, testHostAppPath.get());
        } else {
            logicTestPathsBuilder.add(resolvedTestBundleDirectory);
        }
        xctoolStdoutReader = Optional.of(new AppleTestXctoolStdoutReader(testReportingCallback));
        Optional<String> destinationSpecifierArg;
        if (!destinationSpecifier.get().isEmpty()) {
            destinationSpecifierArg = Optional.of(Joiner.on(',').join(Iterables.transform(destinationSpecifier.get().entrySet(), input -> input.getKey() + "=" + input.getValue())));
        } else {
            destinationSpecifierArg = defaultDestinationSpecifier;
        }
        Optional<String> snapshotReferenceImagesPath = Optional.empty();
        if (this.snapshotReferenceImagesPath.isPresent()) {
            if (this.snapshotReferenceImagesPath.get().isLeft()) {
                snapshotReferenceImagesPath = Optional.of(pathResolver.getAbsolutePath(this.snapshotReferenceImagesPath.get().getLeft()).toString());
            } else if (this.snapshotReferenceImagesPath.get().isRight()) {
                snapshotReferenceImagesPath = Optional.of(getProjectFilesystem().getPathForRelativePath(this.snapshotReferenceImagesPath.get().getRight()).toString());
            }
        }
        XctoolRunTestsStep xctoolStep = new XctoolRunTestsStep(getProjectFilesystem(), pathResolver.getAbsolutePath(xctool.get()), options.getEnvironmentOverrides(), xctoolStutterTimeout, platformName, destinationSpecifierArg, logicTestPathsBuilder.build(), appTestPathsToHostAppsBuilder.build(), resolvedTestOutputPath, xctoolStdoutReader, xcodeDeveloperDirSupplier, options.getTestSelectorList(), context.isDebugEnabled(), Optional.of(testLogDirectoryEnvironmentVariable), Optional.of(resolvedTestLogsPath), Optional.of(testLogLevelEnvironmentVariable), Optional.of(testLogLevel), testRuleTimeoutMs, snapshotReferenceImagesPath);
        steps.add(xctoolStep);
        externalSpec.setType("xctool-" + (testHostApp.isPresent() ? "application" : "logic"));
        externalSpec.setCommand(xctoolStep.getCommand());
        externalSpec.setEnv(xctoolStep.getEnv(context));
    } else {
        xctestOutputReader = Optional.of(new AppleTestXctestOutputReader(testReportingCallback));
        HashMap<String, String> environment = new HashMap<>();
        environment.putAll(xctest.getEnvironment(pathResolver));
        environment.putAll(options.getEnvironmentOverrides());
        if (testHostAppPath.isPresent()) {
            environment.put("XCInjectBundleInto", testHostAppPath.get().toString());
        }
        XctestRunTestsStep xctestStep = new XctestRunTestsStep(getProjectFilesystem(), ImmutableMap.copyOf(environment), xctest.getCommandPrefix(pathResolver), resolvedTestBundleDirectory, resolvedTestOutputPath, xctestOutputReader, xcodeDeveloperDirSupplier);
        steps.add(xctestStep);
        externalSpec.setType("xctest");
        externalSpec.setCommand(xctestStep.getCommand());
        externalSpec.setEnv(xctestStep.getEnv(context));
    }
    return new Pair<>(steps.build(), externalSpec.build());
}
Also used : Path(java.nio.file.Path) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) OptionalCompat(com.facebook.buck.util.OptionalCompat) HasRuntimeDeps(com.facebook.buck.rules.HasRuntimeDeps) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) TestResults(com.facebook.buck.test.TestResults) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Pair(com.facebook.buck.model.Pair) TestRule(com.facebook.buck.rules.TestRule) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableContext(com.facebook.buck.rules.BuildableContext) BuildTarget(com.facebook.buck.model.BuildTarget) StandardCharsets(java.nio.charset.StandardCharsets) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) List(java.util.List) Stream(java.util.stream.Stream) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) Iterables(com.google.common.collect.Iterables) Step(com.facebook.buck.step.Step) Supplier(com.google.common.base.Supplier) SourcePath(com.facebook.buck.rules.SourcePath) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) ExecutionContext(com.facebook.buck.step.ExecutionContext) Label(com.facebook.buck.rules.Label) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) MoreCollectors(com.facebook.buck.util.MoreCollectors) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Files(java.nio.file.Files) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) InputStreamReader(java.io.InputStreamReader) BuildContext(com.facebook.buck.rules.BuildContext) Preconditions(com.google.common.base.Preconditions) BufferedReader(java.io.BufferedReader) BuildTargets(com.facebook.buck.model.BuildTargets) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec) Pair(com.facebook.buck.model.Pair)

Aggregations

Optional (java.util.Optional)2983 List (java.util.List)1807 Map (java.util.Map)1066 ArrayList (java.util.ArrayList)1023 Collectors (java.util.stream.Collectors)952 Set (java.util.Set)753 IOException (java.io.IOException)732 HashMap (java.util.HashMap)631 Test (org.junit.Test)504 Collections (java.util.Collections)496 Collection (java.util.Collection)439 Arrays (java.util.Arrays)438 Logger (org.slf4j.Logger)416 LoggerFactory (org.slf4j.LoggerFactory)409 HashSet (java.util.HashSet)373 Objects (java.util.Objects)318 ImmutableList (com.google.common.collect.ImmutableList)290 Stream (java.util.stream.Stream)277 ImmutableMap (com.google.common.collect.ImmutableMap)243 Function (java.util.function.Function)221