use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class AuditClasspathCommandTest method testJsonClassPathWithVersions.
@Test
public void testJsonClassPathWithVersions() throws Exception {
// Build the test target graph.
TargetNode<?, ?> javaLibrary = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:test-java-library")).addSrc(Paths.get("src/com/facebook/TestJavaLibrary.java")).build();
TargetNode<?, ?> androidLibrary = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:test-android-library")).addSrc(Paths.get("src/com/facebook/TestAndroidLibrary.java")).addDep(javaLibrary.getBuildTarget()).build();
TargetNode<?, ?> version = new VersionedAliasBuilder(BuildTargetFactory.newInstance("//:version")).setVersions("1.0", "//:test-android-library").build();
TargetNode<?, ?> binary = new JavaBinaryRuleBuilder(BuildTargetFactory.newInstance("//:rule")).setDeps(ImmutableSortedSet.of(version.getBuildTarget())).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(javaLibrary, androidLibrary, version, binary);
// Run the command.
auditClasspathCommand.printJsonClasspath(params.withBuckConfig(FakeBuckConfig.builder().setSections(ImmutableMap.of("build", ImmutableMap.of("versions", "true"))).build()), targetGraph, ImmutableSet.of(androidLibrary.getBuildTarget(), javaLibrary.getBuildTarget()));
// Verify output.
Path root = javaLibrary.getBuildTarget().getCellPath();
ObjectMapper mapper = ObjectMappers.newDefaultInstance();
String expected = String.format(EXPECTED_JSON, mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), javaLibrary.getBuildTarget(), "lib__%s__output").resolve(javaLibrary.getBuildTarget().getShortName() + ".jar"))), mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), androidLibrary.getBuildTarget(), "lib__%s__output").resolve(androidLibrary.getBuildTarget().getShortName() + ".jar"))), mapper.valueToTree(root.resolve(BuildTargets.getGenPath(params.getCell().getFilesystem(), javaLibrary.getBuildTarget(), "lib__%s__output").resolve(javaLibrary.getBuildTarget().getShortName() + ".jar"))));
assertEquals(expected, console.getTextWrittenToStdOut());
assertEquals("", console.getTextWrittenToStdErr());
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class TargetsCommandTest method testDetectTestChanges.
@Test
public void testDetectTestChanges() throws CmdLineException, IOException {
BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
BuildTarget libraryTestTarget1 = BuildTargetFactory.newInstance("//foo:xctest1");
BuildTarget libraryTestTarget2 = BuildTargetFactory.newInstance("//foo:xctest2");
BuildTarget testLibraryTarget = BuildTargetFactory.newInstance("//testlib:testlib");
BuildTarget testLibraryTestTarget = BuildTargetFactory.newInstance("//testlib:testlib-xctest");
TargetNode<?, ?> libraryNode = AppleLibraryBuilder.createBuilder(libraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))).setTests(ImmutableSortedSet.of(libraryTestTarget1, libraryTestTarget2)).build();
TargetNode<?, ?> libraryTestNode1 = AppleTestBuilder.createBuilder(libraryTestTarget1).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo1.m")))).setDeps(ImmutableSortedSet.of(libraryTarget)).build();
TargetNode<?, ?> libraryTestNode2 = AppleTestBuilder.createBuilder(libraryTestTarget2).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo2.m")))).setDeps(ImmutableSortedSet.of(testLibraryTarget)).build();
TargetNode<?, ?> testLibraryNode = AppleLibraryBuilder.createBuilder(testLibraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("testlib/testlib.m")))).setTests(ImmutableSortedSet.of(testLibraryTestTarget)).build();
TargetNode<?, ?> testLibraryTestNode = AppleTestBuilder.createBuilder(testLibraryTestTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("testlib/testlib-test.m")))).setDeps(ImmutableSortedSet.of(testLibraryTarget)).build();
ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(libraryNode, libraryTestNode1, libraryTestNode2, testLibraryNode, testLibraryTestNode);
TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
// No target depends on the referenced file.
SortedMap<String, TargetNode<?, ?>> matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/bar.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
assertTrue(matchingBuildRules.isEmpty());
// Test1, test2 and the library depend on the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/testfoo1.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1"), matchingBuildRules.keySet());
// Test1, test2 and the library depend on the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/testfoo2.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2"), matchingBuildRules.keySet());
// Library, test1, test2, test library and its test depend on the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("testlib/testlib.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2", "//testlib:testlib", "//testlib:testlib-xctest"), matchingBuildRules.keySet());
// Library, test1, test2, test library and its test depend on the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("testlib/testlib-test.m"))), Optional.empty(), Optional.empty(), true, "BUCK");
assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2", "//testlib:testlib", "//testlib:testlib-xctest"), matchingBuildRules.keySet());
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class TargetsCommand method computeTargetsAndGraphToShowTargetHash.
private TargetGraphAndTargetNodes computeTargetsAndGraphToShowTargetHash(CommandRunnerParams params, ListeningExecutorService executor, TargetGraphAndTargetNodes targetGraphAndTargetNodes) throws InterruptedException, BuildFileParseException, BuildTargetException, IOException {
if (isDetectTestChanges()) {
ImmutableSet<BuildTarget> explicitTestTargets = TargetGraphAndTargets.getExplicitTestTargets(targetGraphAndTargetNodes.getTargetGraph().getSubgraph(targetGraphAndTargetNodes.getTargetNodes()).getNodes());
LOG.debug("Got explicit test targets: %s", explicitTestTargets);
Iterable<BuildTarget> matchingBuildTargetsWithTests = mergeBuildTargets(targetGraphAndTargetNodes.getTargetNodes(), explicitTestTargets);
// Parse the BUCK files for the tests of the targets passed in from the command line.
TargetGraph targetGraphWithTests = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), executor, matchingBuildTargetsWithTests);
return TargetGraphAndTargetNodes.builder().setTargetGraph(targetGraphWithTests).setTargetNodes(targetGraphWithTests.getAll(matchingBuildTargetsWithTests)).build();
} else {
return targetGraphAndTargetNodes;
}
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class QueryMacroExpander method extractTargets.
private Stream<BuildTarget> extractTargets(BuildTarget target, CellPathResolver cellNames, Optional<BuildRuleResolver> resolver, T input) {
String queryExpression = CharMatcher.anyOf("\"'").trimFrom(input.getQuery().getQuery());
final GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(resolver, targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
try {
QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
HashSet<String> targetLiterals = new HashSet<>();
parsedExp.collectTargetPatterns(targetLiterals);
return targetLiterals.stream().flatMap(pattern -> {
try {
return env.getTargetsMatchingPattern(pattern, executorService).stream();
} catch (Exception e) {
throw new HumanReadableException(e, "Error parsing target expression %s for target %s", pattern, target);
}
}).map(queryTarget -> {
Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
return ((QueryBuildTarget) queryTarget).getBuildTarget();
});
} catch (QueryException e) {
throw new HumanReadableException("Error executing query in macro for target %s", target, e);
}
}
use of com.facebook.buck.rules.TargetGraph 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())));
}
}
Aggregations