1#  Copyright (c) 2023 Huawei Device Co., Ltd.
2#  Licensed under the Apache License, Version 2.0 (the "License");
3#  you may not use this file except in compliance with the License.
4#  You may obtain a copy of the License at
5
6#      http://www.apache.org/licenses/LICENSE-2.0
7
8#  Unless required by applicable law or agreed to in writing, software
9#  distributed under the License is distributed on an "AS IS" BASIS,
10#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11#  See the License for the specific language governing permissions and
12#  limitations under the License.
13
14#!/bin/bash
15
16CUR_PATH=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
17BASE_PATH=$(dirname ${CUR_PATH})
18
19#时间统计
20startTime=`date +%Y%m%d-%H:%M:%S`
21startTime_s=`date +%s`
22
23#npm
24npm config set @ohos:registry=https://repo.harmonyos.com/npm/
25
26#ohpm
27#ohpm config set @ohos:registry=https://repo.harmonyos.com/ohpm/
28
29TEST_HAP_PATH=entry/build/default/outputs/ohosTest/entry-ohosTest-unsigned.hap
30HAP_PATH=entry/build/default/outputs/default/entry-default-unsigned.hap
31STAGE_TEST_HAP_PATH=product/build/default/outputs/ohosTest/product-ohosTest-unsigned.hap
32STAGE_HAP_PATH=product/build/default/outputs/default/product-default-unsigned.hap
33TIME=16
34RESOLVE=0
35
36arg_sign="true"
37arg_sdk_path=${BASE_PATH}/sdk
38arg_full_sdk_path=${BASE_PATH}/sdk-full
39arg_help="0"
40arg_compile_dir=""
41arg_jdk=""
42arg_node=""
43arg_ohpm="false"
44arg_p=""
45
46url=""
47tag_url='url='
48name=""
49tag_name='name='
50branch=""
51tag_branch='branch='
52full_list=()
53tag_full_list='fullSdkAssembleList='
54basic_list=()
55tag_basic_list='basicSignList='
56core_list=()
57tag_core_list='coreSignList='
58system_app_list=()
59tag_system_app_list='systemAppList='
60normal_list=()
61public_list=()
62#没测试用例
63simple_list=() 
64
65function print_help() {
66  cat <<EOF
67  use assembleHap [options] <mainclass> [args...]
68
69    --p=config path
70    --sign=true/false - Whether the signature (default:$arg_sign)
71    --sdk_path=[public sdk absolute address] (default:$arg_sdk_path)
72    --full_sdk_path=[full sdk absolute address] (default:$arg_full_sdk_path)
73    --ohpm=true/false -whether use ohpm (default:$arg_ohpm)  
74    --help  - prints help screen
75
76EOF
77}
78
79function parse_arguments() {
80        local helperKey="";
81        local helperValue="";
82        local current="";
83
84        while [ "$1" != "" ]; do
85                current=$1;
86                helperKey=${current#*--};
87                helperKey=${helperKey%%=*};
88                helperKey=$(echo "$helperKey" | tr '-' '_');
89                helperValue=${current#*=};
90                if [ "$helperValue" == "$current" ]; then
91                        helperValue="1";
92                fi
93                # echo "eval arg_$helperKey=\"$helperValue\"";
94
95                eval "arg_$helperKey=\"$helperValue\"";
96                shift
97        done
98}
99
100parse_arguments ${@};
101
102if [ "$arg_help" != "0" ]; then
103        print_help;
104        exit 1;
105fi
106
107if [ "$arg_p" == "" ]; then
108        echo "--p cannot be empty"
109        exit 1
110fi
111
112if [ "$arg_jdk" != "" ]; then
113        export JAVA_HOME=${arg_jdk}
114        export PATH=$JAVA_HOME/bin:$PATH
115fi
116
117if [ "$arg_node" != "" ]; then
118        export NODE_HOME=${arg_node}
119        export PATH=$NODE_HOME:$PATH
120fi
121
122function clear_dir(){
123        if [ ! -d "$1" ]; then
124                mkdir -p $1
125        else
126                rm -rf $1/*
127        fi
128}
129function clear_file(){
130        if [ -f "$1" ]; then
131                rm $1
132        fi
133        touch $1
134}
135
136function del_dir(){
137        if [ -d "$1" ]; then
138                rm -rf $1/*
139        fi
140}
141function del_file(){
142        if [ -f "$1" ]; then
143                rm $1
144        fi
145}
146
147function is_project_root(){
148        if [[ -f $1"/build-profile.json5" && -f $1"/hvigorfile.js" ]] || [[ -f $1"/build-profile.json5" && -f $1"/hvigorfile.ts" ]]; then
149                while read line
150                do
151                        if [[ ${line} =~ "srcPath" ]]; then
152                                pa=${line%\"*}
153                                pa=${pa##*\".}
154                                if [ -d "$1/AppScope" ]; then
155                                        is_entry=`cat $1${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"entry\""`
156                                        is_feature=`cat $1${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"feature\""`
157                                        if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then
158                                                if [[ -d $1${pa}"/src/ohosTest" ]]; then
159                                                        return 0
160                                                fi
161                                        fi
162                                else
163                                        is_entry=`cat $1${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"entry\""`
164                                        is_feature=`cat $1${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"feature\""`
165                                        if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then
166                                                if [[ -d $1${pa}"/src/ohosTest" ]]; then
167                                                        return 0
168                                                fi
169                                        fi
170                                fi
171                        fi
172                done < $1"/build-profile.json5"
173                return 1
174        else
175                return 2
176        fi
177}
178
179function read_project(){
180        if [ -d "$1" ]
181        then
182                # 是项目工程根目录则记录当前路径 否则递归判断
183                is_project_root $1
184		res=$?
185                if [ ${res} -eq 0 ]; then
186			public_list[${#public_list[*]}]=${1#*compile-tool/projects/${name}/}
187                elif [ ${res} -eq 1 ]; then
188                        simple_list[${#simple_list[*]}]=${1#*compile-tool/projects/${name}/}
189		elif [ ${res} -eq 2 ]; then
190                        for file in `ls $1`
191                        do
192                                read_project "${1}/${file}"
193                        done
194                fi
195        fi
196}
197
198function exclude_full_project(){
199	if [ ${#full_list[*]} -eq 0 ]; then
200		return
201	fi
202	diff_list=()
203	full_true_list=()
204	f=0
205	t=0
206	flag=0
207	for item1 in "${public_list[@]}"; do
208		for item2 in "${full_list[@]}"; do
209			if [[ "${item1}" == "${item2}" ]]; then
210				full_true_list[f]=${item2}
211				f=$((f+1))
212				flag=1
213				break
214			fi
215		done
216		if [ ${flag} -eq 0 ]; then
217			diff_list[t]=${item1}
218			t=$((t+1))
219		else
220			flag=0
221		fi
222	done
223	public_list=(${diff_list[@]})
224	full_list=(${full_true_list[@]})
225	echo "use public sdk project numbers =  "${#public_list[@]}
226	echo "use full sdk project numbers =  "${#full_list[@]}
227}
228
229function del_module_name(){
230        local name=${1}
231        for i in "${!module_name[@]}"
232        do
233                if [[ "${module_name[i]}" == "${name}" ]]; then
234                        unset module_name[i]
235                        echo "移除"${name}" , 剩余 : "${module_name[@]} >> ${compile_result_error}/${hap}".log" 2>&1
236                        return 0
237                fi
238        done
239        return 1
240}
241
242function load_dep(){
243        local cur_m_n=${1}
244	local cur_module
245        for cur_module in ${module_list[@]}
246        do
247                if [[ "${cur_module}" =~ "${cur_m_n}" ]]; then
248                        del_module_name ${cur_m_n}
249                        for m_n_1 in ${module_name[@]}
250                        do
251                                rr=$(cat ${cur_module}"/package.json" | grep "${m_n_1}")
252                                if [[ "${rr}" != "" ]]; then
253                                        load_dep ${m_n_1}
254                                fi
255                        done
256                        cd ${cur_module}
257                        echo ${cur_module}" 执行npm install" >> ${compile_result_error}/${hap}".log" 2>&1
258                        npm i >> ${compile_result_error}/${hap}".log" 2>&1
259                fi
260        done
261}
262
263function load_oh_dep(){
264        local cur_m_n=${1}
265	local cur_module
266        for cur_module in ${module_list[@]}
267        do
268                if [[ "${cur_module}" =~ "${cur_m_n}" ]]; then
269                        del_module_name ${cur_m_n}
270                        for m_n_1 in ${module_name[@]}
271                        do
272                                rr=$(cat ${cur_module}"/oh-package.json5" | grep "${m_n_1}")
273                                if [[ "${rr}" != "" ]]; then
274                                        load_oh_dep ${m_n_1}
275                                fi
276                        done
277                        cd ${cur_module}
278                        echo ${cur_module}" 执行ohpm install" >> ${compile_result_error}/${hap}".log" 2>&1
279                        if [[ -e "oh-package.json5" ]]; then
280                                ohpm i >> ${compile_result_error}/${hap}".log" 2>&1
281                        else   
282                                ohpm init >> ${compile_result_error}/${hap}".log" 2>&1
283                                ohpm install >> ${compile_result_error}/${hap}".log" 2>&1
284                        fi      
285                fi
286        done
287}
288
289
290function compile(){
291        # echo "compile $1"
292        hap=${1////_}
293        project_path=${BASE_PATH}/projects/${name}/${1}
294	compile_result=${2}
295	compile_result_error=${3}
296	nosign_hap_result=${4}
297        compile_result_success=${5}
298        if [ ! -d "${project_path}" ]; then
299                echo "${project_path} 路径不存在"
300                return
301        fi
302        
303        if [ -e "${project_path}/local.properties" ]; then
304                rm ${project_path}"/local.properties"
305        fi
306        
307        # echo "sdk.dir="${arg_full_sdk_path} >> ${project_path}"/local.properties" 2>&1
308        cd ${project_path}
309        if [[ -e "${project_path}/package.json" ]]; then
310                sed -i 's/\"@ohos\/hypium\".*\.[0-9]*\"/\"@ohos\/hypium\": \"1.0.3\"/g' ./package.json
311                sed -i 's/\"@ohos\/hvigor-ohos-plugin\".*\.[0-9]*\"/\"@ohos\/hvigor-ohos-plugin\": \"1.3.1\"/g' ./package.json
312                sed -i 's/\"@ohos\/hvigor\".*\.[0-9]*\"/\"@ohos\/hvigor\": \"1.3.1\"/g' ./package.json
313        fi
314	module_list=()
315        module_name=()
316        out_module=()
317	bundle_name=""
318        while read line
319        do
320                if [[ ${line} =~ "srcPath" ]]; then
321                        pa=${line%\"*}
322                        pa=${pa##*\".}
323                        module_list[${#module_list[*]}]=${project_path}${pa}
324			del_dir ${project_path}${pa}/node_modules
325                        del_file ${project_path}${pa}/package-lock.json
326                        del_dir ${project_path}${pa}/oh_modules
327                        del_file ${project_path}${pa}/oh-package-lock.json5
328                        module_name[${#module_name[*]}]=${pa}
329                        if [ -d "${project_path}/AppScope" ]; then
330				cur_bundle_line=`cat ${project_path}/AppScope/app.json5 | grep "\"bundleName\""`
331                                bundle_name=${cur_bundle_line%\"*}
332                                bundle_name=${bundle_name##*\"}
333                                # echo "bundleName : "${bundle_name}
334                                is_entry=`cat ${project_path}${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"entry\""`
335                                is_feature=`cat ${project_path}${pa}/src/main/module.json5 | sed 's/ //g' | grep "\"type\":\"feature\""`
336                                if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then
337                                        echo "hap输出module: "${project_path}${pa} >> ${compile_result_error}/${hap}".log" 2>&1
338                                        out_module[${#out_module[*]}]=${project_path}${pa}
339                                fi
340                        else
341				cur_bundle_line=`cat ${project_path}${pa}/src/main/config.json | grep "\"bundleName\""`
342                                bundle_name=${cur_bundle_line%\"*}
343                                bundle_name=${bundle_name##*\"}
344                                # echo "bundleName : "${bundle_name}
345                                is_entry=`cat ${project_path}${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"entry\""`
346                                is_feature=`cat ${project_path}${pa}/src/main/config.json | sed 's/ //g' | grep "\"moduleType\":\"feature\""`
347                                if [[ "${is_entry}" != "" || "${is_feature}" != "" ]]; then
348                                        echo "hap输出module: "${project_path}${pa} >> ${compile_result_error}/${hap}".log" 2>&1
349                                        out_module[${#out_module[*]}]=${project_path}${pa}
350                                fi
351                        fi
352                fi
353        done < ${project_path}"/build-profile.json5"
354
355        if [[ -e "${project_path}/oh-package.json5" ]]; then #如果是ohpm包管理方式
356                for module in ${module_list[@]}
357                do
358                        del_module_name ${module##${project_path}}
359                        if [ $? -eq 0 ]; then
360                                for m_n in ${module_name[@]}
361                                do
362                                        echo "查找 ${m_n} 是否是${module} 的依赖" >> ${compile_result_error}/${hap}".log" 2>&1
363                                        rr=$(cat ${module}"/oh-package.json5" | grep "${m_n}")
364                                        if [[ "${rr}" != "" ]]; then
365                                                echo "处理循环依赖 : "${m_n} >> ${compile_result_error}/${hap}".log" 2>&1
366                                                load_oh_dep ${m_n}
367                                        fi
368                                        
369                                done
370                                cd ${module}
371                                echo ${module}" 执行ohpm install" >> ${compile_result_error}/${hap}".log" 2>&1
372
373                                if [[ -e "oh-package.json5" ]]; then
374                                        ohpm i >> ${compile_result_error}/${hap}".log" 2>&1
375                                else   
376                                        ohpm init >> ${compile_result_error}/${hap}".log" 2>&1
377                                        ohpm install >> ${compile_result_error}/${hap}".log" 2>&1
378                                fi    
379
380                        fi
381                done
382        else 
383                for module in ${module_list[@]}
384                do
385                        del_module_name ${module##${project_path}}
386                        if [ $? -eq 0 ]; then
387                                for m_n in ${module_name[@]}
388                                do
389                                        echo "查找 ${m_n} 是否是${module} 的依赖" >> ${compile_result_error}/${hap}".log" 2>&1
390                                        rr=$(cat ${module}"/package.json" | grep "${m_n}")
391                                        if [[ "${rr}" != "" ]]; then
392                                                echo "处理循环依赖 : "${m_n} >> ${compile_result_error}/${hap}".log" 2>&1
393                                                load_dep ${m_n}
394                                        fi
395                                        
396                                done
397                                cd ${module}
398                                echo ${module}" 执行npm install" >> ${compile_result_error}/${hap}".log" 2>&1
399                                npm i >> ${compile_result_error}/${hap}".log" 2>&1
400                        fi
401                done
402                
403        fi
404
405
406	cd ${project_path}
407	del_dir ${project_path}/node_modules
408        del_dir ${project_path}/oh_modules
409        del_file ${project_path}/package-lock.json
410        del_file ${project_path}/oh-package-lock.json5
411        echo ${project_path}" 执行npm install" >> ${compile_result_error}/${hap}".log" 2>&1
412
413        if [[ -e "${project_path}/oh-package.json5" ]]; then
414                ohpm i >> ${compile_result_error}/${hap}".log" 2>&1
415                chmod +x hvigorw 
416                ./hvigorw clean --no-daemon >> ${compile_result_error}/${hap}".log" 2>&1
417        else 
418                npm install >> ${compile_result_error}/${hap}".log" 2>&1
419                node ./node_modules/@ohos/hvigor/bin/hvigor.js clean --no-daemon >> ${compile_result_error}/${hap}".log" 2>&1
420        fi
421
422
423
424        rm -rf ${project_path}/sign_tool
425        cp -r ${BASE_PATH}/tool/sign_tool ${project_path}
426
427	cd ${project_path}/sign_tool
428	if [[ $(echo ${basic_list[@]} | grep "${hap//_//}") != "" ]]; then
429                echo "${hap} 使用basic签名" >> ${compile_result_error}/${hap}".log" 2>&1
430		sed -i "s/\"normal\"/\"system_basic\"/g" ./UnsgnedReleasedProfileTemplate.json >> ${compile_result_error}/${hap}".log" 2>&1
431	elif [[ $(echo ${core_list[@]} | grep "${hap//_//}") != "" ]]; then
432                echo "${hap} 使用core签名" >> ${compile_result_error}/${hap}".log" 2>&1
433		sed -i "s/\"normal\"/\"system_core\"/g" ./UnsgnedReleasedProfileTemplate.json >> ${compile_result_error}/${hap}".log" 2>&1
434	fi
435	if [[ $(echo ${system_app_list[@]} | grep "${hap//_//}") != "" ]]; then
436                echo "${hap} 系统应用" >> ${compile_result_error}/${hap}".log" 2>&1
437		sed -i "s/\"hos_normal_app\"/\"hos_system_app\"/g" ./UnsgnedReleasedProfileTemplate.json >> ${compile_result_error}/${hap}".log" 2>&1
438	fi
439	if [ "${bundle_name}" != "" ]; then
440                echo "bundleName = ${bundle_name}" >> ${compile_result_error}/${hap}".log" 2>&1
441                sed -i "s/\"com.OpenHarmony.app.test\"/\"${bundle_name}\"/g" ./UnsgnedReleasedProfileTemplate.json >> ${compile_result_error}/${hap}".log" 2>&1
442        fi
443
444
445	sign_hap_num=0
446        for module in ${out_module[@]}
447        do
448		cd ${project_path}
449                cur_out_module_name=${module##*/}
450                if [[ -e "${project_path}/oh-package.json5" ]]; then
451                        chmod +x hvigorw
452		        ./hvigorw --mode module -p module=${cur_out_module_name} assembleHap --no-daemon >> ${compile_result_error}/${hap}".log" 2>&1
453                        if [[ -d ${module}"/src/ohosTest" ]]; then
454		                ./hvigorw --mode module -p module=${cur_out_module_name}@ohosTest assembleHap --no-daemon >> ${compile_result_error}/${hap}".log" 2>&1
455                        fi
456                else 
457		        node ./node_modules/@ohos/hvigor/bin/hvigor.js --mode module -p module=${cur_out_module_name} assembleHap  --no-daemon>> ${compile_result_error}/${hap}".log" 2>&1
458                        if [[ -d ${module}"/src/ohosTest" ]]; then
459		                node ./node_modules/@ohos/hvigor/bin/hvigor.js --mode module -p module=${cur_out_module_name}@ohosTest assembleHap  --no-daemon>> ${compile_result_error}/${hap}".log" 2>&1
460                        fi
461                fi
462
463                is_sign=false
464                echo "module = ${module} , cur_out_module_name=${cur_out_module_name}" >> ${compile_result_error}/${hap}".log" 2>&1
465                if [ ! -d ${module}/build/default/outputs/default/ -o ! -d ${module}/build/default/outputs/ohosTest/ -a -d ${module}"/src/ohosTest" ]; then
466                        echo "module = ${module}, assembleHap error !!!" >> ${compile_result_error}/${hap}".log" 2>&1
467                        continue
468                fi
469		sign_hap_path=""
470		test_sign_hap_path=""
471		nosign_hap_path=""
472		test_nosign_hap_path=""
473		for out_file in `ls ${module}/build/default/outputs/default/`
474		do
475			if [[ "${out_file}" =~ "-unsigned.hap" ]]; then
476				echo "发现unsigned包 : "${out_file}",开始使用签名工具签名" >> ${compile_result_error}/${hap}".log" 2>&1
477				nosign_hap_path=${module}/build/default/outputs/default/${out_file}
478				sign_hap_path=${module}/build/default/outputs/default/${out_file/unsigned/signed}
479				cd ${project_path}/sign_tool
480				java -jar ${BASE_PATH}/tool/hap-sign-tool.jar sign-profile -keyAlias "openharmony application profile release" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "OpenHarmonyProfileRelease.pem" -inFile "UnsgnedReleasedProfileTemplate.json" -keystoreFile "OpenHarmony.p12" -outFile "openharmony_sx.p7b" -keyPwd "123456" -keystorePwd "123456" >> ${compile_result_error}/${hap}".log" 2>&1
481				java -jar ${BASE_PATH}/tool/hap-sign-tool.jar sign-app -keyAlias "openharmony application release" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "OpenHarmonyApplication.pem" -profileFile "openharmony_sx.p7b" -inFile "${nosign_hap_path}" -keystoreFile "OpenHarmony.p12" -outFile "${sign_hap_path}" -keyPwd "123456" -keystorePwd "123456" >> ${compile_result_error}/${hap}".log" 2>&1
482			fi
483		done
484
485                if [[ -d ${module}"/src/ohosTest" ]]; then
486                        for out_file in `ls ${module}/build/default/outputs/ohosTest/`
487                        do
488                                if [[ "${out_file}" =~ "-unsigned.hap" ]]; then
489                                        echo "发现unsigned包 : "${out_file}",开始使用签名工具签名" >> ${compile_result_error}/${hap}".log" 2>&1
490                                        test_nosign_hap_path=${module}/build/default/outputs/ohosTest/${out_file}
491                                        test_sign_hap_path=${module}/build/default/outputs/ohosTest/${out_file/unsigned/signed}
492                                        cd ${project_path}/sign_tool
493                                        java -jar ${BASE_PATH}/tool/hap-sign-tool.jar sign-profile -keyAlias "openharmony application profile release" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "OpenHarmonyProfileRelease.pem" -inFile "UnsgnedReleasedProfileTemplate.json" -keystoreFile "OpenHarmony.p12" -outFile "openharmony_sx.p7b" -keyPwd "123456" -keystorePwd "123456" >> ${compile_result_error}/${hap}".log" 2>&1
494                                        java -jar ${BASE_PATH}/tool/hap-sign-tool.jar sign-app -keyAlias "openharmony application release" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "OpenHarmonyApplication.pem" -profileFile "openharmony_sx.p7b" -inFile "${test_nosign_hap_path}" -keystoreFile "OpenHarmony.p12" -outFile "${test_sign_hap_path}" -keyPwd "123456" -keystorePwd "123456" >> ${compile_result_error}/${hap}".log" 2>&1
495                                fi
496                        done
497                fi
498
499		if [[ ${sign_hap_path} != "" && -f ${sign_hap_path} && ( -f ${test_sign_hap_path} || ! -d ${module}"/src/ohosTest") ]]; then
500			cp ${sign_hap_path} ${compile_result}/${hap}_${cur_out_module_name}_signed.hap
501			cp ${nosign_hap_path} ${nosign_hap_result}/${hap}_${cur_out_module_name}_signed.hap
502                        
503                        if [[ -d ${module}"/src/ohosTest" ]]; then
504                                cp ${test_sign_hap_path} ${compile_result}/${hap}_${cur_out_module_name}_ohosTest_signed.hap
505                                cp ${test_nosign_hap_path} ${nosign_hap_result}/${hap}_${cur_out_module_name}_ohosTest_signed.hap
506                        fi
507			sign_hap_num=$(expr ${sign_hap_num} + 1)
508			echo "${module} assembleHap success !!!" >> ${compile_result_error}/${hap}".log" 2>&1
509		else
510			echo "${module} assembleHap error !!!" >> ${compile_result_error}/${hap}".log" 2>&1
511		fi
512        done
513        if [[ ${#out_module[@]} == ${sign_hap_num} ]]; then
514                #rm ${compile_result_error}/${hap}".log"
515                mv ${compile_result_error}/${hap}.log ${compile_result_success}/${hap}.log
516                echo "success compile ${hap} !!! "
517	else
518		echo "error   compile ${hap} QwQ ,log at ${compile_result_error}/${hap}.log"
519	fi
520}
521
522
523function compile_project(){
524        echo "use public sdk compile..."
525        project_path=${1}
526        project_name=${2}
527        COMPILE_RESULT=${BASE_PATH}/out/${project_name}/public-sdk/hap-out-with-nosigned
528        COMPILE_RESULT_ERROR=${BASE_PATH}/out/${project_name}/public-sdk/compile-error-log
529        COMPILE_RESULT_SUCCESS=${BASE_PATH}/out/${project_name}/public-sdk/compile-success-log
530        SIGN_RESULT=${BASE_PATH}/out/${project_name}/public-sdk/hap-out-with-signed
531
532        clear_dir $COMPILE_RESULT
533        clear_dir $COMPILE_RESULT_ERROR
534        clear_dir $COMPILE_RESULT_SUCCESS
535        clear_dir $SIGN_RESULT
536
537
538        export OHOS_SDK_HOME=${arg_sdk_path}
539        export OHOS_BASE_SDK_HOME=${arg_sdk_path}
540        echo "OHOS_SDK_HOME="${OHOS_SDK_HOME}
541        echo "OHOS_BASE_SDK_HOME="${OHOS_BASE_SDK_HOME}
542        
543	EOF=0
544        curr=0
545        while [ $EOF -ne 1 ]; do
546                for ((i=1; i<=$TIME; i++)); do
547                        if  [ ${curr} -eq ${#public_list[@]} ]; then
548                                EOF=1
549                                break
550                        fi
551                        compile ${public_list[$curr]} ${SIGN_RESULT} ${COMPILE_RESULT_ERROR} ${COMPILE_RESULT} ${COMPILE_RESULT_SUCCESS} &
552                        curr=$((curr+1))
553                done
554                wait
555        done
556}
557
558function compile_project_full(){
559        echo "use full sdk compile..."
560        project_path=${1}
561        project_name=${2}
562        COMPILE_RESULT=${BASE_PATH}/out/${project_name}/full-sdk/hap-out-with-nosigned
563        COMPILE_RESULT_ERROR=${BASE_PATH}/out/${project_name}/full-sdk/compile-error-log
564        COMPILE_RESULT_SUCCESS=${BASE_PATH}/out/${project_name}/full-sdk/compile-success-log
565        SIGN_RESULT=${BASE_PATH}/out/${project_name}/full-sdk/hap-out-with-signed
566
567        clear_dir $COMPILE_RESULT
568        clear_dir $COMPILE_RESULT_ERROR
569        clear_dir $COMPILE_RESULT_SUCCESS
570        clear_dir $SIGN_RESULT
571
572        export OHOS_SDK_HOME=${arg_full_sdk_path}
573        export OHOS_BASE_SDK_HOME=${arg_full_sdk_path}
574        echo "OHOS_SDK_HOME="${OHOS_SDK_HOME}
575        echo "OHOS_BASE_SDK_HOME="${OHOS_BASE_SDK_HOME}
576
577	EOF=0
578        curr=0
579        while [ $EOF -ne 1 ]; do
580                for ((i=1; i<=$TIME; i++)); do
581                        if  [ ${curr} -eq ${#full_list[@]} ]; then
582                                EOF=1
583                                break
584                        fi
585                        compile ${full_list[$curr]} ${SIGN_RESULT} ${COMPILE_RESULT_ERROR} ${COMPILE_RESULT} ${COMPILE_RESULT_SUCCESS} &
586                        curr=$((curr+1))
587                done
588                wait
589        done
590}
591
592function load(){
593	project_url=${1}
594	project_branch=${2}
595	project_name=${3}
596	if test -d ${BASE_PATH}/projects/${project_name}
597 	then
598                echo "${project_name} exists,ready for update..."
599                cd ${BASE_PATH}/projects/${project_name}
600                git reset --hard origin/${project_branch}
601                git checkout -b ${project_branch} origin/${project_branch}
602                git checkout ${project_branch}
603                git lfs fetch origin ${project_branch}
604		git pull
605        else
606                echo "${project_name} dose not exist,ready to download..."
607                if [ ! -d ${BASE_PATH}/projects ]; then
608			mkdir -p ${BASE_PATH}/projects
609		fi
610                cd ${BASE_PATH}/projects
611                git lfs clone -b ${project_branch} ${project_url} ${project_name}
612                git checkout -b ${project_branch} origin/${project_branch}
613        fi
614        if [ -d "${BASE_PATH}/projects/${project_name}/${arg_compile_dir}" ]; then
615                read_project ${BASE_PATH}/projects/${project_name}/${arg_compile_dir}
616                exclude_full_project
617		compile_project ${BASE_PATH}/projects/${project_name} ${project_name}
618		compile_project_full ${BASE_PATH}/projects/${project_name} ${project_name}
619        fi
620}
621
622
623while read line
624do
625        if [[ ${line} == ${tag_url}* ]]; then
626                url=${line##*${tag_url}}
627        elif [[ ${line} == ${tag_branch}* ]]; then
628                branch=${line##*${tag_branch}}
629        elif [[ ${line} == ${tag_name}* ]]; then
630                name=${line##*${tag_name}}
631        elif [[ ${line} == ${tag_full_list}* ]]; then
632                temp_string=${line##*${tag_full_list}}
633                full_list=(${temp_string//;/ })
634        elif [[ ${line} == ${tag_basic_list}* ]]; then
635                temp_string=${line##*${tag_basic_list}}
636                basic_list=(${temp_string//;/ })
637        elif [[ ${line} == ${tag_core_list}* ]]; then
638                temp_string=${line##*${tag_core_list}}
639                core_list=(${temp_string//;/ })
640	elif [[ ${line} == ${tag_system_app_list}* ]]; then
641                temp_string=${line##*${tag_system_app_list}}
642                system_app_list=(${temp_string//;/ })
643        fi
644done < ${arg_p}
645
646load ${url} ${branch} ${name}
647
648endTime=`date +%Y%m%d-%H:%M:%S`
649endTime_s=`date +%s`
650sumTime=$[ $endTime_s - $startTime_s ]
651
652echo "$startTime ---> $endTime" "耗时:$sumTime seconds"
653echo "| public-sdk--|-------------|-------------|-------------|-------------|-------------"
654echo "| 仓库        | 项目数量    | 成功数量    | 失败数量    | Hap数量    | 签名成功    "
655echo "|-------------|-------------|-------------|-------------|-------------"
656echo "| ${name} | ${#public_list[@]}         | $(ls -l ${BASE_PATH}/out/${name}/public-sdk/compile-success-log|grep "^-"|wc -l)           | $(ls -l ${BASE_PATH}/out/${name}/public-sdk/compile-error-log|grep "^-"|wc -l)           | "`expr $(ls -l ${BASE_PATH}/out/${name}/public-sdk/hap-out-with-nosigned|grep "^-"|wc -l) / 2`"         | "`expr $(ls -l ${BASE_PATH}/out/${name}/public-sdk/hap-out-with-signed|grep "^-"|wc -l) / 2`
657if (( $(ls -l ${BASE_PATH}/out/${name}/public-sdk/compile-error-log|grep "^-"|wc -l) > 0 )); then
658        echo "| 失败项目列表|-------------|-------------|-------------|-------------|-------------"
659	for z in `ls ${BASE_PATH}/out/${name}/public-sdk/compile-error-log`
660        do
661                z=${z//_//}
662                z=${z%.log}
663                echo "| "${z}" |"
664        done
665fi
666echo "| 失败项目日志汇总地址|-------------|-------------|-------------|-------------------"
667echo "| ${BASE_PATH}/out/${name}/public-sdk/compile-error-log"
668echo "| full-sdk----|-------------|-------------|-------------|-------------|-------------"
669echo "| 仓库        | 项目数量    | 成功数量    | 失败数量    | Hap数量    | 签名成功    "
670echo "|-------------|-------------|-------------|-------------|-------------|-------------"
671echo "| ${name} | ${#full_list[@]}          | $(ls -l ${BASE_PATH}/out/${name}/full-sdk/compile-success-log|grep "^-"|wc -l)           | $(ls -l ${BASE_PATH}/out/${name}/full-sdk/compile-error-log|grep "^-"|wc -l)           | "`expr $(ls -l ${BASE_PATH}/out/${name}/full-sdk/hap-out-with-nosigned|grep "^-"|wc -l) / 2`"          | "`expr $(ls -l ${BASE_PATH}/out/${name}/full-sdk/hap-out-with-signed|grep "^-"|wc -l) / 2`
672if (( $(ls -l ${BASE_PATH}/out/${name}/full-sdk/compile-error-log|grep "^-"|wc -l) > 0 )); then
673        echo "| 失败项目列表|-------------|-------------|-------------|-------------|-------------"
674	for z in `ls ${BASE_PATH}/out/${name}/full-sdk/compile-error-log`
675        do
676                z=${z//_//}
677                z=${z%.log}
678                echo "| "${z}" |"
679        done
680fi
681echo "| 失败项目日志汇总地址|-------------|-------------|-------------------"
682echo "| ${BASE_PATH}/out/${name}/full-sdk/compile-error-log"
683echo "|-------------|-------------|-------------|-------------|-------------"
684echo "| 成功项目日志汇总地址|-------------|-------------|-------------------"
685echo "| ${BASE_PATH}/out/${name}/full-sdk/compile-success-log"
686echo "| 不含测试用例项目|-------------|-------------|-------------------"
687        for simple in ${simple_list[@]};do
688        echo "| "${simple}" |"
689        done
690echo "|-------------|-------------|-------------|-------------|-------------"
691echo "compile completed..."
692