• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import optparse
17import os
18import sys
19import tempfile
20import json
21import shutil
22
23from util import build_utils  # noqa: E402
24
25
26def parse_args(args):
27    args = build_utils.expand_file_args(args)
28
29    parser = optparse.OptionParser()
30    build_utils.add_depfile_option(parser)
31    parser.add_option('--output', help='stamp file')
32    parser.add_option('--js-assets-dir', help='js assets directory')
33    parser.add_option('--nodejs-path', help='path to nodejs app')
34    parser.add_option('--webpack-js', help='path to webpack.js')
35    parser.add_option('--webpack-config-js', help='path to webpack.config.js')
36    parser.add_option('--hap-profile', help='path to hap profile')
37    parser.add_option('--build-mode', help='debug mode or release mode')
38    parser.add_option('--js2abc',
39                      action='store_true',
40                      default=False,
41                      help='whether to transform js to ark bytecode')
42
43    options, _ = parser.parse_args(args)
44    options.js_assets_dir = build_utils.parse_gn_list(options.js_assets_dir)
45    return options
46
47
48def build_ace(cmd, options):
49    with build_utils.temp_dir() as build_dir:
50        gen_dir = os.path.join(build_dir, 'gen')
51        manifest = os.path.join(build_dir, 'manifest.json')
52        my_env = {
53            "aceModuleRoot": options.js_assets_dir[0],
54            "aceModuleBuild": gen_dir,
55            "aceManifestPath": manifest,
56            "buildMode": options.build_mode,
57            "PATH": os.environ.get('PATH'),
58        }
59        if not os.path.exists(manifest) and options.hap_profile:
60            with open(options.hap_profile) as profile:
61                config = json.load(profile)
62                data = dict()
63                data['appID'] = config['app']['bundleName']
64                data['appName'] = config['module']['abilities'][0]['label']
65                data['versionName'] = config['app']['version']['name']
66                data['versionCode'] = config['app']['version']['code']
67                data['pages'] = config['module']['js'][0]['pages']
68                data['deviceType'] = config['module']['deviceType']
69                data['window'] = config['module']['js'][0]['window']
70                build_utils.write_json(data, manifest)
71        build_utils.check_output(cmd, env=my_env)
72        for root, _, files in os.walk(gen_dir):
73            for file in files:
74                filename = os.path.join(root, file)
75                if filename.endswith('.js.map'):
76                    os.unlink(filename)
77        build_utils.zip_dir(options.output,
78                            gen_dir,
79                            zip_prefix_path='assets/js/default/')
80
81
82def main(args):
83    options = parse_args(args)
84
85    inputs = ([
86        options.nodejs_path, options.webpack_js, options.webpack_config_js
87    ])
88    depfiles = (build_utils.get_all_files(options.js_assets_dir[0]))
89
90    cmd = [
91        options.nodejs_path,
92        options.webpack_js,
93        '--config',
94        options.webpack_config_js,
95    ]
96    if options.js2abc:
97        cmd.extend(['--compilerType', 'ark'])
98
99    build_utils.call_and_write_depfile_if_stale(
100        lambda: build_ace(cmd, options),
101        options,
102        depfile_deps=depfiles,
103        input_paths=depfiles + inputs,
104        input_strings=cmd + [options.build_mode],
105        output_paths=([options.output]),
106        force=False,
107        add_pydeps=False)
108
109
110if __name__ == '__main__':
111    sys.exit(main(sys.argv[1:]))
112