• 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     int32_t GetStartId() const;
49     const std::string &GetCachePath() const;
50     bool IsFileList() const;
51     bool GetPreviewMode() const;
52     int32_t GetPriority() const;
53     const std::vector<std::string> &GetAppend() const;
54     bool GetCombine() const;
55     const std::string &GetDependEntry() 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     uint32_t AddCachePath(const std::string& argValue);
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 SetPreviewMode();
74     uint32_t SetPriority(const std::string& argValue);
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     bool IsAscii(const std::string& argValue) const;
80 
81     static const struct option CMD_OPTS[];
82     static const std::string CMD_PARAMS;
83     using HandleArgValue = std::function<uint32_t(const std::string&)>;
84     std::map<int32_t, HandleArgValue> handles_;
85     std::vector<std::string> inputs_;
86     std::string packageName_;
87     std::string output_;
88     std::vector<std::string> resourceHeaderPaths_;
89     bool forceWrite_ = false;
90     std::vector<std::string> moduleNames_;
91     std::string configPath_;
92     std::string restoolPath_;
93     int32_t startId_ = 0;
94     std::string cachePath_;
95     bool isFileList_ = false;
96     bool previewMode_ = false;
97     int32_t priority_ = -1;
98     std::vector<std::string> append_;
99     bool combine_ = false;
100     std::string dependEntry_;
101 };
102 
103 template<class T>
104 class CmdParser : public Singleton<CmdParser<T>> {
105 public:
106     T &GetCmdParser();
107     uint32_t Parse(int argc, char *argv[]);
108     static void ShowUseage();
109 
110 private:
111     T cmdParser_;
112 };
113 
114 template<class T>
ShowUseage()115 void CmdParser<T>::ShowUseage()
116 {
117     std::cout << "This is an OHOS Packaging Tool.\n";
118     std::cout << "Usage:\n";
119     std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
120     std::cout << "[arguments]:\n";
121     std::cout << "    -i    input resource path, can add more.\n";
122     std::cout << "    -p    package name.\n";
123     std::cout << "    -o    output path.\n";
124     std::cout << "    -r    resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
125     std::cout << "    -f    if output path exists,force delete it.\n";
126     std::cout << "    -v    print tool version.\n";
127     std::cout << "    -m    module name, can add more, split by ','(like entry1,entry2,...).\n";
128     std::cout << "    -j    config.json path.\n";
129     std::cout << "    -e    start id mask, e.g 0x01000000, in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n";
130     std::cout << "    -x    resources folder path\n";
131     std::cout << "    -z    flag for incremental compilation\n";
132     std::cout << "    -h    Displays this help menu\n";
133 }
134 
135 template<class T>
GetCmdParser()136 T &CmdParser<T>::GetCmdParser()
137 {
138     return cmdParser_;
139 }
140 
141 template<class T>
Parse(int argc,char * argv[])142 uint32_t CmdParser<T>::Parse(int argc, char *argv[])
143 {
144     if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
145         return RESTOOL_ERROR;
146     }
147     return RESTOOL_SUCCESS;
148 }
149 }
150 }
151 }
152 #endif
153