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-ts2abc-dir', help='path to ark ts2abc dir') 51 parser.add_option('--ark-es2abc-dir', help='path to ark es2abc dir') 52 parser.add_option('--ace-loader-home', help='path to ace-loader dir.') 53 parser.add_option('--ets-loader-home', help='path to ets-loader dir.') 54 parser.add_option('--app-profile', default=False, help='path to app-profile.') 55 parser.add_option('--manifest-file-path', help='path to manifest.json dir.') 56 57 options, _ = parser.parse_args(args) 58 options.js_assets_dir = build_utils.parse_gn_list(options.js_assets_dir) 59 options.ets_assets_dir = build_utils.parse_gn_list(options.ets_assets_dir) 60 options.js_forms_dir = build_utils.parse_gn_list(options.js_forms_dir) 61 options.testrunner_dir = build_utils.parse_gn_list(options.testrunner_dir) 62 return options 63 64def make_my_env(options, js2abc): 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 84def make_manifest_data(config, options, js2abc, asset_index, assets_cnt, src_path): 85 data = dict() 86 data['appID'] = config['app']['bundleName'] 87 if options.app_profile: 88 data['versionName'] = config['app']['versionName'] 89 data['versionCode'] = config['app']['versionCode'] 90 data['pages'] = config['module']['pages'] 91 data['deviceType'] = config['module']['deviceTypes'] 92 else: 93 data['appName'] = config['module']['abilities'][asset_index].get('label') 94 data['versionName'] = config['app']['version']['name'] 95 data['versionCode'] = config['app']['version']['code'] 96 data['deviceType'] = config['module']['deviceType'] 97 for js_module in config['module']['js']: 98 js_module_name = js_module.get('name').split('.')[-1] 99 100 # According to the page name and ability entry match the corresponding page for ability 101 # Compatibility with mismatches due to "MainAbility" and "default" 102 if js_module_name == src_path or (js_module_name == 'MainAbility' and src_path == 'default') \ 103 or (js_module_name == 'default' and src_path == 'MainAbility'): 104 data['pages'] = js_module.get('pages') 105 data['window'] = js_module.get('window') 106 if js_module.get('type') == 'form' and options.js_forms_dir: 107 data['type'] = 'form' 108 if not js2abc: 109 data['mode'] = js_module.get('mode') 110 return data 111 112def build_ace(cmd, options, js2abc, loader_home, assets_dir, assets_name): 113 my_env = make_my_env(options, js2abc) 114 gen_dir = my_env.get("aceModuleBuild") 115 assets_cnt = len(assets_dir) 116 for asset_index in range(assets_cnt): 117 ability_dir = os.path.relpath(assets_dir[asset_index], loader_home) 118 my_env["aceModuleRoot"] = ability_dir 119 if options.js_sources_file: 120 with open(options.js_sources_file, 'wb') as js_sources_file: 121 sources = get_all_js_sources(ability_dir) 122 js_sources_file.write('\n'.join(sources).encode()) 123 src_path = os.path.basename(assets_dir[asset_index]) 124 125 # Create a fixed directory for manifest.json 126 if js2abc: 127 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'js', src_path)) 128 my_env.update({"cachePath": os.path.join(build_dir, ".cache")}) 129 else: 130 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'ets', src_path)) 131 if not os.path.exists(build_dir): 132 os.makedirs(build_dir) 133 manifest = os.path.join(build_dir, 'manifest.json') 134 135 # Determine abilityType according to config.json 136 if assets_name == 'testrunner_dir': 137 my_env["abilityType"] = 'testrunner' 138 elif assets_name != 'js_forms_dir' and not options.app_profile and assets_cnt > 1: 139 with open(options.hap_profile) as profile: 140 config = json.load(profile) 141 if config['module']['abilities'][asset_index].__contains__('forms'): 142 my_env["abilityType"] = 'form' 143 else: 144 my_env["abilityType"] = config['module']['abilities'][asset_index]['type'] 145 else: 146 my_env["abilityType"] = 'page' 147 148 # Generate manifest.json only when abilityType is page 149 data = dict() 150 if my_env["abilityType"] == 'page': 151 with open(options.hap_profile) as profile: 152 config = json.load(profile) 153 data = make_manifest_data(config, options, js2abc, asset_index, assets_cnt, src_path) 154 build_utils.write_json(data, manifest) 155 156 # If missing page, skip it 157 if not data.__contains__('pages'): 158 print('Warning: There is no page matching this {}'.format(src_path)) 159 continue 160 161 if not options.app_profile: 162 my_env["aceManifestPath"] = manifest 163 my_env["aceModuleBuild"] = os.path.join(gen_dir, src_path) 164 build_utils.check_output(cmd, cwd=loader_home, env=my_env) 165 166 if options.app_profile: 167 gen_dir = os.path.dirname(gen_dir) 168 build_utils.zip_dir(options.output, gen_dir) 169 else: 170 build_utils.zip_dir(options.output, gen_dir, zip_prefix_path='assets/js/') 171 172def get_all_js_sources(base): 173 sources = [] 174 for root, _, files in os.walk(base): 175 for file in files: 176 if file[-3:] in ('.js', '.ts'): 177 sources.append(os.path.join(root, file)) 178 179 return sources 180 181def main(args): 182 options = parse_args(args) 183 184 inputs = [ 185 options.nodejs_path, options.webpack_js, options.webpack_config_js, options.webpack_config_ets 186 ] 187 depfiles = [] 188 if not options.js_assets_dir and not options.ets_assets_dir: 189 with ZipFile(options.output, 'w') as file: 190 return 191 192 if options.ark_ts2abc_dir: 193 depfiles.extend(build_utils.get_all_files(options.ark_ts2abc_dir)) 194 195 if options.ark_es2abc_dir: 196 depfiles.extend(build_utils.get_all_files(options.ark_es2abc_dir)) 197 198 depfiles.append(options.webpack_js) 199 depfiles.append(options.webpack_config_js) 200 depfiles.append(options.webpack_config_ets) 201 depfiles.extend(build_utils.get_all_files(options.ace_loader_home)) 202 depfiles.extend(build_utils.get_all_files(options.ets_loader_home)) 203 204 node_js = os.path.relpath(options.nodejs_path, options.ace_loader_home) 205 assets_dict = dict() 206 if options.js_assets_dir: 207 assets_dict['js_assets_dir'] = options.js_assets_dir 208 if options.ets_assets_dir: 209 assets_dict['ets_assets_dir'] = options.ets_assets_dir 210 if options.js_forms_dir: 211 assets_dict['js_forms_dir'] = options.js_forms_dir 212 if options.testrunner_dir: 213 assets_dict['testrunner_dir'] = options.testrunner_dir 214 215 for assets_name, assets_dir in assets_dict.items(): 216 for asset in assets_dir: 217 depfiles.extend(build_utils.get_all_files(asset)) 218 if assets_name == 'ets_assets_dir': 219 js2abc = False 220 loader_home = options.ets_loader_home 221 webpack_config = options.webpack_config_ets 222 else: 223 js2abc = True 224 loader_home = options.ace_loader_home 225 webpack_config = options.webpack_config_js 226 cmd = [ 227 node_js, 228 os.path.relpath(options.webpack_js, loader_home), 229 '--config', 230 os.path.relpath(webpack_config, loader_home) 231 ] 232 ark_es2abc_dir = os.path.relpath(options.ark_es2abc_dir, loader_home) 233 if options.app_profile: 234 cmd.extend(['--env', 'buildMode={}'.format(options.build_mode), 'compilerType=ark', 235 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 236 else: 237 cmd.extend(['--env', 'compilerType=ark', 238 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 239 build_utils.call_and_write_depfile_if_stale( 240 lambda: build_ace(cmd, options, js2abc, loader_home, assets_dir, assets_name), 241 options, 242 depfile_deps=depfiles, 243 input_paths=depfiles + inputs, 244 input_strings=cmd + [options.build_mode], 245 output_paths=([options.output]), 246 force=False, 247 add_pydeps=False) 248 249if __name__ == '__main__': 250 sys.exit(main(sys.argv[1:])) 251