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