-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.ps1
More file actions
113 lines (103 loc) · 4.16 KB
/
Copy pathinstall.ps1
File metadata and controls
113 lines (103 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# podcli installer for Windows - downloads the prebuilt native binary (no Go,
# Node, Python, or ffmpeg needed; the binary provisions those on first run).
# Usage: irm https://raw.githubusercontent.com/nmbrthirteen/podcli/main/install.ps1 | iex
# Uninstall: & ([scriptblock]::Create((irm https://raw.githubusercontent.com/nmbrthirteen/podcli/main/install.ps1))) -Uninstall
# Purge: & ([scriptblock]::Create((irm https://raw.githubusercontent.com/nmbrthirteen/podcli/main/install.ps1))) -Uninstall -Purge
param([switch]$Uninstall, [switch]$Purge)
$ErrorActionPreference = 'Stop'
$repo = 'nmbrthirteen/podcli'
$target = 'windows-amd64'
$homeDir = Join-Path $env:LOCALAPPDATA 'podcli'
$binDir = Join-Path $homeDir 'bin'
function Get-UserPathEntry {
$key = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Environment')
if (-not $key) { return $null }
$kind = [Microsoft.Win32.RegistryValueKind]::ExpandString
try { $kind = $key.GetValueKind('Path') } catch {}
[pscustomobject]@{
Key = $key
Kind = $kind
Value = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
}
}
function Test-PathEntryEquals {
param([string]$Entry, [string]$Target)
try {
return [IO.Path]::GetFullPath([Environment]::ExpandEnvironmentVariables($Entry)).TrimEnd('\') -ieq [IO.Path]::GetFullPath($Target).TrimEnd('\')
} catch {
return $Entry.TrimEnd('\') -ieq $Target.TrimEnd('\')
}
}
if ($Uninstall) {
Write-Host "Uninstalling podcli..."
if ($Purge) {
$targets = @($homeDir)
} else {
$targets = @($binDir, (Join-Path $homeDir 'runtime'), (Join-Path $homeDir 'models'), (Join-Path $homeDir 'tools'))
}
foreach ($p in $targets) {
if (Test-Path $p) {
try {
Remove-Item $p -Recurse -Force -ErrorAction Stop
Write-Host " removed: $p"
} catch {
Write-Warning ("could not remove {0}: {1}" -f $p, $_.Exception.Message)
}
}
}
$pathEntry = Get-UserPathEntry
if ($pathEntry) {
$parts = @($pathEntry.Value -split ';' | Where-Object { $_ })
$kept = @($parts | Where-Object { -not (Test-PathEntryEquals $_ $binDir) })
if ($kept.Count -ne $parts.Count) {
$pathEntry.Key.SetValue('Path', ($kept -join ';'), $pathEntry.Kind)
Write-Host " removed from user PATH (restart your terminal)"
}
}
if ($Purge) {
Write-Host " removed managed data."
} else {
Write-Host " kept user data (config, knowledge, presets, assets, history, cache)."
Write-Host " To remove everything: rerun with -Uninstall -Purge"
}
exit 0
}
New-Item -ItemType Directory -Force -Path $binDir | Out-Null
$version = $env:PODCLI_VERSION
if (-not $version) {
$rel = Invoke-RestMethod "https://api.github.com/repos/$repo/releases/latest" -Headers @{ 'User-Agent' = 'podcli-install' }
$version = $rel.tag_name -replace '^v', ''
}
$asset = "podcli-$target.exe"
$base = "https://github.com/$repo/releases/download/v$version"
Write-Host "Installing podcli v$version ($target)..."
$dest = Join-Path $binDir 'podcli.exe'
Invoke-WebRequest "$base/$asset" -OutFile $dest -UseBasicParsing
try {
$sums = (Invoke-WebRequest "$base/checksums.txt" -UseBasicParsing).Content
if ($sums -is [byte[]]) {
$sums = [System.Text.Encoding]::UTF8.GetString($sums)
}
$want = $sums -split "`n" |
Where-Object { $_ -match ([regex]::Escape($asset) + '\s*$') } |
ForEach-Object { ($_ -split '\s+')[0] } | Select-Object -First 1
if ($want) {
$got = (Get-FileHash $dest -Algorithm SHA256).Hash.ToLower()
if ($got -ne $want.ToLower()) { Remove-Item $dest -Force; throw "checksum mismatch (got $got want $want)" }
Write-Host " checksum verified"
} else {
Write-Host " no checksum entry for $asset - skipped verification"
}
} catch {
Write-Host " checksum verification skipped: $($_.Exception.Message)"
}
$pathEntry = Get-UserPathEntry
if ($pathEntry) {
$parts = @($pathEntry.Value -split ';' | Where-Object { $_ })
if (-not ($parts | Where-Object { Test-PathEntryEquals $_ $binDir })) {
$pathEntry.Key.SetValue('Path', ($binDir + ';' + $pathEntry.Value), $pathEntry.Kind)
Write-Host " added to PATH (restart your terminal)"
}
}
Write-Host ""
Write-Host "Done - run: podcli"