码迷,mamicode.com
首页 > 其他好文 > 详细

Xcode 代码模板

时间:2016-05-12 12:59:09      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

当我们在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"

cmd="Add :Options:1:Suffixes:$classname string $suffixename"
/usr/libexec/PlistBuddy -c "$cmd" "$templatepath"/TemplateInfo.plist 

截图

模板文件

技术分享

生成模板代码

技术分享
技术分享

参考

创建Xcode的工程模板和代码模板

Xcode 代码模板

标签:

原文地址:http://blog.csdn.net/chsadin/article/details/51362074

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!