dart-code-generation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesedart-build-runner-automation
Dart Build Runner 自动化
Goal
目标
Configures and executes Dart's tool to automate code generation, testing, and serving of Dart applications. Analyzes project requirements to inject necessary development dependencies, determines the optimal build strategy (one-time build vs. continuous watching), and resolves file conflict errors during the generation phase.
build_runner配置并执行Dart的工具,以自动化Dart应用的代码生成、测试和服务。分析项目需求以注入必要的开发依赖,确定最优构建策略(一次性构建 vs 持续监听),并解决生成阶段的文件冲突错误。
build_runnerDecision Logic
决策逻辑
When tasked with running a build or code generation process, evaluate the user's current context to select the correct command:
- Is the user actively developing and modifying files? -> Choose .
watch - Is this a CI/CD pipeline or a one-off production build? -> Choose .
build - Is the user building a web application? -> Do NOT use ; delegate to
build_runner serve(seewebdev serve).dart-web-development - Does the user need to run tests on generated code? -> Choose .
test
当需要执行构建或代码生成流程时,评估用户当前环境以选择正确的命令:
- 用户是否正在积极开发并修改文件? -> 选择命令。
watch - 这是否是CI/CD流水线或一次性生产构建? -> 选择命令。
build - 用户是否正在构建Web应用? -> 不要使用;转而使用
build_runner serve(参考webdev serve)。dart-web-development - 用户是否需要对生成的代码运行测试? -> 选择命令。
test
Instructions
操作步骤
-
Verify and Inject Dependencies Inspect thefile. Ensure that
pubspec.yamlis listed underbuild_runner. If testing generated code, also ensuredev_dependenciesis present.build_testyamldev_dependencies: build_runner: ^2.4.0 # Use the latest compatible version build_test: ^3.2.0 # Optional: Only if testing is requiredRun the package fetch command:bashdart pub get -
Determine Project Policy on Generated Files STOP AND ASK THE USER: "Should generated files (e.g.,) be committed to version control, or are they generated on-the-fly in this project?"
.g.dart- If generated on-the-fly: Ensure is added to the
.g.dartfile..gitignore - If committed: Proceed without modifying .
.gitignore
- If generated on-the-fly: Ensure
-
Execute the Build Command Based on the Decision Logic, execute the appropriate command from the project root.For continuous background generation during development (PREFERRED):bash
dart run build_runner watchFor a one-time build:bashdart run build_runner buildFor running tests that require code generation:bashdart run build_runner test -
Validate-and-Fix: Handle Conflicting Outputs Monitor the terminal output for the build command. If the build fails with a(indicating that generated files already exist and conflict with the new build), automatically recover by appending the
ConflictingOutputsExceptionflag.--delete-conflicting-outputsbashdart run build_runner build --delete-conflicting-outputsOr for watch mode:bashdart run build_runner watch --delete-conflicting-outputsVerify that the command succeeds after applying this flag.
- 验证并注入依赖
检查文件,确保
pubspec.yaml已列在build_runner下。如果需要对生成的代码进行测试,还要确保dev_dependencies已存在。build_test
yaml
dev_dependencies:
build_runner: ^2.4.0 # 使用最新兼容版本
build_test: ^3.2.0 # 可选:仅在需要测试时添加运行包获取命令:
bash
dart pub get- 确定项目对生成文件的管理策略
务必先询问用户:“生成的文件(如)是否需要提交到版本控制,还是在项目中按需生成?”
.g.dart
- 如果是按需生成: 确保已添加到
.g.dart文件中。.gitignore - 如果需要提交: 无需修改,直接继续。
.gitignore
- 执行构建命令 根据决策逻辑,从项目根目录执行相应的命令。
- 开发期间推荐使用(持续后台生成):
bash
dart run build_runner watch- 一次性构建:
bash
dart run build_runner build- 运行需要代码生成的测试:
bash
dart run build_runner test- 验证与修复:处理输出冲突
监控构建命令的终端输出。如果构建因失败(表示生成的文件已存在并与新构建冲突),自动通过添加
ConflictingOutputsException参数恢复。--delete-conflicting-outputs
bash
dart run build_runner build --delete-conflicting-outputs- 对于watch模式:
bash
dart run build_runner watch --delete-conflicting-outputs验证添加该参数后命令是否执行成功。
Constraints
约束条件
- Strict Web App Routing: Never use for web applications. You must use the
dart run build_runner servetool instead.webdev - File Tracking: AVOID committing files if the project policy is to generate them on-the-fly. Always clarify this policy before running git commands.
.g.dart - Development Preference: PREFER the command for continuous background generation during active development sessions to prevent stale code errors.
watch - Conflict Resolution: Always use when regenerating files that have changed structure or when switching branches, rather than manually deleting
--delete-conflicting-outputsor.dart_toolfiles..g.dart - Related Skills: Defer to for advanced test configurations and
dart-testingfor web-specific serving and compilation.dart-web-development
- Web应用严格路由: 绝不要对Web应用使用,必须改用
dart run build_runner serve工具。webdev - 文件追踪: 如果项目策略是按需生成文件,请勿提交这些文件。在运行git命令前务必明确此策略。
.g.dart - 开发优先选择: 在活跃开发会话期间,优先使用命令进行持续后台生成,以避免代码过期错误。
watch - 冲突解决: 当重新生成结构已更改的文件或切换分支时,始终使用,而非手动删除
--delete-conflicting-outputs或.dart_tool文件。.g.dart - 相关技能: 高级测试配置请参考,Web特定的服务与编译请参考
dart-testing。dart-web-development