• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.util.AbstractMap;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.function.Function;
22 
23 /**
24  * command parser.
25  *
26  */
27 public class CommandParser {
28     /**
29      * Parses and returns the hap list that supports the device type.
30      */
31     public static final String PARSE_MODE_HAPLIST = "hap-list";
32 
33     /**
34      * Parses and returns the information about the hap.
35      */
36     public static final String PARSE_MODE_HAPINFO = "hap-info";
37 
38     private static final String CMD_MODE = "--mode";
39     private static final String CMD_JSON_PATH = "--json-path";
40     private static final String CMD_PROFILE_PATH = "--profile-path";
41     private static final String CMD_INDEX_PATH = "--index-path";
42     private static final String CMD_JS_PATH = "--js-path";
43     private static final String CMD_ETS_PATH = "--ets-path";
44     private static final String CMD_RPCID_PATH = "--rpcid-path";
45     private static final String CMD_RPCID = "--rpcid";
46     private static final String CMD_SO_PATH = "--maple-so-path";
47     private static final String CMD_SO_DIR = "--maple-so-dir";
48     private static final String CMD_ABILITY_SO_PATH = "--ability-so-path";
49     private static final String CMD_DEX_PATH = "--dex-path";
50     private static final String CMD_ABC_PATH = "--abc-path";
51     private static final String CMD_FILE_PATH = "--file-path";
52     private static final String CMD_LIB_PATH = "--lib-path";
53     private static final String CMD_RES_PATH = "--res-path";
54     private static final String CMD_RESOURCES_PATH = "--resources-path";
55     private static final String CMD_ASSETS_PATH = "--assets-path";
56     private static final String CMD_APK_PATH = "--shell-apk-path";
57     private static final String CMD_HAP_PATH = "--hap-path";
58     private static final String CMD_APP_PATH = "--app-path";
59     private static final String CMD_SIGNATURE_PATH = "--signature-path";
60     private static final String CMD_CERTIFICATE_PATH = "--certificate-path";
61     private static final String CMD_FORCE = "--force";
62     private static final String CMD_OUT_PATH = "--out-path";
63     private static final String CMD_PACK_INFO_PATH = "--pack-info-path";
64     private static final String CMD_BIN_PATH = "--bin-path";
65     private static final String CMD_JAR_PATH = "--jar-path";
66     private static final String CMD_TXT_PATH = "--txt-path";
67     private static final String CMD_HAR_PATH = "--har-path";
68     private static final String CMD_HSP_PATH = "--hsp-path";
69     private static final String CMD_PARSE_MODE = "--p";
70     private static final String CMD_PACK_RES_PATH = "--pack-res-path";
71     private static final String CMD_UNPACKAPK = "--unpackapk";
72     private static final String CMD_UNPACK_CUT_ENTRY_APK = "--unpack-cut_entry";
73     private static final String CMD_SHAREDLIBS_PATH = "--shared-libs-path";
74     private static final String CMD_ENTRYCARD_PATH = "--entrycard-path";
75     private static final String CMD_HAP_LIST = "--hap-list";
76     private static final String CMD_HSP_LIST = "--hsp-list";
77     private static final String CMD_APP_LIST = "--app-list";
78     private static final String CMD_DIR_LIST = "--dir-list";
79     private static final String CMD_HQF_LIST = "--hqf-list";
80     private static final String CMD_APPQF_PATH = "--appqf-path";
81     private static final String CMD_AN_PATH = "--an-path";
82     private static final String CMD_AP_PATH = "--ap-path";
83     private static final String MAIN_MODULE_LIMIT = "--main-module-limit";
84     private static final String NORMAL_MODULE_LIMIT = "--normal-module-limit";
85     private static final String TOTAL_LIMIT = "--total-limit";
86     private static final String VERSION_CODE = "--version-code";
87     private static final String VERSION_NAME = "--version-name";
88     private static final String INPUT_LIST = "--input-list";
89     private static final String INPUT = "--input";
90     private static final String STAT_DUPLICATE = "--stat-duplicate";
91     private static final String STAT_SUFFIX = "--stat-suffix";
92     private static final String STAT_FILE_SIZE = "--stat-file-size";
93     private static final String PARSER_STAT_DUPLICATE_ERROR = "code:9132600 " +
94             "error:statDuplicate is invalid! Must be true or false.";
95     private static final String PARSER_STAT_SUFFIX_ERROR = "code:9132601 " +
96             "error:statSuffix is invalid! Must be true or false.";
97     private static final int PARSE_MODE_VALUE_LENGTH = 2;
98     private static final Log LOG = new Log(CommandParser.class.toString());
99     private static final Map<String, Function<Map.Entry<Utility, String>, Boolean>> commandFuncs = new HashMap<>();
100 
101     static {
initCommandFuncs()102         initCommandFuncs();
103     }
104 
initCommandFuncs()105     private static void initCommandFuncs() {
106         commandFuncs.put(CMD_MODE, entry -> {
107             entry.getKey().setMode(entry.getValue());
108             return true;
109         });
110         commandFuncs.put(CMD_JSON_PATH, entry -> {
111             entry.getKey().setJsonPath(entry.getValue());
112             return true;
113         });
114         commandFuncs.put(CMD_PROFILE_PATH, entry -> {
115             entry.getKey().setProfilePath(entry.getValue());
116             return true;
117         });
118         commandFuncs.put(CMD_INDEX_PATH, entry -> {
119             entry.getKey().setIndexPath(entry.getValue());
120             return true;
121         });
122         commandFuncs.put(CMD_JS_PATH, entry -> {
123             entry.getKey().setJsPath(entry.getValue());
124             return true;
125         });
126         commandFuncs.put(CMD_ETS_PATH, entry -> {
127             entry.getKey().setEtsPath(entry.getValue());
128             return true;
129         });
130         commandFuncs.put(CMD_RPCID_PATH, entry -> {
131             entry.getKey().setRpcidPath(entry.getValue());
132             return true;
133         });
134         commandFuncs.put(CMD_RPCID, entry -> {
135             entry.getKey().setRpcid(entry.getValue());
136             return true;
137         });
138         commandFuncs.put(CMD_SO_PATH, entry -> {
139             entry.getKey().setSoPath(entry.getValue());
140             return true;
141         });
142         commandFuncs.put(CMD_SO_DIR, entry -> {
143             entry.getKey().setSoDir(entry.getValue());
144             return true;
145         });
146         commandFuncs.put(CMD_ABILITY_SO_PATH, entry -> {
147             entry.getKey().setAbilitySoPath(entry.getValue());
148             return true;
149         });
150         commandFuncs.put(CMD_DEX_PATH, entry -> {
151             entry.getKey().setDexPath(entry.getValue());
152             return true;
153         });
154         commandFuncs.put(CMD_ABC_PATH, entry -> {
155             entry.getKey().setAbcPath(entry.getValue());
156             return true;
157         });
158         commandFuncs.put(CMD_FILE_PATH, entry -> {
159             entry.getKey().setFilePath(entry.getValue());
160             return true;
161         });
162         commandFuncs.put(CMD_LIB_PATH, entry -> {
163             entry.getKey().setLibPath(entry.getValue());
164             return true;
165         });
166         commandFuncs.put(CMD_RES_PATH, entry -> {
167             entry.getKey().setResPath(entry.getValue());
168             return true;
169         });
170         commandFuncs.put(CMD_RESOURCES_PATH, entry -> {
171             entry.getKey().setResourcesPath(entry.getValue());
172             return true;
173         });
174         commandFuncs.put(CMD_ASSETS_PATH, entry -> {
175             entry.getKey().setAssetsPath(entry.getValue());
176             return true;
177         });
178         commandFuncs.put(CMD_APK_PATH, entry -> {
179             entry.getKey().setApkPath(entry.getValue());
180             return true;
181         });
182         commandFuncs.put(CMD_HAP_PATH, entry -> {
183             entry.getKey().setHapPath(entry.getValue());
184             return true;
185         });
186         commandFuncs.put(CMD_APP_PATH, entry -> {
187             entry.getKey().setAppPath(entry.getValue());
188             return true;
189         });
190         commandFuncs.put(CMD_SIGNATURE_PATH, entry -> {
191             entry.getKey().setSignaturePath(entry.getValue());
192             return true;
193         });
194         commandFuncs.put(CMD_CERTIFICATE_PATH, entry -> {
195             entry.getKey().setCertificatePath(entry.getValue());
196             return true;
197         });
198         commandFuncs.put(CMD_FORCE, entry -> {
199             entry.getKey().setForceRewrite(entry.getValue());
200             return true;
201         });
202         commandFuncs.put(CMD_OUT_PATH, entry -> {
203             entry.getKey().setOutPath(entry.getValue());
204             return true;
205         });
206         commandFuncs.put(CMD_PACK_INFO_PATH, entry -> {
207             entry.getKey().setPackInfoPath(entry.getValue());
208             return true;
209         });
210         commandFuncs.put(CMD_BIN_PATH, entry -> {
211             entry.getKey().setBinPath(entry.getValue());
212             return true;
213         });
214         commandFuncs.put(CMD_JAR_PATH, entry -> {
215             entry.getKey().setJarPath(entry.getValue());
216             return true;
217         });
218         commandFuncs.put(CMD_TXT_PATH, entry -> {
219             entry.getKey().setTxtPath(entry.getValue());
220             return true;
221         });
222         commandFuncs.put(CMD_HAR_PATH, entry -> {
223             entry.getKey().setHarPath(entry.getValue());
224             return true;
225         });
226         commandFuncs.put(CMD_HSP_PATH, entry -> {
227             entry.getKey().setHspPath(entry.getValue());
228             return true;
229         });
230         commandFuncs.put(CMD_PACK_RES_PATH, entry -> {
231             entry.getKey().setPackResPath(entry.getValue());
232             return true;
233         });
234         commandFuncs.put(CMD_UNPACKAPK, entry -> {
235             entry.getKey().setUnpackApk(entry.getValue());
236             return true;
237         });
238         commandFuncs.put(CMD_UNPACK_CUT_ENTRY_APK, entry -> {
239             entry.getKey().setUnpackCutEntryApk(entry.getValue());
240             return true;
241         });
242         commandFuncs.put(CMD_SHAREDLIBS_PATH, entry -> {
243             entry.getKey().setSharedLibsPath(entry.getValue());
244             return true;
245         });
246         commandFuncs.put(CMD_ENTRYCARD_PATH, entry -> {
247             entry.getKey().setEntryCardPath(entry.getValue());
248             return true;
249         });
250         commandFuncs.put(CMD_HAP_LIST, entry -> {
251             entry.getKey().setHapList(entry.getValue());
252             return true;
253         });
254         commandFuncs.put(CMD_HSP_LIST, entry -> {
255             entry.getKey().setHspList(entry.getValue());
256             return true;
257         });
258         commandFuncs.put(CMD_APP_LIST, entry -> {
259             entry.getKey().setAppList(entry.getValue());
260             return true;
261         });
262         commandFuncs.put(CMD_DIR_LIST, entry -> {
263             entry.getKey().setDirList(entry.getValue());
264             return true;
265         });
266         commandFuncs.put(CMD_HQF_LIST, entry -> {
267             entry.getKey().setHqfList(entry.getValue());
268             return true;
269         });
270         commandFuncs.put(CMD_APPQF_PATH, entry -> {
271             entry.getKey().setAPPQFPath(entry.getValue());
272             return true;
273         });
274         commandFuncs.put(CMD_AN_PATH, entry -> {
275             entry.getKey().setANPath(entry.getValue());
276             return true;
277         });
278         commandFuncs.put(CMD_AP_PATH, entry -> {
279             entry.getKey().setAPPath(entry.getValue());
280             return true;
281         });
282         commandFuncs.put(MAIN_MODULE_LIMIT, entry -> {
283             entry.getKey().setMainModuleLimit(entry.getValue());
284             return true;
285         });
286         commandFuncs.put(NORMAL_MODULE_LIMIT, entry -> {
287             entry.getKey().setNormalModuleLimit(entry.getValue());
288             return true;
289         });
290         commandFuncs.put(TOTAL_LIMIT, entry -> {
291             entry.getKey().setTotalLimit(entry.getValue());
292             return true;
293         });
294         commandFuncs.put(VERSION_CODE, entry -> {
295             entry.getKey().setVersionCode(Integer.parseInt(entry.getValue()));
296             return true;
297         });
298         commandFuncs.put(VERSION_NAME, entry -> {
299             entry.getKey().setVersionName(entry.getValue());
300             return true;
301         });
302         commandFuncs.put(INPUT_LIST, entry -> {
303             entry.getKey().setInputList(entry.getValue());
304             return true;
305         });
306         commandFuncs.put(INPUT, entry -> {
307             entry.getKey().setInput(entry.getValue());
308             return true;
309         });
310         commandFuncs.put(STAT_DUPLICATE, entry -> {
311             if (Boolean.TRUE.toString().equals(entry.getValue()) || Boolean.FALSE.toString().equals(entry.getValue())) {
312                 entry.getKey().setStatDuplicate(Boolean.parseBoolean(entry.getValue()));
313                 return true;
314             } else {
315                 LOG.error(PARSER_STAT_DUPLICATE_ERROR);
316                 return false;
317             }
318         });
319         commandFuncs.put(STAT_SUFFIX, entry -> {
320             if (Boolean.TRUE.toString().equals(entry.getValue()) || Boolean.FALSE.toString().equals(entry.getValue())) {
321                 entry.getKey().setStatSuffix(Boolean.parseBoolean(entry.getValue()));
322                 return true;
323             } else {
324                 LOG.error(PARSER_STAT_SUFFIX_ERROR);
325                 return false;
326             }
327         });
328         commandFuncs.put(STAT_FILE_SIZE, entry -> {
329             entry.getKey().setStatFileSize(entry.getValue());
330             return true;
331         });
332     }
333 
334 
335     /**
336      * judge args is null and enter parser.
337      *
338      * @param utility common data
339      * @param args command line
340      * @return commandParser if input valid
341      */
commandParser(Utility utility, String[] args)342     public static boolean commandParser(Utility utility, String[] args) {
343         if (args == null) {
344             LOG.error("CommandParser::commandParser args is null!");
345             return false;
346         }
347         for (int i = 0; i < args.length - 1; ++i) {
348             String key = args[i];
349             String value = args[i + 1];
350             Map.Entry<Utility, String> entry = new AbstractMap.SimpleEntry<>(utility, value);
351             if (commandFuncs.get(key) != null) {
352                 commandFuncs.get(key).apply(entry);
353                 ++i;
354             } else if (CMD_PARSE_MODE.equals(key)) {
355                 if (i + PARSE_MODE_VALUE_LENGTH >= args.length) {
356                     LOG.error("input wrong number value for --p command");
357                     return false;
358                 }
359                 utility.setParseMode(args[i + 1]);
360                 if (PARSE_MODE_HAPLIST.equals(utility.getParseMode())) {
361                     utility.setDeviceType(args[i + PARSE_MODE_VALUE_LENGTH]);
362                 } else if (PARSE_MODE_HAPINFO.equals(utility.getParseMode())) {
363                     utility.setHapName(args[i + PARSE_MODE_VALUE_LENGTH]);
364                 }
365                 i += PARSE_MODE_VALUE_LENGTH;
366             } else {
367                 LOG.warning(key + " is invalid!");
368             }
369         }
370         return true;
371     }
372 }
373