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 using es2abc 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('--frontend-tool-path', 34 help='path of the frontend conversion tool') 35 parser.add_argument('--extension', 36 help='source file extension') 37 parser.add_argument("--debug", action='store_true', 38 help='whether add debuginfo') 39 parser.add_argument("--module", action='store_true', 40 help='whether is module') 41 parser.add_argument("--commonjs", action='store_true', 42 help='whether is commonjs') 43 parser.add_argument("--merge-abc", action='store_true', 44 help='whether is merge abc') 45 parser.add_argument("--generate-patch", action='store_true', 46 help='generate patch abc') 47 parser.add_argument("--dump-symbol-table", 48 help='dump symbol table of base abc') 49 parser.add_argument("--input-symbol-table", 50 help='input symbol table for patch abc') 51 parser.add_argument("--type-extractor", action='store_true', 52 help='enable type extractor') 53 parser.add_argument("--type-dts-builtin", action='store_true', 54 help='enable builtin type extractor') 55 arguments = parser.parse_args() 56 return arguments 57 58def run_command(cmd, execution_path): 59 print(" ".join(cmd) + " | execution_path: " + execution_path) 60 proc = subprocess.Popen(cmd, cwd=execution_path) 61 proc.wait() 62 63 64def gen_abc_info(input_arguments): 65 frontend_tool_path = input_arguments.frontend_tool_path 66 67 (path, name) = os.path.split(frontend_tool_path) 68 69 cmd = [os.path.join("./", name, "es2abc"), 70 '--output', input_arguments.dst_file, 71 input_arguments.src_js] 72 73 if input_arguments.extension: 74 cmd += ['--extension', input_arguments.extension] 75 if input_arguments.dump_symbol_table: 76 cmd += ['--dump-symbol-table', input_arguments.dump_symbol_table] 77 if input_arguments.input_symbol_table: 78 cmd += ['--input-symbol-table', input_arguments.input_symbol_table] 79 if input_arguments.debug: 80 src_index = cmd.index(input_arguments.src_js) 81 cmd.insert(src_index, '--debug-info') 82 if input_arguments.module: 83 src_index = cmd.index(input_arguments.src_js) 84 cmd.insert(src_index, '--module') 85 if input_arguments.commonjs: 86 src_index = cmd.index(input_arguments.src_js) 87 cmd.insert(src_index, '--commonjs') 88 if input_arguments.merge_abc: 89 src_index = cmd.index(input_arguments.src_js) 90 cmd.insert(src_index, '--merge-abc') 91 if input_arguments.generate_patch: 92 src_index = cmd.index(input_arguments.src_js) 93 cmd.insert(src_index, '--generate-patch') 94 # insert d.ts option to cmd later 95 if input_arguments.type_extractor: 96 src_index = cmd.index(input_arguments.src_js) 97 cmd.insert(src_index, '--type-extractor') 98 if input_arguments.type_dts_builtin: 99 src_index = cmd.index(input_arguments.src_js) 100 cmd.insert(src_index, '--type-dts-builtin') 101 run_command(cmd, path) 102 103 104if __name__ == '__main__': 105 gen_abc_info(parse_args()) 106