1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos; 17 18 /** 19 * command parser 20 * 21 */ 22 public class CommandParser { 23 /** 24 * Parses and returns the hap list that supports the device type. 25 */ 26 public static final String PARSE_MODE_HAPLIST = "hap-list"; 27 28 /** 29 * Parses and returns the information about the hap. 30 */ 31 public static final String PARSE_MODE_HAPINFO = "hap-info"; 32 33 private static final String CMD_MODE = "--mode"; 34 private static final String CMD_JSON_PATH = "--json-path"; 35 private static final String CMD_PROFILE_PATH = "--profile-path"; 36 private static final String CMD_INDEX_PATH = "--index-path"; 37 private static final String CMD_JS_PATH = "--js-path"; 38 private static final String CMD_ETS_PATH = "--ets-path"; 39 private static final String CMD_RPCID_PATH = "--rpcid-path"; 40 private static final String CMD_RPCID = "--rpcid"; 41 private static final String CMD_SO_PATH = "--maple-so-path"; 42 private static final String CMD_SO_DIR = "--maple-so-dir"; 43 private static final String CMD_ABILITY_SO_PATH = "--ability-so-path"; 44 private static final String CMD_DEX_PATH = "--dex-path"; 45 private static final String CMD_ABC_PATH = "--abc-path"; 46 private static final String CMD_FILE_PATH = "--file-path"; 47 private static final String CMD_LIB_PATH = "--lib-path"; 48 private static final String CMD_RES_PATH = "--res-path"; 49 private static final String CMD_RESOURCES_PATH = "--resources-path"; 50 private static final String CMD_ASSETS_PATH = "--assets-path"; 51 private static final String CMD_APK_PATH = "--shell-apk-path"; 52 private static final String CMD_HAP_PATH = "--hap-path"; 53 private static final String CMD_APP_PATH = "--app-path"; 54 private static final String CMD_SIGNATURE_PATH = "--signature-path"; 55 private static final String CMD_CERTIFICATE_PATH = "--certificate-path"; 56 private static final String CMD_FORCE = "--force"; 57 private static final String CMD_OUT_PATH = "--out-path"; 58 private static final String CMD_PACK_INFO_PATH = "--pack-info-path"; 59 private static final String CMD_BIN_PATH = "--bin-path"; 60 private static final String CMD_JAR_PATH = "--jar-path"; 61 private static final String CMD_TXT_PATH = "--txt-path"; 62 private static final String CMD_HAR_PATH = "--har-path"; 63 private static final String CMD_HSP_PATH = "--hsp-path"; 64 private static final String CMD_PARSE_MODE = "--p"; 65 private static final String CMD_PACK_RES_PATH = "--pack-res-path"; 66 private static final String CMD_UNPACKAPK = "--unpackapk"; 67 private static final String CMD_UNPACK_CUT_ENTRY_APK = "--unpack-cut_entry"; 68 private static final String CMD_SHAREDLIBS_PATH = "--shared-libs-path"; 69 private static final String CMD_ENTRYCARD_PATH = "--entrycard-path"; 70 private static final String CMD_HAP_LIST = "--hap-list"; 71 private static final String CMD_HSP_LIST = "--hsp-list"; 72 private static final String CMD_APP_LIST = "--app-list"; 73 private static final String CMD_DIR_LIST = "--dir-list"; 74 private static final String CMD_HQF_LIST = "--hqf-list"; 75 private static final String CMD_APPQF_PATH = "--appqf-path"; 76 private static final String CMD_HQF_PATH = "--hqf-path"; 77 private static final String CMD_AN_PATH = "--an-path"; 78 private static final String CMD_AP_PATH = "--ap-path"; 79 private static final String MAIN_MODULE_LIMIT = "--main-module-limit"; 80 private static final String NORMAL_MODULE_LIMIT = "--normal-module-limit"; 81 private static final String TOTAL_LIMIT = "--total-limit"; 82 private static final Log LOG = new Log(CommandParser.class.toString()); 83 84 /** 85 * judge args is null and enter parser. 86 * 87 * @param utility common data 88 * @param args command line 89 * @return commandParser if input valid 90 */ commandParser(Utility utility, String[] args)91 public static boolean commandParser(Utility utility, String[] args) { 92 if (args == null) { 93 LOG.error("CommandParser::commandParser args is null!"); 94 return false; 95 } 96 97 parser(utility, args); 98 return true; 99 } 100 101 /** 102 * parse args. 103 * 104 * @param utility common data 105 * @param args command line 106 */ parser(Utility utility, String[] args)107 private static void parser(Utility utility, String[] args) { 108 for (int i = 0; i < args.length - 1; i++) { 109 if (parseAppCmd(utility, args[i], args[i + 1]) || parseHapCmd(utility, args[i], args[i + 1]) || 110 parseCommonCmd(utility, args[i], args[i + 1])) { 111 i++; 112 continue; 113 } 114 115 switch (args[i]) { 116 case CMD_BIN_PATH: { 117 utility.setBinPath(args[i + 1]); 118 i++; 119 break; 120 } 121 case CMD_JAR_PATH: { 122 utility.setJarPath(args[i + 1]); 123 i++; 124 break; 125 } 126 case CMD_TXT_PATH: { 127 utility.setTxtPath(args[i + 1]); 128 i++; 129 break; 130 } 131 case CMD_PARSE_MODE: { 132 utility.setParseMode(args[i + 1]); 133 if (PARSE_MODE_HAPLIST.equals(utility.getParseMode())) { 134 utility.setDeviceType(args[i + 2]); 135 } else if (PARSE_MODE_HAPINFO.equals(utility.getParseMode())) { 136 utility.setHapName(args[i + 2]); 137 } 138 i = i + 2; 139 break; 140 } 141 case CMD_UNPACKAPK: { 142 utility.setUnpackApk(args[i + 1]); 143 i++; 144 break; 145 } 146 case CMD_UNPACK_CUT_ENTRY_APK: { 147 utility.setUnpackCutEntryApk(args[i + 1]); 148 i++; 149 break; 150 } 151 case CMD_PACK_INFO_PATH: { 152 utility.setPackInfoPath(args[i + 1]); 153 i++; 154 break; 155 } 156 case CMD_ENTRYCARD_PATH: { 157 utility.setEntryCardPath(args[i + 1]); 158 i++; 159 break; 160 } 161 case CMD_PACK_RES_PATH: { 162 utility.setPackResPath(args[i + 1]); 163 i++; 164 break; 165 } 166 case CMD_ABC_PATH: { 167 utility.setAbcPath(args[i + 1]); 168 i++; 169 break; 170 } 171 case CMD_HQF_LIST: { 172 utility.setHqfList(args[i + 1]); 173 i++; 174 break; 175 } 176 default: { 177 break; 178 } 179 } 180 } 181 } 182 parseCommonCmd(Utility utility, String cmd, String value)183 private static boolean parseCommonCmd(Utility utility, String cmd, String value) { 184 switch (cmd) { 185 case CMD_MODE: { 186 utility.setMode(value); 187 return true; 188 } 189 case CMD_FORCE: { 190 utility.setForceRewrite(value); 191 return true; 192 } 193 case CMD_OUT_PATH: { 194 utility.setOutPath(value); 195 return true; 196 } 197 case CMD_HAR_PATH: { 198 utility.setHarPath(value); 199 return true; 200 } 201 case CMD_HSP_PATH: { 202 utility.setHspPath(value); 203 return true; 204 } 205 case CMD_APPQF_PATH: { 206 utility.setAPPQFPath(value); 207 return true; 208 } 209 default: { 210 return false; 211 } 212 } 213 } 214 215 /** 216 * parse app args. 217 * 218 * @param utility common data 219 * @param cmd command line 220 * @param value the value to be set 221 * @return parse result 222 */ parseAppCmd(Utility utility, String cmd, String value)223 private static boolean parseAppCmd(Utility utility, String cmd, String value) { 224 switch (cmd) { 225 case CMD_HAP_PATH: { 226 utility.setHapPath(value); 227 return true; 228 } 229 case CMD_APP_PATH: { 230 utility.setAppPath(value); 231 return true; 232 } 233 case CMD_CERTIFICATE_PATH: { 234 utility.setCertificatePath(value); 235 return true; 236 } 237 case CMD_SIGNATURE_PATH: { 238 utility.setSignaturePath(value); 239 return true; 240 } 241 case CMD_JSON_PATH: { 242 utility.setJsonPath(value); 243 return true; 244 } 245 case CMD_PACK_INFO_PATH: { 246 utility.setPackInfoPath(value); 247 return true; 248 } 249 case CMD_RES_PATH: { 250 utility.setResPath(value); 251 return true; 252 } 253 case CMD_PACK_RES_PATH: { 254 utility.setPackResPath(value); 255 return true; 256 } 257 case CMD_HAP_LIST: { 258 utility.setHapList(value); 259 return true; 260 } 261 case CMD_APP_LIST: { 262 utility.setAppList(value); 263 return true; 264 } 265 case CMD_HSP_LIST: { 266 utility.setHspList(value); 267 return true; 268 } 269 case MAIN_MODULE_LIMIT: { 270 utility.setMainModuleLimit(value); 271 return true; 272 } 273 case NORMAL_MODULE_LIMIT: { 274 utility.setNormalModuleLimit(value); 275 return true; 276 } 277 case TOTAL_LIMIT: { 278 utility.setTotalLimit(value); 279 return true; 280 } 281 default : { 282 return false; 283 } 284 } 285 } 286 287 /** 288 * parse hap args. 289 * 290 * @param utility common data 291 * @param cmd command line 292 * @param value the value to be set 293 * @return parse result 294 */ parseHapCmd(Utility utility, String cmd, String value)295 private static boolean parseHapCmd(Utility utility, String cmd, String value) { 296 switch (cmd) { 297 case CMD_JSON_PATH: { 298 utility.setJsonPath(value); 299 return true; 300 } 301 case CMD_PROFILE_PATH: { 302 utility.setProfilePath(value); 303 return true; 304 } 305 case CMD_INDEX_PATH: { 306 utility.setIndexPath(value); 307 return true; 308 } 309 case CMD_SO_PATH: { 310 utility.setSoPath(value); 311 return true; 312 } 313 case CMD_ABILITY_SO_PATH: { 314 utility.setAbilitySoPath(value); 315 return true; 316 } 317 case CMD_SO_DIR: { 318 utility.setSoDir(value); 319 return true; 320 } 321 case CMD_DEX_PATH: { 322 utility.setDexPath(value); 323 return true; 324 } 325 case CMD_ABC_PATH: { 326 utility.setAbcPath(value); 327 return true; 328 } 329 case CMD_FILE_PATH: { 330 utility.setFilePath(value); 331 return true; 332 } 333 case CMD_LIB_PATH: { 334 utility.setLibPath(value); 335 return true; 336 } 337 case CMD_ASSETS_PATH: { 338 utility.setAssetsPath(value); 339 return true; 340 } 341 case CMD_RES_PATH: { 342 utility.setResPath(value); 343 return true; 344 } 345 case CMD_RESOURCES_PATH: { 346 utility.setResourcesPath(value); 347 return true; 348 } 349 case CMD_SHAREDLIBS_PATH: { 350 utility.setSharedLibsPath(value); 351 return true; 352 } 353 case CMD_APK_PATH: { 354 utility.setApkPath(value); 355 return true; 356 } 357 case CMD_JAR_PATH: { 358 utility.setJarPath(value); 359 return true; 360 } 361 case CMD_TXT_PATH: { 362 utility.setTxtPath(value); 363 return true; 364 } 365 case CMD_PACK_INFO_PATH: { 366 utility.setPackInfoPath(value); 367 return true; 368 } 369 case CMD_JS_PATH: { 370 utility.setJsPath(value); 371 return true; 372 } 373 case CMD_ETS_PATH: { 374 utility.setEtsPath(value); 375 return true; 376 } 377 case CMD_RPCID_PATH: { 378 utility.setRpcidPath(value); 379 return true; 380 } 381 case CMD_RPCID:{ 382 utility.setRpcid(value); 383 return true; 384 } 385 case CMD_DIR_LIST: { 386 utility.setDirList(value); 387 return true; 388 } 389 case CMD_AN_PATH: { 390 utility.setANPath(value); 391 return true; 392 } 393 case CMD_AP_PATH: { 394 utility.setAPPath(value); 395 return true; 396 } 397 default : { 398 return false; 399 } 400 } 401 } 402 } 403