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