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 const TargetConfig &GetTargetConfigValues() const;
57 bool IsTargetConfig() const;
58
59 private:
60 void InitCommand();
61 uint32_t ParseCommand(int argc, char *argv[]);
62 uint32_t AddInput(const std::string& argValue);
63 uint32_t AddPackageName(const std::string& argValue);
64 uint32_t AddOutput(const std::string& argValue);
65 uint32_t AddResourceHeader(const std::string& argValue);
66 uint32_t ForceWrite();
67 uint32_t PrintVersion();
68 uint32_t AddMoudleNames(const std::string& argValue);
69 uint32_t AddConfig(const std::string& argValue);
70 uint32_t AddStartId(const std::string& argValue);
71 void AdaptResourcesDirForInput();
72 uint32_t CheckParam() const;
73 uint32_t HandleProcess(int c, const std::string& argValue);
74 uint32_t ParseFileList(const std::string& fileListPath);
75 uint32_t AddAppend(const std::string& argValue);
76 uint32_t SetCombine();
77 uint32_t AddDependEntry(const std::string& argValue);
78 uint32_t ShowHelp() const;
79 uint32_t SetIdDefinedOutput(const std::string& argValue);
80 uint32_t SetIdDefinedInputPath(const std::string& argValue);
81 bool IsAscii(const std::string& argValue) const;
82 bool IsLongOpt(char *argv[]) const;
83 uint32_t IconCheck();
84 uint32_t ParseTargetConfig(const std::string& argValue);
85
86 static const struct option CMD_OPTS[];
87 static const std::string CMD_PARAMS;
88 using HandleArgValue = std::function<uint32_t(const std::string&)>;
89 std::map<int32_t, HandleArgValue> handles_;
90 std::vector<std::string> inputs_;
91 std::string packageName_;
92 std::string output_;
93 std::vector<std::string> resourceHeaderPaths_;
94 bool forceWrite_ = false;
95 std::vector<std::string> moduleNames_;
96 std::string configPath_;
97 std::string restoolPath_;
98 int32_t startId_ = 0;
99 bool isFileList_ = false;
100 std::vector<std::string> append_;
101 bool combine_ = false;
102 std::string dependEntry_;
103 std::string idDefinedOutput_;
104 std::string idDefinedInputPath_;
105 bool isIconCheck_ = false;
106 TargetConfig targetConfig_;
107 bool isTtargetConfig_;
108 };
109
110 template<class T>
111 class CmdParser : public Singleton<CmdParser<T>> {
112 public:
113 T &GetCmdParser();
114 uint32_t Parse(int argc, char *argv[]);
115 static void ShowUseage();
116
117 private:
118 T cmdParser_;
119 };
120
121 template<class T>
ShowUseage()122 void CmdParser<T>::ShowUseage()
123 {
124 std::cout << "This is an OHOS Packaging Tool.\n";
125 std::cout << "Usage:\n";
126 std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
127 std::cout << "[arguments]:\n";
128 std::cout << " -i/--inputPath input resource path, can add more.\n";
129 std::cout << " -p/--packageName package name.\n";
130 std::cout << " -o/--outputPath output path.\n";
131 std::cout << " -r/--resHeader resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
132 std::cout << " -f/--forceWrite if output path exists,force delete it.\n";
133 std::cout << " -v/--version print tool version.\n";
134 std::cout << " -m/--modules module name, can add more, split by ','(like entry1,entry2,...).\n";
135 std::cout << " -j/--json config.json path.\n";
136 std::cout << " -e/--startId start id mask, e.g 0x01000000,";
137 std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n";
138 std::cout << " -x/--append resources folder path\n";
139 std::cout << " -z/--combine flag for incremental compilation\n";
140 std::cout << " -h/--help Displays this help menu\n";
141 std::cout << " -l/--fileList input json file of the option set, e.g resConfig.json.";
142 std::cout << " For details, see the developer documentation.\n";
143 std::cout << " --ids save id_defined.json direcory\n";
144 std::cout << " --defined-ids input id_defined.json path\n";
145 std::cout << " --dependEntry Build result directory of the specified entry module when the feature";
146 std::cout << " module resources are independently built in the FA model.\n";
147 std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n";
148 std::cout << " --target-config When used with '-i', selective compilation is supported.\n";
149 }
150
151 template<class T>
GetCmdParser()152 T &CmdParser<T>::GetCmdParser()
153 {
154 return cmdParser_;
155 }
156
157 template<class T>
Parse(int argc,char * argv[])158 uint32_t CmdParser<T>::Parse(int argc, char *argv[])
159 {
160 if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
161 return RESTOOL_ERROR;
162 }
163 return RESTOOL_SUCCESS;
164 }
165 }
166 }
167 }
168 #endif
169