loadAllRoutesWithProjectName function
扫描 packages 并生成 Route 字符串(带 projectName 和注释)
Implementation
Future<List<String>> loadAllRoutesWithProjectName(String packagesPath) async {
final packagesDir = Directory(packagesPath);
if (!await packagesDir.exists()) throw Exception('目录不存在: $packagesPath');
final List<String> allRoutes = [];
await for (final project in packagesDir.list()) {
if (project is! Directory) continue;
// 读取 config.dart 获取 projectName
final configFile = File('${project.path}/lib/config/config.dart');
if (!await configFile.exists()) continue;
final configContent = await configFile.readAsString();
final configMatch = RegExp(
r'''static\s+String\s+projectName\s*=\s*["']([^"']+)["'];''',
).firstMatch(configContent);
if (configMatch == null) continue;
final projectName = configMatch.group(1)!;
final projectCamel = Utils.toCamelCase(projectName);
// 读取 routes.dart
final routesFile = File('${project.path}/lib/router/routes.dart');
if (!await routesFile.exists()) continue;
final routesContent = await routesFile.readAsLines();
String? currentComment;
final routeRegex = RegExp(
r'''static\s+Route\s+(\w+)\s*=\s*const\s+Route\s*\(\s*name\s*:\s*["']([^"']+)["']\s*,\s*path\s*:\s*["']([^"']+)["']\s*\)''',
);
for (final line in routesContent) {
final trimmed = line.trim();
// 捕获注释
if (trimmed.startsWith('/// ')) {
currentComment = trimmed.replaceAll('/// ', '/// $projectName ');
continue;
}
final match = routeRegex.firstMatch(trimmed);
if (match != null) {
final varName = match.group(1)!;
final name = match.group(2)!;
final path = match.group(3)!;
final newVarName =
'${projectCamel}${varName[0].toUpperCase()}${varName.substring(1)}';
// 添加缩进 2 个空格
if (currentComment != null) {
allRoutes.add(' $currentComment');
currentComment = null;
}
allRoutes.add(
' static Route $newVarName = const Route('
"name: '$projectName.$name',"
"path: '/$projectName$path'"
');\n',
);
}
}
}
return allRoutes;
}