use of com.facebook.buck.util.PatternsMatcher in project buck by facebook.
the class TargetsCommand method printJsonForTargets.
@VisibleForTesting
void printJsonForTargets(CommandRunnerParams params, ListeningExecutorService executor, Iterable<TargetNode<?, ?>> targetNodes, ImmutableMap<BuildTarget, ShowOptions> showRulesResult, ImmutableSet<String> outputAttributes) throws BuildFileParseException {
PatternsMatcher attributesPatternsMatcher = new PatternsMatcher(outputAttributes);
// Print the JSON representation of the build node for the specified target(s).
params.getConsole().getStdOut().println("[");
ObjectMapper mapper = params.getObjectMapper();
Iterator<TargetNode<?, ?>> targetNodeIterator = targetNodes.iterator();
while (targetNodeIterator.hasNext()) {
TargetNode<?, ?> targetNode = targetNodeIterator.next();
Map<String, Object> sortedTargetRule;
sortedTargetRule = params.getParser().getRawTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), executor, targetNode);
if (sortedTargetRule == null) {
params.getConsole().printErrorText("unable to find rule for target " + targetNode.getBuildTarget().getFullyQualifiedName());
continue;
}
sortedTargetRule = attributesPatternsMatcher.filterMatchingMapKeys(sortedTargetRule);
ShowOptions showOptions = showRulesResult.get(targetNode.getBuildTarget());
if (showOptions != null) {
putIfValuePresentAndMatches(ShowOptionsName.RULE_KEY.getName(), showOptions.getRuleKey(), sortedTargetRule, attributesPatternsMatcher);
putIfValuePresentAndMatches(ShowOptionsName.OUTPUT_PATH.getName(), showOptions.getOutputPath(), sortedTargetRule, attributesPatternsMatcher);
putIfValuePresentAndMatches(ShowOptionsName.TARGET_HASH.getName(), showOptions.getTargetHash(), sortedTargetRule, attributesPatternsMatcher);
}
String fullyQualifiedNameAttribute = "fully_qualified_name";
if (attributesPatternsMatcher.matches(fullyQualifiedNameAttribute)) {
sortedTargetRule.put(fullyQualifiedNameAttribute, targetNode.getBuildTarget().getFullyQualifiedName());
}
String cellPathAttribute = "buck.cell_path";
if (isShowCellPath() && attributesPatternsMatcher.matches(cellPathAttribute)) {
sortedTargetRule.put(cellPathAttribute, targetNode.getBuildTarget().getCellPath());
}
// Print the build rule information as JSON.
StringWriter stringWriter = new StringWriter();
try {
mapper.writerWithDefaultPrettyPrinter().writeValue(stringWriter, sortedTargetRule);
} catch (IOException e) {
// Shouldn't be possible while writing to a StringWriter...
throw new RuntimeException(e);
}
String output = stringWriter.getBuffer().toString();
if (targetNodeIterator.hasNext()) {
output += ",";
}
params.getConsole().getStdOut().println(output);
}
params.getConsole().getStdOut().println("]");
}
use of com.facebook.buck.util.PatternsMatcher 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);
}
Aggregations