1 /* 2 * Copyright (c) 2023 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 STAGE_CONTEXT_H 17 #define STAGE_CONTEXT_H 18 19 #include <string> 20 #include <vector> 21 #include <optional> 22 #include <map> 23 24 namespace Json2 { 25 class Value; 26 } 27 28 namespace OHOS::Ide { 29 class HspInfo { 30 public: 31 std::string moduleName; 32 std::string resourcePath; 33 std::vector<uint8_t> moduleJsonBuffer; 34 }; 35 class StageContext { 36 public: 37 static StageContext& GetInstance(); 38 const std::optional<std::vector<uint8_t>> ReadFileContents(const std::string& filePath) const; 39 // for Previewer 40 void SetLoaderJsonPath(const std::string& assetPath); 41 void SetHosSdkPath(const std::string& hosSdkPathValue); 42 void GetModulePathMapFromLoaderJson(); 43 std::string GetHspAceModuleBuild(const std::string& hspConfigPath); 44 void ReleaseHspBuffers(); 45 std::map<std::string, std::string> ParseMockJsonFile(const std::string& mockJsonFilePath); 46 // for ArkUI and Ability 47 std::vector<uint8_t>* GetModuleBuffer(const std::string& inputPath); 48 std::vector<uint8_t>* GetLocalModuleBuffer(const std::string& moduleName); 49 std::vector<uint8_t>* GetCloudModuleBuffer(const std::string& moduleName); 50 std::vector<uint8_t>* GetSystemModuleBuffer(const std::string& inputPath, const std::string& moduleName); 51 std::vector<uint8_t>* GetModuleBufferFromHsp(const std::string& hspFilePath, const std::string& fileName); 52 void SetPkgContextInfo(std::map<std::string, std::string>& pkgContextInfoJsonStringMap, 53 std::map<std::string, std::string>& packageNameList); 54 void GetModuleInfo(std::vector<HspInfo>& dependencyHspInfos); 55 private: 56 StageContext() = default; 57 ~StageContext() = default; 58 bool ContainsRelativePath(const std::string& path) const; 59 std::map<std::string, std::string> GetModulePathMap() const; 60 std::string GetCloudHspPath(const std::string& hspDir, const std::string& moduleName); 61 std::string ReplaceLastStr(const std::string& str, const std::string& find, const std::string& replace); 62 int GetHspActualName(const std::string& input, std::string& ret); 63 std::string GetActualCloudHspDir(const std::string& actualName); 64 std::string GetCloudModuleHspPath(const std::string& actualName); 65 std::string GetCloudProjectHspPath(const std::string& actualName); 66 std::string GetCloudHspVersion(const std::string& hspPath, const std::string& actualName); 67 std::vector<int> SplitHspVersion(const std::string& version); 68 int CompareHspVersion(const std::string& version1, const std::string& version2); 69 bool UnzipHspFile(const std::string& hspFilePath, const std::string& writePath, 70 const std::vector<std::string> fileNames); 71 void GetHspinfo(const std::string& packageName, HspInfo& hspInfo); 72 bool GetLocalModuleInfo(HspInfo &hspInfo); 73 bool GetCloudModuleInfo(const std::string& packageName, HspInfo& hspInfo); 74 std::string loaderJsonPath; 75 std::map<std::string, std::string> modulePathMap; 76 std::map<std::string, std::string> hspNameOhmMap; 77 std::map<std::string, std::string> packageNameMap; 78 std::map<std::string, std::string> hspResourcesMap; 79 std::string projectRootPath; 80 std::string buildConfigPath; 81 std::vector<std::vector<uint8_t>*> hspBufferPtrsVec; 82 int GetUpwardDirIndex(const std::string& path, const int upwardLevel) const; 83 std::string localBundleName = "bundle"; 84 std::string hosSdkPath = ""; 85 }; 86 } 87 #endif // STAGE_CONTEXT_H