Skip to content

Commit 9905d89

Browse files
committed
```
feat(cursor): 提升Linux和Mac脚本的跨平台兼容性 - 新增sed_inplace函数,封装sed -i命令以兼容GNU/BSD sed的不同参数格式 - 修复JSON配置文件修改逻辑,使用awk替代sed处理多行JSON格式 - 优化Python脚本调用方式,使用sys.argv传递参数避免特殊字符导致的语法错误 - 统一配置修改流程,复用modify_or_add_config函数处理settings.json更新 - 移除直接字符串拼接到Python代码的危险做法,提升安全性 ```
1 parent 3adf78e commit 9905d89

File tree

2 files changed

+208
-122
lines changed

2 files changed

+208
-122
lines changed

scripts/run/cursor_linux_id_modifier.sh

Lines changed: 102 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ log_cmd_output() {
8787
echo "" >> "$LOG_FILE"
8888
}
8989

90+
# sed -i 兼容封装:优先原地编辑;不支持/失败时回退到临时文件替换,提升跨发行版兼容性
91+
sed_inplace() {
92+
local expr="$1"
93+
local file="$2"
94+
95+
# GNU sed / BusyBox sed:通常支持 sed -i
96+
if sed -i "$expr" "$file" 2>/dev/null; then
97+
return 0
98+
fi
99+
100+
# BSD sed:需要提供 -i '' 形式(少数环境可能出现)
101+
if sed -i '' "$expr" "$file" 2>/dev/null; then
102+
return 0
103+
fi
104+
105+
# 最后兜底:临时文件替换(避免不同 sed 的 -i 语义差异)
106+
local temp_file
107+
temp_file=$(mktemp) || return 1
108+
if sed "$expr" "$file" > "$temp_file"; then
109+
cat "$temp_file" > "$file"
110+
rm -f "$temp_file"
111+
return 0
112+
fi
113+
rm -f "$temp_file"
114+
return 1
115+
}
116+
90117
# 获取当前用户
91118
get_current_user() {
92119
if [ "$EUID" -eq 0 ]; then
@@ -609,12 +636,53 @@ modify_or_add_config() {
609636
elif grep -q "}" "$file"; then
610637
# key不存在, 在最后一个 '}' 前添加新的key-value对
611638
# 注意:这种方式比较脆弱,如果 JSON 格式不标准或最后一行不是 '}' 会失败
612-
sed '$ s/}/,\n "'$key'\": "'$value'\"\n}/' "$file" > "$temp_file" || {
613-
log_error "添加配置失败 (注入): $key to $file"
614-
rm -f "$temp_file"
615-
chmod u-w "$file" # 恢复权限
616-
return 1
617-
}
639+
# 🔧 兼容修复:不依赖 GNU sed 的 \n 替换扩展;同时避免在 `}` 独占一行时生成无效 JSON
640+
if tail -n 1 "$file" | grep -Eq '^[[:space:]]*}[[:space:]]*$'; then
641+
# 多行 JSON:在最后一个 `}` 前插入新行,并为上一条属性补上逗号
642+
awk -v key="$key" -v value="$value" '
643+
{ lines[NR] = $0 }
644+
END {
645+
brace = 0
646+
for (i = NR; i >= 1; i--) {
647+
if (lines[i] ~ /^[[:space:]]*}[[:space:]]*$/) { brace = i; break }
648+
}
649+
if (brace == 0) { exit 2 }
650+
651+
prev = 0
652+
for (i = brace - 1; i >= 1; i--) {
653+
if (lines[i] !~ /^[[:space:]]*$/) { prev = i; break }
654+
}
655+
if (prev > 0) {
656+
line = lines[prev]
657+
sub(/[[:space:]]*$/, "", line)
658+
if (line !~ /{$/ && line !~ /,$/) {
659+
lines[prev] = line ","
660+
} else {
661+
lines[prev] = line
662+
}
663+
}
664+
665+
insert_line = " \"" key "\": \"" value "\""
666+
for (i = 1; i <= NR; i++) {
667+
if (i == brace) { print insert_line }
668+
print lines[i]
669+
}
670+
}
671+
' "$file" > "$temp_file" || {
672+
log_error "添加配置失败 (注入): $key to $file"
673+
rm -f "$temp_file"
674+
chmod u-w "$file" # 恢复权限
675+
return 1
676+
}
677+
else
678+
# 单行 JSON:直接在末尾 `}` 前插入键值(避免依赖 sed 的 \\n 扩展)
679+
sed "s/}[[:space:]]*$/,\"$key\": \"$value\"}/" "$file" > "$temp_file" || {
680+
log_error "添加配置失败 (注入): $key to $file"
681+
rm -f "$temp_file"
682+
chmod u-w "$file" # 恢复权限
683+
return 1
684+
}
685+
fi
618686
log_debug "已添加 key '$key' 到文件 '$file' 中"
619687
else
620688
log_error "无法确定如何添加配置: $key to $file (文件结构可能不标准)"
@@ -1116,49 +1184,49 @@ EOF
11161184
# 如果直接把 someValue.machineId 替换成 "\"<真实值>\"",会形成 ""<真实值>"" 导致 JS 语法错误。
11171185
# 因此这里优先替换完整的字符串字面量(包含外层引号),再兜底替换不带引号的占位符。
11181186
if grep -q 'someValue\.machineId' "$file"; then
1119-
sed -i "s/\"someValue\.machineId\"/\"${machine_id}\"/g" "$file"
1120-
sed -i "s/'someValue\.machineId'/\"${machine_id}\"/g" "$file"
1121-
sed -i "s/someValue\.machineId/\"${machine_id}\"/g" "$file"
1187+
sed_inplace "s/\"someValue\.machineId\"/\"${machine_id}\"/g" "$file"
1188+
sed_inplace "s/'someValue\.machineId'/\"${machine_id}\"/g" "$file"
1189+
sed_inplace "s/someValue\.machineId/\"${machine_id}\"/g" "$file"
11221190
log_info " ✓ [方案A] 替换 someValue.machineId"
11231191
replaced=true
11241192
fi
11251193

11261194
if grep -q 'someValue\.macMachineId' "$file"; then
1127-
sed -i "s/\"someValue\.macMachineId\"/\"${mac_machine_id}\"/g" "$file"
1128-
sed -i "s/'someValue\.macMachineId'/\"${mac_machine_id}\"/g" "$file"
1129-
sed -i "s/someValue\.macMachineId/\"${mac_machine_id}\"/g" "$file"
1195+
sed_inplace "s/\"someValue\.macMachineId\"/\"${mac_machine_id}\"/g" "$file"
1196+
sed_inplace "s/'someValue\.macMachineId'/\"${mac_machine_id}\"/g" "$file"
1197+
sed_inplace "s/someValue\.macMachineId/\"${mac_machine_id}\"/g" "$file"
11301198
log_info " ✓ [方案A] 替换 someValue.macMachineId"
11311199
replaced=true
11321200
fi
11331201

11341202
if grep -q 'someValue\.devDeviceId' "$file"; then
1135-
sed -i "s/\"someValue\.devDeviceId\"/\"${device_id}\"/g" "$file"
1136-
sed -i "s/'someValue\.devDeviceId'/\"${device_id}\"/g" "$file"
1137-
sed -i "s/someValue\.devDeviceId/\"${device_id}\"/g" "$file"
1203+
sed_inplace "s/\"someValue\.devDeviceId\"/\"${device_id}\"/g" "$file"
1204+
sed_inplace "s/'someValue\.devDeviceId'/\"${device_id}\"/g" "$file"
1205+
sed_inplace "s/someValue\.devDeviceId/\"${device_id}\"/g" "$file"
11381206
log_info " ✓ [方案A] 替换 someValue.devDeviceId"
11391207
replaced=true
11401208
fi
11411209

11421210
if grep -q 'someValue\.sqmId' "$file"; then
1143-
sed -i "s/\"someValue\.sqmId\"/\"${sqm_id}\"/g" "$file"
1144-
sed -i "s/'someValue\.sqmId'/\"${sqm_id}\"/g" "$file"
1145-
sed -i "s/someValue\.sqmId/\"${sqm_id}\"/g" "$file"
1211+
sed_inplace "s/\"someValue\.sqmId\"/\"${sqm_id}\"/g" "$file"
1212+
sed_inplace "s/'someValue\.sqmId'/\"${sqm_id}\"/g" "$file"
1213+
sed_inplace "s/someValue\.sqmId/\"${sqm_id}\"/g" "$file"
11461214
log_info " ✓ [方案A] 替换 someValue.sqmId"
11471215
replaced=true
11481216
fi
11491217

11501218
if grep -q 'someValue\.sessionId' "$file"; then
1151-
sed -i "s/\"someValue\.sessionId\"/\"${session_id}\"/g" "$file"
1152-
sed -i "s/'someValue\.sessionId'/\"${session_id}\"/g" "$file"
1153-
sed -i "s/someValue\.sessionId/\"${session_id}\"/g" "$file"
1219+
sed_inplace "s/\"someValue\.sessionId\"/\"${session_id}\"/g" "$file"
1220+
sed_inplace "s/'someValue\.sessionId'/\"${session_id}\"/g" "$file"
1221+
sed_inplace "s/someValue\.sessionId/\"${session_id}\"/g" "$file"
11541222
log_info " ✓ [方案A] 替换 someValue.sessionId"
11551223
replaced=true
11561224
fi
11571225

11581226
if grep -q 'someValue\.firstSessionDate' "$file"; then
1159-
sed -i "s/\"someValue\.firstSessionDate\"/\"${first_session_date}\"/g" "$file"
1160-
sed -i "s/'someValue\.firstSessionDate'/\"${first_session_date}\"/g" "$file"
1161-
sed -i "s/someValue\.firstSessionDate/\"${first_session_date}\"/g" "$file"
1227+
sed_inplace "s/\"someValue\.firstSessionDate\"/\"${first_session_date}\"/g" "$file"
1228+
sed_inplace "s/'someValue\.firstSessionDate'/\"${first_session_date}\"/g" "$file"
1229+
sed_inplace "s/someValue\.firstSessionDate/\"${first_session_date}\"/g" "$file"
11621230
log_info " ✓ [方案A] 替换 someValue.firstSessionDate"
11631231
replaced=true
11641232
fi
@@ -1481,25 +1549,19 @@ disable_auto_update() {
14811549
# 备份
14821550
cp "$config" "${config}.bak_$(date +%Y%m%d%H%M%S)" 2>/dev/null
14831551

1484-
# 尝试修改 JSON (如果存在且是 settings.json)
1485-
if [[ "$config" == *settings.json ]]; then
1486-
# 尝试添加或修改 "update.mode": "none"
1487-
if grep -q '"update.mode"' "$config"; then
1488-
sed -i 's/"update.mode":[[:space:]]*"[^"]*"/"update.mode": "none"/' "$config" || log_warn "修改 settings.json 中的 update.mode 失败"
1489-
elif grep -q "}" "$config"; then # 尝试注入
1490-
sed -i '$ s/}/,\n "update.mode": "none"\n}/' "$config" || log_warn "注入 update.mode 到 settings.json 失败"
1552+
# 尝试修改 JSON (如果存在且是 settings.json)
1553+
if [[ "$config" == *settings.json ]]; then
1554+
# 🔧 兼容修复:复用 modify_or_add_config 统一处理替换/注入,避免 sed -i 与 \n 扩展差异
1555+
if modify_or_add_config "update.mode" "none" "$config"; then
1556+
((disabled_count++))
1557+
log_info "已尝试在 '$config' 中设置 'update.mode' 为 'none'"
14911558
else
1492-
log_warn "无法修改 settings.json 以禁用更新(结构未知)"
1559+
log_warn "修改 settings.json 中的 update.mode 失败: $config"
14931560
fi
1494-
# 确保权限正确
1561+
elif [[ "$config" == *update-config.json ]]; then
1562+
# 直接覆盖 update-config.json
1563+
echo '{"autoCheck": false, "autoDownload": false}' > "$config"
14951564
chown "$CURRENT_USER":"$(id -g -n "$CURRENT_USER")" "$config" || log_warn "设置所有权失败: $config"
1496-
chmod 644 "$config" || log_warn "设置权限失败: $config"
1497-
((disabled_count++))
1498-
log_info "已尝试在 '$config' 中设置 'update.mode' 为 'none'"
1499-
elif [[ "$config" == *update-config.json ]]; then
1500-
# 直接覆盖 update-config.json
1501-
echo '{"autoCheck": false, "autoDownload": false}' > "$config"
1502-
chown "$CURRENT_USER":"$(id -g -n "$CURRENT_USER")" "$config" || log_warn "设置所有权失败: $config"
15031565
chmod 644 "$config" || log_warn "设置权限失败: $config"
15041566
((disabled_count++))
15051567
log_info "已覆盖更新配置文件: $config"

0 commit comments

Comments
 (0)