editPubspecFile function
修改pubspec.yaml文件
Implementation
Future<bool> editPubspecFile({required String projectName}) async {
const filePath = 'pubspec.yaml';
// 读取 pubspec.yaml 文件
final file = File(filePath);
if (!file.existsSync()) {
_print("$filePath not found");
return false;
}
bool isChange = false;
final content = file.readAsStringSync();
final yamlEditor = YamlEditor(content);
// 解析 YAML 内容
final yamlDoc = loadYaml(content);
// 更新版本号
if (yamlDoc['name'] != null) {
// 记录旧项目名称
oldProjectName = yamlDoc['name'];
yamlEditor.update(['name'], projectName);
if (!isChange) isChange = true;
}
// 写回更新后的内容
if (isChange) {
try {
file.writeAsStringSync(yamlEditor.toString());
return true;
} catch (e) {
_print("Failed to write to file: $e");
}
}
return false;
}