aisop/tools/windows/install-release.ps1

79 lines
3.8 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$ReleaseId = $(throw 'Usage: install-release.ps1 -ReleaseId ID [-InstallRoot C:\sophelper] [-Archive path] [-SkipGpuCheck 1]'),
[string]$InstallRoot = 'C:\sophelper',
[string]$Archive = '',
[int]$SkipGpuCheck = 0
)
# 远端安装脚本:解包 win-x64 版本、跨版本复用数据目录、自检并创建快捷方式。
# 由 tools/Deploy-Windows.ps1 通过 SSH 调用,参数不含空格。
$ErrorActionPreference = 'Stop'
$release = Join-Path $InstallRoot ('releases\' + $ReleaseId)
if (!$Archive) { $Archive = Join-Path $InstallRoot ($ReleaseId + '.tar.gz') }
if (!(Test-Path -LiteralPath $Archive)) { throw "Missing archive: $Archive" }
New-Item -ItemType Directory -Path $release -Force | Out-Null
tar -xzf $Archive -C $release
if ($LASTEXITCODE) { throw "解压失败: $Archive" }
$exe = Join-Path $release 'SopHelper.Avalonia.exe'
if (!(Test-Path -LiteralPath $exe)) { throw "Missing executable: $exe" }
# 操作数据、配方、模型跨版本保留在 InstallRoot 下,版本目录内用目录联接指回去。
foreach ($item in @('configs', 'models', 'workspace')) {
$shared = Join-Path $InstallRoot $item
$packed = Join-Path $release $item
if (!(Test-Path -LiteralPath $shared)) {
if ((Test-Path -LiteralPath $packed) -and ((Get-Item -LiteralPath $packed).Attributes -notmatch 'ReparsePoint')) {
Move-Item -LiteralPath $packed -Destination $shared
} else {
New-Item -ItemType Directory -Path $shared -Force | Out-Null
}
}
if ((Test-Path -LiteralPath $packed) -and ((Get-Item -LiteralPath $packed).Attributes -notmatch 'ReparsePoint')) {
Move-Item -LiteralPath $packed -Destination ($packed + '.packaged-' + (Get-Date -Format 'yyyyMMddHHmmss'))
}
if (!(Test-Path -LiteralPath $packed)) {
New-Item -ItemType Junction -Path $packed -Target $shared | Out-Null
}
}
New-Item -ItemType Directory -Path (Join-Path $InstallRoot 'workspace\logs') -Force | Out-Null
$current = Join-Path $InstallRoot 'current'
if (Test-Path -LiteralPath $current) { (Get-Item -LiteralPath $current).Delete() }
New-Item -ItemType Junction -Path $current -Target $release | Out-Null
function Invoke-SelfCheck([string]$Flag, [string]$Name) {
$out = Join-Path $InstallRoot ('workspace\logs\install-' + $Name + '.stdout.log')
$err = Join-Path $InstallRoot ('workspace\logs\install-' + $Name + '.stderr.log')
$process = Start-Process -FilePath $exe -ArgumentList $Flag -NoNewWindow -PassThru -Wait `
-RedirectStandardOutput $out -RedirectStandardError $err
$text = ''
if (Test-Path -LiteralPath $out) { $text = (Get-Content -LiteralPath $out -Raw) }
return [pscustomobject]@{ name = $Flag; exitCode = $process.ExitCode; output = $text.Trim() }
}
$checks = @()
$checks += Invoke-SelfCheck '--check-runtime' 'runtime'
if ($checks[-1].exitCode) { throw ('--check-runtime 失败,详见 ' + (Join-Path $InstallRoot 'workspace\logs\install-runtime.stderr.log')) }
if (!$SkipGpuCheck) {
$checks += Invoke-SelfCheck '--check-gpu' 'gpu'
if ($checks[-1].exitCode) { throw ('--check-gpu 失败,详见 ' + (Join-Path $InstallRoot 'workspace\logs\install-gpu.stderr.log')) }
}
$shell = New-Object -ComObject WScript.Shell
foreach ($folder in @('Desktop', 'Programs')) {
$directory = [Environment]::GetFolderPath($folder)
if (!$directory) { continue }
New-Item -ItemType Directory -Path $directory -Force | Out-Null
$link = $shell.CreateShortcut((Join-Path $directory 'SopHelper.lnk'))
$link.TargetPath = $exe
$link.WorkingDirectory = $release
$link.IconLocation = $exe + ',0'
$link.Description = 'SopHelper SOP 工位检测(' + $ReleaseId + ')'
$link.Save()
}
Write-Output (@{
installed = $release
current = $current
checks = $checks
} | ConvertTo-Json -Depth 5 -Compress)