MAC下app可以通过App Store下载,但是想要上架App Store,需要经历一番“磨难”(详见Apple家规和吐槽),而且上架的app有各种各样的限制(如“沙盒”(wiki在这里)运行,虽然能保护用户,但是对于像安装包这类的app,那就是噩梦了)。所以,也有不少app被制作为dmg文件(wiki在这里)来发布。
下面是一个dmg制作的脚本CreatDMG.sh:
#!/bin/sh
LogFile="/private/var/tmp/VTechMakeProductLog.txt";
fun()
{
StateCode=$?
c_time=$(date +%Y/%m/%d" "%H:%M:%S)
/bin/echo "\n$c_time [build dmg] state=[$StateCode]:$1" >> $LogFile
/bin/echo "$2" >> $LogFile 2>&1
}
if [ $# != 3 ]; then
echo "Usage: CreateDmg.sh volName srcDir destDmg"
echo $VOL
exit 0
fi
VOL="$1"
ORIGIN="$2"
Dest="$3"
DMG="$Dest.dmg"
if [ -e "$DMG" ]; then
rm -f "$DMG"
fi
echo ORIGIN=[$ORIGIN]
SIZE=$(du -sh $ORIGIN)
SIZE=$(echo $SIZE | awk -F'[ ]' '{printf $1}')
UNIT=$(echo $SIZE|awk '{print substr($0,length($0),length($0))}')
SIZE=$(echo $SIZE|awk '{print substr($0,0,length($0)-1)}')
echo unit=[$UNIT];
echo SIZE=[$SIZE]
if [ $UNIT = "G" ]; then
SIZE=$(echo $SIZE | awk '{print $0*1024*1024+10}')"M"
elif [ $UNIT = "M" ];then
SIZE=$(echo $SIZE | awk '{print $0+5}')"M"
elif [ $UNIT = "K" ];then
SIZE=$(echo $SIZE | awk '{print $0/1000+10}')"M"
elif [ $UNIT = "B" ];then
SIZE=$(echo $SIZE | awk '{print $0+1000*1000}')"B"
fi
echo [$SIZE]
if [ -e "/Volumes/$VOL" ]; then
hdiutil create -megabytes $SIZE -fs HFS+ -volname "$VOL" DmgTemp 2>&1
fi
cmdRes=$(hdiutil create -megabytes $SIZE -fs HFS+ -volname "$VOL" DmgTemp 2>&1)
fun "hdiutil create" "$cmdRes"
cmdRes=$(hdiutil mount DmgTemp.dmg 2>&1)
fun "hdiutil mount" "$cmdRes"
cmdRes=$(/usr/bin/codesign --display --verbose=4 "$ORIGIN/$VOL.app" 2>&1)
fun "codesign check" "$cmdRes"
cmdRes=$(ditto -rsrcFork "$ORIGIN" "/Volumes/$VOL" 2>&1)
fun "ditto -rsrcFork" "$cmdRes"
cmdRes=$(hdiutil eject "/Volumes/$VOL" 2>&1)
fun "hdiutil eject" "$cmdRes"
cmdRes=$(hdiutil convert DmgTemp.dmg -format UDZO -o "$DMG" 2>&1)
fun "hdiutil convert" "$cmdRes"
rm "DmgTemp.dmg"
使用方法:
CreateDmg.sh bundle_name bundle_path dmg_full_path
如:需要打包的Learning_Lodge_Cleaner.app存放在/Users/zenghao/Desktop/DMG目录中,最后制作为Users/zenghao/Desktop/Clear.dmg文件:
CreateDmg.sh Learning_Lodge_Cleaner /Users/zenghao/Desktop/DMG /Users/zenghao/Desktop/