updateRoutes method
Implementation
Future<void> updateRoutes(String name) async {
try {
String routePath =
selectModule == 'common' ? './lib/routes/common/index.dart' : './lib/routes/project/$selectModule.dart';
String newRoute = '''
// $pageComment
CustomGetPage(
name: Routes.${_camelCase(name)},
page: () => ${_upperFirst(name)}Page(),
binding: BindingsBuilder(() {
Get.put(${_upperFirst(name)}Controller());
}),
middlewares: [RouteAuthMiddleware()],
),''';
File routeFile = File(routePath);
String content = await routeFile.readAsString();
int insertPosition = content.indexOf('[') + 1;
if (insertPosition <= 0) {
print('未找到插入位置');
return;
}
String newContent = '${content.substring(0, insertPosition)}\n$newRoute${content.substring(insertPosition)}';
await routeFile.writeAsString(newContent);
print('路由配置已成功插入');
// 更新导入
selectModule == 'common' ? await updateCommonImport(name) : await updateProjectImport(name);
await updateRouterConstants(name);
} catch (error) {
print('更新路由失败: $error');
}
}