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 package ohos; 17 18 /** 19 * bundle tool main class. 20 * step1: parse arguments 21 * step2: verity arguments 22 * step3: compress arguments 23 * 24 */ 25 public class CompressEntrance { 26 private static final int EXIT_STATUS_NORMAL = 0; 27 private static final int EXIT_STATUS_EXCEPTION = 1; 28 private static final Log LOG = new Log(CompressEntrance.class.toString()); 29 private static final String HAP_SUFFIX = ".hap"; 30 private static final String EMPTY_STRING = ""; 31 32 /** 33 * Pack the app. 34 * 35 * @param hapPath Indicates the hap path. 36 * @param packInfoPath Indicates the pack.info file path. 37 * @param outPath Indicates the out path. 38 * @return Returns {@code true} if the pack is successful; returns {@code false} otherwise. 39 */ pack(String hapPath, String packInfoPath, String outPath)40 public static boolean pack(String hapPath, String packInfoPath, String outPath) { 41 if (hapPath == null || hapPath.isEmpty()) { 42 LOG.error("CompressEntrance::pack hapPath is invalid!"); 43 return false; 44 } 45 46 if (packInfoPath == null || packInfoPath.isEmpty()) { 47 LOG.error("CompressEntrance::pack packInfoPath is invalid!"); 48 return false; 49 } 50 51 if (outPath == null || outPath.isEmpty()) { 52 LOG.error("CompressEntrance::pack outPath is invalid!"); 53 return false; 54 } 55 56 Utility utility = new Utility(); 57 utility.setMode(Utility.MODE_APP); 58 utility.setHapPath(hapPath); 59 utility.setPackInfoPath(packInfoPath); 60 utility.setOutPath(outPath); 61 utility.setForceRewrite("true"); 62 63 if (!CompressVerify.commandVerify(utility)) { 64 LOG.error("CompressEntrance::startCompress verity failed"); 65 return false; 66 } 67 68 Compressor compressor = new Compressor(); 69 if (!compressor.compressProcess(utility)) { 70 LOG.error("CompressEntrance::startCompress compress failed"); 71 return false; 72 } 73 74 return true; 75 } 76 77 /** 78 * get sha-256 from hap path. 79 * 80 * @param hapPath Indicates the hap path. 81 * @return Returns the string of sha-256 for hap. 82 */ getHapSha256(String hapPath)83 public static String getHapSha256(String hapPath) { 84 if (!hapPath.endsWith(HAP_SUFFIX)) { 85 LOG.error("input file is not a hap."); 86 return EMPTY_STRING; 87 } 88 return FileUtils.getSha256(hapPath); 89 } 90 91 /** 92 * compress tool main function. 93 * 94 * @param args command line 95 */ main(String[] args)96 public static void main(String[] args) { 97 Utility utility = new Utility(); 98 99 if (!CommandParser.commandParser(utility, args)) { 100 LOG.error("CompressEntrance::main exit, parser failed"); 101 ShowHelp.compressHelp(); 102 System.exit(EXIT_STATUS_EXCEPTION); 103 } 104 105 if (!CompressVerify.commandVerify(utility)) { 106 LOG.error("CompressEntrance::main exit, verify failed"); 107 ShowHelp.compressHelp(); 108 System.exit(EXIT_STATUS_EXCEPTION); 109 } 110 111 Compressor compressor = new Compressor(); 112 if (!compressor.compressProcess(utility)) { 113 LOG.error("CompressEntrance::main exit, compress failed"); 114 ShowHelp.compressHelp(); 115 System.exit(EXIT_STATUS_EXCEPTION); 116 } 117 118 if (utility.getGenerateBuildHash()) { 119 utility.setForceRewrite("true"); 120 if (!compressor.compressProcess(utility)) { 121 LOG.error("CompressEntrance::main exit, compress failed"); 122 ShowHelp.compressHelp(); 123 System.exit(EXIT_STATUS_EXCEPTION); 124 } 125 } 126 127 System.exit(EXIT_STATUS_NORMAL); 128 } 129 }