Dart - build_runner を使った自作 package/plugin のデバッグ方法

wasabeef
4 min readAug 22, 2020

--

わさびーふです。

Cover image by Harley-Davidson on Unsplash

json_serializable などを使った時に以下のコマンド叩くと思います。

$ flutter packages pub run build_runner build

Flutter アプリ (project_type: app) のデバッグをする時は、例えば Intellij などが Fluter Run Configuration を自動生成してくれるので、そんなに困ることはありませんが、build_runner コマンドで source_gen などを package/plugin を開発時にコード自動生成する過程でデバッグしたい時があります。いわゆる Flutter というよりかは Dart 側のデバッグ方法です。

Stack Overflow で該当の記事を見つけたのですが、少し情報が不足していたので説明していきます。

よく見かけるこのようなディレクトリ構成で package を作ってるとします。パッケージとそのサンプルプロジェクトです。

my_generator
├── build.yaml ※ build_runner を使う設定ファイル
├── example ※ サンプルプロジェクト(build_runner を実行する側)
├── lib ※ サンプルプロジェクト(build_runner を実行する側)
│ └── builder.dart ※ build_runner で実行されるビルダー
├── pubspec.lock
└── pubspec.yaml

Intellij の設定で説明していきます。VSCode の人は置き換えてください。

flutter packages pub run build_runner build のコマンドは example 内で実行しようとしているので 自動生成されている example/.dart_tool/build/build.dart のパスを確認します。

build.dart screen

② 上部にある ADD CONFIGURATIONS から設定していきます。

Add Configuration screen

Dart Command Line App を選択します。

Add New Configuration

④ Run Configurations の設定を以下の通りにしていきます。
Name:buid.dart
Dart file: example/.dart_tool/build/build.dart のパス
Program arguments: build
Working directory:example のパス

※ チーム開発で共有するために Git 上で管理したい場合には Store as project file にチェックが必要です。

Run/Debug Configurations screen

これで Intellij からでも以下のコマンドが叩かれることになります。

$ flutter packages pub run build_runner build

※ 前回のデバッグビルドから何かしらファイルが変更されていないと再度ブレイクポイントに止まらないので注意してください。

.idea/runConfigurations/build_dart.xml の設定内容も載せておきます。

あとがき

連載は次回(。-人-。)

--

--