flavorspec 0.1.0
flavorspec: ^0.1.0 copied to clipboard
Deterministic per-flavor and per-platform dependency overlays and compile-time defines for Flutter and Dart projects.
flavorspec #
Deterministic per-flavor and per-platform dependency overlays and compile-time defines for Flutter and Dart projects.
The Problem #
Dart and Flutter projects enforce a single pubspec.yaml dependency manifest. There is no native mechanism to include packages conditionally based on build target or deployment tier.
Upstream community requests have documented this need for years:
- flutter/flutter #46979: Request for different dependencies per flavor (e.g., mock services in dev, crash analytics in prod).
- flutter/flutter #81650: Request for per-platform dependencies (e.g., web-only plugins vs mobile native plugins).
- flutter/flutter #139289: Request for compile-time variables loaded cleanly from external files (
--dart-define-from-file).
Existing stopgap packages often attempt in-place YAML rewriting. Naive round-tripping strips comments, reformats arbitrarily, lacks write atomicity, and provides no CI drift prevention.
flavorspec solves this by treating pubspec.yaml as an explicitly generated artifact. The sources of truth are pubspec.base.yaml (your canonical configuration and base dependencies) and flavorspec.yaml (declarative overlays). The generated pubspec.yaml contains a clear header banner and can be checked for drift in CI pipelines.
Installation #
Activate globally for CLI usage across any repository:
dart pub global activate flavorspec
Or add it directly to your project's dev dependencies:
dart pub add dev:flavorspec
Quickstart #
Initialize flavorspec in an existing Dart or Flutter project:
flavorspec init
This creates:
pubspec.base.yaml: A copy of your existingpubspec.yaml.flavorspec.yaml: A starter configuration for flavors and platforms..flavorspec/pubspec.yaml.original: A safety backup of your original pubspec.
Define overlays in flavorspec.yaml:
flavors:
dev:
dependencies:
logging: ^1.2.0
defines:
API_URL: https://dev-api.example.com
ENABLE_LOGS: 'true'
prod:
dependencies:
firebase_crashlytics: ^4.0.0
firebase_analytics: ^11.0.0
defines:
API_URL: https://api.example.com
ENABLE_LOGS: 'false'
platforms:
web:
dependencies:
web: ^0.5.1
android:
dependencies:
path_provider_android: ^2.2.0
ios: {}
Apply your chosen configuration:
flavorspec apply --flavor dev --platform web
Commands #
flavorspec init #
Scaffolds pubspec.base.yaml and flavorspec.yaml non-destructively from an existing pubspec.yaml.
- Flags:
--force,-f: Overwrite existing base configuration if already present.
flavorspec list #
Lists all declared flavors, platforms, and displays the currently active applied state recorded in .flavorspec/state.json.
flavorspec apply #
Composes the base pubspec with the selected platform and flavor overlays, validates semver constraints, and writes pubspec.yaml atomically.
- Options:
--flavor,-f(required): The target flavor (e.g.,dev,prod).--platform,-p: Optional platform overlay (e.g.,web,android).--with-lock: Caches and restores per-flavorpubspec.lockfiles.--verify: Runspub get --dry-runto prove that generated dependencies resolve cleanly.--force: Bypasses git safety checks if uncommitted manual changes exist inpubspec.yaml.
flavorspec diff #
Shows a unified diff of what flavorspec apply would generate compared to the current pubspec.yaml on disk, without writing any files.
- Options:
--flavor,-f(required): The target flavor to preview.--platform,-p: Optional platform overlay to preview.
flavorspec check #
CI verification mode. Compares pubspec.yaml on disk against the expected composition.
- Exits with code 0 when clean.
- Exits with code 1 when drifted, printing a readable unified diff.
- Options:
--flavor,-f: Flavor to verify (defaults to active flavor from.flavorspec/state.json).--platform,-p: Platform overlay to verify.
flavorspec restore #
Restores the original pubspec.yaml saved during initialization from .flavorspec/pubspec.yaml.original. Works even if flavorspec.yaml was deleted.
flavorspec defines #
Extracts merged compile-time defines into a JSON file suitable for Flutter's --dart-define-from-file.
- Options:
--flavor,-f(required): Flavor whose defines to export.--platform,-p: Optional platform overlay to merge.--out,-o(required): Path to the output JSON file.
Example:
flavorspec defines --flavor prod --out config.json
flutter build appbundle --dart-define-from-file=config.json
Composition & Precedence Rules #
Composition follows strict, predictable layer precedence:
pubspec.base.yaml (Base)
↓
platforms.<platform> (Platform Overlay)
↓
flavors.<flavor> (Flavor Overlay)
- Base Layer: Shared dependencies and package metadata.
- Platform Layer: Overrides base dependencies and introduces platform-specific packages.
- Flavor Layer: Overrides base dependencies and platform defines.
Conflict Detection #
When multiple overlays specify the same dependency with different version constraints, silent overrides create hidden bugs.
flavorspec treats differing peer constraints as a configuration conflict:
- Identical constraints pass: If
platforms.webspecifiespkg_a: ^1.0.0andflavors.prodspecifiespkg_a: ^1.0.0, the composition succeeds. - Differing constraints fail: If
platforms.webspecifiespkg_a: ^1.0.0andflavors.prodspecifiespkg_a: ^2.0.0,flavorspecaborts with a clear error:flavorspec error: Conflicting constraints for dependency 'pkg_a' between platform 'web' (^1.0.0) and flavor 'prod' (^2.0.0). File: flavorspec.yaml Key: flavors.prod.dependencies.pkg_a Suggestion: Specify matching constraints across both layers, or define an override in 'dependency_overrides'. - Deliberate overrides: Adding an entry to
dependency_overridesexplicitly resolves the conflict.
Per-Flavor Lock Files (--with-lock) #
Switching flavors typically rewrites pubspec.lock, invalidating CI caches and causing version drift across local machines.
With --with-lock:
- When switching away from a flavor,
flavorspecarchivespubspec.lockto.flavorspec/locks/<flavor>[-<platform>].lock. - When switching to a target flavor,
flavorspecrestores.flavorspec/locks/<flavor>[-<platform>].lockdirectly topubspec.lock.
Guarantees #
- Restores previously resolved transitive dependency trees across flavor switches without hitting the network.
- Prevents unnecessary lockfile churn in pull requests.
Non-Guarantees #
- It does not bypass Dart SDK version constraints.
- If you alter version constraints in
pubspec.base.yamlorflavorspec.yaml, you must rundart pub getonce to regenerate the locked resolution.
Git Safety #
To prevent accidental data loss:
flavorspecchecks whetherpubspec.yamlcontains the# Generated by flavorspecbanner.- It verifies the SHA-256 hash against
.flavorspec/state.jsonto detect manual edits made after generation. - If uncommitted manual modifications are detected,
flavorspec applyaborts unless--forceis supplied.
What to Commit vs What to Gitignore #
| Path | Version Control | Purpose |
|---|---|---|
pubspec.base.yaml |
Commit | Source of truth for base dependencies and metadata. |
flavorspec.yaml |
Commit | Source of truth for flavor and platform overlays. |
.flavorspec/locks/ |
Commit | Preserved per-flavor lockfiles for reproducible builds. |
pubspec.yaml |
Commit or Ignore | Generated file. Teams may commit the default dev flavor or gitignore it. |
.flavorspec/state.json |
Gitignore | Local workspace state tracking the active flavor. |
.flavorspec/pubspec.yaml.original |
Gitignore | Local backup created before flavorspec adoption. |
Recommended .gitignore addition:
# flavorspec local workspace state
.flavorspec/state.json
.flavorspec/pubspec.yaml.original
CI Recipe (GitHub Actions) #
Use flavorspec check in CI to guarantee that pubspec.yaml has not drifted from its declarative source definitions:
name: Verify Pubspec Drift
on:
pull_request:
branches: [ main ]
jobs:
check-flavorspec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: Install flavorspec
run: dart pub global activate flavorspec
- name: Verify pubspec alignment
run: flavorspec check --flavor prod
Limitations #
- Source of truth shift: Developers must edit dependencies in
pubspec.base.yamlorflavorspec.yamlinstead of directly modifyingpubspec.yaml. - First-time lockfile generation: The initial resolution for a new flavor requires running
dart pub getonce after applying the flavor before--with-lockcan archive the lockfile.