1#!/bin/bash 2 3# Copyright (c) 2022 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 16set -e 17 18CUR_PATH=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd) 19BASE_PATH=$(dirname ${CUR_PATH}) 20ROOT_PATH=$(cd ${CUR_PATH}/../../.. && pwd) && cd - 21 22arg_project="" 23arg_sdk_path="" 24arg_build_sdk="false" 25arg_help="0" 26arg_url="" 27arg_branch="" 28arg_npm="" 29arg_out_path="${ROOT_PATH}/out/hap" 30arg_sign_tool="${ROOT_PATH}/developtools/hapsigner/dist" 31arg_p7b="" 32arg_apl="normal" 33arg_feature="hos_normal_app" 34arg_profile="UnsgnedReleasedProfileTemplate.json" 35arg_bundle_name="" 36 37function print_help() { 38 cat <<EOF 39 use assembleHap [options] <mainclass> [args...] 40 41EOF 42} 43 44 45function clear_dir(){ 46 if [ ! -d "$1" ]; then 47 mkdir -p $1 48 fi 49} 50 51 52function is_project_root(){ 53 if [[ -f $1"/build-profile.json5" && -f $1"/hvigorfile.js" ]]; then 54 return 0 55 else 56 return 1 57 fi 58} 59 60function build_sdk() { 61 if [ -d ${ROOT_PATH}/out/sdk/packages/ohos-sdk/linux ]; then 62 echo "ohos-sdk exists." 63 return 0 64 fi 65 pushd ${ROOT_PATH} 66 echo "building the latest ohos-sdk..." 67 ./build.sh --product-name ohos-sdk 68 pushd ${ROOT_PATH}/out/sdk/packages/ohos-sdk/linux 69 ls -d */ | xargs rm -rf 70 for i in $(ls); do 71 unzip $i 72 done 73 for f in $(find . -name npm-install.js); do 74 pushd $(dirname $f) 75 node npm-install.js 76 popd 77 done 78 api_version=$(grep apiVersion toolchains/oh-uni-package.json | awk '{print $2}' | sed -r 's/\",?//g') 79 sdk_version=$(grep version toolchains/oh-uni-package.json | awk '{print $2}' | sed -r 's/\",?//g') 80 for i in $(ls -d */); do 81 mkdir -p $api_version 82 mv $i $api_version 83 mkdir $i 84 ln -s ../$api_version/$i $i/$sdk_version 85 done 86 popd 87 popd 88} 89 90function parse_arguments() { 91 local helperKey=""; 92 local helperValue=""; 93 local current=""; 94 95 while [ "$1" != "" ]; do 96 current=$1; 97 helperKey=${current#*--}; 98 helperKey=${helperKey%%=*}; 99 helperKey=$(echo "$helperKey" | tr '-' '_'); 100 helperValue=${current#*=}; 101 if [ "$helperValue" == "$current" ]; then 102 helperValue="1"; 103 fi 104 #echo "eval arg_$helperKey=\"$helperValue\""; 105 106 eval "arg_$helperKey=\"$helperValue\""; 107 shift 108 done 109} 110 111 112parse_arguments ${@}; 113 114 115if [ "$arg_help" != "0" ]; then 116 print_help; 117 exit 1; 118fi 119 120 121if [ "${arg_project}" == "" -a "${arg_url}" == "" ]; then 122 echo "--project or --url is not null" 123 exit 1; 124fi 125 126 127if [ ! -d "${arg_project}" ]; then 128 echo "${arg_project} is not dir" 129 exit 1; 130fi 131 132 133if [[ ${arg_project} = */ ]]; then 134 arg_project=${arg_project%/} 135fi 136 137 138if [[ ${arg_sign_tool} = */ ]]; then 139 arg_sign_tool=${arg_sign_tool%/} 140fi 141 142if [[ ${arg_p7b} = "" ]]; then 143 arg_p7b=$(find ${arg_project} -name *.p7b | head -n 1) 144 if [[ ${arg_p7b} = "" ]]; then 145 arg_p7b=openharmony_sx.p7b 146 fi 147fi 148 149clear_dir ${arg_out_path} 150export OHOS_SDK_HOME=${arg_sdk_path} 151echo "use sdk:"${OHOS_SDK_HOME} 152npm config set ${arg_npm} 153echo "npm config set ${arg_npm}" 154 155 156if [ "${arg_url}" != "" ]; then 157 if [ "${arg_branch}" == "" ]; then 158 echo "branch is not null" 159 exit 1 160 fi 161 project_name=${arg_url##*/} 162 project_name=${project_name%%.git*} 163 if test -d ${BASE_PATH}/projects/${project_name} 164 then 165 echo "${project_name} exists,ready for update..." 166 cd ${BASE_PATH}/projects/${project_name} 167 git fetch origin ${arg_branch} 168 git reset --hard origin/${arg_branch} 169 git pull 170 else 171 echo "${project_name} dose not exist,ready to download..." 172 cd ${BASE_PATH}/projects 173 git clone -b ${arg_branch} ${arg_url} ${project_name} 174 fi 175 arg_project=${BASE_PATH}/projects/${project_name} 176fi 177 178 179if ! is_project_root ${arg_project}; then 180 echo "${arg_project} is not OpenHarmony Project" 181 exit 1; 182fi 183 184if [ "${arg_build_sdk}" == "true" ]; then 185 build_sdk 186 export OHOS_SDK_HOME=${ROOT_PATH}/out/sdk/packages/ohos-sdk/linux 187 echo "set OHOS_SDK_HOME to" ${OHOS_SDK_HOME} 188fi 189 190echo "start build hap..." 191cd ${arg_project} 192 193 194module_list=() 195module_name=() 196out_module=() 197bundle_name="" 198 199 200function del_module_name(){ 201 name=${1} 202 for i in "${!module_name[@]}" 203 do 204 if [[ "${module_name[i]}" == "${name}" ]]; then 205 unset module_name[i] 206 echo "移除"${name}" , 剩余 : "${module_name[@]} 207 return 0 208 fi 209 done 210 return 1 211} 212 213 214function load_dep(){ 215 cur_m_n=${1} 216 for cur_module in ${module_list[@]} 217 do 218 if [[ "${cur_module}" =~ "${cur_m_n}" ]]; then 219 del_module_name ${cur_m_n} 220 for m_n_1 in ${module_name[@]} 221 do 222 rr=$(cat ${cur_module}"/package.json" | grep "${m_n_1}" || true) 223 if [[ "${rr}" != "" ]]; then 224 load_dep ${m_n_1} 225 fi 226 done 227 cd ${cur_module} 228 echo ${cur_module}" 执行npm install" 229 npm i 230 fi 231 done 232} 233 234 235while read line 236do 237 if [[ ${line} =~ "srcPath" ]]; then 238 pa=${line%\"*} 239 pa=${pa##*\".} 240 module_list[${#module_list[*]}]=${arg_project}${pa} 241 module_name[${#module_name[*]}]=${pa} 242 if [ -d "${arg_project}/AppScope" ]; then 243 cur_bundle_line=`cat ${arg_project}/AppScope/app.json5 | grep "\"bundleName\""` 244 bundle_name=${cur_bundle_line%\"*} 245 bundle_name=${bundle_name##*\"} 246 # echo "bundleName : "${bundle_name} 247 is_entry=`cat ${arg_project}${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"entry\"" || true` 248 is_feature=`cat ${arg_project}${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"feature\"" || true` 249 if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then 250 echo "hap输出module: "${arg_project}${pa} 251 out_module[${#out_module[*]}]=${arg_project}${pa} 252 fi 253 else 254 cur_bundle_line=`cat ${arg_project}${pa}/src/main/config.json | grep "\"bundleName\""` 255 bundle_name=${cur_bundle_line%\"*} 256 bundle_name=${bundle_name##*\"} 257 # echo "bundleName : "${bundle_name} 258 is_entry=`cat ${arg_project}${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"entry\"" || true` 259 is_feature=`cat ${arg_project}${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"feature\"" || true` 260 if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then 261 echo "hap输出module: "${arg_project}${pa} 262 out_module[${#out_module[*]}]=${arg_project}${pa} 263 fi 264 fi 265 fi 266done < ${arg_project}"/build-profile.json5" 267 268 269for module in ${module_list[@]} 270do 271 if del_module_name ${module##${arg_project}}; then 272 for m_n in ${module_name[@]} 273 do 274 rr=$(cat ${module}"/package.json" | grep "${m_n}" || true) 275 if [[ "${rr}" != "" ]]; then 276 load_dep ${m_n} 277 fi 278 done 279 cd ${module} 280 echo ${module}" 执行npm install" 281 npm i 282 fi 283done 284 285 286cd ${arg_project} 287echo ${arg_project}" 执行npm install" 288npm install 289node ./node_modules/@ohos/hvigor/bin/hvigor.js clean 290node ./node_modules/@ohos/hvigor/bin/hvigor.js --mode module clean assembleHap -p debuggable=false 291 292 293for module in ${out_module[@]} 294do 295 cur_out_module_name=${module##*/} 296 is_sign=false 297 echo "module = ${module} , cur_out_module_name=${cur_out_module_name}" 298 if [ ! -d ${module}/build/default/outputs/default/ ]; then 299 echo "module = ${module}, assembleHap error !!!" 300 continue 301 fi 302 for out_file in `ls ${module}/build/default/outputs/default/` 303 do 304 if [[ "${out_file}" =~ "-signed.hap" ]]; then 305 is_sign=true 306 echo "发现signed包 : "${out_file}",直接归档" 307 cp ${module}/build/default/outputs/default/${out_file} ${arg_out_path}/ 308 break 309 fi 310 done 311 if test ${is_sign} = false 312 then 313 hap_name=${arg_project##*/} 314 # echo "${hap_name},skip sign 'hap'. Invalid signingConfig is configured for 'default' product." 315 for out_file in `ls ${module}/build/default/outputs/default/` 316 do 317 if [[ "${out_file}" =~ "-unsigned.hap" ]]; then 318 echo "发现unsigned包 : "${out_file}",开始使用签名工具签名" 319 nosign_hap_path=${module}/build/default/outputs/default/${out_file} 320 sign_hap_path=${module}/build/default/outputs/default/${out_file/unsigned/signed} 321 cp -r ${arg_sign_tool} ${arg_project}/ 322 cd ${arg_project}/dist 323 if [ ! -e ${arg_profile} ]; then 324 echo "${arg_profile} not exist! ! !" 325 exit 1 326 fi 327 if [ "${arg_bundle_name}" != "" ]; then 328 sed -i "s/\"com.OpenHarmony.app.test\"/\"${arg_bundle_name}\"/g" ${arg_profile} 329 else 330 sed -i "s/\"com.OpenHarmony.app.test\"/\"${bundle_name}\"/g" ${arg_profile} 331 fi 332 sed -i "s/\"normal\"/\"${arg_apl}\"/g" ${arg_profile} 333 sed -i "s/\"system_basic\"/\"${arg_apl}\"/g" ${arg_profile} 334 sed -i "s/\"system_core\"/\"${arg_apl}\"/g" ${arg_profile} 335 sed -i "s/\"hos_normal_app\"/\"${arg_feature}\"/g" ${arg_profile} 336 sed -i "s/\"hos_system_app\"/\"${arg_feature}\"/g" ${arg_profile} 337 java -jar hap-sign-tool.jar sign-profile -keyAlias "openharmony application profile release" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "OpenHarmonyProfileRelease.pem" -inFile "${arg_profile}" -keystoreFile "OpenHarmony.p12" -outFile "openharmony_sx.p7b" -keyPwd "123456" -keystorePwd "123456" 338 java -jar hap-sign-tool.jar sign-app -keyAlias "openharmony application release" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "OpenHarmonyApplication.pem" -profileFile "${arg_p7b}" -inFile "${nosign_hap_path}" -keystoreFile "OpenHarmony.p12" -outFile "${sign_hap_path}" -keyPwd "123456" -keystorePwd "123456" 339 cp ${sign_hap_path} ${arg_out_path}/ 340 is_sign=true 341 break 342 fi 343 done 344 if test ${is_sign} = false 345 then 346 echo "${module} assembleHap error !!!" 347 rm -rf ${arg_project}/sign_helper 348 exit 1 349 fi 350 fi 351done 352rm -rf ${arg_project}/sign_helper 353 354 355exit 0 356