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): 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, options, js2abc, asset_index, assets_cnt, src_path): 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, options, js2abc, loader_home, assets_dir, assets_name): 115 my_env = make_my_env(options, js2abc) 116 gen_dir = my_env.get("aceModuleBuild") 117 assets_cnt = len(assets_dir) 118 for asset_index in range(assets_cnt): 119 ability_dir = os.path.relpath(assets_dir[asset_index], loader_home) 120 my_env["aceModuleRoot"] = ability_dir 121 if options.js_sources_file: 122 with open(options.js_sources_file, 'wb') as js_sources_file: 123 sources = get_all_js_sources(ability_dir) 124 js_sources_file.write('\n'.join(sources).encode()) 125 src_path = os.path.basename(assets_dir[asset_index]) 126 127 # Create a fixed directory for manifest.json 128 if js2abc: 129 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'js', src_path)) 130 my_env.update({"cachePath": os.path.join(build_dir, ".cache")}) 131 else: 132 build_dir = os.path.abspath(os.path.join(options.manifest_file_path, 'ets', src_path)) 133 if not os.path.exists(build_dir): 134 os.makedirs(build_dir, exist_ok=True) 135 manifest = os.path.join(build_dir, 'manifest.json') 136 137 # Determine abilityType according to config.json 138 if assets_name == 'testrunner_dir': 139 my_env["abilityType"] = 'testrunner' 140 elif assets_name != 'js_forms_dir' and not options.app_profile and assets_cnt > 1: 141 with open(options.hap_profile) as profile: 142 config = json.load(profile) 143 if config['module']['abilities'][asset_index].__contains__('forms'): 144 my_env["abilityType"] = 'form' 145 else: 146 my_env["abilityType"] = config['module']['abilities'][asset_index]['type'] 147 else: 148 my_env["abilityType"] = 'page' 149 150 # Generate manifest.json only when abilityType is page 151 data = dict() 152 if my_env["abilityType"] == 'page': 153 with open(options.hap_profile) as profile: 154 config = json.load(profile) 155 data = make_manifest_data(config, options, js2abc, asset_index, assets_cnt, src_path) 156 build_utils.write_json(data, manifest) 157 158 # If missing page, skip it 159 if not data.__contains__('pages'): 160 print('Warning: There is no page matching this {}'.format(src_path)) 161 continue 162 163 if not options.app_profile: 164 my_env["aceManifestPath"] = manifest 165 my_env["aceModuleBuild"] = os.path.join(gen_dir, src_path) 166 build_utils.check_output(cmd, cwd=loader_home, env=my_env) 167 168 if options.app_profile: 169 gen_dir = os.path.dirname(gen_dir) 170 build_utils.zip_dir(options.output, gen_dir) 171 else: 172 build_utils.zip_dir(options.output, gen_dir, zip_prefix_path='assets/js/') 173 174 175def get_all_js_sources(base): 176 sources = [] 177 for root, _, files in os.walk(base): 178 for file in files: 179 if file[-3:] in ('.js', '.ts'): 180 sources.append(os.path.join(root, file)) 181 182 return sources 183 184 185def main(args): 186 options = parse_args(args) 187 188 inputs = [ 189 options.nodejs_path, options.webpack_js, options.webpack_config_js, options.webpack_config_ets 190 ] 191 depfiles = [] 192 if not options.js_assets_dir and not options.ets_assets_dir: 193 with ZipFile(options.output, 'w') as file: 194 return 195 196 if options.ark_es2abc_dir: 197 depfiles.extend(build_utils.get_all_files(options.ark_es2abc_dir)) 198 199 depfiles.append(options.webpack_js) 200 depfiles.append(options.webpack_config_js) 201 depfiles.append(options.webpack_config_ets) 202 depfiles.extend(build_utils.get_all_files(options.ace_loader_home)) 203 depfiles.extend(build_utils.get_all_files(options.ets_loader_home)) 204 205 node_js = os.path.relpath(options.nodejs_path, options.ace_loader_home) 206 assets_dict = dict() 207 if options.js_assets_dir: 208 assets_dict['js_assets_dir'] = options.js_assets_dir 209 if options.ets_assets_dir: 210 assets_dict['ets_assets_dir'] = options.ets_assets_dir 211 if options.js_forms_dir: 212 assets_dict['js_forms_dir'] = options.js_forms_dir 213 if options.testrunner_dir: 214 assets_dict['testrunner_dir'] = options.testrunner_dir 215 216 for assets_name, assets_dir in assets_dict.items(): 217 for asset in assets_dir: 218 depfiles.extend(build_utils.get_all_files(asset)) 219 if assets_name == 'ets_assets_dir': 220 js2abc = False 221 loader_home = options.ets_loader_home 222 webpack_config = options.webpack_config_ets 223 else: 224 js2abc = True 225 loader_home = options.ace_loader_home 226 webpack_config = options.webpack_config_js 227 cmd = [ 228 node_js, 229 os.path.relpath(options.webpack_js, loader_home), 230 '--config', 231 os.path.relpath(webpack_config, loader_home) 232 ] 233 ark_es2abc_dir = os.path.relpath(options.ark_es2abc_dir, loader_home) 234 if options.app_profile: 235 cmd.extend(['--env', 'buildMode={}'.format(options.build_mode), 'compilerType=ark', 236 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 237 else: 238 cmd.extend(['--env', 'compilerType=ark', 239 'arkFrontendDir={}'.format(ark_es2abc_dir), 'nodeJs={}'.format(node_js)]) 240 build_utils.call_and_write_depfile_if_stale( 241 lambda: build_ace(cmd, options, js2abc, loader_home, assets_dir, assets_name), 242 options, 243 depfile_deps=depfiles, 244 input_paths=depfiles + inputs, 245 input_strings=cmd + [options.build_mode], 246 output_paths=([options.output]), 247 force=False, 248 add_pydeps=False) 249 250if __name__ == '__main__': 251 sys.exit(main(sys.argv[1:])) 252