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 json 20 21from zipfile import ZipFile # noqa: E402 22from util import build_utils # noqa: E402 23 24 25def parse_args(args): 26 args = build_utils.expand_file_args(args) 27 28 parser = optparse.OptionParser() 29 build_utils.add_depfile_option(parser) 30 parser.add_option('--output', help='stamp file') 31 parser.add_option('--js-assets-dir', help='js assets directory') 32 parser.add_option('--ets-assets-dir', help='ets assets directory') 33 parser.add_option('--js-forms-dir', help='js forms directory') 34 parser.add_option('--testrunner-dir', help='testrunner directory') 35 parser.add_option('--nodejs-path', help='path to nodejs app') 36 parser.add_option('--webpack-js', help='path to webpack.js') 37 parser.add_option('--webpack-config-js', help='path to webpack.config.js') 38 parser.add_option('--webpack-config-ets', help='path to webpack.rich.config.js') 39 parser.add_option('--hap-profile', help='path to hap profile') 40 parser.add_option('--build-mode', help='debug mode or release mode') 41 parser.add_option('--js-sources-file', help='path to js sources file') 42 parser.add_option('--js2abc', 43 action='store_true', 44 default=False, 45 help='whether to transform js to ark bytecode') 46 parser.add_option('--ets2abc', 47 action='store_true', 48 default=False, 49 help='whether to transform ets to ark bytecode') 50 parser.add_option('--ark-es2abc-dir', help='path to ark es2abc dir') 51 parser.add_option('--ace-loader-home', help='path to ace-loader dir.') 52 parser.add_option('--ets-loader-home', help='path to ets-loader dir.') 53 parser.add_option('--app-profile', default=False, help='path to app-profile.') 54 parser.add_option('--manifest-file-path', help='path to manifest.json dir.') 55 56 options, _ = parser.parse_args(args) 57 options.js_assets_dir = build_utils.parse_gn_list(options.js_assets_dir) 58 options.ets_assets_dir = build_utils.parse_gn_list(options.ets_assets_dir) 59 options.js_forms_dir = build_utils.parse_gn_list(options.js_forms_dir) 60 options.testrunner_dir = build_utils.parse_gn_list(options.testrunner_dir) 61 return options 62 63 64def make_my_env(options, js2abc: bool) -> dict: 65 out_dir = os.path.abspath(os.path.dirname(options.output)) 66 gen_dir = os.path.join(out_dir, "gen") 67 assets_dir = os.path.join(out_dir, "assets") 68 if options.app_profile: 69 if js2abc: 70 assets_dir = os.path.join(assets_dir, "js") 71 else: 72 assets_dir = os.path.join(assets_dir, "ets") 73 my_env = { 74 "aceModuleBuild": assets_dir, 75 "buildMode": options.build_mode, 76 "PATH": os.environ.get('PATH'), 77 "appResource": os.path.join(gen_dir, "ResourceTable.txt") 78 } 79 if options.app_profile: 80 my_env["aceProfilePath"] = os.path.join(gen_dir, "resources/base/profile") 81 my_env["aceModuleJsonPath"] = os.path.abspath(options.hap_profile) 82 return my_env 83 84 85def make_manifest_data(config: dict, options, js2abc: bool, asset_index: int, assets_cnt: int, src_path: str) -> dict: 86 data = dict() 87 data['appID'] = config['app']['bundleName'] 88 if options.app_profile: 89 data['versionName'] = config['app']['versionName'] 90 data['versionCode'] = config['app']['versionCode'] 91 data['pages'] = config['module']['pages'] 92 data['deviceType'] = config['module']['deviceTypes'] 93 else: 94 data['appName'] = config['module']['abilities'][asset_index].get('label') 95 data['versionName'] = config['app']['version']['name'] 96 data['versionCode'] = config['app']['version']['code'] 97 data['deviceType'] = config['module']['deviceType'] 98 for js_module in config['module']['js']: 99 js_module_name = js_module.get('name').split('.')[-1] 100 101 # According to the page name and ability entry match the corresponding page for ability 102 # Compatibility with mismatches due to "MainAbility" and "default" 103 if js_module_name == src_path or (js_module_name == 'MainAbility' and src_path == 'default') \ 104 or (js_module_name == 'default' and src_path == 'MainAbility'): 105 data['pages'] = js_module.get('pages') 106 data['window'] = js_module.get('window') 107 if js_module.get('type') == 'form' and options.js_forms_dir: 108 data['type'] = 'form' 109 if not js2abc: 110 data['mode'] = js_module.get('mode') 111 return data 112 113 114def build_ace(cmd: str, options, js2abc: bool, loader_home: str, assets_dir: str, assets_name: str): 115 my_env = make_my_env(options, js2abc) 116 gen_dir = my_env.get("aceModuleBuild") 117 assets_cnt = len(assets_dir) 118 use_compile_cache = os.environ.get("USE_COMPILE_CACHE", "false").lower() == "true" 119 for asset_index in range(assets_cnt): 120 ability_dir = os.path.relpath(assets_dir[asset_index], loader_home) 121 my_env["aceModuleRoot"] = ability_dir 122 if options.js_sources_file: 123 with open(options.js_sources_file, 'wb') as js_sources_file: 124 sources = get_all_js_sources(ability_dir) 125 js_sources_file.write('\n'.join(sources).encode()) 126 src_path = os.path.basename(assets_dir[asset_index]) 127 128 # Create a fixed directory for manifest.json 129 if js2abc: 130 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'js', src_path)) 131 my_env.update({"cachePath": os.path.join(build_dir, ".cache")}) 132 else: 133 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'ets', src_path)) 134 if not os.path.exists(build_dir): 135 os.makedirs(build_dir, exist_ok=True) 136 manifest = os.path.join(build_dir, 'manifest.json') 137 138 # Determine abilityType according to config.json 139 if assets_name == 'testrunner_dir': 140 my_env["abilityType"] = 'testrunner' 141 elif assets_name != 'js_forms_dir' and not options.app_profile and assets_cnt > 1: 142 with open(options.hap_profile) as profile: 143 config = json.load(profile) 144 if config['module']['abilities'][asset_index].__contains__('forms'): 145 my_env["abilityType"] = 'form' 146 else: 147 my_env["abilityType"] = config['module']['abilities'][asset_index]['type'] 148 else: 149 my_env["abilityType"] = 'page' 150 151 # Generate manifest.json only when abilityType is page 152 data = dict() 153 if my_env["abilityType"] == 'page': 154 with open(options.hap_profile) as profile: 155 config = json.load(profile) 156 data = make_manifest_data(config, options, js2abc, asset_index, assets_cnt, src_path) 157 build_utils.write_json(data, manifest) 158 159 # If missing page, skip it 160 if not data.__contains__('pages'): 161 print('Warning: There is no page matching this {}'.format(src_path)) 162 continue 163 164 if not options.app_profile: 165 my_env["aceManifestPath"] = manifest 166 my_env["aceModuleBuild"] = os.path.join(gen_dir, src_path) 167 168 enable_compile_cache(asset_index, assets_cnt, my_env, use_compile_cache) 169 build_utils.check_output(cmd, cwd=loader_home, env=my_env) 170 171 if options.app_profile: 172 gen_dir = os.path.dirname(gen_dir) 173 build_utils.zip_dir(options.output, gen_dir) 174 else: 175 build_utils.zip_dir(options.output, gen_dir, zip_prefix_path='assets/js/') 176 177 178def enable_compile_cache(asset_index: int, assets_cnt: int, env: dict, use_compile_cache: bool): 179 if use_compile_cache: 180 env["useCompileCache"] = "true" 181 if assets_cnt > 1 and asset_index == assets_cnt - 1: 182 env["addTestRunner"] = "true" 183 184 185def get_all_js_sources(base) -> list: 186 sources = [] 187 for root, _, files in os.walk(base): 188 for file in files: 189 if file[-3:] in ('.js', '.ts'): 190 sources.append(os.path.join(root, file)) 191 192 return sources 193 194 195def main(args): 196 options = parse_args(args) 197 198 inputs = [ 199 options.nodejs_path, options.webpack_js, options.webpack_config_js, options.webpack_config_ets 200 ] 201 depfiles = [] 202 if not options.js_assets_dir and not options.ets_assets_dir: 203 with ZipFile(options.output, 'w') as file: 204 return 205 206 if options.ark_es2abc_dir: 207 depfiles.extend(build_utils.get_all_files(options.ark_es2abc_dir)) 208 209 depfiles.append(options.webpack_js) 210 depfiles.append(options.webpack_config_js) 211 depfiles.append(options.webpack_config_ets) 212 depfiles.extend(build_utils.get_all_files(options.ace_loader_home)) 213 depfiles.extend(build_utils.get_all_files(options.ets_loader_home)) 214 215 node_js = os.path.relpath(options.nodejs_path, options.ace_loader_home) 216 assets_dict = dict() 217 if options.js_assets_dir: 218 assets_dict['js_assets_dir'] = options.js_assets_dir 219 if options.ets_assets_dir: 220 assets_dict['ets_assets_dir'] = options.ets_assets_dir 221 if options.js_forms_dir: 222 assets_dict['js_forms_dir'] = options.js_forms_dir 223 if options.testrunner_dir: 224 assets_dict['testrunner_dir'] = options.testrunner_dir 225 226 for assets_name, assets_dir in assets_dict.items(): 227 for asset in assets_dir: 228 depfiles.extend(build_utils.get_all_files(asset)) 229 if assets_name == 'ets_assets_dir': 230 js2abc = False 231 loader_home = options.ets_loader_home 232 webpack_config = options.webpack_config_ets 233 else: 234 js2abc = True 235 loader_home = options.ace_loader_home 236 webpack_config = options.webpack_config_js 237 cmd = [ 238 node_js, 239 os.path.relpath(options.webpack_js, loader_home), 240 '--config', 241 os.path.relpath(webpack_config, loader_home) 242 ] 243 ark_es2abc_dir = os.path.relpath(options.ark_es2abc_dir, loader_home) 244 if options.app_profile: 245 cmd.extend(['--env', 'buildMode={}'.format(options.build_mode), 'compilerType=ark', 246 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 247 else: 248 cmd.extend(['--env', 'compilerType=ark', 249 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 250 build_utils.call_and_write_depfile_if_stale( 251 lambda: build_ace(cmd, options, js2abc, loader_home, assets_dir, assets_name), 252 options, 253 depfile_deps=depfiles, 254 input_paths=depfiles + inputs, 255 input_strings=cmd + [options.build_mode], 256 output_paths=([options.output]), 257 force=False, 258 add_pydeps=False) 259 260if __name__ == '__main__': 261 sys.exit(main(sys.argv[1:])) 262