Swift をコマンドラインから使う
何度目かのゲーム作りをしている。
UI ではなく、戦闘 AI やマップ自動生成などは Xcode ではなく、コマンドライン上で println & dump で確認しながら開発したい。
Swift のインストール
Mavericks に入っている Xcode 6.0 では Swift が動かない。
なので Apple Developer から Xcode 6.1 GM 以降 をダウンロード。
Vim
Xcode の Vim キーバインドである JugglerShu/XVim も素晴らしいけど、Vim の toyamarinyon/vim-swift を利用。
NeoBundle 'thinca/vim-quickrun' NeoBundle 'toyamarinyon/vim-swift' autocmd BufRead,BufNewFile *.swift set filetype=swift let g:quickrun_config['swift'] = { \ 'command': 'xcrun', \ 'cmdopt': 'swift', \ 'exec': '%c %o %s', \}
これで ¥r で quickrun できる。
REPL
Xcode にリッチな Playground があるけど、コマンドラインの REPL がお手軽。
$ xcrun swift
alias に swift で割り当てた。
コンパイル
コマンドラインからコンパイルする予定はないけど、念のため alias に swiftc で割り当てた。
$ SDKPATH=$(/usr/bin/env xcrun --show-sdk-path --sdk macosx) $ xcrun swiftc foo.swift $ file foo foo: Mach-O 64-bit executable x86_64
ユニットテスト
xcodebuild
Xcode でプロジェクトを作ってテストファイルを作成 ( Xcode を使わずコマンドラインからする方法があれば教えてください )。
Xcode 標準の xcodebuild のやり方。
$ xcodebuild test -scheme foo -destination 'platform=iOS Simulator,name=iPad'
情報量が多すぎる。xcpretty があると見やすくなる。
$ gem install xcpretty $ xcodebuild test -scheme foo -destination 'platform=iOS Simulator,name=iPad' |xcpretty -c
xctool
xcodebuild はテスト速度が遅すぎる。そこで Facebook 謹製の xctool。
$ brew install xctool $ xctool -scheme foo run-tests -parallelize -sdk iphonesimulator
倍くらい速い。xcodebuild よりバグった場合に表示されるコード行が多いので見やすい。それに Mac の通知で完了を教えてくれる。
Quick
RSpec 風に書ける。
最近、3 つの Rails アプリの RSpec を 3.x に書き換えたばかりで軽く消耗した身としては RSpec をやめて Test::Unit に戻る に共感中。
この Quick は RSpec 以上に変化&進化中。RSpec 風の記述は好きなので、少し落ち着いたら見てみる。
Guard::Shell
ファイルを更新する毎に swift とテストの実行をするために Guard::Shell 導入。
$ bundle init $ echo "gem 'guard-shell'" >> Gemfile $ bundle $ guard init shell
Guardfile を編集。
guard :shell do watch(/(.*)¥.swift/) do |m| `xcrun swift #{m[0]}` `xctool -scheme foo run-tests -parallelize -sdk iphonesimulator` end end
実行は下記。
$ bundle exec guard
CI
Travis CI が Swift に対応。書き方はこちらで。
パッケージ管理
CocoaPods
Objective-C 系のライブラリをパッケージ管理。