mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-06-08 12:32:06 +08:00
refactor: Improve Cursor app file modification logic in Mac ID modifier script
- Replaced complex file parsing with awk-based content modification - Simplified UUID replacement strategy using crypto.randomUUID() - Enhanced error handling and file processing robustness - Optimized temporary file management during script execution
This commit is contained in:
parent
2a93a856bf
commit
d8f7964d36
@ -8,7 +8,7 @@ RED='\033[0;31m'
|
|||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
BLUE='\033[0;34m'
|
BLUE='\033[0;34m'
|
||||||
NC='\033[0m'
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
# 日志函数
|
# 日志函数
|
||||||
log_info() {
|
log_info() {
|
||||||
@ -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"
|
||||||
@ -246,36 +246,44 @@ modify_cursor_app_files() {
|
|||||||
log_debug "备份已存在: $backup_file"
|
log_debug "备份已存在: $backup_file"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 读取文件内容
|
# 创建临时文件
|
||||||
local content
|
local temp_file=$(mktemp)
|
||||||
content=$(cat "$file") || {
|
|
||||||
log_error "无法读取文件: $file"
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
# 查找 IOPlatformUUID 位置
|
# 读取文件内容并进行修改
|
||||||
local uuid_pos
|
if ! awk '
|
||||||
uuid_pos=$(printf "%s" "$content" | grep -b -o "IOPlatformUUID" | cut -d: -f1)
|
/IOPlatformUUID/ {
|
||||||
if [ -z "$uuid_pos" ]; then
|
in_block = 1
|
||||||
log_warn "未找到 IOPlatformUUID: $file"
|
print "return crypto.randomUUID();"
|
||||||
continue
|
next
|
||||||
}
|
}
|
||||||
|
in_block && /}/ {
|
||||||
# 从 UUID 位置向前查找 switch
|
in_block = 0
|
||||||
local before_uuid="${content:0:$uuid_pos}"
|
next
|
||||||
local switch_pos
|
|
||||||
switch_pos=$(printf "%s" "$before_uuid" | grep -b -o "switch" | tail -n1 | cut -d: -f1)
|
|
||||||
if [ -z "$switch_pos" ]; then
|
|
||||||
log_warn "未找到 switch 关键字: $file"
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
!in_block {
|
||||||
# 构建新的文件内容
|
print
|
||||||
printf "%sreturn crypto.randomUUID();\n%s" "${content:0:$switch_pos}" "${content:$switch_pos}" > "$file" || {
|
|
||||||
log_error "无法写入文件: $file"
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
' "$file" > "$temp_file"; then
|
||||||
|
log_error "处理文件内容失败: $file"
|
||||||
|
rm -f "$temp_file"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 验证临时文件不为空
|
||||||
|
if [ ! -s "$temp_file" ]; then
|
||||||
|
log_error "生成的文件为空: $file"
|
||||||
|
rm -f "$temp_file"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 替换原文件
|
||||||
|
if ! mv "$temp_file" "$file"; then
|
||||||
|
log_error "无法更新文件: $file"
|
||||||
|
rm -f "$temp_file"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 设置权限
|
||||||
chmod 644 "$file"
|
chmod 644 "$file"
|
||||||
chown "$CURRENT_USER" "$file"
|
chown "$CURRENT_USER" "$file"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user