generate method
Executes the plugin's code-generation and configuration logic.
Implementation
@override
Future<void> generate(ProjectContext context) async {
final pubspecPath = '${context.projectPath}/pubspec.yaml';
// 1. Add dependency
Services.yamlService.addDependency(pubspecPath, 'go_router', '^14.0.1');
// 2. Render route_names.dart template
await Services.templateService.renderTemplate(
templateSubPath: 'routing/route_names.dart',
targetPath: '${context.projectPath}/lib/core/router/route_names.dart',
replacements: {},
);
// 3. Build route templates & imports based on Splash/Onboarding selection
String pageImports = '';
String routesList = '';
if (context.includeSplashOnboarding) {
pageImports = "${context.splashViewImport}${context.onboardingViewImport}${context.homeViewImport}import 'route_names.dart';\n";
routesList = '''GoRoute(
path: RouteNames.splash,
builder: (BuildContext context, GoRouterState state) {
return const SplashView();
},
),
GoRoute(
path: RouteNames.onboarding,
builder: (BuildContext context, GoRouterState state) {
return const OnboardingView();
},
),
GoRoute(
path: RouteNames.home,
builder: (BuildContext context, GoRouterState state) {
return const HomeView();
},
),''';
} else {
pageImports = "${context.homeViewImport}import 'route_names.dart';\n";
routesList = '''GoRoute(
path: RouteNames.splash,
builder: (BuildContext context, GoRouterState state) {
return const HomeView();
},
),''';
}
// 4. Render router template
await Services.templateService.renderTemplate(
templateSubPath: 'routing/router.dart',
targetPath: '${context.projectPath}/lib/core/router/router.dart',
replacements: {
'PROJECT_NAME': context.projectName,
'ROUTER_PAGE_IMPORTS': pageImports,
'ROUTER_ROUTES_LIST': routesList,
},
);
// 5. Register route imports in main.dart
final currentImports = context.templateReplacements['ROUTER_IMPORTS'] ?? '';
context.templateReplacements['ROUTER_IMPORTS'] = "${currentImports}import 'core/router/router.dart';\n";
// Configure navigation actions for Splash & Onboarding to use RouteNames
context.templateReplacements['ROUTER_IMPORT'] = "import 'package:go_router/go_router.dart';\nimport '${context.coreRouterRelativePath}route_names.dart';\n";
context.templateReplacements['NAVIGATE_HOME'] = "context.go(RouteNames.home);";
context.templateReplacements['NAVIGATE_ONBOARDING'] = "context.go(RouteNames.onboarding);";
context.templateReplacements['HOME_VIEW_IMPORT'] = '';
context.templateReplacements['SPLASH_HOME_IMPORT'] = '';
context.templateReplacements['SPLASH_ONBOARDING_IMPORT'] = '';
// 4. Register MaterialApp.router / CupertinoApp.router replacements
final isCupertino = context.theme == UITheme.cupertino;
final appClass = isCupertino ? 'CupertinoApp.router' : 'MaterialApp.router';
if (isCupertino) {
context.templateReplacements['APP_ROUTER_OR_MATERIAL_APP'] = '''
$appClass(
routerConfig: router,
title: '${context.projectName}',
theme: AppTheme.theme,
localizationsDelegates: const [
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
)''';
} else {
final themeConfig = context.darkMode
? '''theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
themeMode: ThemeMode.system,'''
: '''theme: AppTheme.lightTheme,
themeMode: ThemeMode.light,''';
context.templateReplacements['APP_ROUTER_OR_MATERIAL_APP'] = '''
$appClass(
routerConfig: router,
title: '${context.projectName}',
$themeConfig
)''';
}
}