dsh-config-sync-plugin 0.3.69 → 0.3.71
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 -21
- package/README.en.md +17 -218
- package/README.md +19 -209
- package/client.js +32 -32
- package/index.js +8 -8
- package/package.json +1 -1
- package/runtime/apply.ps1 +5 -20
- package/runtime/capture.ps1 +1 -1
- package/runtime/dsh-sync-common.ps1 +16 -2
- package/runtime/install-plugin.ps1 +179 -179
package/runtime/apply.ps1
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<#
|
|
2
|
-
DSH 配置同步 —— 应用脚本(把本仓库的配置还原到当前电脑)
|
|
2
|
+
DSH 配置同步 —— 应用脚本(把本仓库的配置还原到当前电脑)
|
|
3
3
|
|
|
4
4
|
用法
|
|
5
5
|
pwsh -File apply.ps1 # 默认:铺配置 + 全量还原所有工作区
|
|
@@ -59,7 +59,7 @@ $ErrorActionPreference = 'Stop'
|
|
|
59
59
|
if (-not $RepoRoot) { $RepoRoot = $PSScriptRoot }
|
|
60
60
|
. (Join-Path $PSScriptRoot 'dsh-sync-common.ps1')
|
|
61
61
|
|
|
62
|
-
if (-not $NoResetNotices) { Reset-SyncNotices $DshHome 'pull' }
|
|
62
|
+
if (-not $NoResetNotices) { Reset-SyncNotices $DshHome 'pull' } else { Set-SyncNoticeContext $DshHome 'pull' }
|
|
63
63
|
|
|
64
64
|
# 客户端可能被卸载重装到别的盘:传进来的 -AppRoot 先校验,对不上就自动定位(注册表 → 常见位置)。
|
|
65
65
|
$resolvedRoot = Resolve-DshAppRoot $AppRoot
|
|
@@ -898,25 +898,10 @@ if ($dropped.Count -gt 0) { Ok "已剔除 $($dropped.Count) 个实验性 bundle
|
|
|
898
898
|
$localPluginMap = [ordered]@{
|
|
899
899
|
'dsh-our-free-model-2.8.0' = 'dsh-our-free-model'
|
|
900
900
|
'dsh-purge-1.4.0' = 'dsh-purge'
|
|
901
|
-
'dsh-config-sync-plugin-0.3.
|
|
902
|
-
|
|
903
|
-
'dsh-config-sync-plugin-0.3.67' = 'dsh-config-sync-plugin'
|
|
904
|
-
'dsh-config-sync-plugin-0.3.66' = 'dsh-config-sync-plugin'
|
|
905
|
-
'dsh-config-sync-plugin-0.3.65' = 'dsh-config-sync-plugin'
|
|
906
|
-
'dsh-config-sync-plugin-0.3.64' = 'dsh-config-sync-plugin'
|
|
907
|
-
'dsh-config-sync-plugin-0.3.63' = 'dsh-config-sync-plugin'
|
|
908
|
-
'dsh-config-sync-plugin-0.3.62' = 'dsh-config-sync-plugin'
|
|
909
|
-
'dsh-config-sync-plugin-0.3.61' = 'dsh-config-sync-plugin'
|
|
910
|
-
'dsh-config-sync-plugin-0.3.59' = 'dsh-config-sync-plugin'
|
|
911
|
-
'dsh-config-sync-plugin-0.3.58' = 'dsh-config-sync-plugin'
|
|
912
|
-
'dsh-config-sync-plugin-0.3.57' = 'dsh-config-sync-plugin'
|
|
913
|
-
'dsh-config-sync-plugin-0.3.56' = 'dsh-config-sync-plugin'
|
|
901
|
+
'dsh-config-sync-plugin-0.3.70' = 'dsh-config-sync-plugin'
|
|
902
|
+
'dsh-config-sync-plugin-0.3.71' = 'dsh-config-sync-plugin'
|
|
914
903
|
'dsh-config-sync-0.1.0' = 'dsh-config-sync'
|
|
915
904
|
}
|
|
916
|
-
foreach ($dirName in $localPluginMap.Keys) {
|
|
917
|
-
$pattern = 'link:[^"]*' + [regex]::Escape($dirName)
|
|
918
|
-
$pkgRaw = [regex]::Replace($pkgRaw, $pattern, ('link:' + $appRootFwd + '/' + $dirName), 'IgnoreCase')
|
|
919
|
-
}
|
|
920
905
|
Write-TextPreserveBom (Join-Path $ProfileDir 'package.json') $pkgRaw
|
|
921
906
|
Ok "已写入 package.json(link 路径指向 $appRootFwd)"
|
|
922
907
|
} else { Warn '跳过(未勾选同步):profile 配置(package.json / lock / cordis 保持本机现状)' }
|
|
@@ -1282,7 +1267,7 @@ foreach ($dirName in $localPluginMap.Keys) {
|
|
|
1282
1267
|
}
|
|
1283
1268
|
$hasNewVersion = $false
|
|
1284
1269
|
if ((Test-Path -LiteralPath $arch) -and $archVer -and $depVer) {
|
|
1285
|
-
$hasNewVersion = ($archVer -
|
|
1270
|
+
$hasNewVersion = ([version]$archVer -gt [version]$depVer)
|
|
1286
1271
|
} elseif (Test-Path -LiteralPath $arch) {
|
|
1287
1272
|
# 读不到版本(老归档、结构异常)时退回 mtime,但要求比已部署目录新 10 秒以上,避免每轮误判
|
|
1288
1273
|
$archTime = (Get-Item -LiteralPath $arch).LastWriteTime
|
package/runtime/capture.ps1
CHANGED
|
@@ -93,7 +93,7 @@ function Get-TreeCmpSummary {
|
|
|
93
93
|
return ('文件比较({0}):比对 {1} 个,深哈希 {2} 个' -f $deep, $script:CmpFileCount, $script:CmpHashCount)
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
if (-not $NoResetNotices) { Reset-SyncNotices $DshHome 'capture' }
|
|
96
|
+
if (-not $NoResetNotices) { Reset-SyncNotices $DshHome 'capture' } else { Set-SyncNoticeContext $DshHome 'capture' }
|
|
97
97
|
|
|
98
98
|
# 客户端可能被卸载重装到别的盘:-AppRoot 先校验,对不上就自动定位(注册表 → 常见位置)。
|
|
99
99
|
$resolvedRoot = Resolve-DshAppRoot $AppRoot
|
|
@@ -30,8 +30,8 @@ function Die($m) { Write-Host " [X] $m" -ForegroundColor Red; exit 1 }
|
|
|
30
30
|
# too-large 超过单文件上限(capture 跳过 / push 前从索引剔除 / 历史里已存在)
|
|
31
31
|
# skipped 被规则或占用跳过(不是失败,但用户该知道)
|
|
32
32
|
# failed 写入失败、被远端拒绝等真失败
|
|
33
|
-
# history 历史提交里已存在的超限文件(远端必然拒收,必须改写历史或换仓库)
|
|
34
|
-
# plugin-missing 插件没装在本机(拉取只还原配置,不装依赖;装完要再拉一次)
|
|
33
|
+
# history 历史提交里已存在的超限文件(远端必然拒收,必须改写历史或换仓库)
|
|
34
|
+
# plugin-missing 插件没装在本机(拉取只还原配置,不装依赖;装完要再拉一次)
|
|
35
35
|
# context-state billion-context 的压缩状态没跟着过来(该会话可能报上下文超长)
|
|
36
36
|
$script:SyncNoticeItems = New-Object 'System.Collections.ArrayList'
|
|
37
37
|
$script:SyncNoticeHome = $null
|
|
@@ -52,6 +52,9 @@ function Read-SyncNotices([string]$dshHome) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
function Save-SyncNotices {
|
|
55
|
+
# 没有明确的 home 就不写:Get-SyncNoticesPath 会回落到 %USERPROFILE%\.dsh,
|
|
56
|
+
# 于是一次「没有上下文」的调用就会把本机真实清单改掉(隔离测试/被 sync 调起的子进程都踩过)。
|
|
57
|
+
if (-not $script:SyncNoticeHome) { return }
|
|
55
58
|
try {
|
|
56
59
|
$p = Get-SyncNoticesPath $script:SyncNoticeHome
|
|
57
60
|
$dir = Split-Path -Parent $p
|
|
@@ -71,6 +74,16 @@ function Save-SyncNotices {
|
|
|
71
74
|
} catch { }
|
|
72
75
|
}
|
|
73
76
|
|
|
77
|
+
# 被 sync 调起的子进程(capture / apply)带 -NoResetNotices:它们不能清空清单,
|
|
78
|
+
# 但**必须**建立 home/mode 上下文 —— 否则 Add-SyncNotice 会回落到 %USERPROFILE%\.dsh,
|
|
79
|
+
# 把本该写到 -DshHome 的条目写进别人家的清单里。
|
|
80
|
+
function Set-SyncNoticeContext([string]$dshHome, [string]$mode) {
|
|
81
|
+
if (-not $dshHome) { return }
|
|
82
|
+
$script:SyncNoticeHome = $dshHome
|
|
83
|
+
if ($mode) { $script:SyncNoticeMode = $mode }
|
|
84
|
+
$script:SyncNoticeLoaded = $true
|
|
85
|
+
}
|
|
86
|
+
|
|
74
87
|
# 每个动作开始时清空。被 sync 调起的子脚本(capture)要传 -NoResetNotices,
|
|
75
88
|
# 否则它一 Reset 就把父进程已经攒下的条目抹掉。
|
|
76
89
|
function Reset-SyncNotices([string]$dshHome, [string]$mode) {
|
|
@@ -84,6 +97,7 @@ function Reset-SyncNotices([string]$dshHome, [string]$mode) {
|
|
|
84
97
|
function Add-SyncNotice {
|
|
85
98
|
param([string]$Kind = 'skipped', [string]$Path = '', [string]$Reason = '', [long]$Bytes = 0)
|
|
86
99
|
if (-not $Path) { return }
|
|
100
|
+
if (-not $script:SyncNoticeHome) { return }
|
|
87
101
|
# 先吃进磁盘上别的进程已经写好的条目(capture 与 sync 是两个进程)
|
|
88
102
|
if (-not $script:SyncNoticeLoaded) {
|
|
89
103
|
$existing = Read-SyncNotices $script:SyncNoticeHome
|
|
@@ -1,179 +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 { '' })
|
|
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 { '' })
|