Search in sources :

Example 6 with Either

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

the class StringWithMacrosTypeCoercer method parse.

private StringWithMacros parse(CellPathResolver cellRoots, ProjectFilesystem filesystem, Path pathRelativeToProjectRoot, String blob) throws CoerceFailedException {
    ImmutableList.Builder<Either<String, Macro>> parts = ImmutableList.builder();
    // Iterate over all macros found in the string, expanding each found macro.
    int lastEnd = 0;
    MacroFinder.MacroFinderAutomaton matcher = new MacroFinder.MacroFinderAutomaton(blob);
    while (matcher.hasNext()) {
        MacroMatchResult matchResult = matcher.next();
        // Add everything from the original string since the last match to this one.
        if (lastEnd < matchResult.getStartIndex()) {
            parts.add(Either.ofLeft(blob.substring(lastEnd, matchResult.getStartIndex())));
        }
        // Look up the macro coercer that owns this macro name.
        String name = matchResult.getMacroType();
        Class<? extends Macro> clazz = macros.get(name);
        if (clazz == null) {
            throw new CoerceFailedException(String.format("expanding %s: no such macro \"%s\"", blob.substring(matchResult.getStartIndex(), matchResult.getEndIndex()), matchResult.getMacroType()));
        }
        MacroTypeCoercer<? extends Macro> coercer = Preconditions.checkNotNull(coercers.get(clazz));
        ImmutableList<String> args = matchResult.getMacroInput();
        // Delegate to the macro coercers to parse the macro..
        Macro macro;
        try {
            macro = coercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, args);
        } catch (CoerceFailedException e) {
            throw new CoerceFailedException(String.format("macro \"%s\": %s", name, e.getMessage()), e);
        }
        parts.add(Either.ofRight(macro));
        lastEnd = matchResult.getEndIndex();
    }
    // Append the remaining part of the original string after the last match.
    if (lastEnd < blob.length()) {
        parts.add(Either.ofLeft(blob.substring(lastEnd, blob.length())));
    }
    return StringWithMacros.of(parts.build());
}
Also used : MacroMatchResult(com.facebook.buck.model.MacroMatchResult) ImmutableList(com.google.common.collect.ImmutableList) Macro(com.facebook.buck.rules.macros.Macro) MacroFinder(com.facebook.buck.model.MacroFinder) Either(com.facebook.buck.model.Either)

Example 7 with Either

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

the class RobolectricTestDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    JavacOptions javacOptions = JavacOptionsFactory.create(templateOptions, params, resolver, ruleFinder, args);
    AndroidLibraryGraphEnhancer graphEnhancer = new AndroidLibraryGraphEnhancer(params.getBuildTarget(), params.copyReplacingExtraDeps(Suppliers.ofInstance(resolver.getAllRules(args.exportedDeps))), javacOptions, DependencyMode.TRANSITIVE, /* forceFinalResourceIds */
    true, /* resourceUnionPackage */
    Optional.empty(), /* rName */
    Optional.empty(), args.useOldStyleableFormat);
    if (CalculateAbi.isAbiTarget(params.getBuildTarget())) {
        if (params.getBuildTarget().getFlavors().contains(AndroidLibraryGraphEnhancer.DUMMY_R_DOT_JAVA_FLAVOR)) {
            return graphEnhancer.getBuildableForAndroidResourcesAbi(resolver, ruleFinder);
        }
        BuildTarget testTarget = CalculateAbi.getLibraryTarget(params.getBuildTarget());
        BuildRule testRule = resolver.requireRule(testTarget);
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(testRule.getSourcePathToOutput()));
    }
    ImmutableList<String> vmArgs = args.vmArgs;
    Optional<DummyRDotJava> dummyRDotJava = graphEnhancer.getBuildableForAndroidResources(resolver, /* createBuildableIfEmpty */
    true);
    ImmutableSet<Either<SourcePath, Path>> additionalClasspathEntries = ImmutableSet.of();
    if (dummyRDotJava.isPresent()) {
        additionalClasspathEntries = ImmutableSet.of(Either.ofLeft(dummyRDotJava.get().getSourcePathToOutput()));
        ImmutableSortedSet<BuildRule> newExtraDeps = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).add(dummyRDotJava.get()).build();
        params = params.copyReplacingExtraDeps(Suppliers.ofInstance(newExtraDeps));
    }
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaTestDescription.CxxLibraryEnhancement cxxLibraryEnhancement = new JavaTestDescription.CxxLibraryEnhancement(params, args.useCxxLibraries, args.cxxLibraryWhitelist, resolver, ruleFinder, cxxPlatform);
    params = cxxLibraryEnhancement.updatedParams;
    // Rewrite dependencies on tests to actually depend on the code which backs the test.
    BuildRuleParams testsLibraryParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), resolver.getAllRules(args.providedDeps)))).build()), Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).addAll(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))).build())).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
    JavaLibrary testsLibrary = resolver.addToIndex(new DefaultJavaLibrary(testsLibraryParams, pathResolver, ruleFinder, args.srcs, validateResources(pathResolver, params.getProjectFilesystem(), args.resources), javacOptions.getGeneratedSourceFolderName(), args.proguardConfig, /* postprocessClassesCommands */
    ImmutableList.of(), /* exportDeps */
    ImmutableSortedSet.of(), /* providedDeps */
    resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, testsLibraryParams.getDeps()), javacOptions.trackClassUsage(), additionalClasspathEntries, new JavacToJarStepFactory(javacOptions, new BootClasspathAppender()), args.resourcesRoot, args.manifestFile, args.mavenCoords, /* tests */
    ImmutableSortedSet.of(), /* classesToRemoveFromJar */
    ImmutableSet.of()));
    return new RobolectricTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testsLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleFinder, testsLibrary, additionalClasspathEntries, args.labels, args.contacts, TestType.JUNIT, javaOptions, vmArgs, cxxLibraryEnhancement.nativeLibsEnvironment, dummyRDotJava, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.getRunTestSeparately(), args.getForkMode(), args.stdOutLogLevel, args.stdErrLogLevel, args.robolectricRuntimeDependency, args.robolectricManifest);
}
Also used : JavacOptions(com.facebook.buck.jvm.java.JavacOptions) JavacToJarStepFactory(com.facebook.buck.jvm.java.JavacToJarStepFactory) Optional(java.util.Optional) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) Either(com.facebook.buck.model.Either) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) BuildRule(com.facebook.buck.rules.BuildRule)

Example 8 with Either

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

the class TypeCoercerTest method traverseWithEitherAndContainer.

@Test
public void traverseWithEitherAndContainer() throws NoSuchFieldException {
    Type type = TestFields.class.getField("eitherStringOrStringList").getGenericType();
    @SuppressWarnings("unchecked") TypeCoercer<Either<String, List<String>>> coercer = (TypeCoercer<Either<String, List<String>>>) typeCoercerFactory.typeCoercerForType(type);
    TestTraversal traversal = new TestTraversal();
    Either<String, List<String>> input = Either.ofRight((List<String>) ImmutableList.of("foo"));
    coercer.traverse(input, traversal);
    assertThat(traversal.getObjects(), Matchers.contains(ImmutableList.of(sameInstance((Object) input.getRight()), sameInstance((Object) input.getRight().get(0)))));
    traversal = new TestTraversal();
    Either<String, List<String>> input2 = Either.ofLeft("foo");
    coercer.traverse(input2, traversal);
    assertThat(traversal.getObjects(), hasSize(1));
    assertThat(traversal.getObjects().get(0), sameInstance((Object) "foo"));
}
Also used : Type(java.lang.reflect.Type) Either(com.facebook.buck.model.Either) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.Test)

Example 9 with Either

use of com.facebook.buck.model.Either 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 10 with Either

use of com.facebook.buck.model.Either 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)

Aggregations

Either (com.facebook.buck.model.Either)13 SourcePath (com.facebook.buck.rules.SourcePath)7 BuildRule (com.facebook.buck.rules.BuildRule)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)5 ImmutableList (com.google.common.collect.ImmutableList)5 Path (java.nio.file.Path)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 Flavor (com.facebook.buck.model.Flavor)4 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)4 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)4 Optional (java.util.Optional)4 Pair (com.facebook.buck.model.Pair)3 HumanReadableException (com.facebook.buck.util.HumanReadableException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)3 MorePaths (com.facebook.buck.io.MorePaths)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 JavacOptions (com.facebook.buck.jvm.java.JavacOptions)2 Flavored (com.facebook.buck.model.Flavored)2