• 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.Optional;
20 
21 /**
22  * hap package to bin package tool main class.
23  *
24  */
25 public class ConvertHapToBin {
26     private static final String HAP_PATH = "--hap-path";
27     private static final String BIN_PATH = "--bin-path";
28     private static final String PROJECT_PATH = "--project-path";
29     private static final String OUTPUT_PATH = "output";
30     private static final int PARA_COUNT = 4;
31     private static final Log LOG = new Log(ConvertHapToBin.class.toString());
32 
33     /**
34      * generate binary file from hap package
35      *
36      * @param hapPath: hap path
37      * @param binPath: bin path
38      * @return true: success, false: fail
39      */
packHapTobin(final String hapPath, final String binPath)40     public static boolean packHapTobin(final String hapPath, final String binPath) {
41         if (hapPath.isEmpty() || binPath.isEmpty()) {
42             LOG.error("hapPath or binPath is null");
43             return false;
44         }
45 
46         if (!FileUtils.checkFileIsExists(hapPath)) {
47             return false;
48         }
49 
50         Optional<String> absHapPath = FileUtils.getFormatedPath(hapPath);
51         File file = new File(absHapPath.get());
52         String projectPath = file.getParent() + File.separator + OUTPUT_PATH;
53         FileUtils.unzip(absHapPath.get(), projectPath);
54 
55         if (!BinaryTool.generateBinaryFile(projectPath, binPath)) {
56             LOG.error("create bin file failed!");
57             return false;
58         }
59         // delete project path
60         FileUtils.deleteDirectory(projectPath);
61         return true;
62     }
63 
64     /**
65      * generate binary file from hap project
66      *
67      * @param projectPath: project path
68      * @param binPath: bin path
69      * @return true: success, false: fail
70      */
packProjectTobin(final String projectPath, final String binPath)71     public static boolean packProjectTobin(final String projectPath, final String binPath) {
72         if (projectPath.isEmpty() || binPath.isEmpty()) {
73             LOG.error("projectPath or binPath is null");
74             return false;
75         }
76 
77         if (!FileUtils.checkFileIsExists(projectPath)) {
78             return false;
79         }
80 
81         Optional<String> absProjectPath = FileUtils.getFormatedPath(projectPath);
82         if (!BinaryTool.generateBinaryFile(absProjectPath.get(), binPath)) {
83             LOG.error("create bin file failed!");
84             return false;
85         }
86         return true;
87     }
88 
89     /**
90      * tool main function.
91      *
92      * @param args command line
93      */
main(String[] args)94     public static void main(String[] args) {
95         if (args.length != PARA_COUNT) {
96             LOG.error("param length is wrong!");
97             return;
98         }
99         String hapPath = "";
100         String binPath = "";
101         String projectPath = "";
102         int count = 0;
103         for (String para : args) {
104             if (HAP_PATH.equals(para) && count < args.length - 1) {
105                 hapPath = args[count + 1];
106             }
107 
108             if (PROJECT_PATH.equals(para) && count < args.length - 1) {
109                 projectPath = args[count + 1];
110             }
111 
112             if (BIN_PATH.equals(para) && count < args.length - 1) {
113                 binPath = args[count + 1];
114             }
115             count++;
116         }
117         if (!hapPath.isEmpty() && !binPath.isEmpty()) {
118             packHapTobin(hapPath, binPath);
119         }
120 
121         if (!projectPath.isEmpty() && !binPath.isEmpty()) {
122             packProjectTobin(projectPath, binPath);
123         }
124     }
125 }