generateGroupedPubspec method

Future<String> generateGroupedPubspec(
  1. GroupedDependencies groupedDeps,
  2. GroupedDependencies groupedDevDeps
)

Generate grouped pubspec.yaml content

Implementation

Future<String> generateGroupedPubspec(
  GroupedDependencies groupedDeps,
  GroupedDependencies groupedDevDeps,
) async {
  final pubspecFile = File(FileConfig.pubspecFile);
  if (!pubspecFile.existsSync()) {
    throw Exception('${FileConfig.pubspecFile} not found');
  }

  final originalContent = await pubspecFile.readAsString();
  final lines = originalContent.split('\n');

  // Find dependency sections
  final devDependenciesSection = _findSection(lines, 'dev_dependencies');

  // Replace dev_dependencies section first (to avoid index shifting issues)
  if (devDependenciesSection != null && groupedDevDeps.grouped.isNotEmpty) {
    _replaceDependencySection(
      lines,
      devDependenciesSection,
      groupedDevDeps,
      'dev_dependencies',
    );
  }

  // Re-find dependencies section after potential changes
  final updatedDependenciesSection = _findSection(lines, 'dependencies');

  // Replace dependencies section
  if (updatedDependenciesSection != null && groupedDeps.grouped.isNotEmpty) {
    _replaceDependencySection(
      lines,
      updatedDependenciesSection,
      groupedDeps,
      'dependencies',
    );
  }

  return lines.join('\n');
}