ensureFlutterLauncherIcons method

Future<void> ensureFlutterLauncherIcons(
  1. File pubspecFile
)

Ensures that the flutter_launcher_icons package is included under the dev_dependencies section of the given pubspecFile.

If the dependency is missing, this method automatically adds it with a default version (^0.14.0) and logs progress messages.

Example:

final pubspec = File('my_app/pubspec.yaml');
await PubspecManager(logger).ensureFlutterLauncherIcons(pubspec);

Throws:

Implementation

Future<void> ensureFlutterLauncherIcons(File pubspecFile) async {
  final content = await pubspecFile.readAsString();
  final yaml = loadYaml(content);

  // Attempt to read dev_dependencies safely
  final devDeps = yaml['dev_dependencies'];
  final hasLauncherIcons =
      devDeps is Map && devDeps.containsKey('flutter_launcher_icons');

  if (!hasLauncherIcons) {
    logger.info('Adding flutter_launcher_icons to dev_dependencies...');

    var updatedContent = content;

    // Ensure the dev_dependencies section exists
    if (!content.contains('dev_dependencies:')) {
      updatedContent += '\ndev_dependencies:\n';
    }

    // Insert or replace flutter_launcher_icons dependency
    updatedContent = updatedContent.replaceFirst(
      RegExp(r'dev_dependencies:\s*\n'),
      'dev_dependencies:\n  flutter_launcher_icons: ^0.14.0\n',
    );

    await pubspecFile.writeAsString(updatedContent);
    logger.info('✓ Added flutter_launcher_icons to pubspec.yaml');
  } else {
    logger.info('flutter_launcher_icons already present in pubspec.yaml');
  }
}