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