From 3811d36b41f9583313e549e4885e9b194f75630c Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Fri, 16 May 2025 15:40:01 +0800 Subject: [PATCH] fix generalNomalize Signed-off-by: lanhaoyu --- adapter/ohos/CommandParser.java | 12 ++++--- adapter/ohos/CompressVerify.java | 12 ++++--- adapter/ohos/Compressor.java | 56 ++++++++++++++++++++++++-------- adapter/ohos/Utility.java | 9 +++++ 4 files changed, 67 insertions(+), 22 deletions(-) diff --git a/adapter/ohos/CommandParser.java b/adapter/ohos/CommandParser.java index bdec936f..214ea1ab 100644 --- a/adapter/ohos/CommandParser.java +++ b/adapter/ohos/CommandParser.java @@ -339,12 +339,13 @@ public class CommandParser { return true; }); commandFuncs.put(VERSION_CODE, entry -> { + entry.getKey().addGeneralNormalizeList(VERSION_CODE_PARAM); try { entry.getKey().setVersionCode(Integer.parseInt(entry.getValue())); - entry.getKey().addGeneralNormalizeList(VERSION_CODE_PARAM); } catch (NumberFormatException ignored) { LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString( "--version-code value is not number or invalid.")); + entry.getKey().setParameterIsInvalid(false); return false; } return true; @@ -417,34 +418,37 @@ public class CommandParser { return true; }); commandFuncs.put(PARAM_MIN_COMPATIBLE_VERSION_CODE, entry -> { + entry.getKey().addGeneralNormalizeList(MIN_COMPATIBLE_VERSION_CODE_PARAM); try { entry.getKey().setMinCompatibleVersionCode(Integer.parseInt(entry.getValue())); - entry.getKey().addGeneralNormalizeList(MIN_COMPATIBLE_VERSION_CODE_PARAM); } catch (NumberFormatException ignored) { LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString( "--min-compatible-version-code value is not number or invalid.")); + entry.getKey().setParameterIsInvalid(false); return false; } return true; }); commandFuncs.put(PARAM_MIN_API_VERSION, entry -> { + entry.getKey().addGeneralNormalizeList(MIN_API_VERSION_PARAM); try { entry.getKey().setMinAPIVersion(Integer.parseInt(entry.getValue())); - entry.getKey().addGeneralNormalizeList(MIN_API_VERSION_PARAM); } catch (NumberFormatException ignored) { LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString( "--min-api-version value is not number or invalid.")); + entry.getKey().setParameterIsInvalid(false); return false; } return true; }); commandFuncs.put(PARAM_TARGET_API_VERSION, entry -> { + entry.getKey().addGeneralNormalizeList(TARGET_API_VERSION_PARAM); try { entry.getKey().setTargetAPIVersion(Integer.parseInt(entry.getValue())); - entry.getKey().addGeneralNormalizeList(TARGET_API_VERSION_PARAM); } catch (NumberFormatException ignored) { LOG.error(PackingToolErrMsg.COMMAND_PARSER_FAILED.toString( "--target-api-version value is not number or invalid.")); + entry.getKey().setParameterIsInvalid(false); return false; } return true; diff --git a/adapter/ohos/CompressVerify.java b/adapter/ohos/CompressVerify.java index a6f535b2..0565aa36 100644 --- a/adapter/ohos/CompressVerify.java +++ b/adapter/ohos/CompressVerify.java @@ -298,7 +298,8 @@ public class CompressVerify { } if (utility.getGeneralNormalizeList().contains(VERSION_CODE)) { - if (utility.getVersionCode() < MINI_NUM || utility.getVersionCode() > MAXI_NUM) { + if (utility.getVersionCode() < MINI_NUM || utility.getVersionCode() > MAXI_NUM || + (!utility.getParameterIsInvalid())) { String errMsg = "--version-code is invalid."; LOG.error(PackingToolErrMsg.GENERAL_NORMALIZE_MODE_ARGS_INVALID.toString(errMsg)); return false; @@ -324,7 +325,8 @@ public class CompressVerify { } if (utility.getGeneralNormalizeList().contains(MIN_COMPATIBLE_VERSION_CODE)) { - if (utility.getMinCompatibleVersionCode() < MINI_NUM || utility.getMinCompatibleVersionCode() > MAXI_NUM) { + if (utility.getMinCompatibleVersionCode() < MINI_NUM || utility.getMinCompatibleVersionCode() > MAXI_NUM || + (!utility.getParameterIsInvalid())) { String errMsg = "--min-compatible-version-code is invalid."; LOG.error(PackingToolErrMsg.GENERAL_NORMALIZE_MODE_ARGS_INVALID.toString(errMsg)); return false; @@ -332,7 +334,8 @@ public class CompressVerify { } if (utility.getGeneralNormalizeList().contains(MIN_API_VERSION)) { - if (utility.getMinAPIVersion() < MINI_NUM || utility.getMinAPIVersion() > MAXI_NUM) { + if (utility.getMinAPIVersion() < MINI_NUM || utility.getMinAPIVersion() > MAXI_NUM || + (!utility.getParameterIsInvalid())) { String errMsg = "--min-api-version is invalid."; LOG.error(PackingToolErrMsg.GENERAL_NORMALIZE_MODE_ARGS_INVALID.toString(errMsg)); return false; @@ -340,7 +343,8 @@ public class CompressVerify { } if (utility.getGeneralNormalizeList().contains(TARGET_API_VERSION)) { - if (utility.getTargetAPIVersion() < MINI_NUM || utility.getTargetAPIVersion() > MAXI_NUM) { + if (utility.getTargetAPIVersion() < MINI_NUM || utility.getTargetAPIVersion() > MAXI_NUM || + (!utility.getParameterIsInvalid())) { String errMsg = "--target-api-version is invalid."; LOG.error(PackingToolErrMsg.GENERAL_NORMALIZE_MODE_ARGS_INVALID.toString(errMsg)); return false; diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 90bda4f0..83f24a86 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -215,7 +215,6 @@ public class Compressor { private List fileNameList = new ArrayList(); private List supportDimensionsList = Arrays.asList(PIC_1X2, PIC_2X2, PIC_2X4, PIC_4X4, PIC_1X1, PIC_6X4); private HashMap hapVerifyInfoMap = new HashMap<>(); - private HashMap outPutMap = new HashMap<>(); public static int getEntryModuleSizeLimit() { return entryModuleSizeLimit; @@ -3816,7 +3815,7 @@ public class Compressor { try { tempDir = Files.createTempDirectory(Paths.get(utility.getOutPath()), "temp"); unpackHap(hapPath, tempDir.toAbsolutePath().toString()); - HashMap outPutMap = new HashMap<>(); + HashMap outPutMap; File moduleFile = new File( tempDir.toAbsolutePath() + LINUX_FILE_SEPARATOR + MODULE_JSON); File configFile = new File( @@ -3874,6 +3873,7 @@ public class Compressor { private HashMap parseAndModifyGeneralModuleJson(String jsonFilePath, Utility utility, String[] name) throws BundleException { + HashMap outPutMap = new HashMap<>(); try (FileInputStream jsonStream = new FileInputStream(jsonFilePath)) { JSONObject jsonObject = JSON.parseObject(jsonStream, JSONObject.class); if (!jsonObject.containsKey(APP)) { @@ -3918,8 +3918,12 @@ public class Compressor { } if (utility.getGeneralNormalizeList().contains(MIN_COMPATIBLE_VERSION_CODE)) { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf( + if (appObject.containsKey(MIN_COMPATIBLE_VERSION_CODE)) { + outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf( appObject.getIntValue(MIN_COMPATIBLE_VERSION_CODE))); + } else { + outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf(appObject.getIntValue(VERSION_CODE))); + } appObject.put(MIN_COMPATIBLE_VERSION_CODE, utility.getMinCompatibleVersionCode()); } @@ -3939,18 +3943,29 @@ public class Compressor { } if (utility.getGeneralNormalizeList().contains(BUNDLE_TYPE)) { - outPutMap.put(BUNDLE_TYPE, appObject.getString(BUNDLE_TYPE)); + if (!appObject.containsKey(BUNDLE_TYPE)) { + if (moduleObject.getBoolean(INSTALLATION_FREE)) { + String errMsg = + "app.json5 file configuration does not match the 'installationFree' setting of true."; + String solution = "Add the bundleType field to the app.json5 file or set it atomicService."; + LOG.error(PackingToolErrMsg.PARSE_STAGE_BUNDLE_TYPE_FAILED.toString(errMsg, solution)); + throw new BundleException(errMsg); + } + outPutMap.put(BUNDLE_TYPE, APP); + } else { + outPutMap.put(BUNDLE_TYPE, 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, utility.getDeliveryWithInstall()); + moduleObject.put(INSTALLATION_FREE, Boolean.parseBoolean(utility.getInstallationFree())); } if (utility.getGeneralNormalizeList().contains(DELIVERY_WITH_INSTALL)) { outPutMap.put(DELIVERY_WITH_INSTALL, moduleObject.getBoolean(DELIVERY_WITH_INSTALL).toString()); - moduleObject.put(DELIVERY_WITH_INSTALL, utility.getInstallationFree()); + moduleObject.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); } writeJson(jsonFilePath, jsonObject); } catch (IOException e) { @@ -3963,6 +3978,7 @@ public class Compressor { private HashMap parseAndModifyGeneralConfigJson(String jsonFilePath, Utility utility, String[] name) throws BundleException { + HashMap outPutMap = new HashMap<>(); try (FileInputStream jsonStream = new FileInputStream(jsonFilePath)) { JSONObject jsonObject = JSON.parseObject(jsonStream, JSONObject.class); if (!jsonObject.containsKey(APP)) { @@ -4028,8 +4044,12 @@ public class Compressor { } if (utility.getGeneralNormalizeList().contains(MIN_COMPATIBLE_VERSION_CODE)) { - outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf( - versionObj.getIntValue(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))); + } else { + outPutMap.put(MIN_COMPATIBLE_VERSION_CODE, String.valueOf(versionObj.getIntValue(CODE))); + } versionObj.put(MIN_COMPATIBLE_VERSION_CODE, utility.getMinCompatibleVersionCode()); } @@ -4054,18 +4074,26 @@ public class Compressor { } if (utility.getGeneralNormalizeList().contains(BUNDLE_TYPE)) { - outPutMap.put(BUNDLE_TYPE, appObject.getString(BUNDLE_TYPE)); + if (!appObject.containsKey(BUNDLE_TYPE)) { + if (distroObj.getBoolean(INSTALLATION_FREE)) { + outPutMap.put(BUNDLE_TYPE, ATOMIC_SERVICE); + } else { + outPutMap.put(BUNDLE_TYPE, APP); + } + } else { + outPutMap.put(BUNDLE_TYPE, 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, utility.getDeliveryWithInstall()); + distroObj.put(INSTALLATION_FREE, Boolean.parseBoolean(utility.getInstallationFree())); } if (utility.getGeneralNormalizeList().contains(DELIVERY_WITH_INSTALL)) { outPutMap.put(DELIVERY_WITH_INSTALL, distroObj.getBoolean(DELIVERY_WITH_INSTALL).toString()); - distroObj.put(DELIVERY_WITH_INSTALL, utility.getInstallationFree()); + distroObj.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); } writeJson(jsonFilePath, jsonObject); } catch (IOException e) { @@ -4165,7 +4193,7 @@ public class Compressor { object.put(DEVICE_TYPE, utility.getDeviceTypes().split(",")); } if (utility.getGeneralNormalizeList().contains(DELIVERY_WITH_INSTALL)) { - object.put(DELIVERY_WITH_INSTALL, utility.getDeliveryWithInstall()); + object.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); } } } @@ -4212,9 +4240,9 @@ public class Compressor { break; } if(key == INSTALLATION_FREE) { - distroObj.put(INSTALLATION_FREE, utility.getInstallationFree()); + distroObj.put(INSTALLATION_FREE, Boolean.parseBoolean(utility.getInstallationFree())); } else if (key == DELIVERY_WITH_INSTALL) { - distroObj.put(DELIVERY_WITH_INSTALL, utility.getDeliveryWithInstall()); + distroObj.put(DELIVERY_WITH_INSTALL, Boolean.parseBoolean(utility.getDeliveryWithInstall())); } } } diff --git a/adapter/ohos/Utility.java b/adapter/ohos/Utility.java index c8597fc6..5c47ea08 100644 --- a/adapter/ohos/Utility.java +++ b/adapter/ohos/Utility.java @@ -154,6 +154,7 @@ public class Utility { private boolean statDuplicate = false; private boolean statSuffix = false; private String statFileSize = ""; + private boolean isSuccess = true; public void setIsParse(boolean isParse) { this.isParse = isParse; @@ -1015,4 +1016,12 @@ public class Utility { public List getGeneralNormalizeList() { return generalNormalizeList; } + + public Boolean getParameterIsInvalid() { + return isSuccess; + } + + public void setParameterIsInvalid(Boolean isSuccess) { + this.isSuccess = isSuccess; + } } -- Gitee