use of com.facebook.buck.query.QueryBuildTarget 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.query.QueryBuildTarget in project buck by facebook.
the class GraphEnhancementQueryEnvironment method getNode.
private TargetNode<?, ?> getNode(QueryTarget target) {
Preconditions.checkState(target instanceof QueryBuildTarget);
Preconditions.checkArgument(targetGraph.isPresent());
BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
return targetGraph.get().getOptional(buildTarget).orElse(null);
}
use of com.facebook.buck.query.QueryBuildTarget in project buck by facebook.
the class BuckQueryEnvironment method getBuildFiles.
@Override
public ImmutableSet<QueryTarget> getBuildFiles(Set<QueryTarget> targets) throws QueryException {
final ProjectFilesystem cellFilesystem = rootCell.getFilesystem();
final Path rootPath = cellFilesystem.getRootPath();
Preconditions.checkState(rootPath.isAbsolute());
ImmutableSet.Builder<QueryTarget> builder = ImmutableSet.builder();
for (QueryTarget target : targets) {
Preconditions.checkState(target instanceof QueryBuildTarget);
BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
Cell cell = rootCell.getCell(buildTarget);
if (!buildFileTrees.containsKey(cell)) {
LOG.info("Creating a new filesystem-backed build file tree for %s", cell.getRoot());
buildFileTrees.put(cell, new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()));
}
BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(cell));
Optional<Path> path = buildFileTree.getBasePathOfAncestorTarget(buildTarget.getBasePath());
Preconditions.checkState(path.isPresent());
Path buildFilePath = MorePaths.relativize(rootPath, cell.getFilesystem().resolve(path.get()).resolve(cell.getBuildFileName()));
Preconditions.checkState(cellFilesystem.exists(buildFilePath));
builder.add(QueryFileTarget.of(buildFilePath));
}
return builder.build();
}
use of com.facebook.buck.query.QueryBuildTarget in project buck by facebook.
the class QueryCommand method collectAndPrintAttributes.
private void collectAndPrintAttributes(CommandRunnerParams params, BuckQueryEnvironment env, Set<QueryTarget> queryResult) throws QueryException {
PatternsMatcher patternsMatcher = new PatternsMatcher(outputAttributes.get());
SortedMap<String, SortedMap<String, Object>> result = Maps.newTreeMap();
for (QueryTarget target : queryResult) {
if (!(target instanceof QueryBuildTarget)) {
continue;
}
TargetNode<?, ?> node = env.getNode(target);
try {
SortedMap<String, Object> sortedTargetRule = params.getParser().getRawTargetNode(env.getParserState(), params.getCell(), node);
if (sortedTargetRule == null) {
params.getConsole().printErrorText("unable to find rule for target " + node.getBuildTarget().getFullyQualifiedName());
continue;
}
SortedMap<String, Object> attributes = Maps.newTreeMap();
if (patternsMatcher.hasPatterns()) {
for (String key : sortedTargetRule.keySet()) {
String snakeCaseKey = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key);
if (patternsMatcher.matches(snakeCaseKey)) {
attributes.put(snakeCaseKey, sortedTargetRule.get(key));
}
}
}
result.put(node.getBuildTarget().getUnflavoredBuildTarget().getFullyQualifiedName(), attributes);
} catch (BuildFileParseException e) {
params.getConsole().printErrorText("unable to find rule for target " + node.getBuildTarget().getFullyQualifiedName());
continue;
}
}
StringWriter stringWriter = new StringWriter();
try {
params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(stringWriter, result);
} catch (IOException e) {
// Shouldn't be possible while writing to a StringWriter...
throw new RuntimeException(e);
}
String output = stringWriter.getBuffer().toString();
params.getConsole().getStdOut().println(output);
}
use of com.facebook.buck.query.QueryBuildTarget in project buck by facebook.
the class BuckQueryEnvironment method buildTransitiveClosure.
@Override
public void buildTransitiveClosure(Set<QueryTarget> targets, int maxDepth, ListeningExecutorService executor) throws QueryException, InterruptedException {
// Filter QueryTargets that are build targets and not yet present in the build target graph.
Set<BuildTarget> graphTargets = getTargetsFromNodes(graph.getNodes());
Set<BuildTarget> newBuildTargets = new HashSet<>();
for (QueryTarget target : targets) {
if (target instanceof QueryBuildTarget) {
BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
if (!graphTargets.contains(buildTarget)) {
newBuildTargets.add(buildTarget);
}
}
}
if (!newBuildTargets.isEmpty()) {
buildGraphForBuildTargets(Sets.union(newBuildTargets, graphTargets));
for (BuildTarget buildTarget : getTargetsFromNodes(graph.getNodes())) {
if (!buildTargetToQueryTarget.containsKey(buildTarget)) {
buildTargetToQueryTarget.put(buildTarget, QueryBuildTarget.of(buildTarget));
}
}
}
}
Aggregations