您的位置:首页 > 编程语言

Xcode 代码模板

2016-05-10 12:03 447 查看
当我们在Xcode中新建一个UIViewController子类的viewController 时候,Xcode会自动帮我们把一部分代码生成(viewDidLoad、didReceiveMemoryWarning方法等,以及注释)。

今天,于是花了一点时间了研究了一下,并整理出一个简化模板拷贝以及修改TemplateInfo.plist 中后缀名的Shell 脚本工具。

脚本

脚本比较简单,就不详细解释了。模板Demo以及脚本都放在github上了

#!/bin/bash
# write by fenglh 2016/05/10

usage(){

local prog="`basename $1`"
echo "Usage: $prog -t 模板文件 [-s 后缀名] "
echo "       $prog -h 帮助."
exit 1
}

showhelp() {
echo "Usage: `basename $1`: -t 模板文件 [-s 后缀名]"
echo "  -t 模板文件 是一个目录,参考BMUseTokenBaseAPIManagerObjective-C。目录名命名方式必须遵循:‘类名+Objective-C’形式"
echo "  -s 后缀名  在xcode新建该类文件时,显示默认的后缀名字"
echo "  -h 显示该帮助"
exit 1
}

templatefile=
suffixename=
classname=
templatepath=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Source/Cocoa\ Touch\ Class.xctemplate

while getopts "t:s:h" arg
do
case $arg in
t)
templatefile=$OPTARG
name=`echo $templatefile | awk -F '/' '{print $NF}'`
[ -z "$name" ] && name=`echo $templatefile | awk -F '/' '{print $(NF-1)}'`
classname=${name%%Objective-C*}
[ -z "$classname" ] && echo "模板文件目录命名不规范" && showhelp $0;
;;
s)  suffixename=$OPTARG;;
h)  showhelp $0;;
?)  usage $0;;
esac
done

[ -z "$suffixename" ] && usage $0
[ ! -d "$templatefile" ] && echo "$templatefile 模板目录不存在!"
cp -af "$templatefile"  "$templatepath"

cmd1="Add :Options:1:Suffixes:$classname string $suffixename"
/usr/libexec/PlistBuddy -c "$cmd1" "$templatepath"/TemplateInfo.plist
cmd2="Add :Options:1:Values:0 string $classname"
/usr/libexec/PlistBuddy -c "$cmd2" "$templatepath"/TemplateInfo.plist
cmd3="Add :Options:2:RequiredOptions:cocoaTouchSubclass:0 string $classname"
/usr/libexec/PlistBuddy -c "$cmd3" "$templatepath"/TemplateInfo.plist


截图

模板文件



生成模板代码





参考

创建Xcode的工程模板和代码模板
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell xcode 代码模板