diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 83f24a865a931aae2c6ab3d1eb93c71f12d42a15..23c6028c8e097f36ffcadfbeecaf43c3944193b7 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -258,7 +258,7 @@ public class Compressor { } public void setModuleName(String name) { - moduleName = name; + this.moduleName = name; } public String getOriginVersionName() { @@ -270,6 +270,79 @@ public class Compressor { } } + private static class GeneralNormalizeUtil { + private int originVersionCode = INVALID_VERSION; + private String originVersionName = ""; + private String moduleName = ""; + private String originBundleName = ""; + private int originMinCompatibleVersionCode = INVALID_VERSION; + private int originMinAPIVersion = INVALID_VERSION; + private int originTargetAPIVersion = INVALID_VERSION; + private String originApiReleaseType = ""; + private String originBundleType = ""; + private boolean originInstallationFree = false; + private boolean originDeliveryWithInstall = false; + private String originDeviceTypes = ""; + private boolean isInstallationFree = false; + private boolean isDeliveryWithInstall = false; + + public void setOriginVersionCode(int originVersionCode) { + this.originVersionCode = originVersionCode; + } + + public void setModuleName(String name) { + this.moduleName = name; + } + + public void setOriginVersionName(String originVersionName) { + this.originVersionName = originVersionName; + } + + public void setOriginBundleName(String originBundleName ) { + this.originBundleName = originBundleName; + } + + public void setOriginMinCompatibleVersionCode(int originMinCompatibleVersionCode) { + this.originMinCompatibleVersionCode = originMinCompatibleVersionCode; + } + + public void setOriginMinAPIVersion(int originMinAPIVersion) { + this.originMinAPIVersion = originMinAPIVersion; + } + + public void setOriginTargetAPIVersion(int originTargetAPIVersion) { + this.originTargetAPIVersion = originTargetAPIVersion; + } + + public void setOriginApiReleaseType(String originApiReleaseType) { + this.originApiReleaseType = originApiReleaseType; + } + + public void setOriginBundleType(String originBundleType) { + this.originBundleType = originBundleType; + } + + public void setIsInstallationFree(boolean isInstallationFree) { + this.isInstallationFree = isInstallationFree; + } + + public void setOriginInstallationFree(boolean originInstallationFree) { + this.originInstallationFree = originInstallationFree; + } + + public void setOriginDeliveryWithInstall(boolean originDeliveryWithInstall) { + this.originDeliveryWithInstall = originDeliveryWithInstall; + } + + public void setIsDeliveryWithInstall(boolean isDeliveryWithInstall) { + this.isDeliveryWithInstall = isDeliveryWithInstall; + } + + public void setOriginDeviceTypes(String originDeviceTypes) { + this.originDeviceTypes = originDeviceTypes; + } + } + /** * parse file size limit from utility. * @@ -3807,15 +3880,15 @@ public class Compressor { } private void generalNormalize(Utility utility) { - List> recordList = new ArrayList<>(); + List utils = new ArrayList<>(); Path tempDir = null; boolean isSuccess = true; String[] name = new String[2]; for (String hapPath : utility.getFormattedHapList()) { try { + GeneralNormalizeUtil util = new GeneralNormalizeUtil(); tempDir = Files.createTempDirectory(Paths.get(utility.getOutPath()), "temp"); unpackHap(hapPath, tempDir.toAbsolutePath().toString()); - HashMap outPutMap; File moduleFile = new File( tempDir.toAbsolutePath() + LINUX_FILE_SEPARATOR + MODULE_JSON); File configFile = new File( @@ -3829,10 +3902,10 @@ public class Compressor { } if (moduleFile.exists()) { String moduleJsonPath = tempDir.resolve(MODULE_JSON).toString(); - outPutMap = parseAndModifyGeneralModuleJson(moduleJsonPath, utility, name); + util = parseAndModifyGeneralModuleJson(moduleJsonPath, utility, name); } else if (configFile.exists()) { String configJsonPath = tempDir.resolve(CONFIG_JSON).toString(); - outPutMap = parseAndModifyGeneralConfigJson(configJsonPath, utility, name); + util = parseAndModifyGeneralConfigJson(configJsonPath, utility, name); } else { LOG.error(PackingToolErrMsg.GENERAL_NORMALIZE_MODE_ARGS_INVALID .toString("Invalid hap structure")); throw new BundleException("generalNormalize failed, invalid hap structure."); @@ -3841,7 +3914,7 @@ public class Compressor { String packInfoPath = tempDir.resolve(PACKINFO_NAME).toString(); parseAndModifyGeneralPackInfo(packInfoPath, utility); } - recordList.add(outPutMap); + utils.add(util); String modifiedHapPath = Paths.get(utility.getOutPath()) + LINUX_FILE_SEPARATOR + Paths.get(hapPath).getFileName().toString(); boolean ret = compressDirToHap(tempDir, modifiedHapPath); @@ -3868,12 +3941,12 @@ public class Compressor { } return; } - writeGeneralRecord(recordList, utility.getOutPath()); + writeGeneralRecord(utils, utility.getOutPath()); } - private HashMap parseAndModifyGeneralModuleJson(String jsonFilePath, Utility utility, String[] name) + private GeneralNormalizeUtil parseAndModifyGeneralModuleJson(String jsonFilePath, Utility utility, String[] name) throws BundleException { - HashMap outPutMap = new HashMap<>(); + GeneralNormalizeUtil util = new GeneralNormalizeUtil(); try (FileInputStream jsonStream = new FileInputStream(jsonFilePath)) { JSONObject jsonObject = JSON.parseObject(jsonStream, JSONObject.class); if (!jsonObject.containsKey(APP)) { @@ -3895,50 +3968,49 @@ public class Compressor { } name[0] = appObject.getString(BUNDLE_NAME); name[1] = moduleObject.getString(NAME); - outPutMap.put(MODULE_NAME_NEW, moduleObject.getString(NAME)); + util.setModuleName(moduleObject.getString(NAME)); if (utility.getGeneralNormalizeList().contains(DEVICE_TYPES)) { - outPutMap.put(DEVICE_TYPES, getJsonString(moduleObject, DEVICE_TYPES)); + util.setOriginDeviceTypes(getJsonString(moduleObject, DEVICE_TYPES)); moduleObject.put(DEVICE_TYPES, utility.getDeviceTypes().split(",")); } if (utility.getGeneralNormalizeList().contains(VERSION_CODE)) { - outPutMap.put(VERSION_CODE, String.valueOf(appObject.getIntValue(VERSION_CODE))); + util.setOriginVersionCode(appObject.getIntValue(VERSION_CODE)); appObject.put(VERSION_CODE, utility.getVersionCode()); } if (utility.getGeneralNormalizeList().contains(VERSION_NAME)) { - outPutMap.put(VERSION_NAME, appObject.getString(VERSION_NAME)); + util.setOriginVersionName(appObject.getString(VERSION_NAME)); appObject.put(VERSION_NAME, utility.getVersionName()); } if (utility.getGeneralNormalizeList().contains(BUNDLE_NAME)) { - outPutMap.put(BUNDLE_NAME, appObject.getString(BUNDLE_NAME)); + util.setOriginBundleName(appObject.getString(BUNDLE_NAME)); appObject.put(BUNDLE_NAME, utility.getBundleName()); } if (utility.getGeneralNormalizeList().contains(MIN_COMPATIBLE_VERSION_CODE)) { if (appObject.containsKey(MIN_COMPATIBLE_VERSION_CODE)) { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf( - appObject.getIntValue(MIN_COMPATIBLE_VERSION_CODE))); + util.setOriginMinCompatibleVersionCode(appObject.getIntValue(MIN_COMPATIBLE_VERSION_CODE)); } else { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf(appObject.getIntValue(VERSION_CODE))); + util.setOriginMinCompatibleVersionCode(appObject.getIntValue(VERSION_CODE)); } appObject.put(MIN_COMPATIBLE_VERSION_CODE, utility.getMinCompatibleVersionCode()); } if (utility.getGeneralNormalizeList().contains(MIN_API_VERSION)) { - outPutMap.put(MIN_API_VERSION, String.valueOf(appObject.getIntValue(MIN_API_VERSION))); + util.setOriginMinAPIVersion(appObject.getIntValue(MIN_API_VERSION)); appObject.put(MIN_API_VERSION, utility.getMinAPIVersion()); } if (utility.getGeneralNormalizeList().contains(TARGET_API_VERSION)) { - outPutMap.put(TARGET_API_VERSION, String.valueOf(appObject.getIntValue(TARGET_API_VERSION))); + util.setOriginTargetAPIVersion(appObject.getIntValue(TARGET_API_VERSION)); appObject.put(TARGET_API_VERSION, utility.getTargetAPIVersion()); } if (utility.getGeneralNormalizeList().contains(API_RELEASE_TYPE)) { - outPutMap.put(API_RELEASE_TYPE, appObject.getString(API_RELEASE_TYPE)); + util.setOriginApiReleaseType(appObject.getString(API_RELEASE_TYPE)); appObject.put(API_RELEASE_TYPE, utility.getApiReleaseType()); } @@ -3951,21 +4023,23 @@ public class Compressor { LOG.error(PackingToolErrMsg.PARSE_STAGE_BUNDLE_TYPE_FAILED.toString(errMsg, solution)); throw new BundleException(errMsg); } - outPutMap.put(BUNDLE_TYPE, APP); + util.setOriginBundleType(APP); } else { - outPutMap.put(BUNDLE_TYPE, appObject.getString(BUNDLE_TYPE)); + util.setOriginBundleType(appObject.getString(BUNDLE_TYPE)); } appObject.put(BUNDLE_TYPE, utility.getBundleType()); } if (utility.getGeneralNormalizeList().contains(INSTALLATION_FREE)) { - outPutMap.put(INSTALLATION_FREE, moduleObject.getBoolean(INSTALLATION_FREE).toString()); - moduleObject.put(INSTALLATION_FREE, Boolean.parseBoolean(utility.getInstallationFree())); + util.setOriginInstallationFree(moduleObject.getBoolean(INSTALLATION_FREE)); + util.setIsInstallationFree(true); + moduleObject.put(INSTALLATION_FREE, utility.getDeliveryWithInstall()); } if (utility.getGeneralNormalizeList().contains(DELIVERY_WITH_INSTALL)) { - outPutMap.put(DELIVERY_WITH_INSTALL, moduleObject.getBoolean(DELIVERY_WITH_INSTALL).toString()); - moduleObject.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); + util.setOriginDeliveryWithInstall(moduleObject.getBoolean(DELIVERY_WITH_INSTALL)); + util.setIsDeliveryWithInstall(true); + moduleObject.put(DELIVERY_WITH_INSTALL, utility.getInstallationFree()); } writeJson(jsonFilePath, jsonObject); } catch (IOException e) { @@ -3973,12 +4047,12 @@ public class Compressor { e.getMessage())); throw new BundleException("Parse and modify module.json exist IOException: " + e.getMessage()); } - return outPutMap; + return util; } - private HashMap parseAndModifyGeneralConfigJson(String jsonFilePath, Utility utility, String[] name) + private GeneralNormalizeUtil parseAndModifyGeneralConfigJson(String jsonFilePath, Utility utility, String[] name) throws BundleException { - HashMap outPutMap = new HashMap<>(); + GeneralNormalizeUtil util = new GeneralNormalizeUtil(); try (FileInputStream jsonStream = new FileInputStream(jsonFilePath)) { JSONObject jsonObject = JSON.parseObject(jsonStream, JSONObject.class); if (!jsonObject.containsKey(APP)) { @@ -4027,73 +4101,74 @@ public class Compressor { } name[0] = appObject.getString(BUNDLE_NAME); name[1] = distroObj.getString(MODULE_NAME_NEW); - outPutMap.put(MODULE_NAME_NEW, distroObj.getString(MODULE_NAME_NEW)); + util.setModuleName(distroObj.getString(MODULE_NAME_NEW)); if (utility.getGeneralNormalizeList().contains(DEVICE_TYPES)) { - outPutMap.put(DEVICE_TYPE, getJsonString(moduleObject, DEVICE_TYPE)); + util.setOriginDeviceTypes(getJsonString(moduleObject, DEVICE_TYPES)); moduleObject.put(DEVICE_TYPE, utility.getDeviceTypes().split(",")); } if (utility.getGeneralNormalizeList().contains(VERSION_CODE)) { - outPutMap.put(CODE, String.valueOf(versionObj.getIntValue(CODE))); + util.setOriginVersionCode(versionObj.getIntValue(CODE)); versionObj.put(CODE, utility.getVersionCode()); } if (utility.getGeneralNormalizeList().contains(VERSION_NAME)) { - outPutMap.put(NAME, versionObj.getString(NAME)); + util.setOriginVersionName(versionObj.getString(NAME)); versionObj.put(NAME, utility.getVersionName()); } if (utility.getGeneralNormalizeList().contains(MIN_COMPATIBLE_VERSION_CODE)) { if (versionObj.containsKey(MIN_COMPATIBLE_VERSION_CODE)) { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf( - versionObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE))); + util.setOriginMinCompatibleVersionCode(versionObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE)); } else { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf(versionObj.getIntValue(CODE))); + util.setOriginMinCompatibleVersionCode(versionObj.getIntValue(CODE)); } versionObj.put(MIN_COMPATIBLE_VERSION_CODE, utility.getMinCompatibleVersionCode()); } if (utility.getGeneralNormalizeList().contains(BUNDLE_NAME)) { - outPutMap.put(BUNDLE_NAME, appObject.getString(BUNDLE_NAME)); + util.setOriginBundleName(appObject.getString(BUNDLE_NAME)); appObject.put(BUNDLE_NAME, utility.getBundleName()); } if (utility.getGeneralNormalizeList().contains(MIN_API_VERSION)) { - outPutMap.put(COMPATIBLE, String.valueOf(apiVersionObj.getIntValue(COMPATIBLE))); + util.setOriginMinAPIVersion(apiVersionObj.getIntValue(COMPATIBLE)); apiVersionObj.put(COMPATIBLE, utility.getMinAPIVersion()); } if (utility.getGeneralNormalizeList().contains(TARGET_API_VERSION)) { - outPutMap.put(TARGET, String.valueOf(apiVersionObj.getIntValue(TARGET))); + util.setOriginTargetAPIVersion(apiVersionObj.getIntValue(TARGET)); apiVersionObj.put(TARGET, utility.getTargetAPIVersion()); } if (utility.getGeneralNormalizeList().contains(API_RELEASE_TYPE)) { - outPutMap.put(RELEASE_TYPE, apiVersionObj.getString(RELEASE_TYPE)); + util.setOriginApiReleaseType(apiVersionObj.getString(RELEASE_TYPE)); apiVersionObj.put(RELEASE_TYPE, utility.getApiReleaseType()); } if (utility.getGeneralNormalizeList().contains(BUNDLE_TYPE)) { if (!appObject.containsKey(BUNDLE_TYPE)) { if (distroObj.getBoolean(INSTALLATION_FREE)) { - outPutMap.put(BUNDLE_TYPE, ATOMIC_SERVICE); + util.setOriginBundleType(ATOMIC_SERVICE); } else { - outPutMap.put(BUNDLE_TYPE, APP); + util.setOriginBundleType(APP); } } else { - outPutMap.put(BUNDLE_TYPE, appObject.getString(BUNDLE_TYPE)); + util.setOriginBundleType(appObject.getString(BUNDLE_TYPE)); } appObject.put(BUNDLE_TYPE, utility.getBundleType()); } if (utility.getGeneralNormalizeList().contains(INSTALLATION_FREE)) { - outPutMap.put(INSTALLATION_FREE, distroObj.getBoolean(INSTALLATION_FREE).toString()); - distroObj.put(INSTALLATION_FREE, Boolean.parseBoolean(utility.getInstallationFree())); + util.setOriginInstallationFree(distroObj.getBoolean(INSTALLATION_FREE)); + util.setIsInstallationFree(true); + distroObj.put(INSTALLATION_FREE, utility.getDeliveryWithInstall()); } if (utility.getGeneralNormalizeList().contains(DELIVERY_WITH_INSTALL)) { - outPutMap.put(DELIVERY_WITH_INSTALL, distroObj.getBoolean(DELIVERY_WITH_INSTALL).toString()); - distroObj.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); + util.setOriginDeliveryWithInstall(distroObj.getBoolean(DELIVERY_WITH_INSTALL)); + util.setIsDeliveryWithInstall(true); + distroObj.put(DELIVERY_WITH_INSTALL, utility.getInstallationFree()); } writeJson(jsonFilePath, jsonObject); } catch (IOException e) { @@ -4101,7 +4176,7 @@ public class Compressor { e.getMessage())); throw new BundleException("Parse and modify config.json exist IOException: " + e.getMessage()); } - return outPutMap; + return util; } private void parseAndModifyGeneralPackInfo(String packInfoPath, Utility utility) @@ -4247,13 +4322,55 @@ public class Compressor { } } - private static void writeGeneralRecord(List> recordList, String outPath) { + private static void writeGeneralRecord(List utils, String outPath) { + JSONArray jsonArray = new JSONArray(); try (FileWriter fileWriter = new FileWriter(outPath + LINUX_FILE_SEPARATOR + GENERAL_RECORD)) { - for (HashMap record : recordList) { - String jsonString = JSON.toJSONString(record); - fileWriter.write(jsonString); - fileWriter.write(","); + for (GeneralNormalizeUtil util : utils) { + JSONObject jsonObject = new JSONObject(); + if (util.originDeviceTypes != null && !util.originDeviceTypes.isEmpty()) { + jsonObject.put("deviceTypes", util.originDeviceTypes); + if (util.originDeviceTypes instanceof String) { + String arrayStr = (String) util.originDeviceTypes; + JSONArray deviceTypes = JSON.parseArray(arrayStr); + jsonObject.put("deviceTypes", deviceTypes); + } + } + if (util.originVersionCode != INVALID_VERSION) { + jsonObject.put("versionCode", util.originVersionCode); + } + if (util.moduleName != null && !util.moduleName.isEmpty()) { + jsonObject.put("moduleName", util.moduleName); + } + if (util.originVersionName != null && !util.originVersionName.isEmpty()) { + jsonObject.put("versionName", util.originVersionName); + } + if (util.originMinCompatibleVersionCode != INVALID_VERSION) { + jsonObject.put("minCompatibleVersionCode", util.originMinCompatibleVersionCode); + } + if (util.originMinAPIVersion != INVALID_VERSION) { + jsonObject.put("minAPIVersion", util.originMinAPIVersion); + } + if (util.originTargetAPIVersion != INVALID_VERSION) { + jsonObject.put("targetAPIVersion", util.originTargetAPIVersion); + } + if (util.originApiReleaseType!= null && !util.originApiReleaseType.isEmpty()) { + jsonObject.put("apiReleaseType", util.originApiReleaseType); + } + if (util.originBundleType != null && !util.originBundleType.isEmpty()) { + jsonObject.put("bundleType", util.originBundleType); + } + if (util.originBundleName != null && !util.originBundleName.isEmpty()) { + jsonObject.put("bundleName", util.originBundleName); + } + if (util.isInstallationFree == true) { + jsonObject.put("installationFree", util.originInstallationFree); + } + if (util.isDeliveryWithInstall == true) { + jsonObject.put("deliveryWithInstall", util.originDeliveryWithInstall); + } + jsonArray.add(jsonObject); } + fileWriter.write(jsonArray.toString()); } catch (IOException e) { LOG.error(PackingToolErrMsg.IO_EXCEPTION.toString("Write general record exist IOException: " + e.getMessage())); diff --git a/packing_tool/frameworks/BUILD.gn b/packing_tool/frameworks/BUILD.gn index 0094eb897bf2be14a22591f98704f1a39d6ac573..5f7f2dcb876409333cd490f012faf9d1933a503e 100644 --- a/packing_tool/frameworks/BUILD.gn +++ b/packing_tool/frameworks/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2024 Huawei Device Co., Ltd. +# Copyright (c) 2024-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -50,6 +50,7 @@ ohos_executable("ohos_packing_tool") { "src/hqf_verify.cpp", "src/hsp_packager.cpp", "src/json/distro_filter.cpp", + "src/json/general_normalize_version_utils.cpp", "src/json/hap_verify_info.cpp", "src/json/hap_verify_utils.cpp", "src/json/json_utils.cpp", diff --git a/packing_tool/frameworks/include/general_normalize.h b/packing_tool/frameworks/include/general_normalize.h index 971e2ac8a03d2990c87f7ca7f35a6b320dd1de57..5308f0067ce604b70d39a09133a0167d99a4f44d 100644 --- a/packing_tool/frameworks/include/general_normalize.h +++ b/packing_tool/frameworks/include/general_normalize.h @@ -16,6 +16,7 @@ #ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_H #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_H +#include "general_normalize_version.h" #include "json/module_json.h" #include "packager.h" #include "unzip_wrapper.h" @@ -36,15 +37,14 @@ protected: int32_t PostProcess() override; private: - bool ModifyModuleJson(const std::string &moduleJsonPath, std::map &modifyMap, + bool ModifyModuleJson(const std::string &moduleJsonPath, GeneralNormalizeVersion &generalNormalizeVersion, std::string &bundleName, std::string &moduleName); - bool ModifyConfigJson(const std::string &moduleJsonPath, std::map &modifyMap, + bool ModifyConfigJson(const std::string &moduleJsonPath, GeneralNormalizeVersion &generalNormalizeVersion, std::string &bundleName, std::string &moduleName); bool ModifyPackInfo(const std::string &packInfoPath); - bool ProcessJsonFiles(const std::string &tempPath, std::list> &modifyMapList, + bool ProcessJsonFiles(const std::string &tempPath, std::list &generalNormalizeVersions, std::string &bundleName, std::string &moduleName); bool CompressDirToHap(const std::string &tempDir, const std::string &modifiedHapPath); - std::string ConvertMapListToString(const std::list>& modifyMapList); ZipWrapper zipWrapper_; UnzipWrapper unzipWrapper_; std::list hspOrhapList_; diff --git a/packing_tool/frameworks/include/json/general_normalize_version.h b/packing_tool/frameworks/include/json/general_normalize_version.h new file mode 100644 index 0000000000000000000000000000000000000000..0ae2d9c8fe7a7889f8d9b6048fc02d4bf5e9899a --- /dev/null +++ b/packing_tool/frameworks/include/json/general_normalize_version.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_H + +#include +#include + +namespace OHOS { +namespace AppPackingTool { +struct GeneralNormalizeVersion { + bool originInstallationFree = false; + bool originDeliveryWithInstall = false; + bool modifyInstallationFree = false; + bool modifyDeliveryWithInstall = false; + int32_t originMinCompatibleVersionCode = -1; + int32_t originMinAPIVersion = -1; + int32_t originTargetAPIVersion = -1; + int32_t originVersionCode = -1; + std::string originBundleName = ""; + std::string originVersionName = ""; + std::string originApiReleaseType = ""; + std::string originBundleType = ""; + std::string moduleName = ""; + std::list originDeviceTypes = {}; +}; +} // namespace AppPackingTool +} // namespace OHOS +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_H diff --git a/packing_tool/frameworks/include/json/general_normalize_version_utils.h b/packing_tool/frameworks/include/json/general_normalize_version_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..82742e64bc055ab7b6fbb3ae60ebbdf4ac986b3b --- /dev/null +++ b/packing_tool/frameworks/include/json/general_normalize_version_utils.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_UTILS_H +#define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_UTILS_H + +#include +#include + +#include "general_normalize_version.h" + +namespace OHOS { +namespace AppPackingTool { +class GeneralNormalizeVersionUtils { +public: + GeneralNormalizeVersionUtils() {}; + virtual ~GeneralNormalizeVersionUtils() {}; + + GeneralNormalizeVersionUtils(const GeneralNormalizeVersionUtils &) = delete; + GeneralNormalizeVersionUtils &operator=(const GeneralNormalizeVersionUtils &) = delete; + + static std::string ArrayToString(const std::list& generalNormalizeVersions); +}; +} // namespace AppPackingTool +} // namespace OHOS +#endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_GENERAL_NORMALIZE_VERSION_UTILS_H diff --git a/packing_tool/frameworks/include/utils.h b/packing_tool/frameworks/include/utils.h index 33ef76ce3399ee7de2833340a4b9e604a2fe6450..57c85de6dd30089306e539b8fe9bcbbf8b60a823 100644 --- a/packing_tool/frameworks/include/utils.h +++ b/packing_tool/frameworks/include/utils.h @@ -74,8 +74,6 @@ public: static bool GetRealPath(const std::string& path, std::string& realPath); static bool GetRealPathOfNoneExistFile(const std::string& path, std::string& realPath); static bool RemoveAllFilesInDirectory(const std::string& directoryPath); - static std::string ArrayToString(const std::list &array); - static std::string BoolToString(bool value); }; } // namespace AppPackingTool } // namespace OHOS diff --git a/packing_tool/frameworks/src/general_normalize.cpp b/packing_tool/frameworks/src/general_normalize.cpp index 78f483afea07e60afe801ba997fb552cc32ab7b2..6f8f2ddc6958af4830c8b2d530ec925d2136e964 100644 --- a/packing_tool/frameworks/src/general_normalize.cpp +++ b/packing_tool/frameworks/src/general_normalize.cpp @@ -19,8 +19,8 @@ #include "hqf_packager.h" #include "hsp_packager.h" #include "json/json_utils.h" +#include "json/general_normalize_version_utils.h" #include "json/module_json.h" -#include "json/normalize_version_utils.h" #include "json/pack_info.h" #include "log.h" #include "utils.h" @@ -178,7 +178,7 @@ int32_t GeneralNormalize::PreProcess() } bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, - std::map &modifyMap, std::string &bundleName, std::string &moduleName) + GeneralNormalizeVersion &generalNormalizeVersion, std::string &bundleName, std::string &moduleName) { ModuleJson moduleJson; if (!moduleJson.ParseFromFile(moduleJsonPath)) { @@ -190,7 +190,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, int32_t versionCode = std::stoi(parameterMap_.at(Constants::PARAM_VERSION_CODE)); Version version; moduleJson.GetStageVersion(version); - modifyMap[VERSION_CODE] = std::to_string(version.versionCode); + generalNormalizeVersion.originVersionCode = version.versionCode; if (!moduleJson.SetVersionCode(versionCode, true)) { LOGE("SetVersionCode failed"); return false; @@ -202,7 +202,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, std::string versionName = parameterMap_.at(Constants::PARAM_VERSION_NAME); Version version; moduleJson.GetStageVersion(version); - modifyMap[VERSION_NAME] = version.versionName; + generalNormalizeVersion.originVersionName = version.versionName; if (!moduleJson.SetVersionName(versionName, true)) { LOGE("SetVersionName failed"); return false; @@ -214,7 +214,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, std::string targetBundleName = parameterMap_.at(Constants::PARAM_BUNDLE_NAME); std::string originBundleName; moduleJson.GetBundleName(originBundleName); - modifyMap[BUNDLE_NAME] = originBundleName; + generalNormalizeVersion.originBundleName = originBundleName; if (!moduleJson.SetBundleName(targetBundleName, true)) { LOGE("SetBundleName failed"); return false; @@ -225,7 +225,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, if (it != parameterMap_.end()) { Version version; moduleJson.GetStageVersion(version); - modifyMap[MIN_COMPATIBLE_VERSION_CODE] = std::to_string(version.minCompatibleVersionCode); + generalNormalizeVersion.originMinCompatibleVersionCode = version.minCompatibleVersionCode; int32_t minCompatibleVersionCode = std::stoi(parameterMap_.at(Constants::PARAM_MIN_COMPATIBLE_VERSION_CODE)); if (!moduleJson.SetMinCompatibleVersionCode(minCompatibleVersionCode, true)) { LOGE("SetMinCompatibleVersionCode failed"); @@ -238,7 +238,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, int32_t minAPIVersion = std::stoi(parameterMap_.at(Constants::PARAM_MIN_API_VERSION)); int32_t originMinAPIVersion = -1; moduleJson.GetMinApiVersion(originMinAPIVersion); - modifyMap[MIN_API_VERSION] = std::to_string(originMinAPIVersion); + generalNormalizeVersion.originMinAPIVersion = originMinAPIVersion; if (!moduleJson.SetMinAPIVersion(minAPIVersion, true)) { LOGE("SetMinAPIVersion failed"); return false; @@ -250,7 +250,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, int32_t targetAPIVersion = std::stoi(parameterMap_.at(Constants::PARAM_TARGET_API_VERSION)); int32_t originTargetAPIVersion = -1; moduleJson.GetTargetApiVersion(originTargetAPIVersion); - modifyMap[TARGET_API_VERSION] = std::to_string(originTargetAPIVersion); + generalNormalizeVersion.originTargetAPIVersion = originTargetAPIVersion; if (!moduleJson.SetTargetAPIVersion(targetAPIVersion, true)) { LOGE("SetTargetAPIVersion failed"); return false; @@ -262,7 +262,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, std::string apiReleaseType = parameterMap_.at(Constants::PARAM_API_RELEASE_TYPE); std::string originApiReleaseType; moduleJson.GetStageApiReleaseType(originApiReleaseType); - modifyMap[API_RELEASE_TYPE] = originApiReleaseType; + generalNormalizeVersion.originApiReleaseType = originApiReleaseType; if (!moduleJson.SetApiReleaseType(apiReleaseType, true)) { LOGE("SetApiReleaseType failed"); return false; @@ -274,7 +274,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, std::string bundleType = parameterMap_.at(Constants::PARAM_BUNDLE_TYPE); std::string originBundleType; moduleJson.GetStageBundleType(originBundleType); - modifyMap[BUNDLE_TYPE] = originBundleType; + generalNormalizeVersion.originBundleType = originBundleType; if (!moduleJson.SetBundleType(bundleType, true)) { LOGE("SetBundleType failed"); return false; @@ -286,7 +286,8 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, bool installationFree = Utils::StringToBool(parameterMap_.at(Constants::PARAM_INSTALLATION_FREE)); bool originInstallationFree; moduleJson.GetStageInstallationFree(originInstallationFree); - modifyMap[INSTALLATION_FREE] = Utils::BoolToString(originInstallationFree); + generalNormalizeVersion.originInstallationFree = originInstallationFree; + generalNormalizeVersion.modifyInstallationFree = true; if (!moduleJson.SetInstallationFree(installationFree, true)) { LOGE("SetInstallationFree failed"); return false; @@ -298,7 +299,8 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, bool deliveryWithInstall = Utils::StringToBool(parameterMap_.at(Constants::PARAM_DELIVERY_WITH_INSTALL)); bool originDeliveryWithInstall; moduleJson.GetDeliveryWithInstall(originDeliveryWithInstall); - modifyMap[DELIVERY_WITH_INSTALL] = Utils::BoolToString(originDeliveryWithInstall); + generalNormalizeVersion.originDeliveryWithInstall = originDeliveryWithInstall; + generalNormalizeVersion.modifyDeliveryWithInstall = true; if (!moduleJson.SetDeliveryWithInstall(deliveryWithInstall, true)) { LOGE("SetDeliveryWithInstall failed"); return false; @@ -311,7 +313,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, Utils::StringToArray(parameterMap_.at(Constants::PARAM_DEVICE_TYPES), deviceTypes); std::list originDeviceTypes; moduleJson.GetStageDeviceTypes(originDeviceTypes); - modifyMap[DEVICE_TYPES] = Utils::ArrayToString(originDeviceTypes); + generalNormalizeVersion.originDeviceTypes = originDeviceTypes; if (!moduleJson.SetDeviceTypes(deviceTypes, true)) { LOGE("SetDeviceTypes failed"); return false; @@ -320,7 +322,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, moduleJson.GetBundleName(bundleName); moduleJson.GetModuleName(moduleName); - modifyMap[MODULE_NAME] = moduleName; + generalNormalizeVersion.moduleName = moduleName; if (!JsonUtils::StrToFile(moduleJson.ToString(), moduleJsonPath)) { LOGE("Parse and modify module.json failed, write Json failed."); return false; @@ -329,7 +331,7 @@ bool GeneralNormalize::ModifyModuleJson(const std::string &moduleJsonPath, } bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, - std::map &modifyMap, std::string &bundleName, std::string &moduleName) + GeneralNormalizeVersion &generalNormalizeVersion, std::string &bundleName, std::string &moduleName) { ModuleJson configJson; if (!configJson.ParseFromFile(configJsonPath)) { @@ -340,7 +342,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { Version version; configJson.GetFaVersion(version); - modifyMap[VERSION_CODE] = std::to_string(version.versionCode); + generalNormalizeVersion.originVersionCode = version.versionCode; int32_t versionCode = std::stoi(parameterMap_.at(Constants::PARAM_VERSION_CODE)); if (!configJson.SetVersionCode(versionCode, false)) { LOGE("SetVersionCode failed"); @@ -352,7 +354,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { Version version; configJson.GetFaVersion(version); - modifyMap[VERSION_NAME] = version.versionName; + generalNormalizeVersion.originVersionName = version.versionName; std::string versionName = parameterMap_.at(Constants::PARAM_VERSION_NAME); if (!configJson.SetVersionName(versionName, false)) { LOGE("SetVersionName failed"); @@ -364,7 +366,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { std::string originBundleName; configJson.GetBundleName(originBundleName); - modifyMap[BUNDLE_NAME] = originBundleName; + generalNormalizeVersion.originBundleName = originBundleName; std::string targetBundleName = parameterMap_.at(Constants::PARAM_BUNDLE_NAME); if (!configJson.SetBundleName(targetBundleName, false)) { LOGE("SetBundleName failed"); @@ -376,7 +378,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { Version version; configJson.GetFaVersion(version); - modifyMap[MIN_COMPATIBLE_VERSION_CODE] = std::to_string(version.minCompatibleVersionCode); + generalNormalizeVersion.originMinCompatibleVersionCode = version.minCompatibleVersionCode; int32_t minCompatibleVersionCode = std::stoi(parameterMap_.at(Constants::PARAM_MIN_COMPATIBLE_VERSION_CODE)); if (!configJson.SetMinCompatibleVersionCode(minCompatibleVersionCode, false)) { LOGE("SetMinCompatibleVersionCode failed"); @@ -388,7 +390,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { ModuleApiVersion moduleApiVersion; configJson.GetFaModuleApiVersion(moduleApiVersion); - modifyMap[MIN_API_VERSION] = std::to_string(moduleApiVersion.compatibleApiVersion); + generalNormalizeVersion.originMinAPIVersion = moduleApiVersion.compatibleApiVersion; int32_t minAPIVersion = std::stoi(parameterMap_.at(Constants::PARAM_MIN_API_VERSION)); if (!configJson.SetMinAPIVersion(minAPIVersion, false)) { LOGE("SetMinAPIVersion failed"); @@ -400,7 +402,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { ModuleApiVersion moduleApiVersion; configJson.GetFaModuleApiVersion(moduleApiVersion); - modifyMap[TARGET_API_VERSION] = std::to_string(moduleApiVersion.targetApiVersion); + generalNormalizeVersion.originTargetAPIVersion = moduleApiVersion.targetApiVersion; int32_t targetAPIVersion = std::stoi(parameterMap_.at(Constants::PARAM_TARGET_API_VERSION)); if (!configJson.SetTargetAPIVersion(targetAPIVersion, false)) { LOGE("SetTargetAPIVersion failed"); @@ -412,7 +414,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { ModuleApiVersion moduleApiVersion; configJson.GetFaModuleApiVersion(moduleApiVersion); - modifyMap[API_RELEASE_TYPE] = moduleApiVersion.releaseType; + generalNormalizeVersion.originApiReleaseType = moduleApiVersion.releaseType; std::string apiReleaseType = parameterMap_.at(Constants::PARAM_API_RELEASE_TYPE); if (!configJson.SetApiReleaseType(apiReleaseType, false)) { LOGE("SetApiReleaseType failed"); @@ -424,7 +426,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { std::string originBundleType; configJson.GetFaBundleType(originBundleType); - modifyMap[BUNDLE_TYPE] = originBundleType; + generalNormalizeVersion.originBundleType = originBundleType; std::string bundleType = parameterMap_.at(Constants::PARAM_BUNDLE_TYPE); if (!configJson.SetBundleType(bundleType, false)) { LOGE("SetBundleType failed"); @@ -436,7 +438,8 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { bool originInstallationFree; configJson.GetFaInstallationFree(originInstallationFree); - modifyMap[INSTALLATION_FREE] = Utils::BoolToString(originInstallationFree); + generalNormalizeVersion.originInstallationFree = originInstallationFree; + generalNormalizeVersion.modifyInstallationFree = true; bool installationFree = Utils::StringToBool(parameterMap_.at(Constants::PARAM_INSTALLATION_FREE)); if (!configJson.SetInstallationFree(installationFree, false)) { LOGE("SetInstallationFree failed"); @@ -448,7 +451,8 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { bool originDeliveryWithInstall; configJson.GetFaDeliveryWithInstall(originDeliveryWithInstall); - modifyMap[DELIVERY_WITH_INSTALL] = Utils::BoolToString(originDeliveryWithInstall); + generalNormalizeVersion.originDeliveryWithInstall = originDeliveryWithInstall; + generalNormalizeVersion.modifyDeliveryWithInstall = true; bool deliveryWithInstall = Utils::StringToBool(parameterMap_.at(Constants::PARAM_DELIVERY_WITH_INSTALL)); if (!configJson.SetDeliveryWithInstall(deliveryWithInstall, false)) { LOGE("SetDeliveryWithInstall failed"); @@ -460,7 +464,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, if (it != parameterMap_.end()) { std::list originDeviceTypes; configJson.GetFaDeviceTypes(originDeviceTypes); - modifyMap[DEVICE_TYPES] = Utils::ArrayToString(originDeviceTypes); + generalNormalizeVersion.originDeviceTypes = originDeviceTypes; std::list deviceTypes; Utils::StringToArray(parameterMap_.at(Constants::PARAM_DEVICE_TYPES), deviceTypes); if (!configJson.SetDeviceTypes(deviceTypes, false)) { @@ -471,7 +475,7 @@ bool GeneralNormalize::ModifyConfigJson(const std::string &configJsonPath, configJson.GetBundleName(bundleName); configJson.GetFaModuleName(moduleName); - modifyMap[MODULE_NAME] = moduleName; + generalNormalizeVersion.moduleName = moduleName; if (!JsonUtils::StrToFile(configJson.ToString(), configJsonPath)) { LOGE("Parse and modify config.json failed, writeJson failed."); return false; @@ -633,6 +637,8 @@ bool GeneralNormalize::CompressDirToHap(const std::string &sourceDir, const std: parameterMap[Constants::PARAM_PACK_INFO_PATH] = filePath; } else if (fileName == Constants::RPCID_SC) { parameterMap[Constants::PARAM_RPCID_PATH] = filePath; + } else if (fileName == Constants::PKG_CONTEXT_JSON) { + parameterMap[Constants::PARAM_PKG_CONTEXT_PATH] = filePath; } } if (Utils::EndsWith(zipFilePath, Constants::HAP_SUFFIX)) { @@ -649,9 +655,9 @@ bool GeneralNormalize::CompressDirToHap(const std::string &sourceDir, const std: bool GeneralNormalize::ProcessJsonFiles(const std::string &tempPath, - std::list> &modifyMapList, std::string &bundleName, std::string &moduleName) + std::list &generalNormalizeVersions, std::string &bundleName, std::string &moduleName) { - std::map modifyMap; + GeneralNormalizeVersion generalNormalizeVersion; std::string moduleJsonPath = tempPath + Constants::LINUX_FILE_SEPARATOR + Constants::MODULE_JSON; std::string configJsonPath = tempPath + Constants::LINUX_FILE_SEPARATOR + Constants::CONFIG_JSON; std::string packInfoPath = tempPath + Constants::LINUX_FILE_SEPARATOR + Constants::PACK_INFO; @@ -666,23 +672,21 @@ bool GeneralNormalize::ProcessJsonFiles(const std::string &tempPath, bool modifyJsonSuccess = false; if (isModuleFileExists) { - modifyJsonSuccess = ModifyModuleJson(moduleJsonPath, modifyMap, bundleName, moduleName); + modifyJsonSuccess = ModifyModuleJson(moduleJsonPath, generalNormalizeVersion, bundleName, moduleName); } else { - modifyJsonSuccess = ModifyConfigJson(configJsonPath, modifyMap, bundleName, moduleName); + modifyJsonSuccess = ModifyConfigJson(configJsonPath, generalNormalizeVersion, bundleName, moduleName); } if (!modifyJsonSuccess) { - modifyMapList.push_back(modifyMap); return false; } if (isPackInfoFileExists) { if (!ModifyPackInfo(packInfoPath)) { - modifyMapList.push_back(modifyMap); return false; } } - modifyMapList.push_back(modifyMap); + generalNormalizeVersions.push_back(generalNormalizeVersion); return true; } @@ -691,13 +695,13 @@ int32_t GeneralNormalize::Process() std::string outPath = parameterMap_.at(Constants::PARAM_OUT_PATH); std::string tempPath = outPath + Constants::LINUX_FILE_SEPARATOR + Constants::COMPRESSOR_GENERALNORMALIZE_TEMP_DIR + Utils::GenerateUUID(); - std::list> modifyMapList; + std::list generalNormalizeVersionList; bool isSuccess = true; std::string bundleName; std::string moduleName; for (const std::string& path : hspOrhapList_) { ZipUtils::Unzip(path, tempPath); - if (!ProcessJsonFiles(tempPath, modifyMapList, bundleName, moduleName)) { + if (!ProcessJsonFiles(tempPath, generalNormalizeVersionList, bundleName, moduleName)) { Utils::ForceRemoveDirectory(tempPath); isSuccess = false; break; @@ -715,7 +719,7 @@ int32_t GeneralNormalize::Process() Utils::RemoveAllFilesInDirectory(outPath); return ERR_INVALID_VALUE; } - if (!JsonUtils::StrToFile(ConvertMapListToString(modifyMapList), outPath + + if (!JsonUtils::StrToFile(GeneralNormalizeVersionUtils::ArrayToString(generalNormalizeVersionList), outPath + Constants::LINUX_FILE_SEPARATOR + Constants::GENERAL_RECORD)) { LOGE("WriteVersionRecord failed."); return ERR_INVALID_VALUE; @@ -723,22 +727,6 @@ int32_t GeneralNormalize::Process() return ERR_OK; } -std::string GeneralNormalize::ConvertMapListToString(const std::list>& - modifyMapList) -{ - std::unique_ptr jArray = PtJson::CreateArray(); - for (const auto& map : modifyMapList) { - std::unique_ptr jMap = PtJson::CreateObject(); - for (const auto& pair : map) { - if (!jMap->Add(pair.first.c_str(), pair.second.c_str())) { - return ""; - } - } - jArray->Push(jMap); - } - return jArray->Stringify(); -} - int32_t GeneralNormalize::PostProcess() { return ERR_OK; diff --git a/packing_tool/frameworks/src/json/general_normalize_version_utils.cpp b/packing_tool/frameworks/src/json/general_normalize_version_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6df8cfb4dd410fbf58c3005a4fce899e1601d436 --- /dev/null +++ b/packing_tool/frameworks/src/json/general_normalize_version_utils.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "general_normalize_version_utils.h" +#include "general_normalize_version.h" + +#include "log.h" +#include "pt_json.h" + +namespace OHOS { +namespace AppPackingTool { +namespace { +const std::string DEVICE_TYPES = "deviceTypes"; +const std::string BUNDLE_NAME = "bundleName"; +const std::string VERSION_CODE = "versionCode"; +const std::string VERSION_NAME = "versionName"; +const std::string MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode"; +const std::string MIN_API_VERSION = "minAPIVersion"; +const std::string TARGET_API_VERSION = "targetAPIVersion"; +const std::string API_RELEASE_TYPE = "apiReleaseType"; +const std::string BUNDLE_TYPE = "bundleType"; +const std::string INSTALLATION_FREE = "installationFree"; +const std::string DELIVERY_WITH_INSTALL = "deliveryWithInstall"; +const std::string MODULE_NAME = "moduleName"; +} + +std::string GeneralNormalizeVersionUtils::ArrayToString(const std::list& generalNormalizeVersions) +{ + std::unique_ptr versionsJson = PtJson::CreateArray(); + for (auto& generalNormalizeVersion : generalNormalizeVersions) { + std::unique_ptr ptJson = PtJson::CreateObject(); + if (generalNormalizeVersion.moduleName != "") { + if (!ptJson->Add(MODULE_NAME.c_str(), generalNormalizeVersion.moduleName.c_str())) { + return ""; + } + } + if (!generalNormalizeVersion.originDeviceTypes.empty()) { + if (!ptJson->Add(DEVICE_TYPES.c_str(), generalNormalizeVersion.originDeviceTypes)) { + return ""; + } + } + if (generalNormalizeVersion.originBundleName != "") { + if (!ptJson->Add(BUNDLE_NAME.c_str(), generalNormalizeVersion.originBundleName.c_str())) { + return ""; + } + } + if (generalNormalizeVersion.originVersionCode != -1) { + if (!ptJson->Add(VERSION_CODE.c_str(), generalNormalizeVersion.originVersionCode)) { + return ""; + } + } + if (generalNormalizeVersion.originVersionName != "") { + if (!ptJson->Add(VERSION_NAME.c_str(), generalNormalizeVersion.originVersionName.c_str())) { + return ""; + } + } + if (generalNormalizeVersion.originMinCompatibleVersionCode != -1) { + if (!ptJson->Add(MIN_COMPATIBLE_VERSION_CODE.c_str(), + generalNormalizeVersion.originMinCompatibleVersionCode)) { + return ""; + } + } + if (generalNormalizeVersion.originMinAPIVersion != -1) { + if (!ptJson->Add(MIN_API_VERSION.c_str(), generalNormalizeVersion.originMinAPIVersion)) { + return ""; + } + } + if (generalNormalizeVersion.originTargetAPIVersion != -1) { + if (!ptJson->Add(TARGET_API_VERSION.c_str(), generalNormalizeVersion.originTargetAPIVersion)) { + return ""; + } + } + if (generalNormalizeVersion.originApiReleaseType != "") { + if (!ptJson->Add(API_RELEASE_TYPE.c_str(), generalNormalizeVersion.originApiReleaseType.c_str())) { + return ""; + } + } + if (generalNormalizeVersion.originBundleType != "") { + if (!ptJson->Add(BUNDLE_TYPE.c_str(), generalNormalizeVersion.originBundleType.c_str())) { + return ""; + } + } + if (generalNormalizeVersion.modifyInstallationFree == true) { + if (!ptJson->Add(INSTALLATION_FREE.c_str(), generalNormalizeVersion.originInstallationFree)) { + return ""; + } + } + if (generalNormalizeVersion.modifyDeliveryWithInstall == true) { + if (!ptJson->Add(DELIVERY_WITH_INSTALL.c_str(), generalNormalizeVersion.originDeliveryWithInstall)) { + return ""; + } + } + versionsJson->Push(ptJson); + } + return versionsJson->Stringify(); +} +} // namespace AppPackingTool +} // namespace OHOS diff --git a/packing_tool/frameworks/src/utils.cpp b/packing_tool/frameworks/src/utils.cpp index d67067959f4778bdfb329ddef3d7e04ca86b9146..ab23efee18635c07cb9ac2e668752e58499f03d7 100644 --- a/packing_tool/frameworks/src/utils.cpp +++ b/packing_tool/frameworks/src/utils.cpp @@ -496,18 +496,5 @@ bool Utils::RemoveAllFilesInDirectory(const std::string& directoryPath) return true; } -std::string Utils::ArrayToString(const std::list &array) -{ - std::unique_ptr jArray = PtJson::CreateArray(); - for (const auto& item : array) { - jArray->Push(item.c_str()); - } - return jArray->Stringify(); -} - -std::string Utils::BoolToString(bool value) -{ - return value ? "true" : "false"; -} } // namespace AppPackingTool } // namespace OHOS diff --git a/packing_tool/frameworks/src/version_normalize.cpp b/packing_tool/frameworks/src/version_normalize.cpp index 850923d4710f8f8aae071a9fc4e43d0ba4c3d47e..9db0d24081b4f9ba612956e49aebc07087229c6c 100644 --- a/packing_tool/frameworks/src/version_normalize.cpp +++ b/packing_tool/frameworks/src/version_normalize.cpp @@ -204,6 +204,8 @@ bool VersionNormalize::CompressDirToHap(const std::string &sourceDir, const std: parameterMap[Constants::PARAM_PACK_INFO_PATH] = filePath; } else if (fileName == Constants::RPCID_SC) { parameterMap[Constants::PARAM_RPCID_PATH] = filePath; + } else if (fileName == Constants::PKG_CONTEXT_JSON) { + parameterMap[Constants::PARAM_PKG_CONTEXT_PATH] = filePath; } } if (Utils::EndsWith(zipFilePath, Constants::HAP_SUFFIX)) { diff --git a/packing_tool/frameworks/test/unittest/shell_command_test/BUILD.gn b/packing_tool/frameworks/test/unittest/shell_command_test/BUILD.gn index c9997a1460fd13f3c868ab5a5f6c28f64bface2c..80b45a1656bdb0e81a98708de5aa0126e9032113 100644 --- a/packing_tool/frameworks/test/unittest/shell_command_test/BUILD.gn +++ b/packing_tool/frameworks/test/unittest/shell_command_test/BUILD.gn @@ -40,6 +40,7 @@ ohos_unittest("shell_command_test") { "../../../src/hqf_verify.cpp", "../../../src/hsp_packager.cpp", "../../../src/json/distro_filter.cpp", + "../../../src/json/general_normalize_version_utils.cpp", "../../../src/json/hap_verify_info.cpp", "../../../src/json/hap_verify_utils.cpp", "../../../src/json/json_utils.cpp", diff --git a/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp b/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp index 9f776d34198a6f7cc1670c96b17c67a25996f0b9..e0bad42db2f2d6fbd65fd64807aaddaa94c0d2d9 100644 --- a/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp +++ b/packing_tool/frameworks/test/unittest/utils_Test/utils_test.cpp @@ -642,30 +642,6 @@ HWTEST_F(UtilsTest, StringToArray_0100, Function | MediumTest | Level1) EXPECT_TRUE(OHOS::AppPackingTool::Utils::StringToArray("1\n2,3\n4, 5", array)); } -/* - * @tc.name: ArrayToString_0100 - * @tc.desc: ArrayToString - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(UtilsTest, ArrayToString_0100, Function | MediumTest | Level1) -{ - const std::list array = {"a", "b", "c"}; - EXPECT_EQ(OHOS::AppPackingTool::Utils::ArrayToString(array), "[\"a\",\"b\",\"c\"]"); -} - -/* - * @tc.name: BoolToString_0100 - * @tc.desc: BoolToString - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(UtilsTest, BoolToString_0100, Function | MediumTest | Level1) -{ - EXPECT_EQ(OHOS::AppPackingTool::Utils::BoolToString(true), "true"); - EXPECT_EQ(OHOS::AppPackingTool::Utils::BoolToString(false), "false"); -} - /* * @tc.name: RemoveAllFilesInDirectory_0100 * @tc.desc: RemoveAllFilesInDirectory