updateRouterConstantsPagesPath method
Implementation
Future<void> updateRouterConstantsPagesPath(String name) async {
try {
String constantsPath = './lib/routes/constants.dart';
File constantsFile = File(constantsPath);
String content = await constantsFile.readAsString();
int startIndex = content.indexOf('static const List<String> pagesPath = [');
int endIndex = content.indexOf('];', startIndex);
if (startIndex == -1 || endIndex == -1) {
print('未找到 pagesPath 定义');
return;
}
List<String> lines = content.substring(startIndex, endIndex + 2).split('\n');
String moduleUpperCase = selectModule.toUpperCase();
String moduleCapitalized = '${selectModule[0].toUpperCase()}${selectModule.substring(1)}';
String newContent = ' ${_camelCase(name)},';
int insertIndex = -1;
bool foundUpperCase = false;
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
if (line == '// $moduleUpperCase') {
insertIndex = i + 1;
foundUpperCase = true;
break;
}
}
if (insertIndex == -1) {
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
if (line == '// $moduleCapitalized') {
insertIndex = i + 1;
break;
}
}
}
if (insertIndex != -1) {
lines.insert(insertIndex, newContent);
String updatedPagesPathContent = lines.join('\n');
String updatedContent =
content.substring(0, startIndex) + updatedPagesPathContent + content.substring(endIndex + 2);
await constantsFile.writeAsString(updatedContent);
print('新内容已插入到 ${foundUpperCase ? moduleUpperCase : moduleCapitalized} 模块下');
} else {
print('未找到 $moduleUpperCase 或 $moduleCapitalized 模块');
}
} catch (error) {
print('更新路由页面路径失败: $error');
}
}