mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-08-02 22:07:36 +08:00
refactor: Enhance Cursor file modification logic with robust error handling
- Replaced awk-based file parsing with more precise sed replacement - Added comprehensive validation checks for file modifications - Improved error logging and handling during file processing - Updated user guidance message to include restart and potential reinstallation instructions - Maintained existing script functionality with more resilient modification approach
This commit is contained in:
parent
3d832fbd7d
commit
63beebef24
@ -230,7 +230,7 @@ modify_cursor_app_files() {
|
|||||||
if [ ! -f "$file" ]; then
|
if [ ! -f "$file" ]; then
|
||||||
log_warn "文件不存在: $file"
|
log_warn "文件不存在: $file"
|
||||||
continue
|
continue
|
||||||
fi
|
}
|
||||||
|
|
||||||
# 创建备份
|
# 创建备份
|
||||||
local backup_file="${file}.bak"
|
local backup_file="${file}.bak"
|
||||||
@ -249,45 +249,53 @@ modify_cursor_app_files() {
|
|||||||
# 创建临时文件
|
# 创建临时文件
|
||||||
local temp_file=$(mktemp)
|
local temp_file=$(mktemp)
|
||||||
|
|
||||||
# 读取文件内容并进行修改
|
# 读取文件内容
|
||||||
if ! awk '
|
local content=$(<"$file")
|
||||||
/IOPlatformUUID/ {
|
|
||||||
in_block = 1
|
# 查找关键位置
|
||||||
print "return crypto.randomUUID();"
|
local uuid_pattern="IOPlatformUUID"
|
||||||
next
|
if ! echo "$content" | grep -q "$uuid_pattern"; then
|
||||||
}
|
log_warn "在文件 $file 中未找到 $uuid_pattern"
|
||||||
in_block && /}/ {
|
rm -f "$temp_file"
|
||||||
in_block = 0
|
continue
|
||||||
next
|
}
|
||||||
}
|
|
||||||
!in_block {
|
# 构建替换内容
|
||||||
print
|
local replacement='case "IOPlatformUUID": return crypto.randomUUID();'
|
||||||
}
|
|
||||||
' "$file" > "$temp_file"; then
|
# 使用 sed 进行替换
|
||||||
|
if ! sed -E "s/(case \"IOPlatformUUID\":)[^}]+}/\1 return crypto.randomUUID();/" "$file" > "$temp_file"; then
|
||||||
log_error "处理文件内容失败: $file"
|
log_error "处理文件内容失败: $file"
|
||||||
rm -f "$temp_file"
|
rm -f "$temp_file"
|
||||||
continue
|
continue
|
||||||
fi
|
}
|
||||||
|
|
||||||
# 验证临时文件不为空
|
# 验证临时文件
|
||||||
if [ ! -s "$temp_file" ]; then
|
if [ ! -s "$temp_file" ]; then
|
||||||
log_error "生成的文件为空: $file"
|
log_error "生成的文件为空: $file"
|
||||||
rm -f "$temp_file"
|
rm -f "$temp_file"
|
||||||
continue
|
continue
|
||||||
fi
|
}
|
||||||
|
|
||||||
|
# 验证文件内容是否包含必要的代码
|
||||||
|
if ! grep -q "crypto.randomUUID()" "$temp_file"; then
|
||||||
|
log_error "修改后的文件缺少必要的代码: $file"
|
||||||
|
rm -f "$temp_file"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
# 替换原文件
|
# 替换原文件
|
||||||
if ! mv "$temp_file" "$file"; then
|
if ! mv "$temp_file" "$file"; then
|
||||||
log_error "无法更新文件: $file"
|
log_error "无法更新文件: $file"
|
||||||
rm -f "$temp_file"
|
rm -f "$temp_file"
|
||||||
continue
|
continue
|
||||||
fi
|
}
|
||||||
|
|
||||||
# 设置权限
|
# 设置权限
|
||||||
chmod 644 "$file"
|
chmod 644 "$file"
|
||||||
chown "$CURRENT_USER" "$file"
|
chown "$CURRENT_USER" "$file"
|
||||||
|
|
||||||
log_info "成功修改文件: $file"
|
log_info "成功修改文件: $file 请重启Cursor,如果重启后无法打开或者报异常,请重新安装Cursor"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user