• 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::string &GetDependEntry() const;
54 
55 private:
56     void InitCommand();
57     uint32_t ParseCommand(int argc, char *argv[]);
58     uint32_t AddInput(const std::string& argValue);
59     uint32_t AddPackageName(const std::string& argValue);
60     uint32_t AddOutput(const std::string& argValue);
61     uint32_t AddResourceHeader(const std::string& argValue);
62     uint32_t ForceWrite();
63     uint32_t PrintVersion();
64     uint32_t AddMoudleNames(const std::string& argValue);
65     uint32_t AddConfig(const std::string& argValue);
66     uint32_t AddStartId(const std::string& argValue);
67     uint32_t AddCachePath(const std::string& argValue);
68     uint32_t CheckParam() const;
69     uint32_t HandleProcess(int c, const std::string& argValue);
70     uint32_t ParseFileList(const std::string& fileListPath);
71     uint32_t SetPreviewMode();
72     uint32_t SetPriority(const std::string& argValue);
73     uint32_t AddDependEntry(const std::string& argValue);
74 
75     static const struct option CMD_OPTS[];
76     static const std::string CMD_PARAMS;
77     using HandleArgValue = std::function<uint32_t(const std::string&)>;
78     std::map<int32_t, HandleArgValue> handles_;
79     std::vector<std::string> inputs_;
80     std::string packageName_;
81     std::string output_;
82     std::vector<std::string> resourceHeaderPaths_;
83     bool forceWrite_ = false;
84     std::vector<std::string> moduleNames_;
85     std::string configPath_;
86     std::string restoolPath_;
87     int32_t startId_ = 0;
88     std::string cachePath_;
89     bool isFileList_ = false;
90     bool previewMode_ = false;
91     int32_t priority_ = -1;
92     std::string dependEntry_;
93 };
94 
95 template<class T>
96 class CmdParser : public Singleton<CmdParser<T>> {
97 public:
98     T &GetCmdParser();
99     uint32_t Parse(int argc, char *argv[]);
100     static void ShowUseage();
101 
102 private:
103     T cmdParser_;
104 };
105 
106 template<class T>
ShowUseage()107 void CmdParser<T>::ShowUseage()
108 {
109     std::cout << "This is an OHOS Packaging Tool.\n";
110     std::cout << "Usage:\n";
111     std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n";
112     std::cout << "[arguments]:\n";
113     std::cout << "    -i    input resource path, can add more.\n";
114     std::cout << "    -p    package name.\n";
115     std::cout << "    -o    output path.\n";
116     std::cout << "    -r    resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
117     std::cout << "    -f    if output path exists,force delete it.\n";
118     std::cout << "    -v    print tool version.\n";
119     std::cout << "    -m    module name, can add more, split by ','(like entry1,entry2,...).\n";
120     std::cout << "    -j    config.json path.\n";
121     std::cout << "    -e    start id mask, e.g 0x01000000, in [0x01000000, 0x06FFFFFF),[0x08000000, 0x41FFFFFF)\n";
122     std::cout << "    -c    increment compile cache directory path.\n";
123 }
124 
125 template<class T>
GetCmdParser()126 T &CmdParser<T>::GetCmdParser()
127 {
128     return cmdParser_;
129 }
130 
131 template<class T>
Parse(int argc,char * argv[])132 uint32_t CmdParser<T>::Parse(int argc, char *argv[])
133 {
134     if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
135         return RESTOOL_ERROR;
136     }
137     return RESTOOL_SUCCESS;
138 }
139 }
140 }
141 }
142 #endif
143