• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H
17 #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H
18 
19 #include <filesystem>
20 #include <functional>
21 #include <iostream>
22 #include <map>
23 #include <regex>
24 #include <string>
25 #include <vector>
26 
27 #include "constants.h"
28 #include "contrib/minizip/zip.h"
29 #include "contrib/minizip/unzip.h"
30 #include "nlohmann/json.hpp"
31 
32 namespace fs = std::filesystem;
33 
34 namespace OHOS {
35 namespace AppPackingTool {
36 
37 enum ErrCode {
38     ERR_OK = 0,
39     ERR_INVALID_VALUE,
40 };
41 
42 struct Parameter {
43     std::string paramName;
44     bool required;
45     Constants::PARAM_TYPE paramType;
46     std::string paramRegrex;
47     std::string zipEntry;
48 };
49 
50 class Packager {
51 public:
52     Packager(const std::map<std::string, std::string> &parameterMap, std::string &resultReceiver);
53     virtual ~Packager();
54 
55     std::string MakePackage();
56 
57     virtual int InitAllowedParam() = 0;
58     virtual int PreProcess() = 0;
59     virtual int Process() = 0;
60     virtual int PostProcess() = 0;
61 
62 protected:
63     const std::map<std::string, std::string> &parameterMap_;
64     std::string &resultReceiver_;
65     std::vector<Parameter> allowedParameters_;
66     nlohmann::json moduleJson;
67     nlohmann::json packInfo;
68 
69     void AddFileToZip(zipFile zf, const fs::path &filePath, const fs::path &zipPath, zip_fileinfo &zipfi);
70     void WriteStringToZip(zipFile zf, const std::string &content, const fs::path &zipPath, zip_fileinfo &zipfi);
71     bool ParseJsonFile(nlohmann::json &jsonObject, std::string filePath);
72     bool CheckFileValid(const std::string &filePath, const std::string &filename);
73     bool endWith(const std::string &str, const std::string &suffix);
74 };
75 
76 }  // namespace AppExecFwk
77 }  // namespace OHOS
78 
79 #endif  // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_PACKAGER_H