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);
}
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"));
}
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);
}
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());
}
}
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());
}
Aggregations