=============================================
あれこれブランチを切り替えながらひたすらアプリのバグの再現テストをしていると、いまどのブランチのコードを実行しているんだっけ? と分からなくなりわりと焦ります。そこでステータスバーにブランチ名を常時表示させることにして、脳みその負荷をちょこっと下げてみます。
Xcode にスクリプトを追加する
メニューから Editor -> Add Build Phase -> Ad Run Script Build Phase を選び、アプリのターゲットに新しい Run Script
を追加します。
次のようなコードを書いて Branch.h を生成するようにします。場所はお好みで。
run_script
branch_file="${SRCROOT}/path/to/Branch.h"
branch=`git branch | grep '^\*' | cut -b 3-`
echo -n '#define CURRENT_BRANCH @"' > $branch_file
echo -n $branch >> $branch_file
echo '"' >> $branch_file
ただし! スクリプトはコードがコンパイルされる前に実行されないといけないので、ドラッグ&ドロップで必ず Compile Sources
フェーズの上に持ってきます。
ついでに .gitignore にも追記しておくとよいでしょう。
.gitignore
path/to/Branch.h
KGStatusBar を導入する
github から KGStatusBar のソースをダウンロードしてプロジェクトに取り込んでおきます。
git clone https://github.com/kevingibbon/KGStatusBar.git
表示用のコードを書く
プロジェクトに Branch.h を追加して、次のようなコードをどこかに書いておきます。
AppDelegate.m
#import "Branch.h"
#import "KGStatusBar.h"
- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
...
#ifdef DEBUG
[[[KGStatusBar alloc] init] showWithStatus: CURRENT_BRANCH barColor: [UIColor grayColor] textColor: [UIColor whiteColor]];
#endif
...
}
ついでにバージョン番号も表示する例です。
NSString *statusString = [NSString stringWithFormat: @"%@ (%@)", CURRENT_BRANCH, [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleShortVersionString"]];
[[KGStatusBar sharedView] showWithStatus: statusString barColor: [UIColor grayColor] textColor: [UIColor whiteColor]];
まとめ
- Run Script フェーズを利用してコンパイル前に最新の Branch.h を作る
- KGStatusBar を使ってお手軽に表示する