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: Generate javascript byte code 19""" 20 21import os 22import subprocess 23import platform 24import argparse 25 26 27def parse_args(): 28 parser = argparse.ArgumentParser() 29 parser.add_argument('--src-js', 30 help='js source file') 31 parser.add_argument('--dst-file', 32 help='the converted target file') 33 parser.add_argument("--node", 34 help='path to nodejs exetuable') 35 parser.add_argument('--frontend-tool-path', 36 help='path to frontend conversion tool') 37 parser.add_argument("--node-modules", 38 help='path to node-modules exetuable') 39 parser.add_argument("--debug", action='store_true', 40 help='whether add debuginfo') 41 parser.add_argument("--module", action='store_true', 42 help='whether is module') 43 parser.add_argument("--commonjs", action='store_true', 44 help='whether is commonjs') 45 parser.add_argument("--q", action='store_true', 46 help='whether is d.ts') 47 parser.add_argument("--b", action='store_true', 48 help='enable builtin types recognition for .d.ts files') 49 parser.add_argument("--functionSourceCode", action='store_true', 50 help='compile abc with function sourcecode info') 51 parser.add_argument("--merge-abc", action='store_true', 52 help='Compile as merge abc') 53 arguments = parser.parse_args() 54 return arguments 55 56 57def set_env(input_arguments): 58 jsoner_format = ":" 59 if platform.system() == "Windows": 60 jsoner_format = ";" 61 os.environ["PATH"] = input_arguments.node + \ 62 jsoner_format + os.environ["PATH"] 63 64 65def run_command(cmd, execution_path): 66 print(" ".join(cmd) + " | execution_path: " + execution_path) 67 proc = subprocess.Popen(cmd, cwd=execution_path) 68 proc.wait() 69 70 71def gen_abc_info(input_arguments): 72 73 set_env(input_arguments) 74 frontend_tool_path = input_arguments.frontend_tool_path 75 76 (path, name) = os.path.split(frontend_tool_path) 77 78 if not os.path.exists(os.path.join(path, "node_modules")): 79 if input_arguments.node_modules: 80 cmd = ['cp', "-rf", input_arguments.node_modules, path] 81 run_command(cmd, path) 82 else: 83 cmd = ['npm', 'install'] 84 run_command(cmd, path) 85 86 cmd = [os.path.join(input_arguments.node, "node"), 87 '--expose-gc', 88 os.path.join(name, 'src/index.js'), 89 input_arguments.src_js, 90 '-o', input_arguments.dst_file, 91 '-t', '0'] 92 93 if input_arguments.debug: 94 cmd.insert(3, '--debug') 95 if input_arguments.module: 96 cmd.insert(4, '-m') 97 if input_arguments.commonjs: 98 cmd.insert(5, '-c') 99 if input_arguments.q: 100 cmd.insert(6, '-q') 101 if input_arguments.b: 102 cmd.insert(7, '-b') 103 if input_arguments.functionSourceCode: 104 cmd.insert(8, '--function-sourcecode') 105 if input_arguments.merge_abc: 106 cmd.insert(9, '--merge-abc') 107 run_command(cmd, path) 108 109 110if __name__ == '__main__': 111 gen_abc_info(parse_args()) 112