1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20import subprocess
21import argparse
22import shutil
23
24
25def get_script_directory():
26    return os.path.dirname(os.path.abspath(__file__))
27
28
29def copy_files_to_dist(target_dir):
30    file_list = [
31        "out/sdk/clang_x64/thirdparty/icu/libhmicui18n.so",
32        "out/sdk/clang_x64/thirdparty/icu/libhmicuuc.so",
33        "out/sdk/clang_x64/arkcompiler/ets_runtime/libprofDumpJson.so",
34        "out/sdk/mingw_x86_64/thirdparty/icu/libhmicui18n.dll",
35        "out/sdk/mingw_x86_64/thirdparty/icu/libhmicuuc.dll",
36        "out/sdk/mingw_x86_64/arkcompiler/ets_runtime/libprofDumpJson.dll",
37        "out/sdk/mingw_x86_64/hiviewdfx/hilog/libhilog.dll",
38        "out/sdk/mingw_x86_64/thirdparty/bounds_checking_function/libsec_shared.dll",
39        "out/sdk/mingw_x86_64/thirdparty/zlib/libshared_libz.dll"
40    ]
41    current_path = get_script_directory()
42    dist_dir = os.path.join(current_path, "dist")
43    for relative_file_path in file_list:
44        absolute_file_path = os.path.join(target_dir, relative_file_path)
45        if os.path.exists(absolute_file_path):
46            print(f'Copied {absolute_file_path} to {dist_dir}')
47            shutil.copy(absolute_file_path, dist_dir)
48        else:
49            print(f"File not found: {absolute_file_path}")
50
51
52def run_command(command):
53    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
54    for line in process.stdout:
55        print(line.decode('utf-8').strip())
56    process.wait()
57    if process.returncode != 0:
58        raise Exception(f"Command failed with return code {process.returncode}")
59
60
61def main(target_dir):
62    original_dir = get_script_directory()
63    try:
64        command = ["npm", "install"]
65        print(f"Running command: {command}")
66        run_command(command)
67        os.chdir(target_dir)
68        print(f"Changed directory to: {os.getcwd()}")
69        command = ["/bin/bash", "build.sh", "--product-name", "ohos-sdk", "--build-target",
70                   "ap_viewer_all_host_tools_packages"]
71        print(f"Running command: {command}")
72        run_command(command)
73        os.chdir(original_dir)
74        print(f"Returned to original directory: {os.getcwd()}")
75        npm_command = ["npm", "run", "build"]
76        print(f"Running command: {npm_command}")
77        run_command(npm_command)
78        copy_files_to_dist(target_dir)
79    except Exception as e:
80        print(f"An error occurred: {e}")
81    finally:
82        os.chdir(original_dir)
83
84
85if __name__ == "__main__":
86    parser = argparse.ArgumentParser(description="Build script with optional target directory.")
87    parser.add_argument("--target-dir", metavar="dir", type=str,
88                        help="The target directory to cd into before running the build script.")
89    args = parser.parse_args()
90    target_directory = args.target_dir if args.target_dir else "../../../../"
91    main(target_directory)
92