Menu

Code Generation Tools

Relevant source files

This document describes the code generation tools used in the Flutter Mobile Project Template to automate code production, ensure consistency, and improve developer productivity. These tools handle tasks ranging from localization generation to GitHub label configuration and dependency analysis.

Localization Generation

The project uses Flutter's built-in localization generation system to convert ARB (Application Resource Bundle) files into type-safe Dart code for internationalization.

Localization Configuration Structure

Each feature package and the main app have their own localization configuration through l10n.yaml files which specify how localization files should be processed.

Sources: apps/app/l10n.yaml packages/features/setting/l10n.yaml packages/features/maintain/l10n.yaml

Localization YAML Configuration

The l10n.yaml files specify:

Configuration KeyPurposeExample
arb-dirDirectory containing ARB fileslib/l10n
output-dirDirectory for generated fileslib/gen/l10n
template-arb-fileBase template ARB fileapp_ja.arb
output-localization-fileName of generated main filel10n.dart
output-className of generated classL10n
preferred-supported-localesPrioritized localesja, en
nullable-getterWhether getters can be nullfalse

Sources: apps/app/l10n.yaml packages/features/setting/l10n.yaml

Localization Generation Process

Sources: apps/app/l10n.yaml apps/app/lib/l10n/app_ja.arb apps/app/lib/l10n/app_en.arb packages/features/setting/lib/src/l10n/features_setting_ja.arb packages/features/setting/lib/src/l10n/features_setting_en.arb

GitHub Label Generation Tools

The template includes two tools for automatically generating GitHub label configurations based on the project's package structure.

Label Generation Process

Sources: tools/gen_labels.dart tools/gen_labeler.dart tools/utils/label.dart tools/utils/constants.dart

gen_labels.dart

This tool generates GitHub issue labels for each Melos package in .github/labels.yml:

  1. Reads the existing labels file
  2. Identifies the auto-generated section using the comment marker
  3. Preserves manual content before the marker
  4. Generates label configurations for each Melos package
  5. Updates the file with both preserved manual content and new generated content

Sources: tools/gen_labels.dart tools/utils/label.dart5-27 tools/utils/constants.dart

gen_labeler.dart

This tool generates GitHub PR labeler configurations in .github/labeler.yml:

  1. Similar workflow to gen_labels.dart
  2. Generates configurations that automatically apply labels to PRs based on which files were modified
  3. Each Melos package gets its own label configuration

Sources: tools/gen_labeler.dart tools/utils/label.dart5-27

Pubspec Diff Analysis Tool

The diff_yaml.dart tool analyzes package dependency changes by comparing version specifications between files and branches.

Diff YAML Tool Functionality

Sources: tools/diff_yaml.dart

Key Features

The tool performs two important comparisons:

  1. Branch Comparison: Compares the current branch's pubspec.lock with a reference branch (typically main) to identify dependency version changes
  2. Notation Comparison: Identifies packages where the version constraint in pubspec.yaml differs from the resolved version in pubspec.lock

Sources: tools/diff_yaml.dart17-55

Output Format

The tool generates a Markdown report with two sections:

  1. Lock File Changes: A table showing changes in package versions between branches
  2. Version Notation Differences: A table comparing version constraints in pubspec.yaml to resolved versions in pubspec.lock

Sources: tools/diff_yaml.dart69-103

Utility Components Supporting Code Generation

Sources: tools/utils/label.dart tools/utils/logger.dart tools/utils/constants.dart tools/utils/command_runner.dart tools/model/melos_package.dart

Shared Utilities

UtilityDescription
label.dartUtilities for finding auto-generated sections in files
logger.dartConsole logging with formatting and error handling
constants.dartShared constant values like comment markers
command_runner.dartWrapper for executing shell commands
path.dartUtilities for working with file paths
melos_package.dartModel representing a package managed by Melos

Sources: tools/utils/label.dart tools/utils/logger.dart tools/utils/constants.dart tools/utils/command_runner.dart tools/model/melos_package.dart

Running Code Generation Tools

Localization Generation

Run Flutter's built-in generator:

flutter gen-l10n

GitHub Label Generation

dart tools/gen_labels.dart
dart tools/gen_labeler.dart

Pubspec Diff Analysis

dart tools/diff_yaml.dart main pubspec.yaml pubspec.lock

Where main is the reference branch to compare against.

Sources: tools/diff_yaml.dart55-106