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 argparse 17import os 18import sys 19import shutil 20import subprocess 21from convert_permissions import convert_permissions 22sys.path.append( 23 os.path.dirname( 24 os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) 25from scripts.util.file_utils import read_json_file, write_json_file 26 27OUT_ROOT_LIST = ["ets1.1/sdk-js", 28 "ets1.2/sdk-js"] 29OUT_SDK_TYPE = ["ets", "ets2"] 30OUT_PERMISSION_FILE = ["permissions.d.ts", "permissions.d.ets"] 31INTERFACE_PATH = "interface/sdk-js" 32OUT_ROOT = "out/sdk-public" 33API_MODIFY_DIR = "build-tools" 34API_MODIFY_TOOL = "delete_systemapi_plugin.js" 35ETS_MODIFY_TOOL = "handleApiFiles.js" 36API_PATH = "api" 37API_GEN_PATH = "build-tools/api" 38KITS_PATH = "kits" 39KITS_GEN_PATH = "build-tools/kits" 40ARKTS_PATH = "arkts" 41ARKTS_GEN_PATH = "build-tools/arkts" 42PARSE_ETS2_API = "arkui_transformer" 43PACKAGE_PATH = "build/arkui_transformer.js" 44TYPE_MODIFY_TOOL = "intToNumber.js" 45TYPE_CHANGE_DIR = "type-change" 46TYPE_CHANGE_API_GEN_PATH = "build-tools/type-change/api" 47TYPE_CHANGE_ARKTS_GEN_PATH = "build-tools/type-change/arkts" 48 49 50def copy_sdk_interface(source_root: str, out_path: str): 51 source = os.path.join(source_root, INTERFACE_PATH) 52 dest = os.path.join(source_root, out_path) 53 if os.path.exists(dest): 54 shutil.rmtree(dest) 55 shutil.copytree(source, dest) 56 57 58def replace_sdk_dir(root_build_dir: str, from_path: str, to_path: str): 59 dest = os.path.join(root_build_dir, to_path) 60 if os.path.exists(dest): 61 shutil.rmtree(dest) 62 source = os.path.join(root_build_dir, from_path) 63 shutil.copytree(source, dest) 64 65 66def copy_arkts_api_method(source_root: str, out_path: str, nodejs: str, sdk_type: str): 67 input_path = os.path.join(source_root, INTERFACE_PATH) 68 input_path = os.path.abspath(input_path) 69 output_path = os.path.join(source_root, out_path) 70 output_path = os.path.abspath(output_path) 71 if os.path.exists(output_path): 72 shutil.rmtree(output_path) 73 74 tool = os.path.join(source_root, INTERFACE_PATH, API_MODIFY_DIR, 75 ETS_MODIFY_TOOL) 76 tool = os.path.abspath(tool) 77 nodejs = os.path.abspath(nodejs) 78 p = subprocess.Popen([nodejs, tool, "--path", input_path, "--output", output_path, "--type", 79 sdk_type, "--create-keep-file", "true"], stdout=subprocess.PIPE) 80 p.wait() 81 82 83def remove_system_api_method(source_root: str, out_path: str, nodejs: str, sdk_type: str): 84 85 tool = os.path.join(source_root, INTERFACE_PATH, API_MODIFY_DIR, API_MODIFY_TOOL) 86 tool = os.path.abspath(tool) 87 api_dir = os.path.join(source_root, out_path, API_PATH) 88 api_dir = os.path.abspath(api_dir) 89 api_out_dir = os.path.join(source_root, out_path, API_MODIFY_DIR) 90 api_out_dir = os.path.abspath(api_out_dir) 91 92 nodejs = os.path.abspath(nodejs) 93 p = subprocess.Popen([nodejs, tool, "--input", api_dir, "--output", 94 api_out_dir, "--type", sdk_type], stdout=subprocess.PIPE) 95 p.wait() 96 97 98def compile_package(options, out_path: str): 99 tool_path = os.path.abspath(os.path.join(options.root_build_dir, INTERFACE_PATH, API_MODIFY_DIR, PARSE_ETS2_API)) 100 npm = os.path.abspath(options.npm_path) 101 package_path = os.path.abspath(os.path.join(tool_path, PACKAGE_PATH)) 102 nodejs = os.path.abspath(options.node_js) 103 input_dir = os.path.abspath(os.path.join(options.root_build_dir, out_path, "api/@internal/component/ets")) 104 output = os.path.abspath(os.path.join(options.output_interface_sdk, "arkui_transformer_api")) 105 custom_env = { 106 'PATH': f"{os.path.dirname(os.path.abspath(options.node_js))}:{os.environ.get('PATH')}", 107 'NODE_HOME': os.path.dirname(os.path.abspath(options.node_js)), 108 } 109 110 process = subprocess.run([npm, "run", "compile:arkui"], env=custom_env, cwd=tool_path, shell=False) 111 112 if os.path.exists(package_path): 113 p = subprocess.run([nodejs, package_path, "--input-dir", input_dir, "--target-dir", output], cwd=tool_path, 114 shell=False) 115 else: 116 print("arkui_transformer: tool path does not exist") 117 118 return process 119 120 121def change_int_to_number(source_root: str, out_path: str, nodejs: str, sdk_type: str): 122 tool = os.path.join(source_root, INTERFACE_PATH, 123 API_MODIFY_DIR, TYPE_MODIFY_TOOL) 124 tool = os.path.abspath(tool) 125 api_dir = os.path.join(source_root, out_path) 126 api_dir = os.path.abspath(api_dir) 127 api_out_dir = os.path.join(source_root, out_path, API_MODIFY_DIR, TYPE_CHANGE_DIR) 128 api_out_dir = os.path.abspath(api_out_dir) 129 130 nodejs = os.path.abspath(nodejs) 131 process_list = [] 132 # 根据规格并发执行文本处理逻辑 133 for index in range(10): 134 process_list.append(subprocess.Popen([nodejs, tool, "--path", api_dir, "--output", api_out_dir, "--index", 135 str(index)], stdout=subprocess.PIPE)) 136 137 for process_obj in process_list: 138 process_obj.wait() 139 140 141def regenerate_sdk_config_file(sdk_build_arkts: str, sdk_description_file: str, 142 output_sdk_desc_file: str): 143 info_list = read_json_file(sdk_description_file) 144 if sdk_build_arkts != "true": 145 arkts_sdk_info_list = [] 146 for info in info_list: 147 install_label_str = str(info.get("install_dir")) 148 if install_label_str.startswith("ets/ets1.2/"): 149 continue 150 elif install_label_str.startswith("ets/ets1.1/"): 151 info["install_dir"] = str(info.get("install_dir")).replace("ets/ets1.1/", "ets/") 152 arkts_sdk_info_list.append(info) 153 else: 154 arkts_sdk_info_list = info_list 155 write_json_file(output_sdk_desc_file, arkts_sdk_info_list) 156 157 158def parse_step(options): 159 for i, out_path in enumerate(OUT_ROOT_LIST): 160 sdk_type = OUT_SDK_TYPE[i] 161 permission_file = OUT_PERMISSION_FILE[i] 162 out_path = os.path.join(options.output_interface_sdk, out_path) 163 out_path = os.path.relpath(out_path, options.root_build_dir) 164 copy_arkts_api_method(options.root_build_dir, out_path, options.node_js, sdk_type) 165 166 if options.sdk_build_public == "true": 167 remove_system_api_method( 168 options.root_build_dir, out_path, options.node_js, sdk_type) 169 replace_sdk_dir(options.root_build_dir, os.path.join( 170 out_path, API_GEN_PATH), os.path.join(out_path, API_PATH)) 171 replace_sdk_dir(options.root_build_dir, os.path.join( 172 out_path, KITS_GEN_PATH), os.path.join(out_path, KITS_PATH)) 173 replace_sdk_dir(options.root_build_dir, os.path.join( 174 out_path, ARKTS_GEN_PATH), os.path.join(out_path, ARKTS_PATH)) 175 176 convert_permissions(options.root_build_dir, out_path, 177 permission_file, options.node_js) 178 179 if sdk_type == "ets": 180 change_int_to_number( 181 options.root_build_dir, out_path, options.node_js, sdk_type) 182 replace_sdk_dir(options.root_build_dir, os.path.join( 183 out_path, TYPE_CHANGE_API_GEN_PATH), os.path.join(out_path, API_PATH)) 184 replace_sdk_dir(options.root_build_dir, os.path.join( 185 out_path, TYPE_CHANGE_ARKTS_GEN_PATH), os.path.join(out_path, ARKTS_PATH)) 186 187 if sdk_type == "ets2": 188 compile_package(options, out_path) 189 190 191def main(): 192 parser = argparse.ArgumentParser() 193 parser.add_argument('--sdk-description-file', required=True) 194 parser.add_argument('--root-build-dir', required=True) 195 parser.add_argument('--node-js', required=True) 196 parser.add_argument('--output-arkts-sdk-desc-file', required=True) 197 parser.add_argument('--sdk-build-public', required=True) 198 parser.add_argument('--sdk-build-arkts', required=True) 199 parser.add_argument('--npm-path', required=True) 200 parser.add_argument('--output-interface-sdk', required=True) 201 202 options = parser.parse_args() 203 204 regenerate_sdk_config_file(options.sdk_build_arkts, options.sdk_description_file, 205 options.output_arkts_sdk_desc_file) 206 207 parse_step(options) 208 209 210if __name__ == '__main__': 211 sys.exit(main()) 212