use of com.google.common.collect.ImmutableSet in project buck by facebook.
the class IjProjectTemplateDataPreparer method createExcludes.
public ImmutableSet<IjFolder> createExcludes(final IjModule module) throws IOException {
final ImmutableSet.Builder<IjFolder> excludesBuilder = ImmutableSet.builder();
final Path moduleBasePath = module.getModuleBasePath();
projectFilesystem.walkRelativeFileTree(moduleBasePath, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// When we create excludes for that module.
if (filesystemTraversalBoundaryPaths.contains(dir) && !moduleBasePath.equals(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
if (isRootAndroidResourceDirectory(module, dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
if (!referencedFolderPaths.contains(dir)) {
excludesBuilder.add(new ExcludeFolder(dir));
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
return excludesBuilder.build();
}
use of com.google.common.collect.ImmutableSet in project buck by facebook.
the class PythonTestDescription method createBuildRule.
@Override
public <A extends Arg> PythonTest createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, final A args) throws HumanReadableException, NoSuchBuildTargetException {
PythonPlatform pythonPlatform = pythonPlatforms.getValue(params.getBuildTarget()).orElse(pythonPlatforms.getValue(args.platform.<Flavor>map(InternalFlavor::of).orElse(pythonPlatforms.getFlavors().iterator().next())));
CxxPlatform cxxPlatform = cxxPlatforms.getValue(params.getBuildTarget()).orElse(defaultCxxPlatform);
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
Path baseModule = PythonUtil.getBasePath(params.getBuildTarget(), args.baseModule);
Optional<ImmutableMap<BuildTarget, Version>> selectedVersions = targetGraph.get(params.getBuildTarget()).getSelectedVersions();
ImmutableMap<Path, SourcePath> srcs = PythonUtil.getModules(params.getBuildTarget(), resolver, ruleFinder, pathResolver, pythonPlatform, cxxPlatform, "srcs", baseModule, args.srcs, args.platformSrcs, args.versionedSrcs, selectedVersions);
ImmutableMap<Path, SourcePath> resources = PythonUtil.getModules(params.getBuildTarget(), resolver, ruleFinder, pathResolver, pythonPlatform, cxxPlatform, "resources", baseModule, args.resources, args.platformResources, args.versionedResources, selectedVersions);
// Convert the passed in module paths into test module names.
ImmutableSet.Builder<String> testModulesBuilder = ImmutableSet.builder();
for (Path name : srcs.keySet()) {
testModulesBuilder.add(PythonUtil.toModuleName(params.getBuildTarget(), name.toString()));
}
ImmutableSet<String> testModules = testModulesBuilder.build();
// Construct a build rule to generate the test modules list source file and
// add it to the build.
BuildRule testModulesBuildRule = createTestModulesSourceBuildRule(params, getTestModulesListPath(params.getBuildTarget(), params.getProjectFilesystem()), testModules);
resolver.addToIndex(testModulesBuildRule);
String mainModule;
if (args.mainModule.isPresent()) {
mainModule = args.mainModule.get();
} else {
mainModule = PythonUtil.toModuleName(params.getBuildTarget(), getTestMainName().toString());
}
// Build up the list of everything going into the python test.
PythonPackageComponents testComponents = PythonPackageComponents.of(ImmutableMap.<Path, SourcePath>builder().put(getTestModulesListName(), testModulesBuildRule.getSourcePathToOutput()).put(getTestMainName(), pythonBuckConfig.getPathToTestMain(params.getProjectFilesystem())).putAll(srcs).build(), resources, ImmutableMap.of(), ImmutableSet.of(), args.zipSafe);
PythonPackageComponents allComponents = PythonUtil.getAllComponents(params, resolver, ruleFinder, testComponents, pythonPlatform, cxxBuckConfig, cxxPlatform, args.linkerFlags.stream().map(MacroArg.toMacroArgFunction(PythonUtil.MACRO_HANDLER, params.getBuildTarget(), params.getCellRoots(), resolver)::apply).collect(MoreCollectors.toImmutableList()), pythonBuckConfig.getNativeLinkStrategy(), args.preloadDeps);
// Build the PEX using a python binary rule with the minimum dependencies.
PythonBinary binary = binaryDescription.createPackageRule(params.withBuildTarget(getBinaryBuildTarget(params.getBuildTarget())), resolver, ruleFinder, pythonPlatform, cxxPlatform, mainModule, args.extension, allComponents, args.buildArgs, args.packageStyle.orElse(pythonBuckConfig.getPackageStyle()), PythonUtil.getPreloadNames(resolver, cxxPlatform, args.preloadDeps));
resolver.addToIndex(binary);
ImmutableList.Builder<Pair<Float, ImmutableSet<Path>>> neededCoverageBuilder = ImmutableList.builder();
for (NeededCoverageSpec coverageSpec : args.neededCoverage) {
BuildRule buildRule = resolver.getRule(coverageSpec.getBuildTarget());
if (params.getDeps().contains(buildRule) && buildRule instanceof PythonLibrary) {
PythonLibrary pythonLibrary = (PythonLibrary) buildRule;
ImmutableSortedSet<Path> paths;
if (coverageSpec.getPathName().isPresent()) {
Path path = coverageSpec.getBuildTarget().getBasePath().resolve(coverageSpec.getPathName().get());
if (!pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform).getModules().keySet().contains(path)) {
throw new HumanReadableException("%s: path %s specified in needed_coverage not found in target %s", params.getBuildTarget(), path, buildRule.getBuildTarget());
}
paths = ImmutableSortedSet.of(path);
} else {
paths = ImmutableSortedSet.copyOf(pythonLibrary.getPythonPackageComponents(pythonPlatform, cxxPlatform).getModules().keySet());
}
neededCoverageBuilder.add(new Pair<Float, ImmutableSet<Path>>(coverageSpec.getNeededCoverageRatio(), paths));
} else {
throw new HumanReadableException("%s: needed_coverage requires a python library dependency. Found %s instead", params.getBuildTarget(), buildRule);
}
}
Supplier<ImmutableMap<String, String>> testEnv = () -> ImmutableMap.copyOf(Maps.transformValues(args.env, MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)));
// Generate and return the python test rule, which depends on the python binary rule above.
return PythonTest.from(params, ruleFinder, testEnv, binary, args.labels, neededCoverageBuilder.build(), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.contacts);
}
use of com.google.common.collect.ImmutableSet in project buck by facebook.
the class QueryTargetAccessor method filterAttributeContents.
/**
* Filters the objects in the given attribute that satisfy the given predicate.
*/
public static <T> ImmutableSet<Object> filterAttributeContents(TargetNode<T, ?> node, String attribute, final Predicate<Object> predicate) {
try {
final ImmutableSet.Builder<Object> builder = ImmutableSet.builder();
Class<?> constructorArgClass = node.getConstructorArg().getClass();
Field field = constructorArgClass.getField(attribute);
ParamInfo info = new ParamInfo(typeCoercerFactory, constructorArgClass, field);
info.traverse(value -> {
if (predicate.apply(value)) {
builder.add(value);
}
}, node.getConstructorArg());
return builder.build();
} catch (NoSuchFieldException e) {
// Ignore if the field does not exist in this rule.
return ImmutableSet.of();
}
}
use of com.google.common.collect.ImmutableSet in project buck by facebook.
the class CellProvider method createForLocalBuild.
/**
* Create a cell provider at a given root.
*/
public static CellProvider createForLocalBuild(ProjectFilesystem rootFilesystem, Watchman watchman, BuckConfig rootConfig, CellConfig rootCellConfigOverrides, KnownBuildRuleTypesFactory knownBuildRuleTypesFactory) throws IOException {
DefaultCellPathResolver rootCellCellPathResolver = new DefaultCellPathResolver(rootFilesystem.getRootPath(), rootConfig.getConfig());
ImmutableMap<RelativeCellName, Path> transitiveCellPathMapping = rootCellCellPathResolver.getTransitivePathMapping();
ImmutableMap<Path, RawConfig> pathToConfigOverrides;
try {
pathToConfigOverrides = rootCellConfigOverrides.getOverridesByPath(transitiveCellPathMapping);
} catch (CellConfig.MalformedOverridesException e) {
throw new HumanReadableException(e.getMessage());
}
ImmutableSet<Path> allRoots = ImmutableSet.copyOf(transitiveCellPathMapping.values());
return new CellProvider(cellProvider -> new CacheLoader<Path, Cell>() {
@Override
public Cell load(Path cellPath) throws IOException, InterruptedException {
Path normalizedCellPath = cellPath.toRealPath().normalize();
Preconditions.checkState(allRoots.contains(normalizedCellPath), "Cell %s outside of transitive closure of root cell (%s).", normalizedCellPath, allRoots);
RawConfig configOverrides = Optional.ofNullable(pathToConfigOverrides.get(normalizedCellPath)).orElse(RawConfig.of(ImmutableMap.of()));
Config config = Configs.createDefaultConfig(normalizedCellPath, configOverrides);
DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(normalizedCellPath, config);
cellPathResolver.getCellPaths().forEach((name, path) -> {
Path pathInRootResolver = rootCellCellPathResolver.getCellPaths().get(name);
if (pathInRootResolver == null) {
throw new HumanReadableException("In the config of %s: %s.%s must exist in the root cell's cell mappings.", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name);
} else if (!pathInRootResolver.equals(path)) {
throw new HumanReadableException("In the config of %s: %s.%s must point to the same directory as the root " + "cell's cell mapping: (root) %s != (current) %s", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name, pathInRootResolver, path);
}
});
ProjectFilesystem cellFilesystem = new ProjectFilesystem(normalizedCellPath, config);
BuckConfig buckConfig = new BuckConfig(config, cellFilesystem, rootConfig.getArchitecture(), rootConfig.getPlatform(), rootConfig.getEnvironment(), cellPathResolver);
return new Cell(cellPathResolver.getKnownRoots(), cellFilesystem, watchman, buckConfig, knownBuildRuleTypesFactory, cellProvider);
}
}, cellProvider -> {
try {
return new Cell(rootCellCellPathResolver.getKnownRoots(), rootFilesystem, watchman, rootConfig, knownBuildRuleTypesFactory, cellProvider);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while loading root cell", e);
} catch (IOException e) {
throw new HumanReadableException("Failed to load root cell", e);
}
});
}
use of com.google.common.collect.ImmutableSet in project buck by facebook.
the class UnexpectedFlavorException method createWithSuggestions.
public static UnexpectedFlavorException createWithSuggestions(Cell cell, BuildTarget target) {
// Get the specific message
String exceptionMessage = createDefaultMessage(cell, target);
// Get some suggestions on how to solve it.
String suggestions = "";
Optional<ImmutableSet<PatternAndMessage>> configMessagesForFlavors = cell.getBuckConfig().getUnexpectedFlavorsMessages();
for (Flavor flavor : target.getFlavors()) {
boolean foundInConfig = false;
if (configMessagesForFlavors.isPresent()) {
for (PatternAndMessage flavorPattern : configMessagesForFlavors.get()) {
if (flavorPattern.getPattern().matcher(flavor.getName()).find()) {
foundInConfig = true;
suggestions += flavor.getName() + " : " + flavorPattern.getMessage() + "\n";
}
}
}
if (!foundInConfig) {
for (PatternAndMessage flavorPattern : suggestedMessagesForFlavors) {
if (flavorPattern.getPattern().matcher(flavor.getName()).find()) {
suggestions += flavor.getName() + " : " + flavorPattern.getMessage() + "\n";
}
}
}
}
if (!suggestions.isEmpty()) {
exceptionMessage += "\nHere are some things you can try to get the following " + "flavors to work::\n" + suggestions;
}
return new UnexpectedFlavorException(exceptionMessage);
}
Aggregations