Search in sources :

Example 1 with StdLogger

use of com.android.common.utils.StdLogger 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)

Aggregations

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