一个 Dart build 包,用于将 Protocol Buffer
文件编译为 Dart 源代码,使用 build_runner (即
Dart 构建管道) 而无需手动安装 protoc
编译器或 Dart Protobuf 插件 protoc_plugin。
protoc_builder 包会将适用于您平台的必要 Protobuf 依赖项下载到
一个本地临时目录,从而简化开发流程。
安装
将必要的依赖项添加到您的 pubspec.yaml 文件中
dev_dependencies:
build_runner: <latest>
protoc_builder: <latest>
配置
您必须将您的 .proto 文件添加到 pubspec.yaml 文件旁边的 build.yaml 文件中
targets:
$default:
sources:
- $package$
- lib/$lib$
- proto/** # Your .proto directory
这将使用 protoc_builder 的默认配置。
您也可以配置自定义选项
targets:
$default:
sources:
- $package$
- lib/$lib$
- proto/**
builders:
protoc_builder:
options:
# The version of the Protobuf compiler to use.
protobuf_version: "3.19.1" # Make sure to use quotation marks.
# The version of the Dart protoc_plugin package to use.
protoc_plugin_version: "20.0.0" # Make sure to use quotation marks.
# Include paths given to the Protobuf compiler during compilation.
proto_paths:
- "proto/"
# The root directory for generated Dart output files.
out_dir: "lib/src/generated"
运行
一切设置完成后,您就可以简单地运行 build_runner 包
pub run build_runner build
build_runner 有时会缓存结果的时间过长,因此在某些情况下,可能需要删除 .dart_tool/build 目录。