• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("--debug", action='store_true',
36                        help='whether add debuginfo')
37    parser.add_argument("--module", action='store_true',
38                        help='whether is module')
39    parser.add_argument("--commonjs", action='store_true',
40                        help='whether is commonjs')
41    parser.add_argument("--merge-abc", action='store_true',
42                        help='whether is merge abc')
43    arguments = parser.parse_args()
44    return arguments
45
46def run_command(cmd, execution_path):
47    print(" ".join(cmd) + " | execution_path: " + execution_path)
48    proc = subprocess.Popen(cmd, cwd=execution_path)
49    proc.wait()
50
51
52def gen_abc_info(input_arguments):
53    frontend_tool_path = input_arguments.frontend_tool_path
54
55    (path, name) = os.path.split(frontend_tool_path)
56
57    cmd = [os.path.join("./", name, "es2abc"),
58           '--output', input_arguments.dst_file,
59           input_arguments.src_js]
60
61    if input_arguments.debug:
62        src_index = cmd.index(input_arguments.src_js)
63        cmd.insert(src_index, '--debug-info')
64    if input_arguments.module:
65        src_index = cmd.index(input_arguments.src_js)
66        cmd.insert(src_index, '--module')
67    if input_arguments.commonjs:
68        src_index = cmd.index(input_arguments.src_js)
69        cmd.insert(src_index, '--commonjs')
70    if input_arguments.merge_abc:
71        src_index = cmd.index(input_arguments.src_js)
72        cmd.insert(src_index, '--merge-abc')
73        # insert d.ts option to cmd later
74    run_command(cmd, path)
75
76
77if __name__ == '__main__':
78    gen_abc_info(parse_args())
79