LoginSignup
5

More than 1 year has passed since last update.

posted at

updated at

SwiftLintの設定ファイルをサーバーに配置して共有する方法

はじめに

本記事は Swift/Kotlin愛好会 Advent Calendar 2020 の18日目の記事です。
SwiftLint 0.42.0から設定ファイルをリモートで取得できるようになったので、その方法を紹介します。

環境

  • OS:macOS Big Sur 11.0.1
  • Swift:5.3.2
  • Xcode:12.3 (12C33)
  • SwiftLint:0.42.0

本記事で説明しないこと

設定ファイルをサーバーに配置して共有する方法

設定ファイルを作成してサーバーに配置し、ローカルで取得するまでの方法を紹介します。

共有する設定ファイルの作成

任意の名前でSwiftLintの設定ファイルを作成します。
通常の設定ファイルと書き方は変わりません。共有したい設定のみ抽出してください。

私は以下を抽出しました。

  • disabled_rules
  • opt_in_rules
  • analyzer_rules
  • ルール個別の設定
uhooi-base-swiftlint-config.yml
disabled_rules:
  #- block_based_kvo
  # ...

opt_in_rules:
  - anyobject_protocol
  # ...

analyzer_rules:
  - unused_declaration
  # ...

line_length:
  warning: 300
  error: 500

identifier_name:
  min_length:
    warning: 1

以下はプロジェクトごとに変わるため、抽出しませんでした。

  • included
  • excluded

共有する設定ファイルをサーバーに配置

作成した設定ファイルをサーバーに配置します。

私はSwiftLintの設定ファイルを配置するためだけのGitHubリポジトリを作成し、そこに配置しました。
https://github.com/uhooi/SwiftLint-Config/blob/main/uhooi-base-swiftlint-config.yml
スクリーンショット 2020-12-17 22.12.32.png

SwiftLintの対応バージョンごとにタグを付ければ、SwiftLintのバージョンが異なるプロジェクトでもタグを指定して設定ファイルを取得して使い回せます。
私はGitHubのリリース機能を使っています。
https://github.com/uhooi/SwiftLint-Config/releases
スクリーンショット 2020-12-17 22.19.03.png

READMEにSwiftLintのバージョンと設定ファイルのリリースの対応表を載せるとわかりやすいです。
(SwiftLintのREADMEに記載されている、SwiftとSwiftLintのバージョン対応表を参考にしました)
スクリーンショット 2020-12-17 22.21.20.png

取得先の設定ファイルの設定

サーバーに配置した設定ファイルを、他の設定ファイルから読み込みます。
parent_config を追記するのみです。

.swiftLint.yml
parent_config: {共有する設定ファイルのURL}

GitHubリポジトリに配置している場合、 https://raw.githubusercontent.com/{ユーザー名}/{リポジトリ名}/{タグまたはブランチ名}/{設定ファイル名} で設定ファイルを取得できます。

.swiftLint.yml
parent_config: https://raw.githubusercontent.com/uhooi/SwiftLint-Config/v1.0.0/uhooi-base-swiftlint-config.yml

parent の名前通り、サーバーに配置した設定ファイルが親の設定になります。
子の設定が優先されて上書きできます。

私は自分が作成した設定ファイルなのでルールは一切上書きせず、 includedexcluded のみ設定しています。

.swiftlint.yml
parent_config: https://raw.githubusercontent.com/uhooi/SwiftLint-Config/v1.0.0/uhooi-base-swiftlint-config.yml

included:
  - Shared
  - UhooiPicBook
  - UhooiPicBookStickers
  - UhooiPicBookWidgets
  - UhooiPicBookWidgetsConfigurableIntent
  #- UhooiPicBookTests
  #- UhooiPicBookUITests

excluded:
  - UhooiPicBook/Generated

設定ファイルがだいぶスッキリしました!

サーバーに配置した設定ファイルの取得

parent_config に設定ファイルのURLを指定してSwiftLintを実行すると、 .swiftlint/RemoteConfigCache フォルダにリモートの設定ファイルがダウンロードされます。

上記のフォルダは名前の通りキャッシュの役目を果たすため、すでに設定ファイルが存在する場合はそのまま使われます。

ダウンロード時に .gitignore へキャッシュフォルダが自動で書き込まれるので、そのままコミットしてください。

.gitignore
+ 
+ 
+ # SwiftLint Remote Config Cache
+ .swiftlint/RemoteConfigCache 

リモートのタイムアウトはデフォルト2秒、キャッシュが存在する場合はデフォルト1秒です。
変更する場合は remote_timeout または remote_timeout_if_cached を指定します。

おわりに

これでSwiftLintの設定を複数のプロジェクトでかんたんに共有できます!
私の設定ファイルを親として使ってもいいですよ :relaxed:

以上、 Swift/Kotlin愛好会 Advent Calendar 2020 の18日目の記事でした。
明日はまだ埋まっていません。ぜひ こちら から参加しましょう!

参考リンク

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
What you can do with signing up
5