migrateAppToRouter function
Implementation
Future<void> migrateAppToRouter(String projectPath) async {
final appFile = File(p.join(projectPath, 'lib', 'app', 'app.dart'));
if (!appFile.existsSync()) return;
final projectName = getProjectName(projectPath);
var content = appFile.readAsStringSync();
// 1. Add Import
final importLine = "import 'package:$projectName/routes/app_router.dart';\n";
if (!content.contains('/routes/app_router.dart')) {
content = importLine + content;
}
// 2. Replace MaterialApp with MaterialApp.router
// This regex matches the return MaterialApp(...) block
final materialAppRegex = RegExp(r'return\s+MaterialApp\([\s\S]+?\);');
final routerMaterialApp = '''return MaterialApp.router(
debugShowCheckedModeBanner: false,
routerConfig: AppRouter.router,
);''';
if (content.contains('MaterialApp(') &&
!content.contains('MaterialApp.router(')) {
content = content.replaceFirst(materialAppRegex, routerMaterialApp);
appFile.writeAsStringSync(content);
print('🚀 Updated lib/app/app.dart to use MaterialApp.router');
}
}