renameFlavor static method
Implementation
static void renameFlavor({
required String oldName,
required String newName,
required AppLogger log,
}) {
final root = ConfigService.root;
// 1. Rename Main File
final oldMainPath = p.join(root, 'lib/main/main_$oldName.dart');
final newMainPath = p.join(root, 'lib/main/main_$newName.dart');
final oldMainFile = File(oldMainPath);
if (oldMainFile.existsSync()) {
log.info(
'📝 Renaming main file: ${p.basename(oldMainPath)} -> ${p.basename(newMainPath)}',
);
var content = oldMainFile.readAsStringSync();
// Update internal references
content = content.replaceAll('Flavor.$oldName', 'Flavor.$newName');
content = content.replaceAll("'$oldName'", "'$newName'");
content = content.replaceAll('.env.$oldName', '.env.$newName');
content = content.replaceAll(
': $oldName',
': $newName',
); // For "Hello Flavor: c1"
content = content.replaceAll(
'firebase_options_$oldName.dart',
'firebase_options_$newName.dart',
);
File(newMainPath).writeAsStringSync(content);
oldMainFile.deleteSync();
}
// 2. AppConfig update is now handled by SetupRunner via RuntimeConfigService.generateAppConfig
// 3. Update single main if exists
final rootMain = File(p.join(root, 'lib/main.dart'));
if (rootMain.existsSync()) {
var content = rootMain.readAsStringSync();
if (content.contains('Flavor.$oldName') ||
content.contains("'$oldName'") ||
content.contains('.env.$oldName') ||
content.contains('firebase_options_$oldName.dart')) {
log.info('📝 Updating lib/main.dart references...');
content = content.replaceAll('Flavor.$oldName', 'Flavor.$newName');
content = content.replaceAll("'$oldName'", "'$newName'");
content = content.replaceAll('.env.$oldName', '.env.$newName');
content = content.replaceAll(': $oldName', ': $newName');
content = content.replaceAll(
'firebase_options_$oldName.dart',
'firebase_options_$newName.dart',
);
content = content.replaceAll(' as $oldName;', ' as $newName;');
content = content.replaceAll(
'$oldName.DefaultFirebaseOptions',
'$newName.DefaultFirebaseOptions',
);
rootMain.writeAsStringSync(content);
}
}
// 4. Firebase options handling
final config = ConfigService.load();
final strategy = config.firebase?.strategy ?? '';
final isUniqueId = strategy.contains('unique_id');
final oldFirebasePath = p.join(root, 'lib/firebase_options_$oldName.dart');
final newFirebasePath = p.join(root, 'lib/firebase_options_$newName.dart');
final oldFirebaseFile = File(oldFirebasePath);
if (oldFirebaseFile.existsSync()) {
if (isUniqueId) {
log.info(
'🗑️ Deleting old Firebase options (Unique ID strategy): ${p.basename(oldFirebasePath)}',
);
oldFirebaseFile.deleteSync();
// Also ensure the main files are cleaned if they were using these options
_cleanupFirebaseFromEntryPoints(oldName, newName, log);
} else {
log.info(
'📝 Renaming Firebase options: ${p.basename(oldFirebasePath)} -> ${p.basename(newFirebasePath)}',
);
oldFirebaseFile.renameSync(newFirebasePath);
}
}
}