use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class AppleDescriptions method createBuildRulesForCoreDataDependencies.
public static Optional<CoreDataModel> createBuildRulesForCoreDataDependencies(TargetGraph targetGraph, BuildRuleParams params, String moduleName, AppleCxxPlatform appleCxxPlatform) {
TargetNode<?, ?> targetNode = targetGraph.get(params.getBuildTarget());
ImmutableSet<AppleWrapperResourceArg> coreDataModelArgs = AppleBuildRules.collectTransitiveBuildRules(targetGraph, Optional.empty(), AppleBuildRules.CORE_DATA_MODEL_DESCRIPTION_CLASSES, ImmutableList.of(targetNode));
BuildRuleParams coreDataModelParams = params.withAppendedFlavor(CoreDataModel.FLAVOR).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of()), Suppliers.ofInstance(ImmutableSortedSet.of()));
if (coreDataModelArgs.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(new CoreDataModel(coreDataModelParams, appleCxxPlatform, moduleName, coreDataModelArgs.stream().map(input -> new PathSourcePath(params.getProjectFilesystem(), input.path)).collect(MoreCollectors.toImmutableSet())));
}
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class NdkLibraryDescription method findSources.
@VisibleForTesting
protected ImmutableSortedSet<SourcePath> findSources(final ProjectFilesystem filesystem, final Path buildRulePath) {
final ImmutableSortedSet.Builder<SourcePath> srcs = ImmutableSortedSet.naturalOrder();
try {
final Path rootDirectory = filesystem.resolve(buildRulePath);
Files.walkFileTree(rootDirectory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), /* maxDepth */
Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (EXTENSIONS_REGEX.matcher(file.toString()).matches()) {
srcs.add(new PathSourcePath(filesystem, buildRulePath.resolve(rootDirectory.relativize(file))));
}
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
return srcs.build();
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class FineGrainedJavaDependencySuggester method extractProvidedSymbolInfoFromSourceFile.
/**
* Extracts the features from {@code src} and updates the collections accordingly.
*/
private void extractProvidedSymbolInfoFromSourceFile(SourcePath src, JavaFileParser javaFileParser, Multimap<String, String> providedSymbolToRequiredSymbols, Map<String, PathSourcePath> providedSymbolToSrc) {
if (!(src instanceof PathSourcePath)) {
return;
}
PathSourcePath path = (PathSourcePath) src;
ProjectFilesystem filesystem = path.getFilesystem();
Optional<String> contents = filesystem.readFileIfItExists(path.getRelativePath());
if (!contents.isPresent()) {
throw new RuntimeException(String.format("Could not read file '%s'", path.getRelativePath()));
}
JavaFileParser.JavaFileFeatures features = javaFileParser.extractFeaturesFromJavaCode(contents.get());
// If there are multiple provided symbols, that is because there are inner classes. Choosing
// the shortest name will effectively select the top-level type.
String providedSymbol = Iterables.getFirst(features.providedSymbols, /* defaultValue */
null);
if (providedSymbol == null) {
console.getStdErr().printf("%s cowardly refuses to provide any types.\n", path.getRelativePath());
return;
}
providedSymbolToSrc.put(providedSymbol, path);
providedSymbolToRequiredSymbols.putAll(providedSymbol, features.requiredSymbols);
providedSymbolToRequiredSymbols.putAll(providedSymbol, features.exportedSymbols);
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class FineGrainedJavaDependencySuggester method createBuildRuleDefinition.
/**
* Creates the build rule definition for the {@code namedComponent}.
*/
private String createBuildRuleDefinition(NamedStronglyConnectedComponent namedComponent, Map<String, PathSourcePath> providedSymbolToSrc, Multimap<String, String> providedSymbolToRequiredSymbols, Map<String, NamedStronglyConnectedComponent> namedComponentsIndex, JavaDepsFinder.DependencyInfo dependencyInfo, MutableDirectedGraph<String> symbolsDependencies, String visibilityArg) {
final TargetNode<?, ?> suggestedNode = graph.get(suggestedTarget);
SortedSet<String> deps = new TreeSet<>(LOCAL_DEPS_FIRST_COMPARATOR);
SortedSet<PathSourcePath> srcs = new TreeSet<>();
for (String providedSymbol : namedComponent.symbols) {
PathSourcePath src = providedSymbolToSrc.get(providedSymbol);
srcs.add(src);
for (String requiredSymbol : providedSymbolToRequiredSymbols.get(providedSymbol)) {
// First, check to see whether the requiredSymbol is in one of the newly created
// strongly connected components. If so, add it to the deps so long as it is not the
// strongly connected component that we are currently exploring.
NamedStronglyConnectedComponent requiredComponent = namedComponentsIndex.get(requiredSymbol);
if (requiredComponent != null) {
if (!requiredComponent.equals(namedComponent)) {
deps.add(":" + requiredComponent.name);
}
continue;
}
Set<TargetNode<?, ?>> depProviders = dependencyInfo.symbolToProviders.get(requiredSymbol);
if (depProviders == null || depProviders.size() == 0) {
console.getStdErr().printf("# Suspicious: no provider for '%s'\n", requiredSymbol);
continue;
}
depProviders = FluentIterable.from(depProviders).filter(provider -> provider.isVisibleTo(graph, suggestedNode)).toSet();
TargetNode<?, ?> depProvider;
if (depProviders.size() == 1) {
depProvider = Iterables.getOnlyElement(depProviders);
} else {
console.getStdErr().printf("# Suspicious: no lone provider for '%s': [%s]\n", requiredSymbol, Joiner.on(", ").join(depProviders));
continue;
}
if (!depProvider.equals(suggestedNode)) {
deps.add(depProvider.toString());
}
}
// Find deps within package.
for (String requiredSymbol : symbolsDependencies.getOutgoingNodesFor(providedSymbol)) {
NamedStronglyConnectedComponent componentDep = Preconditions.checkNotNull(namedComponentsIndex.get(requiredSymbol));
if (!componentDep.equals(namedComponent)) {
deps.add(":" + componentDep.name);
}
}
}
final Path basePathForSuggestedTarget = suggestedTarget.getBasePath();
Iterable<String> relativeSrcs = FluentIterable.from(srcs).transform(input -> basePathForSuggestedTarget.relativize(input.getRelativePath()).toString());
StringBuilder rule = new StringBuilder("\njava_library(\n" + " name = '" + namedComponent.name + "',\n" + " srcs = [\n");
for (String src : relativeSrcs) {
rule.append(String.format(" '%s',\n", src));
}
rule.append(" ],\n" + " deps = [\n");
for (String dep : deps) {
rule.append(String.format(" '%s',\n", dep));
}
rule.append(" ],\n" + visibilityArg + ")\n");
return rule.toString();
}
use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.
the class FineGrainedJavaDependencySuggester method suggestRefactoring.
/**
* Suggests a refactoring by printing it to stdout (with warnings printed to stderr).
* @throws IllegalArgumentException
*/
void suggestRefactoring() {
final TargetNode<?, ?> suggestedNode = graph.get(suggestedTarget);
if (!(suggestedNode.getConstructorArg() instanceof JavaLibraryDescription.Arg)) {
console.printErrorText(String.format("'%s' does not correspond to a Java rule", suggestedTarget));
throw new IllegalArgumentException();
}
JavaLibraryDescription.Arg arg = (JavaLibraryDescription.Arg) suggestedNode.getConstructorArg();
JavaFileParser javaFileParser = javaDepsFinder.getJavaFileParser();
Multimap<String, String> providedSymbolToRequiredSymbols = HashMultimap.create();
Map<String, PathSourcePath> providedSymbolToSrc = new HashMap<>();
for (SourcePath src : arg.srcs) {
extractProvidedSymbolInfoFromSourceFile(src, javaFileParser, providedSymbolToRequiredSymbols, providedSymbolToSrc);
}
// Create a MutableDirectedGraph from the providedSymbolToRequiredSymbols.
MutableDirectedGraph<String> symbolsDependencies = new MutableDirectedGraph<>();
// dependencies.
for (String providedSymbol : providedSymbolToSrc.keySet()) {
// Add a node for the providedSymbol in case it has no edges.
symbolsDependencies.addNode(providedSymbol);
for (String requiredSymbol : providedSymbolToRequiredSymbols.get(providedSymbol)) {
if (providedSymbolToRequiredSymbols.containsKey(requiredSymbol) && !providedSymbol.equals(requiredSymbol)) {
symbolsDependencies.addEdge(providedSymbol, requiredSymbol);
}
}
}
// Determine the strongly connected components.
Set<Set<String>> stronglyConnectedComponents = symbolsDependencies.findStronglyConnectedComponents();
// Maps a providedSymbol to the component that contains it.
Map<String, NamedStronglyConnectedComponent> namedComponentsIndex = new TreeMap<>();
Set<NamedStronglyConnectedComponent> namedComponents = new TreeSet<>();
for (Set<String> stronglyConnectedComponent : stronglyConnectedComponents) {
// We just use the first provided symbol in the strongly connected component as the canonical
// name for the component. Maybe not the best name, but certainly not the worst.
String name = Iterables.getFirst(stronglyConnectedComponent, /* defaultValue */
null);
if (name == null) {
throw new IllegalStateException("A strongly connected component was created with zero nodes.");
}
NamedStronglyConnectedComponent namedComponent = new NamedStronglyConnectedComponent(name, stronglyConnectedComponent);
namedComponents.add(namedComponent);
for (String providedSymbol : stronglyConnectedComponent) {
namedComponentsIndex.put(providedSymbol, namedComponent);
}
}
// Visibility argument.
StringBuilder visibilityBuilder = new StringBuilder(" visibility = [\n");
SortedSet<String> visibilities = FluentIterable.from(suggestedNode.getVisibilityPatterns()).transform(VisibilityPattern::getRepresentation).toSortedSet(Ordering.natural());
for (String visibility : visibilities) {
visibilityBuilder.append(" '" + visibility + "',\n");
}
visibilityBuilder.append(" ],\n");
String visibilityArg = visibilityBuilder.toString();
// Print out the new version of the original rule.
console.getStdOut().printf("java_library(\n" + " name = '%s',\n" + " exported_deps = [\n", suggestedTarget.getShortName());
for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
console.getStdOut().printf(" ':%s',\n", namedComponent.name);
}
console.getStdOut().print(" ],\n" + visibilityArg + ")\n");
// Print out a rule for each of the strongly connected components.
JavaDepsFinder.DependencyInfo dependencyInfo = javaDepsFinder.findDependencyInfoForGraph(graph);
for (NamedStronglyConnectedComponent namedComponent : namedComponents) {
String buildRuleDefinition = createBuildRuleDefinition(namedComponent, providedSymbolToSrc, providedSymbolToRequiredSymbols, namedComponentsIndex, dependencyInfo, symbolsDependencies, visibilityArg);
console.getStdOut().print(buildRuleDefinition);
}
}
Aggregations