188c88e8eSopenharmony_ci#!/usr/bin/env python
288c88e8eSopenharmony_ci# -*- coding: utf-8 -*-
388c88e8eSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
488c88e8eSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
588c88e8eSopenharmony_ci# you may not use this file except in compliance with the License.
688c88e8eSopenharmony_ci# You may obtain a copy of the License at
788c88e8eSopenharmony_ci#
888c88e8eSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
988c88e8eSopenharmony_ci#
1088c88e8eSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1188c88e8eSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1288c88e8eSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388c88e8eSopenharmony_ci# See the License for the specific language governing permissions and
1488c88e8eSopenharmony_ci# limitations under the License.
1588c88e8eSopenharmony_ci
1688c88e8eSopenharmony_ciimport os
1788c88e8eSopenharmony_ciimport sys
1888c88e8eSopenharmony_ciimport subprocess
1988c88e8eSopenharmony_ciimport argparse
2088c88e8eSopenharmony_ci
2188c88e8eSopenharmony_cistandard_system_build_dir = os.path.join(os.path.dirname(__file__), os.pardir,
2288c88e8eSopenharmony_ci    os.pardir, 'build', 'scripts', 'util')
2388c88e8eSopenharmony_cibuild_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
2488c88e8eSopenharmony_ci    os.pardir, os.pardir, os.pardir, 'build', 'maple', 'java', 'util')
2588c88e8eSopenharmony_ciif os.path.exists(standard_system_build_dir):
2688c88e8eSopenharmony_ci    sys.path.append(
2788c88e8eSopenharmony_ci        os.path.join(standard_system_build_dir, os.pardir, os.pardir))
2888c88e8eSopenharmony_ci    from scripts.util import build_utils  # noqa: E402
2988c88e8eSopenharmony_ciif os.path.exists(build_dir):
3088c88e8eSopenharmony_ci    sys.path.append(os.path.join(build_dir, os.pardir, os.pardir, os.pardir))
3188c88e8eSopenharmony_ci    from maple.java.util import build_utils  # noqa: E402
3288c88e8eSopenharmony_ci
3388c88e8eSopenharmony_ci
3488c88e8eSopenharmony_cidef parse_args():
3588c88e8eSopenharmony_ci
3688c88e8eSopenharmony_ci    parser = argparse.ArgumentParser()
3788c88e8eSopenharmony_ci    build_utils.add_depfile_option(parser)
3888c88e8eSopenharmony_ci
3988c88e8eSopenharmony_ci    parser.add_argument('--node', help='path to nodejs exetuable')
4088c88e8eSopenharmony_ci    parser.add_argument('--babel-js', help='path to babel.js')
4188c88e8eSopenharmony_ci    parser.add_argument("--weex-loader-src-dir",
4288c88e8eSopenharmony_ci                        help='path to weex-loader/src')
4388c88e8eSopenharmony_ci    parser.add_argument('--babel-config-js', help='path babel.config.js')
4488c88e8eSopenharmony_ci    parser.add_argument('--module-source-js', help='path module-source.js')
4588c88e8eSopenharmony_ci    parser.add_argument('--uglify-source-js', help='path uglify-source.js')
4688c88e8eSopenharmony_ci    parser.add_argument('--output-dir', help='path to output')
4788c88e8eSopenharmony_ci
4888c88e8eSopenharmony_ci    options = parser.parse_args()
4988c88e8eSopenharmony_ci    return options
5088c88e8eSopenharmony_ci
5188c88e8eSopenharmony_ci
5288c88e8eSopenharmony_cidef do_build(build_cmd, copy_cmd, uglify_cmd):
5388c88e8eSopenharmony_ci    for cmd in [build_cmd, copy_cmd, uglify_cmd]:
5488c88e8eSopenharmony_ci        build_utils.check_output(cmd)
5588c88e8eSopenharmony_ci
5688c88e8eSopenharmony_ci
5788c88e8eSopenharmony_cidef main():
5888c88e8eSopenharmony_ci    options = parse_args()
5988c88e8eSopenharmony_ci    build_cmd = [
6088c88e8eSopenharmony_ci        options.node, options.babel_js, options.weex_loader_src_dir
6188c88e8eSopenharmony_ci    ]
6288c88e8eSopenharmony_ci    build_cmd.extend(['--out-dir', options.output_dir])
6388c88e8eSopenharmony_ci    build_cmd.extend(['--config-file', options.babel_config_js])
6488c88e8eSopenharmony_ci    depfile_deps = [options.node, options.babel_js, options.babel_config_js]
6588c88e8eSopenharmony_ci    depfile_deps.extend(build_utils.get_all_files(options.weex_loader_src_dir))
6688c88e8eSopenharmony_ci
6788c88e8eSopenharmony_ci    copy_cmd = [options.node, options.module_source_js, options.output_dir]
6888c88e8eSopenharmony_ci    depfile_deps.append(options.module_source_js)
6988c88e8eSopenharmony_ci
7088c88e8eSopenharmony_ci    uglify_cmd = [options.node, options.uglify_source_js, options.output_dir]
7188c88e8eSopenharmony_ci    depfile_deps.append(options.uglify_source_js)
7288c88e8eSopenharmony_ci
7388c88e8eSopenharmony_ci    build_utils.call_and_write_depfile_if_stale(
7488c88e8eSopenharmony_ci        lambda: do_build(build_cmd, copy_cmd, uglify_cmd),
7588c88e8eSopenharmony_ci        options,
7688c88e8eSopenharmony_ci        depfile_deps=depfile_deps,
7788c88e8eSopenharmony_ci        input_paths=depfile_deps,
7888c88e8eSopenharmony_ci        output_paths=([options.output_dir]))
7988c88e8eSopenharmony_ci
8088c88e8eSopenharmony_ci
8188c88e8eSopenharmony_ciif __name__ == '__main__':
8288c88e8eSopenharmony_ci    sys.exit(main())
83