LoginSignup
12
9

More than 5 years have passed since last update.

xcprettyをさらに活用する

Last updated at Posted at 2018-01-03

iOSアプリ開発では欠かすことができないxcodebuildですが、そのままのログ出力は非常に読みづらいです。

そんなログを見やすくするものに、fastlaneなど色々な箇所で利用されているxcprettyがあります。

このxcprettyですが、自身で拡張をすることができます。
拡張することにより、ログの見せ方を変えたりエラーなどの情報をファイルに出力させたりすることもできます。
このファイルをもちいてPRへのコメントやCIで通知させるといったこともできます。

その拡張方法と利用方法について以下に簡単に説明したいと思います。

Custom Formatterの例

まずはサンプルになる既存のgemについて紹介します。

例えば、Travis CIでは以下のCustom formatterを使用しています。

他にもJSON形式で出力してくれるCustom formatterがあります。

これを使えば、errors, warningsなどをJSON形式で出力してくれます。

呼び出し方

これらの公開されているgemの場合はまずgemをインストールし、以下のような形式で呼び出すことができます。

xcodebuild | xcpretty -f `xcpretty-json-formatter`

また、fastlaneを利用している場合、gymだとオプションとしてxcpretty_formatterが定義されています。
ここに上記のように xcpretty-json-formatter を指定すれば利用することができます。

サンプル
 gym(
   scheme: "MyApp",
   xcpretty_formatter: `xcpretty-json-formatter`
 )

参考

オリジナルのCustom Formatterの作り方

xcprettyのREADMEに多少情報が書いています。

XCPretty::Formatterのsubclass

通常以下のようなログが出てるかと思いますが、このログをそのまま出しておくなら XCPretty::Simple にするのが良いです。

▸ Check Dependencies
▸ Running script '[CP] Embed Pods Frameworks'
▸ Build Succeeded

format_*のメソッドをoverrideして利用

これらの該当となるメソッドは以下にあります。

例を出すと以下のようなものがあります。
名前からだいたい想像がつくかと思います。

format_compile_error
format_passing_test

サンプルプロジェクト

xcpretty-sample-formatterというのを作った場合の例は以下のとおりです。

dir構成例
 - bin/
   - xcpretty-sample-formatter
 - lib/
   - sample_formatter.rb
 - spec/
 - Gemfile
 - Gemfile.lock
 - README.md
 - Rakefile 
 - xcpretty-sample-formatter.gemspec

bin以下にあるファイルは以下のような感じになります。

bin/xcpretty-sample-formatter
#!/usr/bin/env ruby

print File.expand_path('../../lib/sample_formatter.rb', __FILE__)

メインとなるのは sample_formatter.rb になります。
単にwarningがあるファイルを出力するだけの簡単なものです。

lib/sample_formatter.rb
class SampleFormatter < XCPretty::Simple
  def initialize(use_unicode, colorize)
    super
  end

  def format_compile_warning(file, file_path, reason, line, cursor)
    super
    "warning_file:#{file}"
  end
end

SampleFormatter

自前で作っている場合の呼び出し方は以下のとおりです。

xcodebuild | xcpretty -f `#{your_path}/bin/xcpretty-sample-formatter`

このような感じで簡単に色々とログ周りをいじることができます。

参考資料

12
9
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
9