loadPubspecAssets method

List<String> loadPubspecAssets(
  1. AssetConfig config
)

Loads and validates the pubspec.yaml configuration.

Parameters:

  • config: The asset configuration containing pubspec directory path

Returns a list of asset paths defined in pubspec.yaml.

Throws ConfigurationException if pubspec.yaml is invalid or missing.

Implementation

List<String> loadPubspecAssets(AssetConfig config) {
  final pubspecPath = config.getPubspecPath();

  if (!exists(pubspecPath)) {
    throw ConfigurationException(
      'pubspec.yaml not found at: $pubspecPath',
      suggestions: [
        'Ensure the pubspec_dir in morpheme.yaml points to the correct directory',
        'Create a pubspec.yaml file in the specified directory',
        'Check that the path is relative to the project root',
      ],
    );
  }

  final pubspecYaml = YamlHelper.loadFileYaml(pubspecPath);

  if (!pubspecYaml.containsKey('flutter')) {
    throw ConfigurationException(
      'Flutter configuration not found in pubspec.yaml',
      suggestions: [
        'Add a "flutter:" section to pubspec.yaml',
        'Ensure this is a Flutter project with proper pubspec.yaml structure',
      ],
    );
  }

  final flutter = pubspecYaml['flutter'];
  if (flutter is! Map || !flutter.containsKey('assets')) {
    throw ConfigurationException(
      'Assets configuration not found in flutter section of pubspec.yaml',
      suggestions: [
        'Add an "assets:" list under the "flutter:" section',
        'Example: flutter:\\n  assets:\\n    - assets/images/',
      ],
    );
  }

  final assetsRaw = flutter['assets'];
  if (assetsRaw is! List) {
    throw ConfigurationException(
      'Assets must be defined as a list in pubspec.yaml',
      suggestions: [
        'Ensure assets is a YAML list, not a single value',
        'Example: assets:\\n  - assets/images/\\n  - assets/icons/',
      ],
    );
  }

  // Filter out density-specific image directories (e.g., 2.0x, 3.0x)
  final assets = assetsRaw
      .cast<String>()
      .where((asset) => !_isDensitySpecificPath(asset))
      .toList();

  if (assets.isEmpty) {
    throw ConfigurationException(
      'No valid asset paths found in pubspec.yaml',
      suggestions: [
        'Add asset directory paths to the assets list',
        'Ensure asset paths exist and are accessible',
        'Example: assets:\\n  - assets/images/\\n  - assets/fonts/',
      ],
    );
  }

  return assets;
}