spacecommander:Objective-C代碼格式化及自動提交優化

前言


眾所周知,程序員們的代碼風格因人而異,好在有自動規范代碼格式工具可以幫助解決代碼風格迥異的問題。結合同事的推薦以及自己的搜索、實踐,個人更推薦使用square/spacecommander。優點:

  1. 使用方法極其簡單。
  2. 功能豐富。
  3. 可默認及自定義忽略不需格式化的文件。

盡管spacecommander已經大大方便了代碼的格式化,但并沒有提供自動格式化及提交代碼的功能。再加上自己在實踐過程中遇到的問題,促使我對spacecommander進行了一番改造,具體而言,就是fork了一下原工程,修改了其中的腳本。參見:whoyoung/spacecommander

square/spacecommander使用及功能簡介


Installation Locally

(我也不知道為什么我寫著寫著,就開始用英文寫了,只好用中文再翻譯了一下了。。。)

  1. clone or download this repository to your Mac's path, for example: /Users/young/Documents/iOS/spacecommander.
    (翻譯:克隆或下載倉庫到本地路徑)
  2. 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
    (翻譯:用終端定位到自己想格式化的工程根目錄下,執行命令)

Usage

(翻譯:執行完上面的命令后,就可以嘗試修改工程文件,然后在sourcetree點擊提交,接下來按彈出的提示,拷貝命令在終端執行,然后再次點擊提交就OK了)
After Installation Locally, modifying your project's files, clicking sourcetree's button - "commit" to commit changes. then, you may see, like this:

committing.png

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.
之所以在這里強調一下"/Users/young/Documents/iOS/spacecommander"/format-objc-files.sh -s這個命令是否成功將你的代碼格式化了,是因為我在自己的Mac上執行這個命令,并不起作用。而在朋友的電腦上卻能正確執行。分析了各種原因,也沒能解決。這也直接促成了我修改了 spacecommander 的腳本,順帶著實現了自動格式化代碼并提交。參見下面??的介紹:優化: 自動格式化提交,無需手動拷貝命令執行

.clang-format

執行過setup-repo命令后,在你的目標工程根目錄下會生成一個符號鏈接文件:.clang-format (這是一個隱藏文件,可以用快捷鍵 shift+command+. 顯示隱藏的文件)。這個文件就是格式化代碼的依據,自己可以按自己的喜好來定: 所有可配置的格式 。我修改了其中兩個配置:

  • BreakBeforeBraces: Attach
  • MaxEmptyLinesToKeep: 1

.clang-format的配置屬性中: DisableFormat (BOOL) 決定著當前的配置文件是否起作用。spacecommander 默認配置為DisableFormat: false。所以,如果不想暫時不想格式化代碼,只要改成DisableFormat: true就行了。然鵝,我的電腦有毒,改了這個配置竟不起作用!!!只好再次祭出修改spacecommander 腳本大法了。參見下面??的介紹:Enable or Disable 格式化代碼

Ignore single file (忽略格式化單個文件)

如果不想格式化某個文件,可以在文件的第一行加上: #pragma Formatter Exempt 或者 // MARK: Formatter Exempt

Ignore files within directories (忽略格式化指定目錄下的所有文件)

如果想忽略某些目錄下的所有文件,只需在工程根目錄下新建一個文件.formatting-directory-ignore ,在文件中加入想忽略的文件路徑,不同路徑用換行符分隔,路徑后面不要有空格。例如,我的工程中有兩個目錄dir1和dir2:

忽略目錄.png

.formatting-directory-ignore文件內容如下:

ignore.png

files within directories (格式化指定目錄下的所有文件)

如果僅僅想格式化指定目錄下的文件,與Ignore files within directories操作方法一樣,區別在于新建的文件名叫.formatting-directory

Functions


  • format-objc-file.sh <file> 格式化單個文件
  • format-objc-files.sh 格式化所有修改過的文件(實際上是遍歷所有修改過的文件,逐個執行 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> 將格式化的代碼標準輸出到一個新的文件,不改變原文件

優化: 自動格式化提交,無需手動拷貝命令執行

通過clone或下載 whoyoung/spacecommander到Mac上,路徑:例如/Users/young/Documents/iOS/spacecommander-whoyoung,然后在目標工程根目錄下執行"/Users/young/Documents/iOS/spacecommander-whoyoung"/setup-repo.sh命令即可。以后每次點擊提交,都會自動格式化代碼并提交,不會再彈出提示框,要求手動拷貝命令并執行了。
其他操作及命令與square/spacecommander 一致。
實現自動格式化并提交的原理挺簡單,只是把spacecommander目錄下的format-objc-hook腳本修改了一下:

  1. 將代碼塊(記為:代碼塊A,這部分代碼的作用是獲取所有修改過的文件):
objc_files=$(objc_files_to_format "$1")
[ -z "$objc_files" ] && exit 0

下面的代碼全部注釋掉

  1. 代碼塊A下面寫入代碼塊B(這部分代碼的作用是直接調用腳本format-objc-files.sh格式化所有修改過的文件)
$("$DIR"/format-objc-files.sh -s)
exit 0

之所以這么修改,是因為腳本 format-objc-files.shformat-objc-hook里的同一個命令: objc_files=$(objc_files_to_format "$1"),在我的Mac上執行的結果不一樣。format-objc-hook腳本執行的結果是正確的。既然format-objc-hook這個腳本里獲取到的objc_files是正確的,那就直接在format-objc-hook里執行format-objc-files.sh就行了。順帶著也就實現了點擊提交按鈕,自動格式化及提交代碼的功能。

將 .clang-format 文件加入到 .ignore_file 腳本優化

在工程目錄下執行 setup-repo.sh 命令后,我們會發現工程目錄下的 .gitignore 文件發生了改變: ".clang-format" 被拼接到了 .gitignore 文件內文本的末尾。當原 .gitignore 文件內文本末尾沒有空白行時,就會導致".clang-format" 與最后一個文本拼接在一起,從而使 .gitignore 規則發生變化。
為了避免這一潛在的影響,需要在.gitignore 文件插入一個換行符之后再拼接".clang-format"。
通過修改 setup-repo.sh 腳本的 ensure_git_ignores_clang_format_file() 方法, 使用echo -e "\n.clang-format" >> "$gitignore_file" 即可實現插入換行符。完整方法如下:

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 格式化代碼

(最新代碼已去掉這個功能,有此需求的,仍然可以按照下面的方法實現)
上面??提到了,配置DisableFormat: true在我的Mac上并沒能禁止代碼格式化功能。通過修改spacecommander目錄下的format-objc-hooksetup-repo.sh 腳本:

  1. format-objc-hook
    代碼塊A的上面寫入代碼塊C(這部分代碼的作用是判斷目標工程根目錄下.git/hooks/pre-commit調用腳本 format-objc-hook傳的第一個參數是否為“-false”,若為"-false",直接退出命令,不再往下執行。否則,就繼續往下執行格式化代碼操作)
if [ "$1" == "-false" ]; then
    exit 0
fi
  1. setup-repo.sh
    這個腳本執行的方法如下:
ensure_pre_commit_file_exists && 
ensure_pre_commit_file_is_executable && 
ensure_hook_is_installed && 
ensure_git_ignores_clang_format_file && 
symlink_clang_format

概括來講就是在目標工程根目錄下生成可執行腳本.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的差異是執行format-objc-hook腳本是否傳“-false”。
通過注釋#enable auto-format下面的代碼或者#disable auto-format下面的代碼便可禁止或者開啟格式化代碼功能

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容