直接上脚本
#!/bin/bash
#################################
# 制作dmg镜像、pkg安装包
#################################
# 工程目录
projectDir="$HOME/src/chai"
# 编译目录
buildDir="$HOME/qt/build/chai-Release"
# 应用程序名
appName="chai.app"
# 镜像目录
dmgDir="$HOME/Desktop/dmg"
# 证书
cefFlag="Developer ID Application: Shenzhen chai Co., Ltd. (MVGBX5DTX9)"
cefFlag2="3rd Party Mac Developer Installer: Shenzhen chai Co., Ltd. (MVGBX5DTX9)"
### 函数定义
# 签名
function signature()
{
# 证书解锁,输入密码交互
(/usr/bin/expect <<-EOF
spawn security unlock-keychain login.keychain
expect {
"password*" { send "123456\r" }
}
expect eof
EOF
)
# 开始签名
# 对qt的app进行签名
modulesName=$(find $1/Contents/Frameworks -name *.app)
for module in ${modulesName[@]}
do
codesign -s "$cefFlag" $module
done
# 对qt模块进行签名
frameworksName=$(ls $1/Contents/Frameworks/ | cut -d '.' -f 1)
for framework in ${frameworksName[@]}
do
codesign -s "$cefFlag" $1/Contents/Frameworks/$framework.framework/$framework
done
# 对so进行签名
sosName=$(find $1/Contents/PlugIns/ -name *.dylib)
for so in ${sosName[@]}
do
codesign -s "$cefFlag" $so
done
codesign -s "$cefFlag" $1
}
### 工程编译
echo "**************************************** start make ****************************************"
rm -rf $buildDir
mkdir -p $buildDir
cd $buildDir
# 获取环境变量VERSION以及git的版本md5值,
# buildVer="V1.0.0 2e923332a004adcad600236a2f27ccab45e8b93c"
buildVer="${VERSION} $(cat $projectDir/.git/refs/heads/develop)"
$HOME/Qt5.9.7/5.9.7/clang_64/bin/qmake $projectDir/chai.pro -o $buildDir
sed -i "" "s/VER_CHAI/VER_CHAI=\"\\\\\"${buildVer}\\\\\"\"/g" Makefile
make
cd -
### 拷贝编译项目
echo "**************************************** copy project ****************************************"
mkdir -p $dmgDir
cp -r $buildDir/$appName $dmgDir
ln -s /Applications $dmgDir/Applications
### 拷贝其他资源文件
echo "**************************************** copy resources ****************************************"
resArr=(
cfg.db
Resources/appIcon.icns
)
for res in ${resArr[@]}
do
if [ -d "$projectDir/$res" ];then
cp -r $projectDir/$res $dmgDir/$appName/Contents/Resources/$(basename $res)
find $dmgDir/$appName/Contents/Resources/$(basename $res) -type d -exec chmod 755 {} \;
find $dmgDir/$appName/Contents/Resources/$(basename $res) -type f -exec chmod 666 {} \;
elif [ "dylib" == "${res##*.}" ];then
cp $projectDir/$res $dmgDir/$appName/Contents/Resources/
chmod 755 $dmgDir/$appName/Contents/Resources/$(basename $res)
else
cp $projectDir/$res $dmgDir/$appName/Contents/Resources/
chmod 666 $dmgDir/$appName/Contents/Resources/$(basename $res)
fi
done
# 设置Info.plist文件
infoArr=(
# 应用程序图标
"Set :CFBundleIconFile appIcon.icns"
# 设置网页启动
"Add :CFBundleURLTypes array"
"Add :CFBundleURLTypes:0 dict"
"Add :CFBundleURLTypes:0:CFBundleTypeRole string 'Editor'"
"Add :CFBundleURLTypes:0:CFBundleURLName string 'com.chai.xxx'"
"Add :CFBundleURLTypes:0:CFBundleURLSchemes array"
# 设置启动的 scheme
"Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string 'chai'"
)
for info in "${infoArr[@]}"
do
/usr/libexec/PlistBuddy -c "$info" $dmgDir/$appName/Contents/Info.plist
done
### 拷贝安装包资源
mkdir $dmgDir/.res
resArr=(
Resources/install_bg.png
)
for res in "${resArr[@]}"
do
cp $projectDir/$res $dmgDir/.res/
done
### 拷贝qt相关的资源、文件、so
echo "**************************************** copy qt resources ****************************************"
$HOME/Qt5.9.7/5.9.7/clang_64/bin/macdeployqt $dmgDir/$appName -qmldir=$projectDir
### 开始签名
echo "**************************************** signature ****************************************"
signature "$dmgDir/$appName"
### 打包pkg
echo "**************************************** tar pkg ****************************************"
rm $HOME/Desktop/chai.pkg
productbuild --component "$dmgDir/$appName" /Applications --sign "$cefFlag2" "$HOME/Desktop/chai.pkg"
# 制作dmg镜像文件
echo "**************************************** tar dmg ****************************************"
rm $HOME/Desktop/chai_rw.dmg
hdiutil create -format UDRW -srcfolder $dmgDir $HOME/Desktop/chai_rw.dmg
# 把dmg文件挂载上,打开挂载的磁盘,开始布局安装背景及图标大小位置,通过以下命令将dmg文件的读写性质转换为压缩性质
# 转换为dmg镜像的压缩格式
# VERSION取自环境变量
#rm $HOME/Desktop/chai-${VERSION}.dmg;hdiutil convert -format UDZO -o $HOME/Desktop/chai-${VERSION}.dmg $HOME/Desktop/chai_rw.dmg
macOS qt 一键镜像 一键dmg 一键签名 一键打包