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 #include "resource_merge.h"
17 #include "file_entry.h"
18
19 namespace OHOS {
20 namespace Global {
21 namespace Restool {
22 using namespace std;
23 const vector<ConfigParser::ModuleType> ResourceMerge::ORDERS = {
24 ConfigParser::ModuleType::NONE,
25 ConfigParser::ModuleType::ENTRY,
26 ConfigParser::ModuleType::FEATURE,
27 ConfigParser::ModuleType::SHARED,
28 ConfigParser::ModuleType::HAR
29 };
30
ResourceMerge()31 ResourceMerge::ResourceMerge()
32 {
33 }
34
~ResourceMerge()35 ResourceMerge::~ResourceMerge()
36 {
37 }
38
Init(const PackageParser & packageParser)39 uint32_t ResourceMerge::Init(const PackageParser &packageParser)
40 {
41 const vector<string> &inputs = packageParser.GetInputs();
42 if (packageParser.IsFileList()) {
43 inputsOrder_ = inputs;
44 return RESTOOL_SUCCESS;
45 }
46
47 map<ConfigParser::ModuleType, vector<string>> inputTypes;
48 string hapDir = "";
49 for (auto it = inputs.crbegin(); it != inputs.crend(); it++) {
50 string configPath = ResourceUtil::GetMainPath(*it).Append(ConfigParser::GetConfigName()).GetPath();
51 string resourceDir = FileEntry::FilePath(*it).GetPath();
52 string indexPath = ResourceUtil::GetMainPath(*it).Append(RESOURCE_INDEX_FILE).GetPath();
53 if (ResourceUtil::FileExist(indexPath)) {
54 hapDir = resourceDir;
55 continue;
56 }
57 ConfigParser::ModuleType moduleType = ConfigParser::ModuleType::NONE;
58 if (!ResourceUtil::FileExist(configPath)) {
59 inputTypes[moduleType].push_back(resourceDir);
60 continue;
61 }
62 ConfigParser configParser(configPath);
63 if (configParser.Init() != RESTOOL_SUCCESS) {
64 return RESTOOL_ERROR;
65 }
66 moduleType = configParser.GetModuleType();
67 inputTypes[moduleType].push_back(resourceDir);
68 }
69
70 for (const auto &type : ORDERS) {
71 if (inputTypes.find(type) == inputTypes.end()) {
72 continue;
73 }
74 inputsOrder_.insert(inputsOrder_.end(), inputTypes[type].begin(), inputTypes[type].end());
75 }
76 if (hapDir != "") {
77 inputsOrder_.insert(inputsOrder_.begin(), hapDir);
78 }
79 return RESTOOL_SUCCESS;
80 }
81
GetInputs() const82 const vector<string> &ResourceMerge::GetInputs() const
83 {
84 return inputsOrder_;
85 }
86 }
87 }
88 }