moyun-ultimate/update.ps1
moyun 4b9eb3c1ff feat: 0.6.0 - update check and one-command upgrade
Answers: when I ship a new version, how does the user find out, and what do
they do about it.

Three ways to learn:
1. moyun_ultimate_profile now returns an update block: current, latest,
   available, notes, howTo, page.
2. New tool moyun_check_update (force: true re-queries).
3. On startup the plugin checks the latest release in the background and logs
   a line when a newer version exists.

One command to upgrade:
  powershell -ExecutionPolicy Bypass -File .\update.ps1

It queries the release, downloads the tgz, backs up the current directories as
*.bak-update-<stamp>, extracts over them, and PRESERVES the user's mode choice
(complement stays complement, it is not clobbered into full).

Variants: -Check (report only), -Force (reinstall), -DryRun (dry run).

Opt out with checkUpdate: false in the bundle patch.

Version comparison is numeric per segment so 0.10.0 > 0.9.9. Network failure
degrades quietly; startup is not blocked (8s timeout, background promise).
2026-11-17 15:14:23 +08:00

207 lines
7.3 KiB
PowerShell

# moyun-ultimate updater (ASCII only - PS 5.1 reads BOM-less UTF-8 as ANSI)
#
# -Check only report whether a newer version exists
# -Force reinstall even if already up to date
# -DryRun show what would happen, download nothing
#
# Keeps your chosen mode: if the installed bundle patch was switched to
# COMPLEMENT (a cordis.patch.yml.full exists), the patch is preserved.
[CmdletBinding()]
param(
[string]$Profile = 'desktop',
[string]$DshHome = '',
[switch]$Check,
[switch]$Force,
[switch]$DryRun
)
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$PkgName = 'moyun-ultimate'
$Owner = 'moyun'
$Repo = 'moyun-ultimate'
$Base = 'https://git.lingdongjiansuo.cn'
$ApiUrl = "$Base/api/v1/repos/$Owner/$Repo/releases/latest"
$RelPage = "$Base/$Owner/$Repo/releases"
if (-not $DshHome) {
if ($env:DSH_HOME) { $DshHome = $env:DSH_HOME }
elseif (Test-Path 'G:\DeepSeek_dsh') { $DshHome = 'G:\DeepSeek_dsh' }
elseif (Test-Path (Join-Path $env:USERPROFILE '.dsh')) { $DshHome = Join-Path $env:USERPROFILE '.dsh' }
else { throw 'DSH_HOME not found. Pass -DshHome <path>.' }
}
$profileDir = Join-Path $DshHome "profiles\$Profile"
$plugDir = Join-Path $profileDir "plugins\$PkgName"
$runDir = Join-Path $profileDir "node_modules\$PkgName"
Write-Output ''
Write-Output 'moyun-ultimate updater'
Write-Output '======================'
# ---- current version ----
$current = $null
$localPkg = Join-Path $runDir 'package.json'
if (-not (Test-Path $localPkg)) { $localPkg = Join-Path $plugDir 'package.json' }
if (Test-Path $localPkg) {
try { $current = (Get-Content -LiteralPath $localPkg -Raw -Encoding UTF8 | ConvertFrom-Json).version } catch {}
}
if (-not $current) {
Write-Output " not installed under $profileDir"
Write-Output ' install first: powershell -ExecutionPolicy Bypass -File .\install.ps1'
exit 1
}
Write-Output (" current : " + $current)
Write-Output (" source : " + $localPkg)
# ---- latest release ----
try {
$rel = Invoke-RestMethod -Uri $ApiUrl -UseBasicParsing -TimeoutSec 25
} catch {
Write-Output (" cannot reach update server: " + $_.Exception.Message)
Write-Output (" check manually: " + $RelPage)
exit 1
}
$latest = ($rel.tag_name -replace '^[vV]', '')
Write-Output (" latest : " + $latest + " (" + $rel.name + ")")
function CmpVer([string]$a, [string]$b) {
$pa = ($a -replace '^[vV]', '') -split '\.'
$pb = ($b -replace '^[vV]', '') -split '\.'
$n = [Math]::Max($pa.Count, $pb.Count)
for ($i = 0; $i -lt $n; $i++) {
$x = 0; $y = 0
if ($i -lt $pa.Count) { [void][int]::TryParse($pa[$i], [ref]$x) }
if ($i -lt $pb.Count) { [void][int]::TryParse($pb[$i], [ref]$y) }
if ($x -gt $y) { return 1 }
if ($x -lt $y) { return -1 }
}
return 0
}
$cmp = CmpVer $latest $current
if ($cmp -le 0 -and -not $Force) {
Write-Output ''
Write-Output (' up to date (' + $current + ')')
Write-Output ''
exit 0
}
if ($cmp -gt 0) { Write-Output (" update available: " + $current + " -> " + $latest) }
else { Write-Output (" same or older, reinstalling because -Force") }
if ($Check) {
Write-Output ''
Write-Output ' -Check only: nothing was downloaded.'
Write-Output (' to upgrade: powershell -ExecutionPolicy Bypass -File .\update.ps1')
exit 0
}
# ---- asset ----
$asset = $rel.assets | Where-Object { $_.name -like '*.tgz' } | Select-Object -First 1
if (-not $asset) {
Write-Output ' no .tgz asset on that release; download manually:'
Write-Output (' ' + $rel.html_url)
exit 1
}
Write-Output (" asset : " + $asset.name + " " + $asset.size + " B")
if ($DryRun) {
Write-Output ''
Write-Output ' -DryRun: would download and install the above.'
exit 0
}
# ---- download ----
$tmpDir = Join-Path ([IO.Path]::GetTempPath()) ("moyun-up-" + [guid]::NewGuid().ToString('N').Substring(0, 8))
New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null
$tgz = Join-Path $tmpDir $asset.name
Write-Output ' downloading...'
try {
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $tgz -UseBasicParsing -TimeoutSec 180
} catch {
Write-Output (" download failed: " + $_.Exception.Message)
Write-Output (" you can download manually: " + $asset.browser_download_url)
Remove-Item $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
exit 1
}
$got = (Get-Item $tgz).Length
Write-Output (" downloaded " + $got + " B")
# ---- extract ----
$ex = Join-Path $tmpDir 'x'
New-Item -ItemType Directory -Force -Path $ex | Out-Null
try {
& tar -xzf $tgz -C $ex 2>&1 | Out-Null
} catch {
Write-Output ' extract failed (tar missing?)'
exit 1
}
$src = Join-Path $ex 'package'
if (-not (Test-Path $src)) {
Write-Output ' archive layout unexpected (no package/ dir)'
exit 1
}
$newVer = 'unknown'
try { $newVer = (Get-Content (Join-Path $src 'package.json') -Raw -Encoding UTF8 | ConvertFrom-Json).version } catch {}
Write-Output (" package version: " + $newVer)
# ---- remember current mode (complement keeps its patch) ----
$keepPatch = $false
$patchWasComplement = $false
$runPatch = Join-Path $runDir 'cordis.patch.yml'
if ((Test-Path (Join-Path $runDir 'cordis.patch.yml.full')) -or (Test-Path (Join-Path $plugDir 'cordis.patch.yml.full'))) {
$keepPatch = $true
$patchWasComplement = $true
}
if (Test-Path $runPatch) {
$pt = Get-Content $runPatch -Raw -Encoding UTF8
if ($pt -match 'COMPLEMENT MODE') { $keepPatch = $true; $patchWasComplement = $true }
}
# ---- backup + install ----
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
foreach ($d in @($plugDir, $runDir)) {
if (-not (Test-Path $d)) { continue }
$bak = "$d.bak-update-$stamp"
Copy-Item $d $bak -Recurse -Force
Write-Output (" backup: " + $bak)
# keep user's patch choice
$savedPatch = $null
if ($keepPatch) {
$p = Join-Path $d 'cordis.patch.yml'
if (Test-Path $p) { $savedPatch = Get-Content $p -Raw -Encoding UTF8 }
}
Remove-Item $d -Recurse -Force
New-Item -ItemType Directory -Force -Path $d | Out-Null
Copy-Item "$src\*" $d -Recurse -Force
if ($keepPatch -and $savedPatch) {
[IO.File]::WriteAllText((Join-Path $d 'cordis.patch.yml'), $savedPatch, (New-Object Text.UTF8Encoding($false)))
Write-Output (" kept existing patch (COMPLEMENT mode preserved)")
} elseif ($patchWasComplement) {
# restore the full copy marker so switch-mode still works
$fullSrc = Join-Path $src 'cordis.patch.yml'
if (Test-Path $fullSrc) {
Copy-Item $fullSrc (Join-Path $d 'cordis.patch.yml.full') -Force
}
Write-Output (" wrote new full patch as cordis.patch.yml.full")
}
Write-Output (" updated: " + $d)
}
# ---- package.json registration stays untouched ----
Write-Output ''
Write-Output (' updated to ' + $newVer + ' (was ' + $current + ')')
Write-Output ''
Write-Output 'Next:'
Write-Output ' 1. Fully quit DSH (including the tray icon), then reopen it.'
Write-Output ' 2. Verify: powershell -ExecutionPolicy Bypass -File .\doctor.ps1'
Write-Output ''
Write-Output 'Rollback this update:'
Write-Output (" the previous directories were saved as *" + ".bak-update-" + $stamp)
Write-Output ' or reinstall a specific version from the releases page:'
Write-Output (" " + $RelPage)
Remove-Item $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
exit 0