LoginSignup
11
4

More than 1 year has passed since last update.

SwiftFormatのプラグインの導入方法(Xcode project 編)

Last updated at Posted at 2022-12-31

SwiftFormat に、Swift Package Manager ビルドツールプラグインの機能が追加されました。この SwiftFormat プラグインの導入方法についてREADMEに沿って説明します。
おそらく 2022/11/24 にリリースされた SwiftFormat 0.50.4 で対応されたのではないかと思います。

SwiftFormat 0.50.4 リリースノート

Added Swift package command plugin

関連PR

README.md

インフォメーション
記事執筆現在、既に 0.50.7 がリリースされています。
導入される方は最新のリリースを導入すると良いと思います :ok_woman:

SwiftFormat のプラグインとは

この機能により、SwiftFormat を SwiftPM コマンド プラグインとして使用できます。

なお、そもそもプラグインとは何かを知りたい方は、以下の記事を読まれることをおすすめします :pray: :sparkles:

開発環境

  • Xcode 14.0 以降
  • Swift 5.6 以降

SwiftFormat のプラグインの導入方法

SwiftFormat のプラグインの導入方法は、2種類あります。

  • Xcode project
    • Xcode project の "Package Dependencies" から SwiftFormat を追加する方法
  • Package.swift
    • Package.swift に記述する方法

詳しくは README.md に記述されているので、ご参照ください。
当記事では、Xcode project で導入する方法をスクショと共に説明します。

Xcode project 編

1. プロジェクト設定の "Package Dependencies" タブを開き、"+" ボタンをクリックします。

1.png

2. 右上の検索バーに github.com/nicklockwood/SwiftFormat を入力し、SwiftFormat を検索します。
"Dependency Rule" を適宜変更してください(画像の場合は、 Up to Next Minor Version で、0.50.4 以上を指定しています)。

2.png

3. "Package Product" で選択候補が3つ出ますが、 SwiftFormat (Library) のみチェックを入れます。

3.png

4. パッケージ追加後の様子

4.png

Xcode プロジェクトからのプラグインの実行方法

1. プロジェクト上で右クリックし、 SwiftFormatPlugin をクリックします。

5.png

2. Run をクリックします。

6.png

実行前

before.png

実行後

after.png

インフォメーション
SwiftFormat のルールを記述する .swiftformat ファイルを作成しない場合は、SwiftFormat のデフォルトのルールが適用されます。

検討事項
Xcodeからプラグインを実行する場合の実行タイミングは、毎回のコミット前にするのがいいかな?と思います。
チーム開発をする場合は、コマンドラインからのプラグインの実行を、ビルド時などどこかのタイミングに仕込んで自動化した方がいいのかな? :thinking: と思います。
https://github.com/nicklockwood/SwiftFormat#trigger-plugin-from-command-line

SwiftFormat のルールに関する設定

SwiftFormat のルールをカスタマイズしたい場合、従来どおり .swiftformat ファイルを作成します。

.swiftformat
# format options
--allman true
--indent tab # tabs FTW!

# file options
--exclude Pods

# rules
--disable elseOnSameLine,semicolons

従来どおり、新しいバージョンのSwiftFormatのルールを適用させるため、Swift のバージョンを指定する .swift-version ファイルを作成します。

.swift-version
5.6

.swiftformat ファイルと.swift-version ファイルの置き場所

  • .swiftformat ファイル

The plugin will find an existing .swiftformat in your package root folder and honor it automatically.
(このプラグインは、あなたのパッケージのルートフォルダにある既存の .swiftformat を見つけ、自動的にそれを尊重します。)

  • .swift-version ファイル

The preferred option is to add a .swift-version file to your project directory. This is a text file that should contain the minimum Swift version supported by your project, and is a standard already used by other tools.
(望ましい選択肢は、プロジェクトのディレクトリに .swift-version ファイルを追加することです。これは、あなたのプロジェクトがサポートする最小のSwiftバージョンを含むべきテキストファイルで、すでに他のツールで使用されている標準的なものです。)

  • ルートディレクトリの状態

root-directory.png

最後に

以上、 SwiftFormat のプラグインの導入方法の説明でした。
ご覧いただきありがとうございます。

参考リンク

11
4
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
11
4