Search in sources :

Example 1 with PatternsMatcher

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("]");
}
Also used : PatternsMatcher(com.facebook.buck.util.PatternsMatcher) TargetNode(com.facebook.buck.rules.TargetNode) StringWriter(java.io.StringWriter) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with PatternsMatcher

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);
}
Also used : PatternsMatcher(com.facebook.buck.util.PatternsMatcher) IOException(java.io.IOException) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) QueryTarget(com.facebook.buck.query.QueryTarget) StringWriter(java.io.StringWriter) SortedMap(java.util.SortedMap) QueryBuildTarget(com.facebook.buck.query.QueryBuildTarget)

Aggregations

PatternsMatcher (com.facebook.buck.util.PatternsMatcher)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)1 QueryBuildTarget (com.facebook.buck.query.QueryBuildTarget)1 QueryTarget (com.facebook.buck.query.QueryTarget)1 TargetNode (com.facebook.buck.rules.TargetNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SortedMap (java.util.SortedMap)1