| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.manifmerger; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.VisibleForTesting; |
| 21 |
import com.android.utils.ILogger; |
| 22 |
import com.android.utils.StdLogger; |
| 23 |
import com.google.common.base.Charsets; |
| 24 |
import com.google.common.base.Joiner; |
| 25 |
import com.google.common.base.Strings; |
| 26 |
import com.google.common.io.Files; |
| 27 |
|
| 28 |
import java.io.File; |
| 29 |
import java.io.FileNotFoundException; |
| 30 |
import java.io.IOException; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.Iterator; |
| 33 |
import java.util.Locale; |
| 34 |
import java.util.StringTokenizer; |
| 35 |
|
| 36 |
/** |
| 37 |
* Command line interface to the {@link ManifestMerger2} |
| 38 |
*/ |
| 39 |
public class Merger { |
| 40 |
|
| 41 |
public static void main(String[] args) { |
| 42 |
try { |
| 43 |
System.exit(new Merger().process(args)); |
| 44 |
} catch (FileNotFoundException e) { |
| 45 |
System.exit(1); |
| 46 |
} |
| 47 |
System.exit(0); |
| 48 |
} |
| 49 |
|
| 50 |
public int process(String[] args) throws FileNotFoundException { |
| 51 |
|
| 52 |
Iterator<String> arguments = Arrays.asList(args).iterator(); |
| 53 |
// first pass to get all mandatory parameters. |
| 54 |
String mainManifest = null; |
| 55 |
StdLogger.Level logLevel = StdLogger.Level.INFO; |
| 56 |
ILogger logger = new StdLogger(logLevel); |
| 57 |
while (arguments.hasNext()) { |
| 58 |
String selector = arguments.next(); |
| 59 |
if (!selector.startsWith("--")) { |
| 60 |
logger.error(null /* throwable */, |
| 61 |
"Invalid parameter " + selector + ", expected a command switch"); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
if ("--usage".equals(selector)) { |
| 65 |
usage(); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
if (!arguments.hasNext()) { |
| 69 |
logger.error(null /* throwable */, |
| 70 |
"Command switch " + selector + " has no value associated"); |
| 71 |
return 1; |
| 72 |
} |
| 73 |
String value = arguments.next(); |
| 74 |
|
| 75 |
if ("--main".equals(selector)) { |
| 76 |
mainManifest = value; |
| 77 |
} |
| 78 |
if ("--log".equals(selector)) { |
| 79 |
logLevel = StdLogger.Level.valueOf(value); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (mainManifest == null) { |
| 84 |
System.err.println("--main command switch not provided."); |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
// recreate the logger with the provided log level for the rest of the processing. |
| 89 |
logger = createLogger(logLevel); |
| 90 |
File mainManifestFile = checkPath(mainManifest); |
| 91 |
ManifestMerger2.Invoker invoker = createInvoker( |
| 92 |
mainManifestFile, logger); |
| 93 |
|
| 94 |
// second pass, get optional parameters and store them in the invoker. |
| 95 |
arguments = Arrays.asList(args).iterator(); |
| 96 |
File outFile = null; |
| 97 |
|
| 98 |
// first pass to get all mandatory parameters. |
| 99 |
while (arguments.hasNext()) { |
| 100 |
String selector = arguments.next(); |
| 101 |
String value = arguments.next(); |
| 102 |
if (Strings.isNullOrEmpty(value)) { |
| 103 |
logger.error(null /* throwable */, |
| 104 |
"Empty value for switch " + selector); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
if ("--libs".equals(selector)) { |
| 108 |
StringTokenizer stringTokenizer = new StringTokenizer(value, File.pathSeparator); |
| 109 |
while (stringTokenizer.hasMoreElements()) { |
| 110 |
File library = checkPath(stringTokenizer.nextToken()); |
| 111 |
invoker.addLibraryManifest(library); |
| 112 |
} |
| 113 |
} |
| 114 |
if ("--overlays".equals(selector)) { |
| 115 |
StringTokenizer stringTokenizer = new StringTokenizer(value, File.pathSeparator); |
| 116 |
while (stringTokenizer.hasMoreElements()) { |
| 117 |
File library = checkPath(stringTokenizer.nextToken()); |
| 118 |
invoker.addFlavorAndBuildTypeManifest(library); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
if ("--property".equals(selector)) { |
| 123 |
if (!value.contains("=")) { |
| 124 |
logger.error(null /* throwable */, |
| 125 |
"Invalid property setting, should be NAME=VALUE format"); |
| 126 |
return 1; |
| 127 |
} |
| 128 |
try { |
| 129 |
ManifestSystemProperty manifestSystemProperty = ManifestSystemProperty |
| 130 |
.valueOf(value.substring(0, value.indexOf('=')) |
| 131 |
.toUpperCase(Locale.ENGLISH)); |
| 132 |
invoker.setOverride(manifestSystemProperty, value.substring(value.indexOf('=') + 1)); |
| 133 |
} catch (IllegalArgumentException e) { |
| 134 |
logger.error(e, "Invalid property name "+ value.substring(0, value.indexOf('=')) |
| 135 |
+ ", allowed properties are : " + Joiner |
| 136 |
.on(',').join(ManifestSystemProperty.values())); |
| 137 |
return 1; |
| 138 |
} |
| 139 |
} |
| 140 |
if ("--placeholder".equals(selector)) { |
| 141 |
if (!value.contains("=")) { |
| 142 |
logger.error(null /* throwable */, |
| 143 |
"Invalid placeholder setting, should be NAME=VALUE format"); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
invoker.setPlaceHolderValue(value.substring(0, value.indexOf('=')), |
| 147 |
value.substring(value.indexOf('=') + 1)); |
| 148 |
} |
| 149 |
if ("--out".equals(selector)) { |
| 150 |
outFile = new File(value); |
| 151 |
} |
| 152 |
} |
| 153 |
try { |
| 154 |
MergingReport merge = invoker.merge(); |
| 155 |
if (merge.getResult().isSuccess()) { |
| 156 |
String mergedDocument = merge.getMergedDocument(MergingReport.MergedManifestKind.MERGED); |
| 157 |
if (mergedDocument != null) { |
| 158 |
if (outFile != null) { |
| 159 |
try { |
| 160 |
Files.write(mergedDocument, outFile, Charsets.UTF_8); |
| 161 |
} catch (IOException e) { |
| 162 |
throw new RuntimeException(e); |
| 163 |
} |
| 164 |
} else { |
| 165 |
System.out.println(mergedDocument); |
| 166 |
} |
| 167 |
} |
| 168 |
} else { |
| 169 |
for (MergingReport.Record record : merge.getLoggingRecords()) { |
| 170 |
System.err.println(record); |
| 171 |
} |
| 172 |
} |
| 173 |
} catch (ManifestMerger2.MergeFailureException e) { |
| 174 |
logger.error(e, "Exception while merging manifests"); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
protected ManifestMerger2.Invoker createInvoker(@NonNull File mainManifestFile, |
| 181 |
@NonNull ILogger logger) { |
| 182 |
return ManifestMerger2.newMerger(mainManifestFile, logger, ManifestMerger2.MergeType.APPLICATION); |
| 183 |
} |
| 184 |
|
| 185 |
public static void usage() { |
| 186 |
System.out.println("Android Manifest Merger Tool Version 2\n"); |
| 187 |
System.out.println("Usage:"); |
| 188 |
System.out.println("Merger --main mainAndroidManifest.xml"); |
| 189 |
System.out.println("\t--log [VERBOSE, INFO, WARNING, ERROR]"); |
| 190 |
System.out.println("\t--libs [path separated list of lib's manifests]"); |
| 191 |
System.out.println("\t--overlays [path separated list of overlay's manifests]"); |
| 192 |
System.out.println("\t--property [" |
| 193 |
+ Joiner.on(" | ").join(ManifestSystemProperty.values()) |
| 194 |
+ "=value]"); |
| 195 |
System.out.println("\t--placeholder [name=value]"); |
| 196 |
System.out.println("\t--out [path of the output file]"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
@VisibleForTesting |
| 201 |
protected File checkPath(@NonNull String path) throws FileNotFoundException { |
| 202 |
@NonNull File file = new File(path); |
| 203 |
if (!file.exists()) { |
| 204 |
System.err.println(path + " does not exist"); |
| 205 |
throw new FileNotFoundException(path); |
| 206 |
} |
| 207 |
return file; |
| 208 |
} |
| 209 |
|
| 210 |
@VisibleForTesting |
| 211 |
protected ILogger createLogger(@NonNull StdLogger.Level level) { |
| 212 |
return new StdLogger(level); |
| 213 |
} |
| 214 |
|
| 215 |
} |