Search in sources :

Example 1 with BuildTargetPattern

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

the class QueryTargetTranslator method translateTargets.

@Override
public Optional<Query> translateTargets(CellPathResolver cellPathResolver, BuildTargetPatternParser<BuildTargetPattern> pattern, TargetNodeTranslator translator, Query query) {
    // Extract all build targets from the original query string.
    ImmutableList<BuildTarget> targets;
    try {
        targets = QueryUtils.extractBuildTargets(cellPathResolver, pattern, query).collect(MoreCollectors.toImmutableList());
    } catch (QueryException e) {
        throw new RuntimeException("Error parsing/executing query from deps", e);
    }
    // If there's no targets, bail early.
    if (targets.isEmpty()) {
        return Optional.empty();
    }
    // A pattern matching all of the build targets in the query string.
    Pattern targetsPattern = Pattern.compile(targets.stream().map(Object::toString).map(Pattern::quote).collect(Collectors.joining("|")));
    // Build a new query string from the original by translating all build targets.
    String queryString = query.getQuery();
    Matcher matcher = targetsPattern.matcher(queryString);
    StringBuilder builder = new StringBuilder();
    int lastEnd = 0;
    while (matcher.find()) {
        builder.append(queryString.substring(lastEnd, matcher.start()));
        BuildTarget target = BuildTargetParser.INSTANCE.parse(matcher.group(), pattern, cellPathResolver);
        Optional<BuildTarget> translated = translator.translate(cellPathResolver, pattern, target);
        builder.append(translated.orElse(target).getFullyQualifiedName());
        lastEnd = matcher.end();
    }
    builder.append(queryString.substring(lastEnd, queryString.length()));
    String newQuery = builder.toString();
    return queryString.equals(newQuery) ? Optional.empty() : Optional.of(Query.of(newQuery));
}
Also used : BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Pattern(java.util.regex.Pattern) QueryException(com.facebook.buck.query.QueryException) Matcher(java.util.regex.Matcher) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 2 with BuildTargetPattern

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

the class TargetNodeTranslator method translateNode.

/**
   * @return a copy of the given {@link TargetNode} with all found {@link BuildTarget}s translated,
   *         or {@link Optional#empty()} if the node requires no translation.
   */
public <A> Optional<TargetNode<A, ?>> translateNode(TargetNode<A, ?> node) {
    CellPathResolver cellPathResolver = node.getCellNames();
    BuildTargetPatternParser<BuildTargetPattern> pattern = BuildTargetPatternParser.forBaseName(node.getBuildTarget().getBaseName());
    Optional<BuildTarget> target = translateBuildTarget(node.getBuildTarget());
    Optional<A> constructorArg = translateConstructorArg(cellPathResolver, pattern, node);
    Optional<ImmutableSet<BuildTarget>> declaredDeps = translateSet(cellPathResolver, pattern, node.getDeclaredDeps());
    Optional<ImmutableSet<BuildTarget>> extraDeps = translateSet(cellPathResolver, pattern, node.getExtraDeps());
    Optional<ImmutableMap<BuildTarget, Version>> newSelectedVersions = getSelectedVersions(node.getBuildTarget());
    Optional<ImmutableMap<BuildTarget, Version>> oldSelectedVersions = node.getSelectedVersions();
    Optional<Optional<ImmutableMap<BuildTarget, Version>>> selectedVersions = oldSelectedVersions.equals(newSelectedVersions) ? Optional.empty() : Optional.of(newSelectedVersions);
    // If nothing has changed, don't generate a new node.
    if (!target.isPresent() && !constructorArg.isPresent() && !declaredDeps.isPresent() && !extraDeps.isPresent() && !selectedVersions.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(node.withTargetConstructorArgDepsAndSelectedVerisons(target.orElse(node.getBuildTarget()), constructorArg.orElse(node.getConstructorArg()), declaredDeps.orElse(node.getDeclaredDeps()), extraDeps.orElse(node.getExtraDeps()), selectedVersions.orElse(oldSelectedVersions)));
}
Also used : BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) Optional(java.util.Optional) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) CellPathResolver(com.facebook.buck.rules.CellPathResolver)

Example 3 with BuildTargetPattern

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

the class BuildTargetPatternParserTest method testParse.

@Test
public void testParse() throws NoSuchBuildTargetException {
    BuildTargetPatternParser<BuildTargetPattern> buildTargetPatternParser = BuildTargetPatternParser.forVisibilityArgument();
    assertEquals(new ImmediateDirectoryBuildTargetPattern(filesystem.getRootPath(), vfs.getPath("test/com/facebook/buck/parser/")), buildTargetPatternParser.parse(createCellRoots(filesystem), "//test/com/facebook/buck/parser:"));
    assertEquals(new SingletonBuildTargetPattern(filesystem.getRootPath(), "//test/com/facebook/buck/parser:parser"), buildTargetPatternParser.parse(createCellRoots(filesystem), "//test/com/facebook/buck/parser:parser"));
    assertEquals(new SubdirectoryBuildTargetPattern(filesystem.getRootPath(), vfs.getPath("test/com/facebook/buck/parser/")), buildTargetPatternParser.parse(createCellRoots(filesystem), "//test/com/facebook/buck/parser/..."));
}
Also used : SubdirectoryBuildTargetPattern(com.facebook.buck.model.SubdirectoryBuildTargetPattern) ImmediateDirectoryBuildTargetPattern(com.facebook.buck.model.ImmediateDirectoryBuildTargetPattern) BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) SingletonBuildTargetPattern(com.facebook.buck.model.SingletonBuildTargetPattern) ImmediateDirectoryBuildTargetPattern(com.facebook.buck.model.ImmediateDirectoryBuildTargetPattern) SingletonBuildTargetPattern(com.facebook.buck.model.SingletonBuildTargetPattern) SubdirectoryBuildTargetPattern(com.facebook.buck.model.SubdirectoryBuildTargetPattern) Test(org.junit.Test)

Example 4 with BuildTargetPattern

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

the class BuildTargetPatternParserTest method visibilityCanContainCrossCellReference.

@Test
public void visibilityCanContainCrossCellReference() {
    BuildTargetPatternParser<BuildTargetPattern> buildTargetPatternParser = BuildTargetPatternParser.forVisibilityArgument();
    final ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    CellPathResolver cellNames = new FakeCellPathResolver(ImmutableMap.of("other", filesystem.getRootPath()));
    assertEquals(new SingletonBuildTargetPattern(filesystem.getRootPath(), "//:something"), buildTargetPatternParser.parse(cellNames, "other//:something"));
    assertEquals(new SubdirectoryBuildTargetPattern(filesystem.getRootPath(), filesystem.getPath("sub")), buildTargetPatternParser.parse(cellNames, "other//sub/..."));
}
Also used : SubdirectoryBuildTargetPattern(com.facebook.buck.model.SubdirectoryBuildTargetPattern) ImmediateDirectoryBuildTargetPattern(com.facebook.buck.model.ImmediateDirectoryBuildTargetPattern) BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) SingletonBuildTargetPattern(com.facebook.buck.model.SingletonBuildTargetPattern) SingletonBuildTargetPattern(com.facebook.buck.model.SingletonBuildTargetPattern) FakeCellPathResolver(com.facebook.buck.rules.FakeCellPathResolver) SubdirectoryBuildTargetPattern(com.facebook.buck.model.SubdirectoryBuildTargetPattern) CellPathResolver(com.facebook.buck.rules.CellPathResolver) FakeCellPathResolver(com.facebook.buck.rules.FakeCellPathResolver) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 5 with BuildTargetPattern

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

the class BuildTargetParserTest method testParseWithVisibilityContext.

@Test
public void testParseWithVisibilityContext() {
    // Invoke the BuildTargetParser using the VISIBILITY context.
    BuildTargetPatternParser<BuildTargetPattern> buildTargetPatternParser = BuildTargetPatternParser.forVisibilityArgument();
    BuildTarget target = parser.parse("//java/com/example:", buildTargetPatternParser, createCellRoots(null));
    assertEquals("A build target that ends with a colon should be treated as a wildcard build target " + "when parsed in the context of a visibility argument.", "//java/com/example:", target.getFullyQualifiedName());
}
Also used : BuildTargetPattern(com.facebook.buck.model.BuildTargetPattern) BuildTarget(com.facebook.buck.model.BuildTarget) Test(org.junit.Test)

Aggregations

BuildTargetPattern (com.facebook.buck.model.BuildTargetPattern)7 BuildTarget (com.facebook.buck.model.BuildTarget)4 Test (org.junit.Test)4 ImmediateDirectoryBuildTargetPattern (com.facebook.buck.model.ImmediateDirectoryBuildTargetPattern)3 SingletonBuildTargetPattern (com.facebook.buck.model.SingletonBuildTargetPattern)3 SubdirectoryBuildTargetPattern (com.facebook.buck.model.SubdirectoryBuildTargetPattern)3 CellPathResolver (com.facebook.buck.rules.CellPathResolver)3 QueryException (com.facebook.buck.query.QueryException)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Optional (java.util.Optional)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildTargetPatternParser (com.facebook.buck.parser.BuildTargetPatternParser)1 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)1 QueryExpression (com.facebook.buck.query.QueryExpression)1 QueryTarget (com.facebook.buck.query.QueryTarget)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 FakeCellPathResolver (com.facebook.buck.rules.FakeCellPathResolver)1 TargetGraph (com.facebook.buck.rules.TargetGraph)1