Files
Button/sshDeploy/build-gui.ps1
T
2026-08-24 16:40:48 +03:00

96 lines
2.4 KiB
PowerShell

param(
[switch]$OneFile = $true
)
$ErrorActionPreference = 'Stop'
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $here
function Test-PyInstaller($pyExe, [string[]]$pyArgs) {
$prev = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& $pyExe @pyArgs -m PyInstaller --version 2>&1 | Out-Null
return $LASTEXITCODE -eq 0
} finally {
$ErrorActionPreference = $prev
}
}
$pyCmd = $null
$candidates = @()
if (Get-Command python -ErrorAction SilentlyContinue) { $candidates += ,@('python') }
if (Get-Command py -ErrorAction SilentlyContinue) { $candidates += ,@('py', '-3') }
foreach ($candidate in $candidates) {
$pyExe = $candidate[0]
$pyArgs = @()
if ($candidate.Length -gt 1) {
$pyArgs = $candidate[1..($candidate.Length - 1)]
}
if (Test-PyInstaller $pyExe $pyArgs) {
$pyCmd = $candidate
break
}
}
if (-not $pyCmd) {
Write-Host "[error] Python 3 with PyInstaller not found." -ForegroundColor Red
Write-Host " Install: py -3 -m pip install pyinstaller" -ForegroundColor Yellow
if ($candidates.Count -gt 0) {
Write-Host " PyInstaller is missing in the Python that runs first (often 'python' != 'py -3')." -ForegroundColor Yellow
}
exit 1
}
$pyExe = $pyCmd[0]
$pyArgs = @()
if ($pyCmd.Length -gt 1) {
$pyArgs = $pyCmd[1..($pyCmd.Length - 1)]
}
Write-Host "[build] using: $pyExe $($pyArgs -join ' ')" -ForegroundColor Cyan
$releaseDir = Join-Path $here 'dist\sshDeploy-gui'
$pyiDist = Join-Path $here 'dist-pyinstaller'
if (Test-Path $releaseDir) { Remove-Item $releaseDir -Recurse -Force }
if (Test-Path $pyiDist) { Remove-Item $pyiDist -Recurse -Force }
$args = @(
'--noconfirm',
'--clean',
'--name', 'sshDeploy-gui',
'--distpath', $pyiDist
)
if ($OneFile) { $args += '--onefile' }
$args += 'gui_app.py'
& $pyExe @pyArgs -m PyInstaller @args
New-Item -ItemType Directory -Force -Path $releaseDir | Out-Null
$artifacts = @(
'config.json',
'config.example.json',
'README.md',
'plink.exe',
'pscp.exe'
)
foreach ($name in $artifacts) {
$src = Join-Path $here $name
if (Test-Path $src) {
Copy-Item $src $releaseDir -Force
}
}
$builtExe = Join-Path $pyiDist 'sshDeploy-gui.exe'
if (Test-Path $builtExe) {
Copy-Item $builtExe (Join-Path $releaseDir 'sshDeploy-gui.exe') -Force
}
if (Test-Path $pyiDist) {
Remove-Item $pyiDist -Recurse -Force
}
Write-Host "[ok] Ready: $releaseDir" -ForegroundColor Green