LoginSignup
3
2

More than 3 years have passed since last update.

Swiftlint導入までと環境構築スクリプト

Last updated at Posted at 2019-09-16

チームで開発をしていると書き方にばらつきがあったりしますが
保守的な背景から、これに制約を設けることでばらつきが無いように統一したくなります

そんなときにSwiftlintを使うと便利です

今回はbrewでswiftlintを入れます

インストール

brew install swiftlintf

Xcodeで設定

Build PhaseでNew Run Script Phaseを選択
Run Scriptが作成されるのでわかりやすいように名前をRun Swiftlintとかにしましょう

スクリプトを記述します

if which swiftlint >/dev/null; then
swiftlint autocorrect --format
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"

swiftlintが入っているか確認して、あればswiftlint autocorrect --formatでインデント修正含むソースコードの自動修正(--formatオプションがインデント修正は含まないソースコードの自動修正)、swiftlintでソースコードの静的解析を行います
swiftlintが入ってなければechoで指定した文章をログに出力します

swiftlintのルールをカスタマイズ

ただデフォルトのままだとルールが厳しすぎるのでプロジェクトと同じディレクトリにymlファイルを作ってルールを変更します

まずはデフォルトで有効にされているものの中から必要なものを無効にしたいものだけをymlファイルに書いていけばいいと思います

以下は私が設定したものになります
利用する場合は自己責任でお願いします
ルールに関してはこちらがまとめられてます

swiftlint.yml


# Lint対象にするファイル
included:
  - test/
  - testTests/
  - testUITests/
# あればその他ディレクトリも 

# Lint対象から外すファイル
excluded:
  - Pods/

# デフォルト以外で有効にするルール
opt_in_rules:
  - closure_end_indentation
  - first_where
  - operator_usage_whitespace

# デフォルトから無効にするルール
disabled_rules:
  - trailing_whitespace
  - type_name
  - identifier_name
  - cyclomatic_complexity
  - shorthand_operator
  - nesting
  - closure_end_indentation
  - discarded_notification_center_observer

force_cast: warning
force_try: warning

file_length:
  warning: 1200
  error: 1200

function_body_length:
  warning: 400
  error: 400

line_length:
  warning: 450
  error: 450

type_body_length:
  warning: 600
  error: 600

function_parameter_count:
  warning: 7
  error: 8

環境構築スクリプト作成

チーム開発でリポジトリをクローンした後にコマンド一発でswiftlintのような開発に必要な環境を構築できると楽です
swiftlintだけなら問題ないですが、他にも必要になってきたら面倒なので用意したほうがいいです
参考: https://qiita.com/lovee/items/38cdfd5d2f5827f27427#11-環境構築スクリプトを作成

シェルスクリプトを作成します
シェルスクリプトとは?みたいな人はこの記事がわかりやすいかも

bootstrap.sh

#!/bin/sh

# この一行目で、実行時どのシェルでスクリプトを実行するかが決まる。

echo "Install Swiftlint"
brew install swiftlint

echo "Install bundler and gems"
gem install bundler
bundle install --path vendor/bundl

echo "Install dependencies via CocoaPods"
bundle exec pod install

open test.xcworkspace

あとはsh bootstrap.shをターミナルで叩けば一発です

参考

https://github.com/realm/SwiftLint#usage
https://qiita.com/shtnkgm/items/6dd756aa14926736c6f5
https://ticklecode.com/install-swiftlint/

3
2
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
3
2