前言
眾所周知,程序員們的代碼風(fēng)格因人而異,好在有自動規(guī)范代碼格式工具可以幫助解決代碼風(fēng)格迥異的問題沃疮。結(jié)合同事的推薦以及自己的搜索、實踐梅肤,個人更推薦使用square/spacecommander司蔬。優(yōu)點:
- 使用方法極其簡單。
- 功能豐富姨蝴。
- 可默認(rèn)及自定義忽略不需格式化的文件俊啼。
盡管spacecommander已經(jīng)大大方便了代碼的格式化,但并沒有提供自動格式化及提交代碼的功能左医。再加上自己在實踐過程中遇到的問題授帕,促使我對spacecommander進(jìn)行了一番改造同木,具體而言,就是fork了一下原工程跛十,修改了其中的腳本彤路。參見:whoyoung/spacecommander。
square/spacecommander使用及功能簡介
Installation Locally
(我也不知道為什么我寫著寫著芥映,就開始用英文寫了洲尊,只好用中文再翻譯了一下了。屏轰。颊郎。)
- clone or download this repository to your Mac's path, for example: /Users/young/Documents/iOS/spacecommander.
(翻譯:克隆或下載倉庫到本地路徑) - cd "your target project's path" in terminal. For example:
cd /Users/young/Documents/iOS/testClangFormat
. Then run the command (notice: the path should be replaced by your own spacecommander's path):"/Users/young/Documents/iOS/spacecommander"/setup-repo.sh
(翻譯:用終端定位到自己想格式化的工程根目錄下,執(zhí)行命令)
Usage
(翻譯:執(zhí)行完上面的命令后霎苗,就可以嘗試修改工程文件姆吭,然后在sourcetree點擊提交,接下來按彈出的提示唁盏,拷貝命令在終端執(zhí)行内狸,然后再次點擊提交就OK了)
After Installation Locally, modifying your project's files, clicking sourcetree's button - "commit" to commit changes. then, you may see, like this:
According the tips, you just copy the command, then in your target project's path, run :
"/Users/young/Documents/iOS/spacecommander"/format-objc-file.sh 'testClangFormat/AppDelegate.h' && git add 'testClangFormat/AppDelegate.h';
"/Users/young/Documents/iOS/spacecommander"/format-objc-file.sh 'testClangFormat/AppDelegate.m' && git add 'testClangFormat/AppDelegate.m'
or
"/Users/young/Documents/iOS/spacecommander"/format-objc-files.sh -s
Then, commit changes again, you will find these files had been formatted.
If you think it has meeting your demand ,and "/Users/young/Documents/iOS/spacecommander"/format-objc-files.sh -s
is run correctly。You just ignore these following introduces is OK.
之所以在這里強(qiáng)調(diào)一下"/Users/young/Documents/iOS/spacecommander"/format-objc-files.sh -s
這個命令是否成功將你的代碼格式化了厘擂,是因為我在自己的Mac上執(zhí)行這個命令昆淡,并不起作用。而在朋友的電腦上卻能正確執(zhí)行刽严。分析了各種原因昂灵,也沒能解決。這也直接促成了我修改了 spacecommander 的腳本舞萄,順帶著實現(xiàn)了自動格式化代碼并提交眨补。參見下面??的介紹:優(yōu)化: 自動格式化提交,無需手動拷貝命令執(zhí)行
.clang-format
執(zhí)行過setup-repo命令后倒脓,在你的目標(biāo)工程根目錄下會生成一個符號鏈接文件:.clang-format (這是一個隱藏文件撑螺,可以用快捷鍵 shift+command+. 顯示隱藏的文件)。這個文件就是格式化代碼的依據(jù)崎弃,自己可以按自己的喜好來定: 所有可配置的格式 甘晤。我修改了其中兩個配置:
- BreakBeforeBraces: Attach
- MaxEmptyLinesToKeep: 1
在.clang-format的配置屬性中: DisableFormat (BOOL) 決定著當(dāng)前的配置文件是否起作用。spacecommander 默認(rèn)配置為DisableFormat: false饲做。所以线婚,如果不想暫時不想格式化代碼,只要改成DisableFormat: true就行了盆均。然鵝酌伊,我的電腦有毒,改了這個配置竟不起作用W鹤佟>幼!只好再次祭出修改spacecommander 腳本大法了驴娃。參見下面??的介紹:Enable or Disable 格式化代碼
Ignore single file (忽略格式化單個文件)
如果不想格式化某個文件奏候,可以在文件的第一行加上: #pragma Formatter Exempt
或者 // MARK: Formatter Exempt
Ignore files within directories (忽略格式化指定目錄下的所有文件)
如果想忽略某些目錄下的所有文件,只需在工程根目錄下新建一個文件.formatting-directory-ignore
,在文件中加入想忽略的文件路徑,不同路徑用換行符分隔唇敞,路徑后面不要有空格蔗草。例如,我的工程中有兩個目錄dir1和dir2:
.formatting-directory-ignore
文件內(nèi)容如下:
files within directories (格式化指定目錄下的所有文件)
如果僅僅想格式化指定目錄下的文件疆柔,與Ignore files within directories操作方法一樣咒精,區(qū)別在于新建的文件名叫.formatting-directory
Functions
- format-objc-file.sh <file> 格式化單個文件
-
format-objc-files.sh 格式化所有修改過的文件(實際上是遍歷所有修改過的文件,逐個執(zhí)行
format-objc-file.sh <file> && git add <file>
命令) - format-objc-files-in-repo.sh 格式化整個工程里的Objective-C文件(不包括用pod或Carthage管理的代碼)
- format-objc-file-dry-run.sh <file> 將格式化的代碼標(biāo)準(zhǔn)輸出到一個新的文件旷档,不改變原文件
優(yōu)化: 自動格式化提交模叙,無需手動拷貝命令執(zhí)行
通過clone或下載 whoyoung/spacecommander到Mac上,路徑:例如/Users/young/Documents/iOS/spacecommander-whoyoung
鞋屈,然后在目標(biāo)工程根目錄下執(zhí)行"/Users/young/Documents/iOS/spacecommander-whoyoung"/setup-repo.sh
命令即可范咨。以后每次點擊提交,都會自動格式化代碼并提交厂庇,不會再彈出提示框渠啊,要求手動拷貝命令并執(zhí)行了。
其他操作及命令與square/spacecommander 一致权旷。
實現(xiàn)自動格式化并提交的原理挺簡單替蛉,只是把spacecommander目錄下的format-objc-hook腳本修改了一下:
- 將代碼塊(記為:代碼塊A,這部分代碼的作用是獲取所有修改過的文件):
objc_files=$(objc_files_to_format "$1")
[ -z "$objc_files" ] && exit 0
下面的代碼全部注釋掉
- 在代碼塊A下面寫入代碼塊B(這部分代碼的作用是直接調(diào)用腳本format-objc-files.sh格式化所有修改過的文件)
$("$DIR"/format-objc-files.sh -s)
exit 0
之所以這么修改拄氯,是因為腳本 format-objc-files.sh 和format-objc-hook里的同一個命令: objc_files=$(objc_files_to_format "$1")
躲查,在我的Mac上執(zhí)行的結(jié)果不一樣。format-objc-hook腳本執(zhí)行的結(jié)果是正確的坤邪。既然format-objc-hook這個腳本里獲取到的objc_files是正確的熙含,那就直接在format-objc-hook里執(zhí)行format-objc-files.sh就行了。順帶著也就實現(xiàn)了點擊提交按鈕艇纺,自動格式化及提交代碼的功能怎静。
將 .clang-format 文件加入到 .ignore_file 腳本優(yōu)化
在工程目錄下執(zhí)行 setup-repo.sh 命令后,我們會發(fā)現(xiàn)工程目錄下的 .gitignore 文件發(fā)生了改變: ".clang-format" 被拼接到了 .gitignore 文件內(nèi)文本的末尾黔衡。當(dāng)原 .gitignore 文件內(nèi)文本末尾沒有空白行時蚓聘,就會導(dǎo)致".clang-format" 與最后一個文本拼接在一起,從而使 .gitignore 規(guī)則發(fā)生變化盟劫。
為了避免這一潛在的影響夜牡,需要在.gitignore 文件插入一個換行符之后再拼接".clang-format"。
通過修改 setup-repo.sh 腳本的 ensure_git_ignores_clang_format_file() 方法, 使用echo -e "\n.clang-format" >> "$gitignore_file"
即可實現(xiàn)插入換行符。完整方法如下:
function ensure_git_ignores_clang_format_file() {
gitignore_file='.gitignore';
grep -q ".clang-format" "$gitignore_file"
if [ $? -gt 0 ]; then
echo -e "\n.clang-format" >> "$gitignore_file"
fi
}
Enable or Disable 格式化代碼
(最新代碼已去掉這個功能塘装,有此需求的急迂,仍然可以按照下面的方法實現(xiàn))
上面??提到了,配置DisableFormat: true在我的Mac上并沒能禁止代碼格式化功能蹦肴。通過修改spacecommander目錄下的format-objc-hook和setup-repo.sh 腳本:
-
format-objc-hook
在代碼塊A的上面寫入代碼塊C(這部分代碼的作用是判斷目標(biāo)工程根目錄下.git/hooks/pre-commit調(diào)用腳本 format-objc-hook傳的第一個參數(shù)是否為“-false”,若為"-false"僚碎,直接退出命令,不再往下執(zhí)行阴幌。否則勺阐,就繼續(xù)往下執(zhí)行格式化代碼操作)
if [ "$1" == "-false" ]; then
exit 0
fi
-
setup-repo.sh
這個腳本執(zhí)行的方法如下:
ensure_pre_commit_file_exists &&
ensure_pre_commit_file_is_executable &&
ensure_hook_is_installed &&
ensure_git_ignores_clang_format_file &&
symlink_clang_format
概括來講就是在目標(biāo)工程根目錄下生成可執(zhí)行腳本.git/hooks/pre-commit
以及符號鏈接文件.clang-format
。我修改了setup-repo.sh的方法ensure_hook_is_installed()矛双,生成的pre-commit腳本如下:
#!/usr/bin/env bash
current_repo_path=$(git rev-parse --show-toplevel)
repo_to_format="/Users/young/Documents/iOS/RefreshChilrenComponents"
#enable auto-format
if [ "$current_repo_path" == "$repo_to_format" ] &&
[ -e "/Users/young/Documents/iOS/spacecommander-whoyoung"/format-objc-hook ];
then "/Users/young/Documents/iOS/spacecommander-whoyoung"/format-objc-hook ||
exit 1; fi
#disable auto-format
# if [ "$current_repo_path" == "$repo_to_format" ] &&
[ -e "/Users/young/Documents/iOS/spacecommander-whoyoung"/format-objc-hook ];
then "/Users/young/Documents/iOS/spacecommander-whoyoung"/format-objc-hook -false ||
exit 1; fi
從腳本中可以看出Enable和Disable的差異是執(zhí)行format-objc-hook腳本是否傳“-false”渊抽。
通過注釋#enable auto-format下面的代碼或者#disable auto-format下面的代碼便可禁止或者開啟格式化代碼功能