LoginSignup
12
10

More than 5 years have passed since last update.

SwiftLintを変更のあったファイルのみに実行する方法

Last updated at Posted at 2018-02-16

SwiftLintの個別ファイル実行方法

公式にも書いてあるRun Script Phaseを少し書き換えてgitのdiff差分で回すだけ。

if which swiftlint >/dev/null; then
  git diff --name-only | grep .swift | while read filename; do
    swiftlint --path "$filename"
  done
else
  echo "SwiftLint does not exist, download from https://github.com/realm/SwiftLint"
fi
  • この方法だと、全体に実行しないので、ビルド実行時間が短くなる利点もあり
  • git diff--cachedを付けるべきかは、開発フローによるのでお好みで

なぜ差分だけに実行?

既存サービスの途中からSwiftLintを導入した場合、膨大な数の警告が出て、lint以外の警告を見逃してしまうため。

本当は、個別ファイルの保存のタイミングで自動実行できると良いのだけど、そこまでは方法わからず。
Xcode Source Editor Extensionを作ればいけるのかな。

おまけ: swiftlint autocorrect --format

re-indentしてくれる。autocorrectするときは、合わせてどうぞ。

$ swiftlint help autocorrect 

[--format]
    should reformat the Swift files

公式サイトのCHANGELOG.md説明

--format option to autocorrect command which re-indents Swift files much like pasting into Xcode would.
12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10