1 /*
2 * Copyright (c) 2022 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 #include "cmd_list.h"
17 #include "restool_errors.h"
18
19 namespace OHOS {
20 namespace Global {
21 namespace Restool {
22 using namespace std;
Init(const string & filePath,HandleBack callback)23 uint32_t CmdList::Init(const string &filePath, HandleBack callback)
24 {
25 Json::Value root;
26 if (!ResourceUtil::OpenJsonFile(filePath, root)) {
27 return RESTOOL_ERROR;
28 }
29
30 if (!callback) {
31 return RESTOOL_SUCCESS;
32 }
33
34 InitFileListCommand(root, callback);
35
36 for (auto iter : fileListHandles_) {
37 if (iter() != RESTOOL_SUCCESS) {
38 return RESTOOL_ERROR;
39 }
40 }
41
42 callback(Option::FORCEWRITE, "");
43 return RESTOOL_SUCCESS;
44 }
45
46 // below private
InitFileListCommand(Json::Value & root,HandleBack callback)47 void CmdList::InitFileListCommand(Json::Value &root, HandleBack callback)
48 {
49 using namespace placeholders;
50 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["configPath"], Option::JSON, callback));
51 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["packageName"], Option::PACKAGENAME, callback));
52 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["output"], Option::OUTPUTPATH, callback));
53 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["startId"], Option::STARTID, callback));
54 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["entryCompiledResource"],
55 Option::DEPENDENTRY, callback));
56 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["ids"], Option::IDS, callback));
57 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["definedIds"], Option::DEFINED_IDS, callback));
58 fileListHandles_.push_back(bind(&CmdList::GetString, this, root["applicationResource"],
59 Option::INPUTPATH, callback));
60 fileListHandles_.push_back(bind(&CmdList::GetArray, this, root["ResourceTable"], Option::RESHEADER, callback));
61 fileListHandles_.push_back(bind(&CmdList::GetArray, this, root["moduleResources"], Option::INPUTPATH, callback));
62 fileListHandles_.push_back(bind(&CmdList::GetArray, this, root["dependencies"], Option::INPUTPATH, callback));
63 fileListHandles_.push_back(bind(&CmdList::GetModuleNames, this, root["moduleNames"], Option::MODULES, callback));
64 fileListHandles_.push_back(bind(&CmdList::GetBool, this, root["iconCheck"], Option::ICON_CHECK, callback));
65 }
66
GetString(const Json::Value & node,int c,HandleBack callback)67 uint32_t CmdList::GetString(const Json::Value &node, int c, HandleBack callback)
68 {
69 if (!node.isString()) {
70 return RESTOOL_SUCCESS;
71 }
72
73 if (callback(c, node.asString()) != RESTOOL_SUCCESS) {
74 return RESTOOL_ERROR;
75 }
76 return RESTOOL_SUCCESS;
77 }
78
GetArray(const Json::Value & node,int c,HandleBack callback)79 uint32_t CmdList::GetArray(const Json::Value &node, int c, HandleBack callback)
80 {
81 if (!node.isArray()) {
82 return RESTOOL_SUCCESS;
83 }
84
85 for (Json::ArrayIndex i = 0; i < node.size(); i++) {
86 if (!node[i].isString()) {
87 return RESTOOL_ERROR;
88 }
89 if (callback(c, node[i].asString()) != RESTOOL_SUCCESS) {
90 return RESTOOL_ERROR;
91 }
92 }
93 return RESTOOL_SUCCESS;
94 }
95
GetModuleNames(const Json::Value & node,int c,HandleBack callback)96 uint32_t CmdList::GetModuleNames(const Json::Value &node, int c, HandleBack callback)
97 {
98 string moduleNames;
99 if (node.isString()) {
100 return GetString(node, c, callback);
101 }
102 if (GetArray(node, c, [&moduleNames](int c, const string &argValue) {
103 if (!moduleNames.empty()) {
104 moduleNames.append(",");
105 }
106 moduleNames.append(argValue);
107 return RESTOOL_SUCCESS;
108 }) != RESTOOL_SUCCESS) {
109 return RESTOOL_ERROR;
110 }
111
112 if (!moduleNames.empty() && callback(c, moduleNames) != RESTOOL_SUCCESS) {
113 return RESTOOL_ERROR;
114 }
115 return RESTOOL_SUCCESS;
116 }
117
GetBool(const Json::Value & node,int c,HandleBack callback)118 uint32_t CmdList::GetBool(const Json::Value &node, int c, HandleBack callback)
119 {
120 if (node.type() != Json::booleanValue) {
121 return RESTOOL_SUCCESS;
122 }
123
124 if (node.asBool() && callback(c, "") != RESTOOL_SUCCESS) {
125 return RESTOOL_ERROR;
126 }
127 return RESTOOL_SUCCESS;
128 }
129 }
130 }
131 }
132