dsh-config-sync-plugin 0.0.0-stage → 0.3.56
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 +21 -0
- package/README.en.md +656 -0
- package/README.md +626 -2
- package/client.js +3208 -0
- package/cordis.patch.yml +16 -0
- package/index.js +4281 -0
- package/package.json +58 -4
- package/runtime/apply.ps1 +1406 -0
- package/runtime/capture.ps1 +1188 -0
- package/runtime/dsh-sync-common.ps1 +1002 -0
- package/runtime/install-plugin.ps1 +179 -0
- package/runtime/post-exit.ps1 +809 -0
- package/runtime/restore.ps1 +537 -0
- package/runtime/sync.ps1 +1029 -0
- package/runtime/tools/fix-session-cwd.mjs +200 -0
- package/tools/clientcheck.mjs +259 -0
- package/tools/pack-plugin.ps1 +79 -0
- package/tools/selftest.mjs +1461 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
<#
|
|
2
|
+
运行中把插件新版本装进插件目录(**不退出** DSH)。
|
|
3
|
+
|
|
4
|
+
由宿主的 install-live 动作调用:
|
|
5
|
+
powershell -NoProfile -ExecutionPolicy Bypass -File install-plugin.ps1 `
|
|
6
|
+
-PluginDir <AppRoot>\dsh-config-sync-<ver> -Version 0.3.4 -Sha256 <hex> `
|
|
7
|
+
-TgzUrls "https://cdn.jsdelivr.net/.../0.3.4.tgz|https://raw.githubusercontent.com/.../0.3.4.tgz" `
|
|
8
|
+
-DshHome C:\Users\xx\.dsh
|
|
9
|
+
|
|
10
|
+
要点
|
|
11
|
+
- 下载地址是**候选列表**(| 分隔),按顺序试,哪个先成功就用哪个:国内直连 raw.githubusercontent.com
|
|
12
|
+
经常不通,CDN 兜在前面才不至于"检查能过、下载过不去"。
|
|
13
|
+
- 校验 sha256 → 解包 → 比对包里的 version 与清单一致,三步都过才动插件目录。
|
|
14
|
+
- 动手前整目录备份到 <DshHome>\backups\plugin-<旧版本>-<时间戳>;覆盖失败就把它拷回去。
|
|
15
|
+
- 不重启、不关 DSH:覆盖的是磁盘文件,进程里跑的还是旧代码,所以要提示"重启生效"。
|
|
16
|
+
- 结果固定打一行 RESULT:{json} 给宿主解析(ok / version / backup / log / error)。
|
|
17
|
+
#>
|
|
18
|
+
param(
|
|
19
|
+
[Parameter(Mandatory = $true)][string]$PluginDir,
|
|
20
|
+
[string]$Version = '',
|
|
21
|
+
[string]$Sha256 = '',
|
|
22
|
+
# 候选下载地址,用 | 分隔
|
|
23
|
+
[string]$TgzUrls = '',
|
|
24
|
+
# 已经下好的本地包(有就不用下载)
|
|
25
|
+
[string]$TgzFile = '',
|
|
26
|
+
[string]$DshHome = $(Join-Path $env:USERPROFILE '.dsh'),
|
|
27
|
+
[string]$LogFile = ''
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
$ErrorActionPreference = 'Continue'
|
|
31
|
+
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
|
|
32
|
+
|
|
33
|
+
if (-not $LogFile) {
|
|
34
|
+
$logDir = Join-Path $DshHome 'logs'
|
|
35
|
+
if (-not (Test-Path -LiteralPath $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
|
|
36
|
+
$LogFile = Join-Path $logDir ("plugin-install-{0}.log" -f $stamp)
|
|
37
|
+
}
|
|
38
|
+
function Log([string]$text) {
|
|
39
|
+
$line = "[{0}] {1}" -f (Get-Date -Format 'HH:mm:ss'), $text
|
|
40
|
+
try { Add-Content -LiteralPath $LogFile -Value $line -Encoding utf8 } catch { }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function EmitResult([bool]$ok, [string]$errorText, [string]$backupPath) {
|
|
44
|
+
$obj = [ordered]@{
|
|
45
|
+
ok = $ok
|
|
46
|
+
version = $Version
|
|
47
|
+
backup = $backupPath
|
|
48
|
+
log = $LogFile
|
|
49
|
+
error = $errorText
|
|
50
|
+
}
|
|
51
|
+
# 宿主只认这一行,别把它混进进度输出里
|
|
52
|
+
Write-Output ("RESULT:" + ($obj | ConvertTo-Json -Compress -Depth 3))
|
|
53
|
+
exit 0
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
# 失败时用备份把插件目录恢复成动手前的样子
|
|
57
|
+
function Restore-Backup([string]$backup, [string]$target) {
|
|
58
|
+
if (-not $backup -or -not (Test-Path -LiteralPath $backup)) { return $false }
|
|
59
|
+
try {
|
|
60
|
+
foreach ($it in @(Get-ChildItem -LiteralPath $target -Force)) {
|
|
61
|
+
Remove-Item -LiteralPath $it.FullName -Recurse -Force -ErrorAction SilentlyContinue
|
|
62
|
+
}
|
|
63
|
+
foreach ($it in @(Get-ChildItem -LiteralPath $backup -Force)) {
|
|
64
|
+
Copy-Item -LiteralPath $it.FullName -Destination $target -Recurse -Force
|
|
65
|
+
}
|
|
66
|
+
return $true
|
|
67
|
+
} catch {
|
|
68
|
+
Log ("回滚失败:{0}" -f $_.Exception.Message)
|
|
69
|
+
return $false
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
Log ("==== 安装插件 {0} 到 {1} ====" -f $Version, $PluginDir)
|
|
74
|
+
|
|
75
|
+
if (-not (Test-Path -LiteralPath $PluginDir)) { EmitResult $false ("找不到插件目录:" + $PluginDir) '' }
|
|
76
|
+
|
|
77
|
+
$currentVersion = ''
|
|
78
|
+
try { $currentVersion = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version } catch { }
|
|
79
|
+
Log ("当前版本:{0}" -f $currentVersion)
|
|
80
|
+
|
|
81
|
+
$tmp = Join-Path $env:TEMP ("dsh-plugin-live-{0}-{1}.tgz" -f $Version, $stamp)
|
|
82
|
+
$pkgPath = ''
|
|
83
|
+
if ($TgzFile -and (Test-Path -LiteralPath $TgzFile)) {
|
|
84
|
+
$pkgPath = $TgzFile
|
|
85
|
+
Log ("使用本地包:{0}" -f $pkgPath)
|
|
86
|
+
} else {
|
|
87
|
+
$urls = @($TgzUrls -split '\|' | Where-Object { $_ -and $_.Trim() })
|
|
88
|
+
if ($urls.Count -eq 0) { EmitResult $false '没有给下载地址,也没有本地包' '' }
|
|
89
|
+
$errors = @()
|
|
90
|
+
foreach ($u in $urls) {
|
|
91
|
+
Log ("尝试下载:{0}" -f $u)
|
|
92
|
+
try {
|
|
93
|
+
Invoke-WebRequest -Uri $u.Trim() -OutFile $tmp -UseBasicParsing -TimeoutSec 90
|
|
94
|
+
if ((Test-Path -LiteralPath $tmp) -and ((Get-Item -LiteralPath $tmp).Length -gt 0)) {
|
|
95
|
+
$pkgPath = $tmp
|
|
96
|
+
Log ("下载成功:{0:N0} 字节" -f (Get-Item -LiteralPath $tmp).Length)
|
|
97
|
+
break
|
|
98
|
+
}
|
|
99
|
+
$errors += ("{0}:文件没有落盘" -f $u)
|
|
100
|
+
} catch {
|
|
101
|
+
$errors += ("{0}:{1}" -f $u, $_.Exception.Message)
|
|
102
|
+
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (-not $pkgPath) {
|
|
106
|
+
$msg = '下载安装包失败(每个地址都试过了):' + ($errors -join ';')
|
|
107
|
+
Log $msg
|
|
108
|
+
EmitResult $false $msg ''
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if ($Sha256) {
|
|
113
|
+
$hash = (Get-FileHash -LiteralPath $pkgPath -Algorithm SHA256).Hash.ToLower()
|
|
114
|
+
Log ("sha256:{0}" -f $hash)
|
|
115
|
+
if ($hash -ne $Sha256.ToLower()) {
|
|
116
|
+
Log 'sha256 与清单不一致,放弃安装'
|
|
117
|
+
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
|
118
|
+
EmitResult $false '安装包 sha256 校验不通过(没下载全或被改动)' ''
|
|
119
|
+
}
|
|
120
|
+
Log 'sha256 校验通过'
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
$tar = Join-Path $env:SystemRoot 'System32\tar.exe'
|
|
124
|
+
if (-not (Test-Path -LiteralPath $tar)) { $tar = 'tar.exe' }
|
|
125
|
+
$stage = Join-Path $env:TEMP ("dsh-plugin-live-stage-" + $stamp)
|
|
126
|
+
$payload = ''
|
|
127
|
+
try {
|
|
128
|
+
if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force }
|
|
129
|
+
New-Item -ItemType Directory -Path $stage -Force | Out-Null
|
|
130
|
+
& $tar -xzf $pkgPath -C $stage *> $null
|
|
131
|
+
if ($LASTEXITCODE -ne 0) { throw ("解包失败,退出码 {0}" -f $LASTEXITCODE) }
|
|
132
|
+
$payload = Join-Path $stage 'package'
|
|
133
|
+
if (-not (Test-Path -LiteralPath $payload)) { throw '安装包结构不对(里面没有 package 目录)' }
|
|
134
|
+
$newVersion = (Get-Content -LiteralPath (Join-Path $payload 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
|
|
135
|
+
Log ("包内版本:{0}" -f $newVersion)
|
|
136
|
+
if ($Version -and $newVersion -ne $Version) { throw ("安装包里的版本是 {0},与清单 {1} 不一致" -f $newVersion, $Version) }
|
|
137
|
+
} catch {
|
|
138
|
+
Log ("解包/校验失败:{0}" -f $_.Exception.Message)
|
|
139
|
+
if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
|
|
140
|
+
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
|
141
|
+
EmitResult $false ('安装包有问题:' + $_.Exception.Message) ''
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
# 整目录备份:写坏了能手动拷回去,也能自动回滚
|
|
145
|
+
$backup = Join-Path (Join-Path $DshHome 'backups') ("plugin-{0}-{1}" -f $(if ($currentVersion) { $currentVersion } else { 'unknown' }), $stamp)
|
|
146
|
+
$backupOk = $false
|
|
147
|
+
try {
|
|
148
|
+
New-Item -ItemType Directory -Path (Split-Path -Parent $backup) -Force | Out-Null
|
|
149
|
+
Copy-Item -LiteralPath $PluginDir -Destination $backup -Recurse -Force
|
|
150
|
+
$backupOk = $true
|
|
151
|
+
Log ("旧版已备份:{0}" -f $backup)
|
|
152
|
+
} catch {
|
|
153
|
+
Log ("备份失败(继续安装,出问题就没有自动回滚了):{0}" -f $_.Exception.Message)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
Log '覆盖安装…'
|
|
157
|
+
$installError = ''
|
|
158
|
+
try {
|
|
159
|
+
foreach ($it in @(Get-ChildItem -LiteralPath $payload -Force)) {
|
|
160
|
+
Copy-Item -LiteralPath $it.FullName -Destination $PluginDir -Recurse -Force
|
|
161
|
+
}
|
|
162
|
+
$installed = (Get-Content -LiteralPath (Join-Path $PluginDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json).version
|
|
163
|
+
Log ("安装完成,插件目录现在指向版本 {0}" -f $installed)
|
|
164
|
+
} catch {
|
|
165
|
+
$installError = $_.Exception.Message
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if ($installError) {
|
|
169
|
+
$restored = Restore-Backup $(if ($backupOk) { $backup } else { '' }) $PluginDir
|
|
170
|
+
$note = if ($restored) { '已用备份回滚到安装前的状态' } else { '没能自动回滚,手动把 ' + $backup + ' 拷回去' }
|
|
171
|
+
Log ("写入失败:{0}({1})" -f $installError, $note)
|
|
172
|
+
if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
|
|
173
|
+
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
|
174
|
+
EmitResult $false ("写入失败:" + $installError + '(' + $note + ')') $(if ($backupOk) { $backup } else { '' })
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if ($stage -and (Test-Path -LiteralPath $stage)) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue }
|
|
178
|
+
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
|
179
|
+
EmitResult $true '' $(if ($backupOk) { $backup } else { '' })
|