dsh-profile-config-sync 0.5.0 → 0.6.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -1
- package/README.en.md +174 -738
- package/README.md +246 -571
- package/client.js +609 -292
- package/cordis.patch.yml +1 -1
- package/docs/CHANGELOG.en.md +28 -0
- package/docs/CHANGELOG.md +81 -0
- package/docs/HISTORY.en.md +221 -0
- package/docs/HISTORY.md +252 -0
- package/docs/MULTI-PLATFORM.md +165 -0
- package/docs/REFERENCE.md +172 -0
- package/index.js +1049 -189
- package/package.json +11 -5
- package/runtime/apply.ps1 +79 -33
- package/runtime/atomic-write.mjs +57 -0
- package/runtime/capture.ps1 +188 -37
- package/runtime/contract.mjs +59 -7
- package/runtime/dsh-sync-common.ps1 +361 -22
- package/runtime/install-plugin.ps1 +94 -3
- package/runtime/live-log.mjs +75 -0
- package/runtime/platform.mjs +35 -0
- package/runtime/post-exit.ps1 +192 -28
- package/runtime/restore.ps1 +76 -8
- package/runtime/sync-engine.mjs +1324 -857
- package/runtime/sync.ps1 +192 -82
- package/runtime/tools/fix-session-cwd.mjs +12 -3
- package/runtime/tools/workspace-view.mjs +7 -3
- package/runtime/webdav.mjs +269 -23
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 在线推送日志的清洗(从 index.js 抽出来单独放,便于自检直接跑样例)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:Windows 上在线推送用 `powershell.exe -EncodedCommand` 跑 sync.ps1。
|
|
5
|
+
* PowerShell 会把 `Write-Host` / `Write-Output` / 进度记录序列化成 CLIXML 打到 **stderr**,
|
|
6
|
+
* stdout 里才是干净正文。stderr 是**按块**到达的,一个块常常从 XML 中间被切开
|
|
7
|
+
* (实测片段形如 `2</U32><U32 N="ManagedThreadId">21</U32></Props></Obj>…`)。
|
|
8
|
+
*
|
|
9
|
+
* 0.5.0 的判定要求「整块以 #<CLIXML 或 <Objs 开头」,切开的块判定失败就被原样写进日志:
|
|
10
|
+
* 一次推送写出 4.9 MB XML(3700 条「上传进度」各带一份完整记录),用户看到的最近日志全是乱码。
|
|
11
|
+
*
|
|
12
|
+
* 现在的规则:
|
|
13
|
+
* 1. 只按标签抓消息文本(Error > Message > ToString),逐块抓、不依赖块边界;
|
|
14
|
+
* 2. 抓到就只返回这些文本(去重、限条数);
|
|
15
|
+
* 3. 一块都抓不到且看着像 XML → 整块丢弃(真正的 PowerShell 错误一定带 <S S="Error"> 记录);
|
|
16
|
+
* 4. 不像 XML → 原样返回(git / pnpm 等原生程序的标准错误本来就是纯文本)。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** PowerShell CLIXML 的转义:实体(含数字实体)+ `_x000D_` / `_x000A_`。 */
|
|
20
|
+
export function decodeXmlText(input) {
|
|
21
|
+
return String(input == null ? '' : input)
|
|
22
|
+
.replace(/_x000[dD]_/g, '\n')
|
|
23
|
+
.replace(/_x000[aA]_/g, '\n')
|
|
24
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, h) => {
|
|
25
|
+
try { return String.fromCodePoint(parseInt(h, 16)) } catch { return '' }
|
|
26
|
+
})
|
|
27
|
+
.replace(/&#(\d+);/g, (_, d) => {
|
|
28
|
+
try { return String.fromCodePoint(Number(d)) } catch { return '' }
|
|
29
|
+
})
|
|
30
|
+
.replace(/</g, '<')
|
|
31
|
+
.replace(/>/g, '>')
|
|
32
|
+
.replace(/"/g, '"')
|
|
33
|
+
.replace(/'/g, "'")
|
|
34
|
+
.replace(/&/g, '&')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 这段文本看起来像 CLIXML / 被切开的 CLIXML 片段吗? */
|
|
38
|
+
export function looksClixml(text) {
|
|
39
|
+
return /#<\s*CLIXML|<Objs\b|<Obj\b|<\/Obj>|RefId=|<TNRef|<ToString>|<Props>|<LST>/i.test(String(text || ''))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 清洗一块子进程输出。
|
|
44
|
+
* @param {string|Buffer} raw 原始输出块
|
|
45
|
+
* @param {boolean} fromStderr 是否来自 stderr
|
|
46
|
+
* @param {{maxLines?: number}} [opts]
|
|
47
|
+
*/
|
|
48
|
+
export function extractLiveLogText(raw, fromStderr, opts = {}) {
|
|
49
|
+
const text = String(raw == null ? '' : raw)
|
|
50
|
+
if (!fromStderr) return text
|
|
51
|
+
if (!looksClixml(text)) return text
|
|
52
|
+
const maxLines = Number(opts.maxLines) > 0 ? Number(opts.maxLines) : 40
|
|
53
|
+
const picked = []
|
|
54
|
+
const grab = (re) => {
|
|
55
|
+
const r = new RegExp(re.source, re.flags)
|
|
56
|
+
let m
|
|
57
|
+
while ((m = r.exec(text)) !== null) picked.push(m[1])
|
|
58
|
+
}
|
|
59
|
+
grab(/<S\s+S="Error">([\s\S]*?)<\/S>/gi)
|
|
60
|
+
grab(/<S\s+N="Message">([\s\S]*?)<\/S>/gi)
|
|
61
|
+
if (!picked.length) grab(/<ToString>([\s\S]*?)<\/ToString>/gi)
|
|
62
|
+
const seen = new Set()
|
|
63
|
+
const out = []
|
|
64
|
+
for (const p of picked) {
|
|
65
|
+
const line = decodeXmlText(p).trim()
|
|
66
|
+
if (!line || seen.has(line)) continue
|
|
67
|
+
seen.add(line)
|
|
68
|
+
out.push(line)
|
|
69
|
+
}
|
|
70
|
+
if (!out.length) return '' // 纯 XML 噪声(进度/information 记录)整块丢弃
|
|
71
|
+
// 留**最后** maxLines 条:消息是按时间顺序来的,被截断的是开头的老进度;
|
|
72
|
+
// 真正的报错总在最后,留前面的会把结论丢掉(面板「最近日志」同样看尾部)。
|
|
73
|
+
if (out.length > maxLines) out.splice(0, out.length - maxLines)
|
|
74
|
+
return out.join('\n')
|
|
75
|
+
}
|
package/runtime/platform.mjs
CHANGED
|
@@ -362,6 +362,41 @@ export function homeDisplay() {
|
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
/** 是否支持符号链接(POSIX 恒 true;Windows 上创建 junction 不需要管理员)。 */
|
|
365
|
+
/**
|
|
366
|
+
* 自选同步目录的路径归一化(按平台分档校验)。
|
|
367
|
+
*
|
|
368
|
+
* 为什么单独抽出来:这段逻辑曾经写死 Windows 语义,POSIX 上 /Users/x/docs 会被
|
|
369
|
+
* 「把 / 换成 \」后判成非法再静默丢弃 —— 面板显示保存成功,实际一条都没存。
|
|
370
|
+
* 现在被拒的路径会收集进 dropped,由调用方整批报错,绝不静默丢。
|
|
371
|
+
*/
|
|
372
|
+
export function normalizeSyncExtraPaths(list, platform = process.platform) {
|
|
373
|
+
const win = platform === 'win32'
|
|
374
|
+
const paths = []
|
|
375
|
+
const dropped = []
|
|
376
|
+
const used = new Set()
|
|
377
|
+
const baseName = (s) => String(s).replace(/[\\/]+$/, '').split(/[\\/]/).pop() || 'extra'
|
|
378
|
+
for (const item of Array.isArray(list) ? list : []) {
|
|
379
|
+
if (!item || typeof item.src !== 'string' || !item.src.trim()) continue
|
|
380
|
+
const raw = item.src.trim()
|
|
381
|
+
// Windows 上允许用户写正斜杠(C:/a/b),统一成反斜杠;POSIX 原样保留
|
|
382
|
+
const abs = win ? raw.replace(/\//g, '\\') : raw
|
|
383
|
+
const okPath = win
|
|
384
|
+
? (/^[A-Za-z]:[\\/]/.test(abs) || abs.startsWith('\\\\'))
|
|
385
|
+
: abs.startsWith('/')
|
|
386
|
+
const okAbs = win ? (abs.startsWith('\\\\') || /^[A-Za-z]:\\/.test(abs)) : abs.startsWith('/')
|
|
387
|
+
if (!okPath || !okAbs) { dropped.push(raw); continue }
|
|
388
|
+
const label = (typeof item.label === 'string' && item.label.trim())
|
|
389
|
+
? item.label.trim()
|
|
390
|
+
: (typeof item.id === 'string' && item.id.trim() ? item.id.trim() : baseName(abs))
|
|
391
|
+
let id = String(label).replace(/[\\/:*?"<>|\s]+/g, '-').replace(/^-+|-+$/g, '') || 'extra'
|
|
392
|
+
let n = 2
|
|
393
|
+
const root = id
|
|
394
|
+
while (used.has(id.toLowerCase())) id = `${root}-${n++}`
|
|
395
|
+
used.add(id.toLowerCase())
|
|
396
|
+
paths.push({ id, src: abs, label })
|
|
397
|
+
}
|
|
398
|
+
return { paths, dropped }
|
|
399
|
+
}
|
|
365
400
|
export function supportsSymlink() {
|
|
366
401
|
return !IS_WIN
|
|
367
402
|
}
|
package/runtime/post-exit.ps1
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<#
|
|
1
|
+
<#
|
|
2
2
|
退出后执行同步:等 DSH 完全退出,再跑 pull 或 push(插件在 DSH 进程里,自己等不了自己退出)
|
|
3
3
|
|
|
4
4
|
由插件在 DSH 运行期用 detached 方式拉起:
|
|
@@ -30,6 +30,9 @@ param(
|
|
|
30
30
|
[string]$LogFile,
|
|
31
31
|
# 仅 Phase=wait 用:由外部启动器(如 ShellExecute 脱离 Job Object)回读本进程真实 PID
|
|
32
32
|
[string]$PidFile = '',
|
|
33
|
+
# 强杀残留客户端只在「插件自己请求过关闭」时允许(见 index.js closeRequested);
|
|
34
|
+
# 被动等待不该偷偷杀掉用户收进托盘的客户端。
|
|
35
|
+
[switch]$CloseWhenIdle,
|
|
33
36
|
# ---- 仅 -Mode restore 用:还原到哪份快照 ----
|
|
34
37
|
[string]$BackupDir = '',
|
|
35
38
|
# 备份类型:profile(配置快照,默认)/ plugin(插件本体备份)/ session(会话目录备份)
|
|
@@ -41,7 +44,9 @@ param(
|
|
|
41
44
|
[string]$UpdateTgz = '',
|
|
42
45
|
[string]$UpdateSha256 = '',
|
|
43
46
|
# 仓库是私有库时必须带的只读令牌(Gitee 私人令牌);留空就按匿名方式下载
|
|
44
|
-
[string]$UpdateToken = ''
|
|
47
|
+
[string]$UpdateToken = '',
|
|
48
|
+
# 结果展示窗口的停留秒数(默认 360 = 6 分钟)。自动化验收传小值,避免每套夹具干等 6 分钟。
|
|
49
|
+
[int]$HoldSeconds = 360
|
|
45
50
|
)
|
|
46
51
|
|
|
47
52
|
# ---------- Phase=wait:只等退出,然后换成可见窗口执行 ----------
|
|
@@ -57,6 +62,7 @@ if ($Phase -eq 'wait') {
|
|
|
57
62
|
}
|
|
58
63
|
$deadline = (Get-Date).AddMinutes($MaxWaitMinutes)
|
|
59
64
|
$noWindowTicks = 0
|
|
65
|
+
$toldNoWindow = $false
|
|
60
66
|
$waitLog = Join-Path $DshHome 'logs\auto-sync-wait.log'
|
|
61
67
|
function WaitLog([string]$text) {
|
|
62
68
|
try {
|
|
@@ -81,13 +87,19 @@ if ($Phase -eq 'wait') {
|
|
|
81
87
|
$newest = $byName | Sort-Object StartTime -Descending | Select-Object -First 1
|
|
82
88
|
$ageSec = if ($newest -and $newest.StartTime) { ((Get-Date) - $newest.StartTime).TotalSeconds } else { 0 }
|
|
83
89
|
if ($visible.Count -eq 0 -and $ageSec -gt 60) { $noWindowTicks++ } else { $noWindowTicks = 0 }
|
|
84
|
-
if ($noWindowTicks -ge 8) {
|
|
90
|
+
if ($noWindowTicks -ge 8 -and $CloseWhenIdle) {
|
|
85
91
|
WaitLog '前台窗口已关闭且进程不退,强制结束残留进程'
|
|
86
92
|
try { Stop-Process -Name 'DeepSeek Harness' -Force -ErrorAction SilentlyContinue } catch { }
|
|
87
93
|
Start-Sleep -Seconds 3
|
|
88
94
|
if (@(Get-Process -Name 'DeepSeek Harness' -ErrorAction SilentlyContinue).Count -eq 0) { WaitLog '已全部结束'; break }
|
|
89
95
|
$noWindowTicks = 0
|
|
90
96
|
}
|
|
97
|
+
# 没拿到强杀授权时,把「窗口没了但进程还在」说清楚,让用户知道为什么没同步、
|
|
98
|
+
# 而不是看到一条「等待超时」却不知道是谁在等。
|
|
99
|
+
if ($noWindowTicks -ge 8 -and -not $CloseWhenIdle -and -not $toldNoWindow) {
|
|
100
|
+
$toldNoWindow = $true
|
|
101
|
+
WaitLog '客户端没有任何可见窗口但进程仍在运行;未收到强杀授权,继续等待(收进托盘不会自动同步)'
|
|
102
|
+
}
|
|
91
103
|
if ((Get-Date) -gt $deadline) { WaitLog '等待超时,放弃本次同步'; exit 2 }
|
|
92
104
|
Start-Sleep -Seconds 2
|
|
93
105
|
}
|
|
@@ -122,7 +134,7 @@ $ErrorActionPreference = 'Continue'
|
|
|
122
134
|
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
|
|
123
135
|
|
|
124
136
|
# 窗口是可见的:给它一个能看懂的标题,并把控制台编码设成 UTF-8(否则中文进度全是乱码)
|
|
125
|
-
try { $Host.UI.RawUI.WindowTitle = ('DSH
|
|
137
|
+
try { $Host.UI.RawUI.WindowTitle = ('DSH 配置备份与同步 · ' + $(if ($Mode -eq 'update') { '插件更新' } elseif ($Mode -eq 'relaunch') { '重启' } elseif ($Mode -eq 'restore') { '还原' } elseif ($Mode -eq 'pull') { '拉取' } else { '推送' })) } catch { }
|
|
126
138
|
try { [Console]::OutputEncoding = New-Object Text.UTF8Encoding $false; $OutputEncoding = [Console]::OutputEncoding } catch { }
|
|
127
139
|
|
|
128
140
|
if (-not $LogFile) {
|
|
@@ -137,13 +149,112 @@ function Log([string]$text) {
|
|
|
137
149
|
try { Write-Host $line } catch { }
|
|
138
150
|
}
|
|
139
151
|
|
|
152
|
+
# ---------- 环境自愈:模块被别的 PowerShell 版本抢走时,这一步决定后面还能不能跑 ----------
|
|
153
|
+
# 实测:DSH 从 pwsh(PowerShell 7)里启动时,子进程 powershell.exe 5.1 继承了 PS7 的 PSModulePath,
|
|
154
|
+
# 自动加载先撞上 PS7 的 Microsoft.PowerShell.Utility(7.0.0.0,5.1 装不进去),于是
|
|
155
|
+
# Get-FileHash / Invoke-WebRequest 直接「无法识别」——更新流程把空 hash 当校验失败,永远装不上。
|
|
156
|
+
try {
|
|
157
|
+
if ($PSVersionTable -and $PSVersionTable.PSEdition -ne 'Core') {
|
|
158
|
+
$selfModules = Join-Path $PSHOME 'Modules'
|
|
159
|
+
if ($selfModules -and (Test-Path -LiteralPath $selfModules)) {
|
|
160
|
+
$cur = [string]$env:PSModulePath
|
|
161
|
+
$rest = @($cur -split ';' | Where-Object { $_ -and ($_.TrimEnd('\') -ne $selfModules.TrimEnd('\')) })
|
|
162
|
+
$env:PSModulePath = (@($selfModules) + $rest) -join ';'
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} catch { }
|
|
166
|
+
|
|
167
|
+
# Get-FileHash 的 .NET 等价实现:模块加载失败时也别让一个 cmdlet 决定整条链路生死
|
|
168
|
+
if (-not (Get-Command Get-FileHash -ErrorAction SilentlyContinue)) {
|
|
169
|
+
function Get-FileHash {
|
|
170
|
+
param(
|
|
171
|
+
[Parameter(Mandatory = $true)][string]$LiteralPath,
|
|
172
|
+
[ValidateSet('SHA256', 'SHA1', 'MD5')][string]$Algorithm = 'SHA256'
|
|
173
|
+
)
|
|
174
|
+
$algo = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
|
|
175
|
+
$fs = [System.IO.File]::Open($LiteralPath, 'Open', 'Read', 'ReadWrite')
|
|
176
|
+
try { $bytes = $algo.ComputeHash($fs) } finally { $fs.Dispose(); $algo.Dispose() }
|
|
177
|
+
[pscustomobject]@{
|
|
178
|
+
Algorithm = $Algorithm
|
|
179
|
+
Hash = (($bytes | ForEach-Object { $_.ToString('X2') }) -join '')
|
|
180
|
+
Path = $LiteralPath
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
# 下载兜底:Invoke-WebRequest 不可用(或被策略挡掉)时直接用 .NET,带上同样的头与超时
|
|
186
|
+
function Save-UrlToFile {
|
|
187
|
+
param(
|
|
188
|
+
[Parameter(Mandatory = $true)][string]$Uri,
|
|
189
|
+
[Parameter(Mandatory = $true)][string]$OutFile,
|
|
190
|
+
[hashtable]$Headers = @{},
|
|
191
|
+
[int]$TimeoutSec = 180
|
|
192
|
+
)
|
|
193
|
+
# 这个兜底实现(HttpWebRequest)在 5.1 里会把 Authorization 头吃掉(实测:头存进对象但发不出去),
|
|
194
|
+
# 所以带令牌的下载宁可直接报错并给出办法,也不让用户对着一个没头没脑的 401 猜。
|
|
195
|
+
foreach ($hk in @($Headers.Keys)) {
|
|
196
|
+
if ([string]$hk -ieq 'Authorization') {
|
|
197
|
+
throw '当前 PowerShell 环境不支持带令牌下载(模块被别的 PowerShell 版本占用了):请在面板里用「更新」,或从开始菜单/桌面重新启动 DSH 后重试'
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
$req = [System.Net.HttpWebRequest]::Create($Uri)
|
|
201
|
+
$req.Timeout = $TimeoutSec * 1000
|
|
202
|
+
$req.ReadWriteTimeout = $TimeoutSec * 1000
|
|
203
|
+
$req.UserAgent = 'dsh-profile-config-sync'
|
|
204
|
+
foreach ($k in @($Headers.Keys)) {
|
|
205
|
+
$v = [string]$Headers[$k]
|
|
206
|
+
if ($k -eq 'User-Agent') { $req.UserAgent = $v } else { $req.Headers[$k] = $v }
|
|
207
|
+
}
|
|
208
|
+
$resp = $req.GetResponse()
|
|
209
|
+
try {
|
|
210
|
+
$in = $resp.GetResponseStream()
|
|
211
|
+
$out = [System.IO.File]::Create($OutFile)
|
|
212
|
+
try { $in.CopyTo($out) } finally { $out.Dispose() }
|
|
213
|
+
} finally { $resp.Close() }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
# 统一下载入口:优先 cmdlet,缺失时自动走 Save-UrlToFile
|
|
217
|
+
function Invoke-DownloadFile {
|
|
218
|
+
param(
|
|
219
|
+
[Parameter(Mandatory = $true)][string]$Uri,
|
|
220
|
+
[Parameter(Mandatory = $true)][string]$OutFile,
|
|
221
|
+
[hashtable]$Headers = @{},
|
|
222
|
+
[int]$TimeoutSec = 180
|
|
223
|
+
)
|
|
224
|
+
Remove-Item -LiteralPath $OutFile -Force -ErrorAction SilentlyContinue
|
|
225
|
+
# 注意:Get-Command 只表示「命令能被发现」,不代表能执行 —— PS7 的模块目录排在 PSModulePath 前面时,
|
|
226
|
+
# Invoke-WebRequest 会被解析到装不进 5.1 的版本,调用当场抛装配错误(实测)。所以必须 try/catch 兜底,
|
|
227
|
+
# 而不是只看命令在不在。
|
|
228
|
+
if (Get-Command Invoke-WebRequest -ErrorAction SilentlyContinue) {
|
|
229
|
+
try {
|
|
230
|
+
Invoke-WebRequest -Uri $Uri -Headers $Headers -OutFile $OutFile -UseBasicParsing -TimeoutSec $TimeoutSec
|
|
231
|
+
return
|
|
232
|
+
} catch {
|
|
233
|
+
Remove-Item -LiteralPath $OutFile -Force -ErrorAction SilentlyContinue
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
Save-UrlToFile -Uri $Uri -OutFile $OutFile -Headers $Headers -TimeoutSec $TimeoutSec
|
|
237
|
+
}
|
|
238
|
+
# 本脚本刻意不 dot-source common(它要在客户端退出后独立运行)。
|
|
239
|
+
# 但 workspace.json / state / lock 这些文件别的进程会同时读,裸写会留半截内容,
|
|
240
|
+
# 所以这里带一份最小的原子写:临时文件 + Move-Item -Force(同卷 rename 原子),失败短重试。
|
|
241
|
+
function Write-TextAtomic([string]$path, [string]$text) {
|
|
242
|
+
$dir = Split-Path -Parent $path
|
|
243
|
+
if ($dir -and -not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
|
244
|
+
$tmp = $path + ".tmp-" + $PID
|
|
245
|
+
[IO.File]::WriteAllText($tmp, $text, (New-Object Text.UTF8Encoding($false)))
|
|
246
|
+
for ($i = 0; $i -lt 20; $i++) {
|
|
247
|
+
try { Move-Item -LiteralPath $tmp -Destination $path -Force -ErrorAction Stop; return $true }
|
|
248
|
+
catch { Start-Sleep -Milliseconds (10 * ($i + 1)) }
|
|
249
|
+
}
|
|
250
|
+
try { [IO.File]::WriteAllText($path, $text, (New-Object Text.UTF8Encoding($false))); return $false }
|
|
251
|
+
finally { if (Test-Path -LiteralPath $tmp) { Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue } }
|
|
252
|
+
}
|
|
140
253
|
$ResultFile = Join-Path $DshHome 'auto-sync-result.json'
|
|
141
254
|
# 接管互斥锁:等待阶段记的是那个等候进程的 pid,这里换成真正干活的进程;
|
|
142
255
|
# 别的同步动作看到这把锁(且 pid 活着)就会排队等待/放弃,不会两个一起改配置。
|
|
143
256
|
try {
|
|
144
|
-
|
|
145
|
-
([ordered]@{ pid = $PID; mode = $Mode; at = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() } | ConvertTo-Json),
|
|
146
|
-
(New-Object Text.UTF8Encoding $false))
|
|
257
|
+
$null = Write-TextAtomic (Join-Path $DshHome 'auto-sync.lock') ([ordered]@{ pid = $PID; mode = $Mode; at = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() } | ConvertTo-Json)
|
|
147
258
|
} catch { }
|
|
148
259
|
# 结果文件给客户端下次启动读:ok/mode/at/code/message/restored/log (+ notices)
|
|
149
260
|
function Write-Result([bool]$ok, [int]$code, [string]$message, [bool]$restored) {
|
|
@@ -173,7 +284,7 @@ function Write-Result([bool]$ok, [int]$code, [string]$message, [bool]$restored)
|
|
|
173
284
|
$dir = Split-Path -Parent $ResultFile
|
|
174
285
|
if ($dir -and -not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
|
175
286
|
$json = $obj | ConvertTo-Json -Depth 6
|
|
176
|
-
|
|
287
|
+
$null = Write-TextAtomic $ResultFile $json
|
|
177
288
|
Log ("结果已写入:{0}" -f $ResultFile)
|
|
178
289
|
} catch { Log ("写结果文件失败:{0}" -f $_.Exception.Message) }
|
|
179
290
|
}
|
|
@@ -262,11 +373,11 @@ function Finish([int]$code, [string]$note) {
|
|
|
262
373
|
if ($code -ne 0) { Show-FailureSummary }
|
|
263
374
|
Write-Host ''
|
|
264
375
|
Write-Host ("================ {0} ================" -f $title)
|
|
265
|
-
Write-Host
|
|
376
|
+
Write-Host (" 窗口将在 {0}后自动关闭;也可直接点右上角 × 关闭(客户端在后台运行,不受影响)。" -f $(if ($HoldSeconds -ge 60) { ('{0} 分钟' -f [Math]::Round($HoldSeconds / 60, 1)) } else { ('{0} 秒' -f $HoldSeconds) }))
|
|
266
377
|
if ($code -ne 0) { Write-Host ' 失败原因已集中在上面「失败原因集中展示」一段,不必再翻日志。' -ForegroundColor Red }
|
|
267
378
|
Write-Host '==========================================='
|
|
268
379
|
} catch { }
|
|
269
|
-
try { Start-Sleep -Seconds
|
|
380
|
+
try { if ($HoldSeconds -gt 0) { Start-Sleep -Seconds $HoldSeconds } } catch { }
|
|
270
381
|
} else {
|
|
271
382
|
try { Start-Sleep -Seconds 3 } catch { }
|
|
272
383
|
}
|
|
@@ -403,7 +514,7 @@ function Set-DefaultWorkspace {
|
|
|
403
514
|
Copy-Item -LiteralPath $reg -Destination ($reg + '.bak-' + $stamp) -Force
|
|
404
515
|
$obj.global.defaultWorkspaceId = $target
|
|
405
516
|
$text = (($obj | ConvertTo-Json -Depth 12) -replace "`r`n", "`n") + "`n"
|
|
406
|
-
|
|
517
|
+
$null = Write-TextAtomic $reg $text
|
|
407
518
|
|
|
408
519
|
# 回读校验:值改对了、工作区条数没变,才算成功;否则静默保持原样会让「新会话落在哪」继续不对。
|
|
409
520
|
$back = Get-Content -LiteralPath $reg -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
@@ -463,7 +574,18 @@ function RestartClient([switch]$SkipWatermark) {
|
|
|
463
574
|
Log '启动后进程又退了(旧实例的锁可能还没释放),等 8 秒重试'
|
|
464
575
|
Start-Sleep -Seconds 8
|
|
465
576
|
}
|
|
466
|
-
if (-not $ok) {
|
|
577
|
+
if (-not $ok) {
|
|
578
|
+
# 调用方在上面已经把结果文件写成「成功 + 已重启」。同步/安装本身确实做完了,但客户端没起来,
|
|
579
|
+
# 不改写就是假绿:用户看到「已拉取、已重启」,面前却一个进程都没有。这里按模式覆写成失败。
|
|
580
|
+
$restartFailText = switch ($Mode) {
|
|
581
|
+
'update' { '插件已更新,但客户端没能自动重启(连续 3 次没起来);详见日志' }
|
|
582
|
+
'restore' { '配置已还原,但客户端没能自动重启(连续 3 次没起来);详见日志' }
|
|
583
|
+
'relaunch' { '客户端没能自动重启(连续 3 次没起来);详见日志' }
|
|
584
|
+
default { '同步已完成,但客户端没能自动重启(连续 3 次没起来);详见日志' }
|
|
585
|
+
}
|
|
586
|
+
try { Write-Result $false 4 $restartFailText $SkipWatermark } catch { }
|
|
587
|
+
Finish 4 '重启失败(试了 3 次都没起来)'
|
|
588
|
+
}
|
|
467
589
|
|
|
468
590
|
# 重启成功后再刷一次"上次同步时间"。apply/capture 刚写过的文件 mtime 落在这之前,
|
|
469
591
|
# 客户端起来 8 秒的首检会把它们当成"本机有未推送的新内容",立刻排一个空推送
|
|
@@ -484,7 +606,7 @@ function RestartClient([switch]$SkipWatermark) {
|
|
|
484
606
|
head = $old.head
|
|
485
607
|
}
|
|
486
608
|
}
|
|
487
|
-
|
|
609
|
+
$null = Write-TextAtomic $stateFile ($state | ConvertTo-Json -Depth 4)
|
|
488
610
|
Log '已刷新同步分水岭(重启后的首检不会误报待推送)'
|
|
489
611
|
} catch { Log ("刷新同步分水岭失败:{0}" -f $_.Exception.Message) }
|
|
490
612
|
} else {
|
|
@@ -501,9 +623,16 @@ function RestartClient([switch]$SkipWatermark) {
|
|
|
501
623
|
# 不需要装东西的纯重启。做的事只有一件:等退出 → 用 explorer 代启拉回来。
|
|
502
624
|
if ($Mode -eq 'relaunch') {
|
|
503
625
|
Log '只重启客户端(新版本插件在重启后生效)'
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
626
|
+
if ($Restart) {
|
|
627
|
+
# 先重启再写结果:重启失败时 RestartClient 会把结果覆写成失败,不会被这里的「已重启」盖掉。
|
|
628
|
+
RestartClient
|
|
629
|
+
Write-Result $true 0 '已重启客户端' $false
|
|
630
|
+
Finish 0 '完成(已重启)'
|
|
631
|
+
} else {
|
|
632
|
+
# 旧写法不管有没有真重启都写「已重启客户端」——面板于是显示成功,实际什么都没发生。
|
|
633
|
+
Write-Result $true 0 '本次只做了退出,-Restart 未指定,客户端没有自动重启' $false
|
|
634
|
+
Finish 0 '完成(未重启:本次没有要求重启)'
|
|
635
|
+
}
|
|
507
636
|
}
|
|
508
637
|
|
|
509
638
|
# ---------- 仅更新模式:下载插件包 → 校验 sha256 → 备份旧版 → 覆盖安装 → 重启 ----------
|
|
@@ -511,8 +640,16 @@ if ($Mode -eq 'relaunch') {
|
|
|
511
640
|
# 所以不能走 sync.ps1,也不能先 apply(那会把旧插件部署回去)。
|
|
512
641
|
# 覆盖发生在 DSH 已经退出之后 —— 这样运行中的面板不会被卸载,用户也就不会被弹回默认页。
|
|
513
642
|
if ($Mode -eq 'update') {
|
|
514
|
-
if (-not $PluginDir -or -not (Test-Path -LiteralPath $PluginDir)) {
|
|
515
|
-
|
|
643
|
+
if (-not $PluginDir -or -not (Test-Path -LiteralPath $PluginDir)) {
|
|
644
|
+
$msg = '插件更新失败:找不到插件目录(' + $PluginDir + ')'
|
|
645
|
+
Write-Result $false 6 $msg $false
|
|
646
|
+
Finish 6 $msg
|
|
647
|
+
}
|
|
648
|
+
if (-not $UpdateTgz) {
|
|
649
|
+
$msg = '插件更新失败:更新清单里没有安装包(tgz)地址'
|
|
650
|
+
Write-Result $false 6 $msg $false
|
|
651
|
+
Finish 6 $msg
|
|
652
|
+
}
|
|
516
653
|
|
|
517
654
|
$currentVersion = ''
|
|
518
655
|
try { $currentVersion = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version } catch { }
|
|
@@ -529,21 +666,27 @@ if ($Mode -eq 'update') {
|
|
|
529
666
|
Log ("使用已下载的安装包:{0}({1:N0} 字节)" -f $UpdateTgz, (Get-Item -LiteralPath $UpdateTgz).Length)
|
|
530
667
|
try { Copy-Item -LiteralPath $UpdateTgz -Destination $tmp -Force } catch { $tmp = $UpdateTgz }
|
|
531
668
|
} else {
|
|
532
|
-
#
|
|
669
|
+
# 私有仓库:Gitee 走 API raw + access_token;GitHub 直连域走 Bearer 头(绝不传给第三方反代)
|
|
533
670
|
$fetchUrl = $UpdateTgz
|
|
671
|
+
$fetchHeaders = @{}
|
|
534
672
|
if ($UpdateToken) {
|
|
535
|
-
# raw 链接 → API raw 端点;分支不能留在路径里,要改成 ref 参数
|
|
536
673
|
$m = [regex]::Match($UpdateTgz, '^https://gitee\.com/([^/]+)/([^/]+)/raw/([^/]+)/(.+)$')
|
|
537
674
|
if ($m.Success) {
|
|
538
675
|
$fetchUrl = ("https://gitee.com/api/v5/repos/{0}/{1}/raw/{2}?ref={3}" -f $m.Groups[1].Value, $m.Groups[2].Value, $m.Groups[4].Value, $m.Groups[3].Value)
|
|
676
|
+
$sep = if ($fetchUrl.Contains('?')) { '&' } else { '?' }
|
|
677
|
+
$fetchUrl = "$fetchUrl${sep}access_token=$([uri]::EscapeDataString($UpdateToken))"
|
|
678
|
+
} elseif ($UpdateTgz -match '^https://(?:raw\.githubusercontent\.com|github\.com|api\.github\.com)/') {
|
|
679
|
+
$fetchHeaders['Authorization'] = "Bearer $UpdateToken"
|
|
680
|
+
$fetchHeaders['User-Agent'] = 'dsh-profile-config-sync'
|
|
681
|
+
} elseif ($UpdateTgz -match '^https://(?:[^/]+\.)?gitee\.com/') {
|
|
682
|
+
$sep = if ($fetchUrl.Contains('?')) { '&' } else { '?' }
|
|
683
|
+
$fetchUrl = "$fetchUrl${sep}access_token=$([uri]::EscapeDataString($UpdateToken))"
|
|
539
684
|
}
|
|
540
|
-
$sep = if ($fetchUrl.Contains('?')) { '&' } else { '?' }
|
|
541
|
-
$fetchUrl = "$fetchUrl${sep}access_token=$([uri]::EscapeDataString($UpdateToken))"
|
|
542
685
|
}
|
|
543
686
|
Log ("下载安装包:{0}{1}" -f $UpdateTgz, $(if ($UpdateToken) { '(私有仓库,带令牌)' } else { '' }))
|
|
544
687
|
$downloadError = $null
|
|
545
688
|
try {
|
|
546
|
-
Invoke-
|
|
689
|
+
Invoke-DownloadFile -Uri $fetchUrl -OutFile $tmp -Headers $fetchHeaders -TimeoutSec 180
|
|
547
690
|
} catch {
|
|
548
691
|
$downloadError = $_.Exception.Message
|
|
549
692
|
}
|
|
@@ -557,8 +700,17 @@ if ($Mode -eq 'update') {
|
|
|
557
700
|
}
|
|
558
701
|
|
|
559
702
|
if ($UpdateSha256) {
|
|
560
|
-
$hash =
|
|
561
|
-
|
|
703
|
+
$hash = ''
|
|
704
|
+
try { $hash = [string]((Get-FileHash -LiteralPath $tmp -Algorithm SHA256).Hash) } catch { $hash = '' }
|
|
705
|
+
$hash = $hash.Trim().ToLower()
|
|
706
|
+
Log ("sha256:{0}" -f $(if ($hash) { $hash } else { '(算不出来)' }))
|
|
707
|
+
if (-not $hash) {
|
|
708
|
+
# 之前这里会把「本机算不出 hash」写成「包可能被改动」,让人去怀疑网络/文件。
|
|
709
|
+
$msg = '插件更新失败:本机 PowerShell 无法计算安装包 sha256(Get-FileHash 不可用且 .NET 回退也失败)'
|
|
710
|
+
Log $msg
|
|
711
|
+
Write-Result $false 7 $msg $false
|
|
712
|
+
Finish 7 '更新失败:无法校验安装包'
|
|
713
|
+
}
|
|
562
714
|
if ($hash -ne $UpdateSha256.ToLower()) {
|
|
563
715
|
Write-Result $false 7 '插件更新失败:安装包 sha256 校验不通过(可能没下载全或被改动)' $false
|
|
564
716
|
Finish 7 '更新失败:sha256 不符'
|
|
@@ -762,7 +914,13 @@ if ($Mode -eq 'pull') {
|
|
|
762
914
|
$LASTEXITCODE = -1
|
|
763
915
|
$invokeOk = $false
|
|
764
916
|
try {
|
|
765
|
-
|
|
917
|
+
# 管道会把子脚本的退出码吃掉(实测 exit 3 到这里变成 1),所以同时在输出里认这句话。
|
|
918
|
+
$syncSkipped = $false
|
|
919
|
+
& $sync @syncArgs *>&1 | ForEach-Object {
|
|
920
|
+
$lineText = [string]$_
|
|
921
|
+
if ($lineText -match '另一个同步动作正在进行') { $syncSkipped = $true }
|
|
922
|
+
Log $lineText
|
|
923
|
+
}
|
|
766
924
|
$invokeOk = $true
|
|
767
925
|
} catch {
|
|
768
926
|
Log ('执行体调用异常:{0}' -f $_.Exception.Message)
|
|
@@ -778,7 +936,10 @@ if (-not $invokeOk) {
|
|
|
778
936
|
}
|
|
779
937
|
Log ("sync.ps1 {0} 退出码:{1}" -f $Mode, $code)
|
|
780
938
|
|
|
781
|
-
|
|
939
|
+
# 3 = 另一个同步动作正在进行(capture/apply 的互斥):什么都没做,但绝不能算失败 ——
|
|
940
|
+
# 否则 pull 会走下面的自动回滚,把一个健康的仓库 reset 回拉取前,纯属自伤。
|
|
941
|
+
$skipped = ($code -eq 3) -or $syncSkipped
|
|
942
|
+
$failed = ($code -ne 0 -and -not $skipped)
|
|
782
943
|
|
|
783
944
|
# ---------- 失败自动回滚(仅 pull) ----------
|
|
784
945
|
$conflictPending = ($code -eq 6)
|
|
@@ -826,13 +987,16 @@ if ($Mode -eq 'pull') {
|
|
|
826
987
|
if ($conflictPending) {
|
|
827
988
|
# 冲突不是"失败":本机文件挡住了快进,仓库没动、配置没动,等用户在面板里逐项选完再拉
|
|
828
989
|
Write-Result $false 6 '拉取被本机文件挡住:面板「同步冲突」里逐项选「用本机 / 用云端」后再拉取' $false
|
|
990
|
+
} elseif ($skipped) {
|
|
991
|
+
Write-Result $true 0 '另一个同步动作正在进行,本次拉取已跳过(不是故障)' $false
|
|
829
992
|
} elseif ($failed) {
|
|
830
993
|
Write-Result $false $code ("拉取失败" + $(if ($restored) { ',已自动还原上一次的配置' } else { ',且未能完整还原' }) + ';详见日志') $restored
|
|
831
994
|
} else {
|
|
832
995
|
Write-Result $true 0 '云端配置已拉取到本机' $false
|
|
833
996
|
}
|
|
834
997
|
} else {
|
|
835
|
-
if ($
|
|
998
|
+
if ($skipped) { Write-Result $true 0 '另一个同步动作正在进行,本次推送已跳过(不是故障)' $false }
|
|
999
|
+
elseif ($failed) { Write-Result $false $code '推送失败(改动还在本地,下次再推)' $false }
|
|
836
1000
|
else { Write-Result $true 0 '本机改动已推送到云端' $false }
|
|
837
1001
|
}
|
|
838
1002
|
|
package/runtime/restore.ps1
CHANGED
|
@@ -52,7 +52,13 @@ function Resolve-AppRoot([string]$explicit) {
|
|
|
52
52
|
if ($explicit) { $cands += $explicit }
|
|
53
53
|
$cands += @('D:\DeepSeekHarness', 'D:\DSH', 'G:\DSH', 'G:\DeepSeekHarness', 'C:\DeepSeekHarness')
|
|
54
54
|
foreach ($c in $cands) {
|
|
55
|
-
if (
|
|
55
|
+
if (-not $c) { continue }
|
|
56
|
+
# 不要用 Join-Path:探测列表里带着不存在的盘(这台机器没有 G:),
|
|
57
|
+
# PS 5.1 的 Join-Path 会当场抛 DriveNotFoundException,而脚本开头把
|
|
58
|
+
# $ErrorActionPreference 设成了 Stop —— 于是「还原」在第一步就崩,什么都没做。
|
|
59
|
+
# 字符串拼接不碰盘符校验,Test-Path 也再兜一层 SilentlyContinue。
|
|
60
|
+
$probe = ($c.TrimEnd('\') + '\resources\app\package.json')
|
|
61
|
+
if (Test-Path -LiteralPath $probe -ErrorAction SilentlyContinue) { return $c }
|
|
56
62
|
}
|
|
57
63
|
if ($explicit) { return $explicit }
|
|
58
64
|
return 'D:\DeepSeekHarness'
|
|
@@ -218,8 +224,20 @@ if ($FromCommit) {
|
|
|
218
224
|
$gitExe = Get-GitExe
|
|
219
225
|
if (-not $gitExe) { Die "找不到 git,无法按提交还原。请安装 Git 或改用 -BackupDir。" }
|
|
220
226
|
# 注意:不能用 "{0}^{commit}" -f ,`{commit}` 会被当成格式占位符 → "Error formatting a string"
|
|
221
|
-
|
|
222
|
-
|
|
227
|
+
# 也不能裸跑:$ErrorActionPreference='Stop' 下 native 的 stderr 会被包成终止错误,
|
|
228
|
+
# 提交号无效时 git 写 "fatal: Needed a single revision",异常先抛出,下面那句提示永远不会显示。
|
|
229
|
+
$verifyCode = -1
|
|
230
|
+
$eapVerify = $ErrorActionPreference
|
|
231
|
+
try {
|
|
232
|
+
$ErrorActionPreference = 'Continue'
|
|
233
|
+
& $gitExe -C $RepoRoot rev-parse --verify ($FromCommit + '^{commit}') 2>&1 | Out-Null
|
|
234
|
+
$verifyCode = $LASTEXITCODE
|
|
235
|
+
} catch {
|
|
236
|
+
$verifyCode = 1
|
|
237
|
+
} finally {
|
|
238
|
+
$ErrorActionPreference = $eapVerify
|
|
239
|
+
}
|
|
240
|
+
if ($verifyCode -ne 0) { Die "git 里找不到提交:$FromCommit" }
|
|
223
241
|
$mode = 'git'
|
|
224
242
|
Say "来源:git 提交 $FromCommit 的 profile\"
|
|
225
243
|
} else {
|
|
@@ -261,6 +279,7 @@ if (-not $DryRun) {
|
|
|
261
279
|
$src = Join-Path $ProfileDir $f
|
|
262
280
|
if (Test-Path -LiteralPath $src) { Copy-Item -LiteralPath $src -Destination (Join-Path $safety $f) -Force; $n++ }
|
|
263
281
|
}
|
|
282
|
+
# HomeFiles(AGENTS.md 等):两套备份布局都落在快照根,直接按同名取
|
|
264
283
|
foreach ($f in $HomeFiles) {
|
|
265
284
|
$src = Join-Path $DshHome $f
|
|
266
285
|
if (Test-Path -LiteralPath $src) { Copy-Item -LiteralPath $src -Destination (Join-Path $safety $f) -Force; $n++ }
|
|
@@ -270,21 +289,41 @@ if (-not $DryRun) {
|
|
|
270
289
|
}
|
|
271
290
|
|
|
272
291
|
# ---------- 3. 还原 profile 配置 ----------
|
|
292
|
+
# 仓库里的 profile 文件可能有占位符(capture 会把本机绝对路径换成 {{ROOT:*}} 之类):
|
|
293
|
+
# 写回本机前必须展开,否则另一台机器的 profile 里会留下裸 {{DSH_HOME}},宿主解析直接 fatal。
|
|
294
|
+
$profileRoots = Get-SyncRoots $DshHome $AppRoot $WorkspaceHome
|
|
273
295
|
if ($mode -eq 'git') {
|
|
274
296
|
foreach ($f in $ProfileFiles) {
|
|
275
297
|
if ($DryRun) { Say " 将写入 profile\$f(取自 $FromCommit)"; continue }
|
|
276
298
|
$raw = (& $gitExe -C $RepoRoot show ($FromCommit + ':profile/' + $f)) -join "`n"
|
|
277
299
|
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($raw)) { Warn "提交 $FromCommit 里没有 profile\$f,跳过"; continue }
|
|
278
|
-
|
|
300
|
+
$expandedRaw = Expand-TokenText $raw $profileRoots
|
|
301
|
+
Write-TextPreserveBom (Join-Path $ProfileDir $f) ($expandedRaw + "`n")
|
|
279
302
|
Ok "已写回 profile\$f"
|
|
280
303
|
}
|
|
281
304
|
} else {
|
|
282
305
|
$restored = 0
|
|
306
|
+
# 快照布局有两种,都要认:
|
|
307
|
+
# config-<stamp>/package.json … —— PowersShell apply 的备份(平铺)
|
|
308
|
+
# apply-<stamp>/profiles/<name>/package.json —— 引擎(macOS/Linux)apply 的备份(按 dshHome 相对路径)
|
|
309
|
+
# 只认平铺会让「把另一台机器上的快照还原到这台」直接失败。
|
|
310
|
+
$profileSourceDir = $BackupDir
|
|
311
|
+
foreach ($cand in @((Join-Path $BackupDir 'profile'), (Join-Path $BackupDir ('profiles' + [IO.Path]::DirectorySeparatorChar + $ProfileName)))) {
|
|
312
|
+
if (Test-Path -LiteralPath (Join-Path $cand 'package.json')) { $profileSourceDir = $cand; break }
|
|
313
|
+
}
|
|
314
|
+
if ($profileSourceDir -ne $BackupDir) { Say " 快照布局:引擎备份($profileSourceDir)" }
|
|
283
315
|
foreach ($f in $ProfileFiles) {
|
|
284
|
-
$src = Join-Path $
|
|
316
|
+
$src = Join-Path $profileSourceDir $f
|
|
285
317
|
if (-not (Test-Path -LiteralPath $src)) { Warn "快照里没有 $f,跳过(该文件保持现状)"; continue }
|
|
286
318
|
if ($DryRun) { Say " 将写回 profile\$f"; continue }
|
|
287
|
-
|
|
319
|
+
# 快照里可能是占位符形态(另一台机器 capture 的仓库-derived 快照),写回前必须展开:
|
|
320
|
+
# 直接 Copy-Item 等于把 {{DSH_HOME}} 这类裸串铺进本机 profile,宿主解析 YAML 会 fatal。
|
|
321
|
+
$dstF = Join-Path $ProfileDir $f
|
|
322
|
+
Copy-Item -LiteralPath $src -Destination $dstF -Force
|
|
323
|
+
$rawSnap = Read-TextPreserveBom $dstF
|
|
324
|
+
if ($rawSnap -match '\{\{(APP_ROOT|DSH_HOME|WORKSPACE_HOME|ROOT:[A-Za-z0-9_.\-]+)\}\}') {
|
|
325
|
+
Write-TextPreserveBom $dstF (Expand-TokenText $rawSnap $profileRoots)
|
|
326
|
+
}
|
|
288
327
|
$restored++
|
|
289
328
|
}
|
|
290
329
|
if (-not $DryRun -and $restored -eq 0) { Die "快照里一个 profile 文件都没有,$BackupDir 可能不是 apply 生成的备份点。" }
|
|
@@ -292,9 +331,20 @@ if ($mode -eq 'git') {
|
|
|
292
331
|
|
|
293
332
|
foreach ($f in $HomeFiles) {
|
|
294
333
|
$src = Join-Path $BackupDir $f
|
|
334
|
+
# 仓库布局把提示词放在 prompt\ 下(apply 备份是平铺在快照根)。
|
|
335
|
+
# 只认平铺会让「把同步仓库当快照还原」静默地什么都没做 —— 用户以为还原成功,提示词却没回来。
|
|
336
|
+
if (-not (Test-Path -LiteralPath $src)) {
|
|
337
|
+
$alt = Join-Path (Join-Path $BackupDir 'prompt') $f
|
|
338
|
+
if (Test-Path -LiteralPath $alt) { $src = $alt }
|
|
339
|
+
}
|
|
295
340
|
if (-not (Test-Path -LiteralPath $src)) { continue }
|
|
296
341
|
if ($DryRun) { Say " 将写回 $f"; continue }
|
|
297
|
-
|
|
342
|
+
$dstHome = Join-Path $DshHome $f
|
|
343
|
+
Copy-Item -LiteralPath $src -Destination $dstHome -Force
|
|
344
|
+
$rawHome = Read-TextPreserveBom $dstHome
|
|
345
|
+
if ($rawHome -match '\{\{(APP_ROOT|DSH_HOME|WORKSPACE_HOME|ROOT:[A-Za-z0-9_.\-]+)\}\}') {
|
|
346
|
+
Write-TextPreserveBom $dstHome (Expand-TokenText $rawHome $profileRoots)
|
|
347
|
+
}
|
|
298
348
|
Ok "已写回 $f"
|
|
299
349
|
}
|
|
300
350
|
|
|
@@ -315,11 +365,29 @@ if ($mode -eq 'git') {
|
|
|
315
365
|
$cnt = 0
|
|
316
366
|
Get-ChildItem -LiteralPath $bkSettings -File -Force | Where-Object { $_.Name -ne '__backup_manifest.json' } | ForEach-Object {
|
|
317
367
|
$prop = if ($bkManifest) { $bkManifest.PSObject.Properties[$_.Name] } else { $null }
|
|
318
|
-
$rel = if ($prop) { [string]$prop.Value } else { $_.Name }
|
|
368
|
+
$rel = if ($prop) { [string]$prop.Value } else { [string]$_.Name }
|
|
369
|
+
# 没有备份清单时不能一律丢到 ~/.dsh 根:settings\dsh-workspaces.json 的真实位置是
|
|
370
|
+
# ~/.dsh\storages\workspace.json,丢到根目录等于 DSH 读不到、工作区全部"消失"。
|
|
371
|
+
# 用 capture/apply 同一份映射表反查真实位置。
|
|
372
|
+
if (-not $prop) {
|
|
373
|
+
$leafName = [string]$_.Name
|
|
374
|
+
foreach ($mm in (Get-SyncSettingsMap $DshHome $ProfileName)) {
|
|
375
|
+
if ((Split-Path -Leaf $mm.Repo) -eq $leafName) {
|
|
376
|
+
$rel = ([string]$mm.Src).Substring(([string]$DshHome).TrimEnd('\').Length).TrimStart('\')
|
|
377
|
+
break
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
319
381
|
$dest = Join-Path $DshHome $rel
|
|
320
382
|
$destDir = Split-Path -Parent $dest
|
|
321
383
|
if ($destDir -and -not (Test-Path -LiteralPath $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
|
|
322
384
|
Copy-Item -LiteralPath $_.FullName -Destination $dest -Force
|
|
385
|
+
# 同步仓库里的设置文件带占位符(实测 dsh-workspaces.json 里有 {{ROOT:userHome}}、{{DSH_HOME}}):
|
|
386
|
+
# 逐字节拷进 ~/.dsh 会让 DSH 读到字面量占位符当路径,工作区直接失效。
|
|
387
|
+
$rawSet = Read-TextPreserveBom $dest
|
|
388
|
+
if ($rawSet -match '\{\{(APP_ROOT|DSH_HOME|WORKSPACE_HOME|ROOT:[A-Za-z0-9_.\-]+)\}\}') {
|
|
389
|
+
Write-TextPreserveBom $dest (Expand-TokenText $rawSet $profileRoots)
|
|
390
|
+
}
|
|
323
391
|
$cnt++
|
|
324
392
|
}
|
|
325
393
|
Ok "已写回 settings 下 $cnt 个插件设置文件到各自原位"
|