A Dart build package to compile Protocol Buffer files to Dart source code using build_runner (i.e. the Dart build pipline) without needing to manually install the protoc compiler or the Dart Protobuf plugin protoc_plugin.
The protoc_builder package downloads the necessary Protobuf dependencies for your platform to a
temporary local directory, thereby streamlining the development process.
Installation
Add the necessary dependencies to your pubspec.yaml file:
dev_dependencies:
build_runner: <latest>
protoc_builder: <latest>
Configuration
You must add your .proto files to a build.yaml file next to the pubspec.yaml:
targets:
$default:
sources:
# Note: setting `sources` replaces the build_runner defaults, so keep the
# directories that other builders need as well (e.g. `lib/**`, `test/**`).
- lib/**
- proto/** # Your .proto directory
- $package$
- lib/$lib$
This will use the default configuration for the protoc_builder.
You may also configure custom options:
targets:
$default:
sources:
- lib/**
- proto/**
- $package$
- lib/$lib$
builders:
protoc_builder:
options:
# The version of the Protobuf compiler to use.
# (Default: "36.1", make sure to use quotation marks)
protobuf_version: "36.1"
# The version of the Dart protoc_plugin package to use.
# (Default: "25.0.0", make sure to use quotation marks)
protoc_plugin_version: "25.0.0"
# Directory which is treated as the root of all Protobuf files.
# (Default: "proto/")
root_dir: "proto/"
# Include paths given to the Protobuf compiler during compilation.
# (Default: ["proto/"])
proto_paths:
- "proto/"
# The root directory for generated Dart output files.
# (Default: "lib/src/proto")
out_dir: "lib/src/generated"
# Enable the gRPC flag for the Dart protoc plugin to generate `.pbgrpc.dart` files.
# (Default: false)
grpc: true
# Use the "protoc" command that's available on the PATH instead of downloading one
# (Default: false)
use_installed_protoc: false
# Whether the protoc_plugin Dart scripts should be precompiled for better performance.
# (Default: true)
precompile_protoc_plugin: true
# Custom protoc_plugin parameters.
# (Default: [])
protoc_plugin_parameters: []
Well-known types
Imports of the well-known types bundled with the Protobuf compiler (e.g.
google/protobuf/timestamp.proto) resolve automatically. As of
protoc_plugin 25.0.0 and protobuf 6.0.0 (the default versions), the
generated code imports them from the protobuf package, so no additional
setup is needed.
Pub workspaces
The builder can be used in packages that are part of a pub workspace. Either build a single member package from its own directory, or build all members at once from the workspace root:
dart run build_runner build --workspace
Running
Once everything is set up, you may simply run the build_runner package:
dart run build_runner build
The build_runner sometimes caches results longer than it should, so in some cases, it may be necessary to delete the .dart_tool/build directory.