Search in sources :

Example 21 with Pair

use of com.facebook.buck.model.Pair in project buck by facebook.

the class JsLibraryDescription method createDevFileRule.

private static <A extends Arg> BuildRule createDevFileRule(BuildRuleParams params, SourcePathRuleFinder ruleFinder, SourcePathResolver sourcePathResolver, A args, Either<SourcePath, Pair<SourcePath, String>> source, WorkerTool worker) {
    final SourcePath sourcePath = source.transform(x -> x, Pair::getFirst);
    final Optional<String> subPath = Optional.ofNullable(source.transform(x -> null, Pair::getSecond));
    final Optional<Path> virtualPath = args.basePath.map(basePath -> changePathPrefix(sourcePath, basePath, params, sourcePathResolver, params.getBuildTarget().getUnflavoredBuildTarget()).resolve(subPath.orElse("")));
    return new JsFile.JsFileDev(ruleFinder.getRule(sourcePath).map(params::copyAppendingExtraDeps).orElse(params), sourcePath, subPath, virtualPath, args.extraArgs, worker);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) Flavored(com.facebook.buck.model.Flavored) Function(java.util.function.Function) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Pair(com.facebook.buck.model.Pair) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) TargetGraph(com.facebook.buck.rules.TargetGraph) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) MorePaths(com.facebook.buck.io.MorePaths) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Paths(java.nio.file.Paths) Hint(com.facebook.buck.rules.Hint) WorkerTool(com.facebook.buck.shell.WorkerTool) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Description(com.facebook.buck.rules.Description) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) Pair(com.facebook.buck.model.Pair)

Example 22 with Pair

use of com.facebook.buck.model.Pair in project buck by facebook.

the class JsLibraryDescription method mapSourcesToFlavors.

private static ImmutableBiMap<Either<SourcePath, Pair<SourcePath, String>>, Flavor> mapSourcesToFlavors(SourcePathResolver sourcePathResolver, ImmutableSet<Either<SourcePath, Pair<SourcePath, String>>> sources) {
    final ImmutableBiMap.Builder<Either<SourcePath, Pair<SourcePath, String>>, Flavor> builder = ImmutableBiMap.builder();
    for (Either<SourcePath, Pair<SourcePath, String>> source : sources) {
        final Path relativePath = source.isLeft() ? sourcePathResolver.getRelativePath(source.getLeft()) : Paths.get(source.getRight().getSecond());
        builder.put(source, JsFlavors.fileFlavorForSourcePath(relativePath));
    }
    return builder.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Either(com.facebook.buck.model.Either) Flavor(com.facebook.buck.model.Flavor) Pair(com.facebook.buck.model.Pair)

Example 23 with Pair

use of com.facebook.buck.model.Pair in project buck by facebook.

the class TypeCoercerTest method traverseWithPair.

@Test
public void traverseWithPair() throws NoSuchFieldException {
    Type type = TestFields.class.getField("pairOfPathsAndStrings").getGenericType();
    @SuppressWarnings("unchecked") TypeCoercer<Pair<Path, String>> coercer = (TypeCoercer<Pair<Path, String>>) typeCoercerFactory.typeCoercerForType(type);
    TestTraversal traversal = new TestTraversal();
    Pair<Path, String> input = new Pair<>(Paths.get("foo"), "bar");
    coercer.traverse(input, traversal);
    assertThat(traversal.getObjects(), Matchers.contains(ImmutableList.of(sameInstance((Object) input.getFirst()), sameInstance((Object) input.getSecond()))));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) Type(java.lang.reflect.Type) Pair(com.facebook.buck.model.Pair) Test(org.junit.Test)

Example 24 with Pair

use of com.facebook.buck.model.Pair in project buck by facebook.

the class AndroidResourceDescription method collectInputSourcePaths.

private static Pair<Optional<SymlinkTree>, Optional<SourcePath>> collectInputSourcePaths(BuildRuleResolver ruleResolver, BuildTarget resourceRuleTarget, Flavor symlinkTreeFlavor, Optional<Either<SourcePath, ImmutableSortedMap<String, SourcePath>>> attribute) {
    if (!attribute.isPresent()) {
        return new Pair<>(Optional.empty(), Optional.empty());
    }
    if (attribute.get().isLeft()) {
        SourcePath inputSourcePath = attribute.get().getLeft();
        if (!(inputSourcePath instanceof PathSourcePath)) {
            // in advance to create a symlink tree.  Instead, we have to pass the source path as is.
            return new Pair<>(Optional.empty(), Optional.of(inputSourcePath));
        }
    }
    BuildTarget symlinkTreeTarget = resourceRuleTarget.withAppendedFlavors(symlinkTreeFlavor);
    SymlinkTree symlinkTree;
    try {
        symlinkTree = (SymlinkTree) ruleResolver.requireRule(symlinkTreeTarget);
    } catch (NoSuchBuildTargetException e) {
        throw new RuntimeException(e);
    }
    return new Pair<>(Optional.of(symlinkTree), Optional.of(symlinkTree.getSourcePathToOutput()));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) BuildTarget(com.facebook.buck.model.BuildTarget) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Pair(com.facebook.buck.model.Pair)

Example 25 with Pair

use of com.facebook.buck.model.Pair in project buck by facebook.

the class AndroidResourceDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, final BuildRuleResolver resolver, A args) {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    if (params.getBuildTarget().getFlavors().contains(RESOURCES_SYMLINK_TREE_FLAVOR)) {
        return createSymlinkTree(ruleFinder, params, args.res, "res");
    } else if (params.getBuildTarget().getFlavors().contains(ASSETS_SYMLINK_TREE_FLAVOR)) {
        return createSymlinkTree(ruleFinder, params, args.assets, "assets");
    }
    // Only allow android resource and library rules as dependencies.
    Optional<BuildRule> invalidDep = params.getDeclaredDeps().get().stream().filter(rule -> !(rule instanceof AndroidResource || rule instanceof AndroidLibrary)).findFirst();
    if (invalidDep.isPresent()) {
        throw new HumanReadableException(params.getBuildTarget() + " (android_resource): dependency " + invalidDep.get().getBuildTarget() + " (" + invalidDep.get().getType() + ") is not of type android_resource or android_library.");
    }
    // We don't handle the resources parameter well in `AndroidResource` rules, as instead of
    // hashing the contents of the entire resources directory, we try to filter out anything that
    // doesn't look like a resource.  This means when our resources are supplied from another rule,
    // we have to resort to some hackery to make sure things work correctly.
    Pair<Optional<SymlinkTree>, Optional<SourcePath>> resInputs = collectInputSourcePaths(resolver, params.getBuildTarget(), RESOURCES_SYMLINK_TREE_FLAVOR, args.res);
    Pair<Optional<SymlinkTree>, Optional<SourcePath>> assetsInputs = collectInputSourcePaths(resolver, params.getBuildTarget(), ASSETS_SYMLINK_TREE_FLAVOR, args.assets);
    params = params.copyAppendingExtraDeps(Iterables.concat(resInputs.getSecond().map(ruleFinder::filterBuildRuleInputs).orElse(ImmutableSet.of()), assetsInputs.getSecond().map(ruleFinder::filterBuildRuleInputs).orElse(ImmutableSet.of())));
    return new AndroidResource(// step.
    params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(AndroidResourceHelper.androidResOnly(params.getDeclaredDeps().get())), params.getExtraDeps()), ruleFinder, resolver.getAllRules(args.deps), resInputs.getSecond().orElse(null), resInputs.getFirst().map(SymlinkTree::getLinks).orElse(ImmutableSortedMap.of()), args.rDotJavaPackage.orElse(null), assetsInputs.getSecond().orElse(null), assetsInputs.getFirst().map(SymlinkTree::getLinks).orElse(ImmutableSortedMap.of()), args.manifest.orElse(null), args.hasWhitelistedStrings.orElse(false), args.resourceUnion.orElse(false), isGrayscaleImageProcessingEnabled);
}
Also used : 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) Optional(java.util.Optional) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Aggregations

Pair (com.facebook.buck.model.Pair)32 Path (java.nio.file.Path)15 SourcePath (com.facebook.buck.rules.SourcePath)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 BuildTarget (com.facebook.buck.model.BuildTarget)8 BuildRule (com.facebook.buck.rules.BuildRule)8 HumanReadableException (com.facebook.buck.util.HumanReadableException)8 Optional (java.util.Optional)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 IOException (java.io.IOException)7 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)6 ImmutableList (com.google.common.collect.ImmutableList)6 Test (org.junit.Test)6 Flavor (com.facebook.buck.model.Flavor)5 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)4 Elf (com.facebook.buck.cxx.elf.Elf)3