14514f5e3Sopenharmony_ci#!/usr/bin/env python3
24514f5e3Sopenharmony_ci# -*- coding: utf-8 -*-
34514f5e3Sopenharmony_ci
44514f5e3Sopenharmony_ci#
54514f5e3Sopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
64514f5e3Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
74514f5e3Sopenharmony_ci# you may not use this file except in compliance with the License.
84514f5e3Sopenharmony_ci# You may obtain a copy of the License at
94514f5e3Sopenharmony_ci#
104514f5e3Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
114514f5e3Sopenharmony_ci#
124514f5e3Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
134514f5e3Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
144514f5e3Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
154514f5e3Sopenharmony_ci# See the License for the specific language governing permissions and
164514f5e3Sopenharmony_ci# limitations under the License.
174514f5e3Sopenharmony_ci#
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ciimport os
204514f5e3Sopenharmony_ciimport subprocess
214514f5e3Sopenharmony_ciimport argparse
224514f5e3Sopenharmony_ciimport shutil
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cidef get_script_directory():
264514f5e3Sopenharmony_ci    return os.path.dirname(os.path.abspath(__file__))
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cidef copy_files_to_dist(target_dir):
304514f5e3Sopenharmony_ci    file_list = [
314514f5e3Sopenharmony_ci        "out/sdk/clang_x64/thirdparty/icu/libhmicui18n.so",
324514f5e3Sopenharmony_ci        "out/sdk/clang_x64/thirdparty/icu/libhmicuuc.so",
334514f5e3Sopenharmony_ci        "out/sdk/clang_x64/arkcompiler/ets_runtime/libprofDumpJson.so",
344514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/thirdparty/icu/libhmicui18n.dll",
354514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/thirdparty/icu/libhmicuuc.dll",
364514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/arkcompiler/ets_runtime/libprofDumpJson.dll",
374514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/hiviewdfx/hilog/libhilog.dll",
384514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/thirdparty/bounds_checking_function/libsec_shared.dll",
394514f5e3Sopenharmony_ci        "out/sdk/mingw_x86_64/thirdparty/zlib/libshared_libz.dll"
404514f5e3Sopenharmony_ci    ]
414514f5e3Sopenharmony_ci    current_path = get_script_directory()
424514f5e3Sopenharmony_ci    dist_dir = os.path.join(current_path, "dist")
434514f5e3Sopenharmony_ci    for relative_file_path in file_list:
444514f5e3Sopenharmony_ci        absolute_file_path = os.path.join(target_dir, relative_file_path)
454514f5e3Sopenharmony_ci        if os.path.exists(absolute_file_path):
464514f5e3Sopenharmony_ci            print(f'Copied {absolute_file_path} to {dist_dir}')
474514f5e3Sopenharmony_ci            shutil.copy(absolute_file_path, dist_dir)
484514f5e3Sopenharmony_ci        else:
494514f5e3Sopenharmony_ci            print(f"File not found: {absolute_file_path}")
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_cidef run_command(command):
534514f5e3Sopenharmony_ci    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
544514f5e3Sopenharmony_ci    for line in process.stdout:
554514f5e3Sopenharmony_ci        print(line.decode('utf-8').strip())
564514f5e3Sopenharmony_ci    process.wait()
574514f5e3Sopenharmony_ci    if process.returncode != 0:
584514f5e3Sopenharmony_ci        raise Exception(f"Command failed with return code {process.returncode}")
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_cidef main(target_dir):
624514f5e3Sopenharmony_ci    original_dir = get_script_directory()
634514f5e3Sopenharmony_ci    try:
644514f5e3Sopenharmony_ci        command = ["npm", "install"]
654514f5e3Sopenharmony_ci        print(f"Running command: {command}")
664514f5e3Sopenharmony_ci        run_command(command)
674514f5e3Sopenharmony_ci        os.chdir(target_dir)
684514f5e3Sopenharmony_ci        print(f"Changed directory to: {os.getcwd()}")
694514f5e3Sopenharmony_ci        command = ["/bin/bash", "build.sh", "--product-name", "ohos-sdk", "--build-target",
704514f5e3Sopenharmony_ci                   "ap_viewer_all_host_tools_packages"]
714514f5e3Sopenharmony_ci        print(f"Running command: {command}")
724514f5e3Sopenharmony_ci        run_command(command)
734514f5e3Sopenharmony_ci        os.chdir(original_dir)
744514f5e3Sopenharmony_ci        print(f"Returned to original directory: {os.getcwd()}")
754514f5e3Sopenharmony_ci        npm_command = ["npm", "run", "build"]
764514f5e3Sopenharmony_ci        print(f"Running command: {npm_command}")
774514f5e3Sopenharmony_ci        run_command(npm_command)
784514f5e3Sopenharmony_ci        copy_files_to_dist(target_dir)
794514f5e3Sopenharmony_ci    except Exception as e:
804514f5e3Sopenharmony_ci        print(f"An error occurred: {e}")
814514f5e3Sopenharmony_ci    finally:
824514f5e3Sopenharmony_ci        os.chdir(original_dir)
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ciif __name__ == "__main__":
864514f5e3Sopenharmony_ci    parser = argparse.ArgumentParser(description="Build script with optional target directory.")
874514f5e3Sopenharmony_ci    parser.add_argument("--target-dir", metavar="dir", type=str,
884514f5e3Sopenharmony_ci                        help="The target directory to cd into before running the build script.")
894514f5e3Sopenharmony_ci    args = parser.parse_args()
904514f5e3Sopenharmony_ci    target_directory = args.target_dir if args.target_dir else "../../../../"
914514f5e3Sopenharmony_ci    main(target_directory)
92