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