1#!/usr/bin/env python3 2# coding: utf-8 3 4""" 5Copyright (c) 2021 Huawei Device Co., Ltd. 6Licensed under the Apache License, Version 2.0 (the "License"); 7you may not use this file except in compliance with the License. 8You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12Unless required by applicable law or agreed to in writing, software 13distributed under the License is distributed on an "AS IS" BASIS, 14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15See the License for the specific language governing permissions and 16limitations under the License. 17 18Description: Compile ark front-end code with tsc 19""" 20 21import os 22import sys 23import subprocess 24import argparse 25import platform 26import shutil 27 28 29def parse_args(): 30 parser = argparse.ArgumentParser() 31 32 parser.add_argument('--src-dir', 33 help='Source directory') 34 parser.add_argument('--dist-dir', 35 help='Destination directory') 36 parser.add_argument('--platform', 37 help='platform, as: linux, mac, win') 38 parser.add_argument('--node', 39 help='node path') 40 parser.add_argument("--node-modules", 41 help='path to node-modules exetuable') 42 parser.add_argument("--buildMode", 43 help='buildMode, as: debug, release') 44 parser.add_argument("--js2abc", 45 help='js2abc file') 46 return parser.parse_args() 47 48 49def set_env(node_dir): 50 if platform.system() == "Windows": 51 jsoner_format = ";" 52 else: 53 jsoner_format = ":" 54 os.environ["PATH"] = f'{node_dir}{jsoner_format}{os.environ["PATH"]}' 55 56 57def run_command(cmd, execution_path=os.getcwd()): 58 print(" ".join(cmd) + " | execution_path: " + execution_path) 59 proc = subprocess.Popen(cmd, cwd=execution_path) 60 ret = proc.wait() 61 assert not ret, f'\n{" ".join(cmd)} failed' 62 63 64def node_modules(options): 65 src_dir = options.src_dir 66 dist_dir = options.dist_dir 67 run_command(['cp', '-f', os.path.join(src_dir, "package.json"), 68 os.path.join(dist_dir, "package.json")]) 69 run_command(['cp', '-f', os.path.join(src_dir, "package-lock.json"), 70 os.path.join(dist_dir, "package-lock.json")]) 71 72 if options.node_modules: 73 run_command(['cp', '-rf', options.node_modules, 74 os.path.join(dist_dir, "node_modules")]) 75 else: 76 run_command(['npm', 'install'], dist_dir) 77 78 79def per_platform_config(options, inp_dir): 80 dist_dir = options.dist_dir 81 if os.path.exists(os.path.join(dist_dir, inp_dir)): 82 shutil.rmtree(os.path.join(dist_dir, inp_dir), ignore_errors=True) 83 cmd = ['mv', 'dist', inp_dir] 84 run_command(cmd, dist_dir) 85 run_command(['cp', '-f', "package.json", 86 "./{}/package.json".format(inp_dir)], dist_dir) 87 run_command(['cp', '-f', "package-lock.json", 88 "./{}/package-lock.json".format(inp_dir)], dist_dir) 89 (js2abc_dir, _) = os.path.split(options.js2abc) 90 build_dir = os.path.join(js2abc_dir, inp_dir) 91 if os.path.exists(build_dir): 92 shutil.rmtree(build_dir) 93 run_command(['cp', '-r', os.path.join(dist_dir, inp_dir), js2abc_dir]) 94 bin_dir = os.path.join(build_dir, 'bin') 95 if not os.path.exists(bin_dir): 96 os.mkdir(bin_dir) 97 run_command(['cp', '-f', options.js2abc, bin_dir]) 98 obj_bin_dir = os.path.join(dist_dir, inp_dir, 'bin/') 99 if not os.path.exists(obj_bin_dir): 100 os.mkdir(obj_bin_dir) 101 run_command(['cp', '-f', options.js2abc, obj_bin_dir]) 102 run_command(['cp', '-r', os.path.join(dist_dir,"node_modules"), 103 os.path.join(dist_dir, inp_dir)]) 104 105 106def npm_run_build(options): 107 plat_form = options.platform 108 os.chdir(options.dist_dir) 109 webpack = "node_modules/webpack/bin/webpack.js" 110 111 cmd = [webpack, '--config', 'webpack.config.js', '--progress', 112 '--env', 'buildMode={}'.format(options.buildMode)] 113 if os.getenv("NO_DEVTOOL"): 114 cmd += ['--no-devtool'] 115 run_command(cmd, options.dist_dir) 116 if plat_form == "linux": 117 per_platform_config(options, "build") 118 elif plat_form == "win": 119 per_platform_config(options, "build-win") 120 elif plat_form == 'mac': 121 per_platform_config(options, "build-mac") 122 123 124def main(): 125 args = parse_args() 126 set_env(args.node) 127 if not os.path.exists(os.path.join(args.dist_dir, "node_modules")): 128 node_modules(args) 129 npm_run_build(args) 130 131 132if __name__ == "__main__": 133 sys.exit(main()) 134