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