-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
98 lines (84 loc) · 3.37 KB
/
Copy pathinstall.ps1
File metadata and controls
98 lines (84 loc) · 3.37 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
param(
[string]$InstallDir = (Join-Path $env:LOCALAPPDATA "Programs\Aisets"),
[string]$Repo = $(if ($env:AISETS_REPO) { $env:AISETS_REPO } else { "runkids/aisets" })
)
$ErrorActionPreference = "Stop"
$BinaryName = "aisets"
$ExeName = "$BinaryName.exe"
$ImgtoolsExeName = "aisets-imgtools.exe"
function Add-DirectoryToUserPath {
param([string]$Directory)
if (-not $Directory -or -not (Test-Path $Directory)) {
return
}
$normalized = [System.IO.Path]::GetFullPath($Directory).TrimEnd('\')
$current = [Environment]::GetEnvironmentVariable("Path", "User")
$parts = @()
if ($current) {
$parts = $current -split ';' | Where-Object { $_ }
}
$alreadyPresent = $false
foreach ($part in $parts) {
if ([System.IO.Path]::GetFullPath($part).TrimEnd('\').Equals($normalized, [System.StringComparison]::OrdinalIgnoreCase)) {
$alreadyPresent = $true
break
}
}
if (-not $alreadyPresent) {
$next = if ($current) { "$current;$normalized" } else { $normalized }
[Environment]::SetEnvironmentVariable("Path", $next, "User")
Write-Host "Added to user PATH: $normalized"
}
$envParts = $env:Path -split ';' | Where-Object { $_ }
$inCurrentProcess = $false
foreach ($part in $envParts) {
if ([System.IO.Path]::GetFullPath($part).TrimEnd('\').Equals($normalized, [System.StringComparison]::OrdinalIgnoreCase)) {
$inCurrentProcess = $true
break
}
}
if (-not $inCurrentProcess) {
$env:Path = "$normalized;$env:Path"
}
}
function Get-AssetStudioArch {
switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) {
"X64" { return "amd64" }
"Arm64" { return "arm64" }
default { throw "Unsupported architecture: $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)" }
}
}
$arch = Get-AssetStudioArch
try {
$latest = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest"
} catch {
throw "No GitHub release found for $Repo. Create the GitHub repo and publish a release first, or pass -Repo owner/name for a different repo."
}
$tag = $latest.tag_name
if (-not $tag) {
throw "Latest release tag is empty."
}
$version = $tag.TrimStart('v')
$archive = "${BinaryName}_${version}_windows_${arch}.zip"
$asset = $latest.assets | Where-Object { $_.name -eq $archive } | Select-Object -First 1
$url = if ($asset) { $asset.browser_download_url } else { "https://github.com/$Repo/releases/download/$tag/$archive" }
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
$archivePath = Join-Path $tmp $archive
Write-Host "Downloading $url"
Invoke-WebRequest -Uri $url -OutFile $archivePath
Expand-Archive -Path $archivePath -DestinationPath $tmp -Force
$imgtoolsPath = Join-Path $tmp $ImgtoolsExeName
if (-not (Test-Path $imgtoolsPath)) {
throw "Release archive did not include $ImgtoolsExeName."
}
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
Copy-Item -Path (Join-Path $tmp $ExeName) -Destination (Join-Path $InstallDir $ExeName) -Force
Copy-Item -Path $imgtoolsPath -Destination (Join-Path $InstallDir $ImgtoolsExeName) -Force
Add-DirectoryToUserPath $InstallDir
$installed = Join-Path $InstallDir $ExeName
Write-Host "Installed $(& $installed version) to $installed"
} finally {
Remove-Item -Path $tmp -Recurse -Force -ErrorAction SilentlyContinue
}