Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion go/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ $ErrorActionPreference = "Stop"

$repo = "SCE-Development/SCE-CLI"
$installDir = "$env:LOCALAPPDATA\sce"
$target = Join-Path $installDir "sce.exe"
$staged = Join-Path $installDir "sce.exe.new"
$helper = Join-Path $env:TEMP "sce-update-$PID.ps1"

# Detect architecture
$arch = if ([Environment]::Is64BitOperatingSystem) { "amd64" } else { "amd64" }
Expand All @@ -12,7 +15,48 @@ $url = "https://github.com/$repo/releases/latest/download/$binary"

Write-Host "downloading sce for windows/$arch..."
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
Invoke-WebRequest -Uri $url -OutFile "$installDir\sce.exe" -UseBasicParsing
Remove-Item -Path $staged -Force -ErrorAction SilentlyContinue
Invoke-WebRequest -Uri $url -OutFile $staged -UseBasicParsing

$replaceScript = @"
param(
[string]
`$Target,
[string]
`$Source
)

while (`$true) {
try {
if (-not (Test-Path -Path `$Target)) {
break
}

`$stream = [System.IO.File]::Open(`$Target, 'Open', 'ReadWrite', 'None')
`$stream.Close()
break
} catch {
Start-Sleep -Milliseconds 250
}
}

Move-Item -Force -Path `$Source -Destination `$Target
"@

Set-Content -Path $helper -Value $replaceScript -Encoding UTF8
Start-Process powershell -WindowStyle Hidden -ArgumentList @(
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-File",
$helper,
"-Target",
$target,
"-Source",
$staged
) | Out-Null

Write-Host "update downloaded; applying it after this command exits..."

# Add to PATH if not already there
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
Expand Down