Search in sources :

Example 1 with ILogger

use of com.android.common.utils.ILogger in project buck by facebook.

the class Merger method process.

public int process(String[] args) throws FileNotFoundException {
    Iterator<String> arguments = Arrays.asList(args).iterator();
    // first pass to get all mandatory parameters.
    String mainManifest = null;
    StdLogger.Level logLevel = StdLogger.Level.INFO;
    ILogger logger = new StdLogger(logLevel);
    while (arguments.hasNext()) {
        String selector = arguments.next();
        if (!selector.startsWith("--")) {
            logger.error(null, /* throwable */
            "Invalid parameter " + selector + ", expected a command switch");
            return 1;
        }
        if ("--usage".equals(selector)) {
            usage();
            return 0;
        }
        if (!arguments.hasNext()) {
            logger.error(null, /* throwable */
            "Command switch " + selector + " has no value associated");
            return 1;
        }
        String value = arguments.next();
        if ("--main".equals(selector)) {
            mainManifest = value;
        }
        if ("--log".equals(selector)) {
            logLevel = StdLogger.Level.valueOf(value);
        }
    }
    if (mainManifest == null) {
        System.err.println("--main command switch not provided.");
        return 1;
    }
    // recreate the logger with the provided log level for the rest of the processing.
    logger = createLogger(logLevel);
    File mainManifestFile = checkPath(mainManifest);
    ManifestMerger2.Invoker invoker = createInvoker(mainManifestFile, logger);
    // second pass, get optional parameters and store them in the invoker.
    arguments = Arrays.asList(args).iterator();
    File outFile = null;
    // first pass to get all mandatory parameters.
    while (arguments.hasNext()) {
        String selector = arguments.next();
        String value = arguments.next();
        if (Strings.isNullOrEmpty(value)) {
            logger.error(null, /* throwable */
            "Empty value for switch " + selector);
            return 1;
        }
        if ("--libs".equals(selector)) {
            StringTokenizer stringTokenizer = new StringTokenizer(value, File.pathSeparator);
            while (stringTokenizer.hasMoreElements()) {
                File library = checkPath(stringTokenizer.nextToken());
                invoker.addLibraryManifest(library);
            }
        }
        if ("--overlays".equals(selector)) {
            StringTokenizer stringTokenizer = new StringTokenizer(value, File.pathSeparator);
            while (stringTokenizer.hasMoreElements()) {
                File library = checkPath(stringTokenizer.nextToken());
                invoker.addFlavorAndBuildTypeManifest(library);
            }
        }
        if ("--property".equals(selector)) {
            if (!value.contains("=")) {
                logger.error(null, /* throwable */
                "Invalid property setting, should be NAME=VALUE format");
                return 1;
            }
            try {
                ManifestSystemProperty manifestSystemProperty = ManifestSystemProperty.valueOf(value.substring(0, value.indexOf('=')).toUpperCase(Locale.ENGLISH));
                invoker.setOverride(manifestSystemProperty, value.substring(value.indexOf('=') + 1));
            } catch (IllegalArgumentException e) {
                logger.error(e, "Invalid property name " + value.substring(0, value.indexOf('=')) + ", allowed properties are : " + Joiner.on(',').join(ManifestSystemProperty.values()));
                return 1;
            }
        }
        if ("--placeholder".equals(selector)) {
            if (!value.contains("=")) {
                logger.error(null, /* throwable */
                "Invalid placeholder setting, should be NAME=VALUE format");
                return 1;
            }
            invoker.setPlaceHolderValue(value.substring(0, value.indexOf('=')), value.substring(value.indexOf('=') + 1));
        }
        if ("--out".equals(selector)) {
            outFile = new File(value);
        }
    }
    try {
        MergingReport merge = invoker.merge();
        if (merge.getResult().isSuccess()) {
            String mergedDocument = merge.getMergedDocument(MergingReport.MergedManifestKind.MERGED);
            if (mergedDocument != null) {
                if (outFile != null) {
                    try {
                        Files.write(mergedDocument, outFile, Charsets.UTF_8);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    System.out.println(mergedDocument);
                }
            }
        } else {
            for (MergingReport.Record record : merge.getLoggingRecords()) {
                System.err.println(record);
            }
        }
    } catch (ManifestMerger2.MergeFailureException e) {
        logger.error(e, "Exception while merging manifests");
        return 1;
    }
    return 0;
}
Also used : IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) StdLogger(com.android.common.utils.StdLogger) ILogger(com.android.common.utils.ILogger) File(java.io.File)

Example 2 with ILogger

use of com.android.common.utils.ILogger in project buck by facebook.

the class XmlElement method mergeChild.

// merge a child of a lower priority node into this higher priority node.
private void mergeChild(@NonNull XmlElement lowerPriorityChild, @NonNull MergingReport.Builder mergingReport) {
    ILogger logger = mergingReport.getLogger();
    // If this a custom element, we just blindly merge it in.
    if (lowerPriorityChild.getType() == ManifestModel.NodeTypes.CUSTOM) {
        handleCustomElement(lowerPriorityChild, mergingReport);
        return;
    }
    Optional<XmlElement> thisChildOptional = getNodeByTypeAndKey(lowerPriorityChild.getType(), lowerPriorityChild.getKey());
    // only in the lower priority document ?
    if (!thisChildOptional.isPresent()) {
        addElement(lowerPriorityChild, mergingReport);
        return;
    }
    // it's defined in both files.
    logger.verbose(lowerPriorityChild.getId() + " defined in both files...");
    XmlElement thisChild = thisChildOptional.get();
    switch(thisChild.getType().getMergeType()) {
        case CONFLICT:
            addMessage(mergingReport, MergingReport.Record.Severity.ERROR, String.format("Node %1$s cannot be present in more than one input file and it's " + "present at %2$s and %3$s", thisChild.getType(), thisChild.printPosition(), lowerPriorityChild.printPosition()));
            break;
        case ALWAYS:
            // no merging, we consume the lower priority node unmodified.
            // if the two elements are equal, just skip it.
            // but check first that we are not supposed to replace or remove it.
            @NonNull NodeOperationType operationType = calculateNodeOperationType(thisChild, lowerPriorityChild);
            if (operationType == NodeOperationType.REMOVE || operationType == NodeOperationType.REPLACE) {
                mergingReport.getActionRecorder().recordNodeAction(thisChild, Actions.ActionType.REJECTED, lowerPriorityChild);
                break;
            }
            if (thisChild.getType().areMultipleDeclarationAllowed()) {
                mergeChildrenWithMultipleDeclarations(lowerPriorityChild, mergingReport);
            } else {
                if (!thisChild.isEquals(lowerPriorityChild)) {
                    addElement(lowerPriorityChild, mergingReport);
                }
            }
            break;
        default:
            // 2 nodes exist, some merging need to happen
            handleTwoElementsExistence(thisChild, lowerPriorityChild, mergingReport);
            break;
    }
}
Also used : NonNull(com.android.annotations.NonNull) ILogger(com.android.common.utils.ILogger)

Aggregations

ILogger (com.android.common.utils.ILogger)2 NonNull (com.android.annotations.NonNull)1 StdLogger (com.android.common.utils.StdLogger)1 File (java.io.File)1 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1