dart-package-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart Package Management
Dart 包管理
Goal
目标
Configures and manages Dart packages, monorepo workspaces, and directory layouts. Enforces standards, dependency resolution, and strict public/private API boundaries. Assumes a modern Dart environment (SDK >= 3.6.0) utilizing the official package layout conventions and workspace features for multi-package repositories.
pubspec.yaml配置并管理Dart包、单仓库工作区(monorepo workspaces)和目录布局。强制执行标准、依赖项解析以及严格的公共/私有API边界。假设使用现代Dart环境(SDK >= 3.6.0),遵循官方包布局规范和多包仓库的工作区特性。
pubspec.yamlInstructions
操作步骤
-
Determine Repository Architecture Evaluate the user's project scope using the following Decision Logic:
- Is the project a single standalone package? -> Proceed to Step 2a (Standard Package).
- Does the project contain multiple interdependent packages? -> Proceed to Step 2b (Monorepo Workspace).
- STOP AND ASK THE USER: If the repository structure is ambiguous, ask: "Should this project be configured as a single Dart package or a monorepo workspace containing multiple packages?"
-
ConfigureBased on the architecture decision, generate the appropriate configuration files.
pubspec.yamlStep 2a: Standard Package Create a standardat the project root.pubspec.yamlyamlname: package_name description: >- A concise description of the package (60-180 characters). version: 1.0.0 homepage: https://example.com/package_name environment: sdk: ^3.6.0 dependencies: path: ^1.9.0 dev_dependencies: test: ^2.4.0Step 2b: Monorepo Workspace Create a rootto define the workspace. Use glob patterns (pubspec.yaml) to automatically include sub-packages.*yamlname: root_workspace publish_to: none environment: sdk: ^3.6.0 workspace: - packages/*For each child package (e.g.,), enforce thepackages/client_package/pubspec.yamldirective and ensure the SDK constraint matches the root.resolution: workspaceyamlname: client_package description: Client implementation for the workspace. version: 0.1.0 environment: sdk: ^3.6.0 resolution: workspace dependencies: shared_package: ^1.0.0 # Resolves locally within the workspace -
Scaffold Package Layout Enforce the standard Dart directory structure. Create the following directories and files as needed:
- : Publicly exported libraries and assets.
lib/ - : Private implementation files.
lib/src/ - : Public command-line executables.
bin/ - : Internal development scripts.
tool/ - : Unit and integration tests.
test/ - : Usage examples.
example/
-
Implement Import Boundaries Apply strict import rules based on file location.
- Within reaching into
lib/: Use relative imports.lib/src/dart// lib/my_package.dart import 'src/internal_logic.dart'; - Outside (e.g.,
lib/,test/,bin/) reaching intoexample/: Uselib/src/imports.package:dart// test/my_package_test.dart import 'package:my_package/src/internal_logic.dart';
- Within
-
Manage Dependencies and State Execute package resolution commands to generate theand
package_config.jsonfiles.pubspec.lockbash# For initial setup or adding new dependencies dart pub get # To upgrade existing dependencies to their latest compatible versions dart pub upgrade -
Validate and Fix Verify the configuration state.
- Run (if in a monorepo) to ensure all child packages are recognized.
dart pub workspace list - Run to catch import boundary violations (
dart analyze).avoid_relative_lib_imports - If fails due to a stray
dart pub getin a non-workspace directory, delete the stray file or add it to thepubspec.yamllist, then re-runworkspace:.dart pub get
- Run
-
确定仓库架构 使用以下决策逻辑评估用户的项目范围:
- 项目是单个独立包吗? → 进入步骤2a(标准包)。
- 项目包含多个相互依赖的包吗? → 进入步骤2b(Monorepo工作区)。
- 停止并询问用户: 如果仓库结构不明确,请询问:“此项目应配置为单个Dart包,还是包含多个包的Monorepo工作区?”
-
配置根据架构决策,生成对应的配置文件。
pubspec.yaml步骤2a:标准包 在项目根目录创建标准的文件。pubspec.yamlyamlname: package_name description: >- A concise description of the package (60-180 characters). version: 1.0.0 homepage: https://example.com/package_name environment: sdk: ^3.6.0 dependencies: path: ^1.9.0 dev_dependencies: test: ^2.4.0步骤2b:Monorepo工作区 创建根目录的来定义工作区。使用通配符模式(pubspec.yaml)自动包含子包。*yamlname: root_workspace publish_to: none environment: sdk: ^3.6.0 workspace: - packages/*对于每个子包(例如),强制执行packages/client_package/pubspec.yaml指令,并确保SDK约束与根目录一致。resolution: workspaceyamlname: client_package description: Client implementation for the workspace. version: 0.1.0 environment: sdk: ^3.6.0 resolution: workspace dependencies: shared_package: ^1.0.0 # Resolves locally within the workspace -
搭建包目录结构 强制执行标准的Dart目录结构。根据需要创建以下目录和文件:
- : 公开导出的库和资源。
lib/ - : 私有实现文件。
lib/src/ - : 公开的命令行可执行文件。
bin/ - : 内部开发脚本。
tool/ - : 单元和集成测试。
test/ - : 使用示例。
example/
-
实现导入边界 根据文件位置应用严格的导入规则。
- 在内部导入
lib/中的文件: 使用相对导入。lib/src/dart// lib/my_package.dart import 'src/internal_logic.dart'; - 在外部(如
lib/、test/、bin/)导入example/中的文件: 使用lib/src/导入。package:dart// test/my_package_test.dart import 'package:my_package/src/internal_logic.dart';
- 在
-
管理依赖项与状态 执行包解析命令以生成和
package_config.json文件。pubspec.lockbash# For initial setup or adding new dependencies dart pub get # To upgrade existing dependencies to their latest compatible versions dart pub upgrade -
验证与修复 验证配置状态。
- 运行(如果是单仓库)以确保所有子包都能被识别。
dart pub workspace list - 运行来捕获导入边界违规(
dart analyze规则)。avoid_relative_lib_imports - 如果由于非工作区目录中存在多余的导致
pubspec.yaml失败,请删除该多余文件或将其添加到dart pub get列表中,然后重新运行workspace:。dart pub get
- 运行
Constraints
约束条件
- DO maintain a valid with clear version constraints (prefer
pubspec.yamlsyntax, e.g.,^).^2.1.0 - DO follow the directory convention for public exports; never place entrypoints (
lib/) directly inmain().lib/ - DO use and
dart pub getto manage thedart pub upgrade..dart_tool/package_config.json - DO implement workspaces in monorepos to share dependencies across local packages.
- DO ensure all public assets (e.g., CSS, images) are listed correctly in the pubspec or placed in the top-level directory for web/asset sharing.
lib/ - DO NOT check the directory into source control; ensure it is added to
.dart_tool/..gitignore - DO NOT use on packages with an SDK constraint lower than
resolution: workspace.^3.6.0 - DO NOT import from another package's directory under any circumstances.
lib/src/ - Related Skills: .
dart-api-design
- 务必维护有效的文件,使用清晰的版本约束(优先使用
pubspec.yaml语法,例如^)。^2.1.0 - 务必遵循目录的公共导出约定;切勿将入口点(
lib/)直接放在main()中。lib/ - 务必使用和
dart pub get来管理dart pub upgrade。.dart_tool/package_config.json - 务必在单仓库(monorepo)中实现工作区,以便在本地包之间共享依赖项。
- 务必确保所有公共资源(如CSS、图片)在pubspec中正确列出,或放置在顶层目录中以用于Web/资源共享。
lib/ - 切勿将目录提交到版本控制系统;确保将其添加到
.dart_tool/中。.gitignore - 切勿在SDK约束低于的包上使用
^3.6.0。resolution: workspace - 任何情况下都切勿从其他包的目录导入内容。
lib/src/ - 相关技能:。
dart-api-design