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