1 /*
2 * Copyright (c) 2021 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 #ifndef OHOS_RESTOOL_CMD_PARSER_H
17 #define OHOS_RESTOOL_CMD_PARSER_H
18
19 #include <getopt.h>
20 #include <iostream>
21 #include <functional>
22 #include <vector>
23 #include "singleton.h"
24 #include "resource_data.h"
25 #include "restool_errors.h"
26
27 namespace OHOS {
28 namespace Global {
29 namespace Restool {
30 class ICmdParser {
31 public:
32 virtual uint32_t Parse(int argc, char *argv[]) = 0;
33 };
34
35 class PackageParser : public ICmdParser {
36 public:
PackageParser()37 PackageParser() {};
38 virtual ~PackageParser() = default;
39 uint32_t Parse(int argc, char *argv[]) override;
40 const std::vector<std::string> &GetInputs() const;
41 const std::string &GetPackageName() const;
42 const std::string &GetOutput() const;
43 const std::vector<std::string> &GetResourceHeaders() const;
44 bool GetForceWrite() const;
45 const std::vector<std::string> &GetModuleNames() const;
46 const std::string &GetConfig() const;
47 const std::string &GetRestoolPath() const;
48 int32_t GetStartId() const;
49 bool IsFileList() const;
50 const std::vector<std::string> &GetAppend() const;
51 bool GetCombine() const;
52 const std::string &GetDependEntry() const;
53 const std::string &GetIdDefinedOutput() const;
54 const std::string &GetIdDefinedInputPath() const;
55 bool GetIconCheck() const;
56
57 private:
58 void InitCommand();
59 uint32_t ParseCommand(int argc, char *argv[]);
60 uint32_t AddInput(const std::string& argValue);
61 uint32_t AddPackageName(const std::string& argValue);
62 uint32_t AddOutput(const std::string& argValue);
63 uint32_t AddResourceHeader(const std::string& argValue);
64 uint32_t ForceWrite();
65 uint32_t PrintVersion();
66 uint32_t AddMoudleNames(const std::string& argValue);
67 uint32_t AddConfig(const std::string& argValue);
68 uint32_t AddStartId(const std::string& argValue);
69 void AdaptResourcesDirForInput();
70 uint32_t CheckParam() const;
71 uint32_t HandleProcess(int c, const std::string& argValue);
72 uint32_t ParseFileList(const std::string& fileListPath);
73 uint32_t AddAppend(const std::string& argValue);
74 uint32_t SetCombine();
75 uint32_t AddDependEntry(const std::string& argValue);
76 uint32_t ShowHelp() const;
77 uint32_t SetIdDefinedOutput(const std::string& argValue);
78 uint32_t SetIdDefinedInputPath(const std::string& argValue);
79 bool IsAscii(const std::string& argValue) const;
80 bool IsLongOpt(char *argv[]) const;
81 uint32_t IconCheck();
82
83 static const struct option CMD_OPTS[];
84 static const std::string CMD_PARAMS;
85 using HandleArgValue = std::function<uint32_t(const std::string&)>;
86 std::map<int32_t, HandleArgValue> handles_;
87 std::vector<std::string> inputs_;
88 std::string packageName_;
89 std::string output_;
90 std::vector<std::string> resourceHeaderPaths_;
91 bool forceWrite_ = false;
92 std::vector<std::string> moduleNames_;
93 std::string configPath_;
94 std::string restoolPath_;
95 int32_t startId_ = 0;
96 bool isFileList_ = false;
97 std::vector<std::string> append_;
98 bool combine_ = false;
99 std::string dependEntry_;
100 std::string idDefinedOutput_;
101 std::string idDefinedInputPath_;
102 bool isIconCheck_ = false;
103 };
104
105 template<class T>
106 class CmdParser : public Singleton<CmdParser<T>> {
107 public:
108 T &GetCmdParser();
109 uint32_t Parse(int argc, char *argv[]);
110 static void ShowUseage();
111
112 private:
113 T cmdParser_;
114 };
115
116 template<class T>
ShowUseage()117 void CmdParser<T>::ShowUseage()
118 {
119 std::cout << "This is an OHOS Packaging Tool.\n";
120 std::cout << "Usage:\n";
121 std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
122 std::cout << "[arguments]:\n";
123 std::cout << " -i/--inputPath input resource path, can add more.\n";
124 std::cout << " -p/--packageName package name.\n";
125 std::cout << " -o/--outputPath output path.\n";
126 std::cout << " -r/--resHeader resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
127 std::cout << " -f/--forceWrite if output path exists,force delete it.\n";
128 std::cout << " -v/--version print tool version.\n";
129 std::cout << " -m/--modules module name, can add more, split by ','(like entry1,entry2,...).\n";
130 std::cout << " -j/--json config.json path.\n";
131 std::cout << " -e/--startId start id mask, e.g 0x01000000,";
132 std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n";
133 std::cout << " -x/--append resources folder path\n";
134 std::cout << " -z/--combine flag for incremental compilation\n";
135 std::cout << " -h/--help Displays this help menu\n";
136 std::cout << " -l/--fileList input json file of the option set, e.g resConfig.json.";
137 std::cout << " For details, see the developer documentation.\n";
138 std::cout << " --ids save id_defined.json direcory\n";
139 std::cout << " --defined-ids input id_defined.json path\n";
140 std::cout << " --dependEntry Build result directory of the specified entry module when the feature";
141 std::cout << " module resources are independently built in the FA model.\n";
142 std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n";
143 }
144
145 template<class T>
GetCmdParser()146 T &CmdParser<T>::GetCmdParser()
147 {
148 return cmdParser_;
149 }
150
151 template<class T>
Parse(int argc,char * argv[])152 uint32_t CmdParser<T>::Parse(int argc, char *argv[])
153 {
154 if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
155 return RESTOOL_ERROR;
156 }
157 return RESTOOL_SUCCESS;
158 }
159 }
160 }
161 }
162 #endif
163