register static method
bool
register(
- String name, {
- required String viewDir,
- required ProjectContext ctx,
- bool dryRun = false,
Registers the route for name whose view lives under viewDir
(e.g. lib/presentation/todo). No-op in dryRun.
Implementation
static bool register(
String name, {
required String viewDir,
required ProjectContext ctx,
bool dryRun = false,
}) {
final routeName = Naming.camel(name);
final className = '${Naming.pascal(name)}View';
final snake = Naming.snake(name);
_ensureSetup(dryRun: dryRun);
final appRoutesFile = File('$routesPath/app_routes.dart');
final routePageFile = File('$routesPath/route_page.dart');
if (appRoutesFile.existsSync()) {
final existing = appRoutesFile.readAsStringSync();
if (existing.contains('static const String $routeName ') ||
existing.contains('static const String $routeName=')) {
print(
' Route "$routeName" already registered — skipping route update.');
return false;
}
}
if (dryRun) {
print(' ~ $routesPath/app_routes.dart (dry-run, +route $routeName)');
print(' ~ $routesPath/route_page.dart (dry-run, +route $routeName)');
return true;
}
// app_routes.dart: add the path constant.
final appRoutes = appRoutesFile.readAsStringSync();
appRoutesFile.writeAsStringSync(appRoutes.replaceFirst(
RegExp(r'\}\s*$'),
" static const String $routeName = '/$routeName';\n}\n",
));
// route_page.dart: add the import and the GoRoute entry.
final pkg = ctx.packageName ?? 'app';
// viewDir is like `lib/presentation/todo`; strip the leading `lib/`.
final importPath = viewDir.replaceFirst(RegExp(r'^lib/'), '');
final viewImport =
"import 'package:$pkg/$importPath/views/${snake}_view.dart';";
final route = '''
GoRoute(
name: AppRoutes.$routeName,
path: AppRoutes.$routeName,
builder: (context, state) => const $className(),
),
''';
var routePage = routePageFile.readAsStringSync();
if (!routePage.contains(viewImport)) {
routePage = routePage.replaceFirst(
RegExp(r'\nfinal GoRouter router = GoRouter\('),
'\n$viewImport\n\nfinal GoRouter router = GoRouter(',
);
}
routePage = routePage.replaceFirst('routes: [', 'routes: [\n$route');
routePageFile.writeAsStringSync(routePage);
print(' ~ $routesPath/app_routes.dart (+route $routeName)');
print(' ~ $routesPath/route_page.dart (+route $routeName)');
return true;
}