-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall.ps1
More file actions
194 lines (160 loc) · 5.65 KB
/
Copy pathinstall.ps1
File metadata and controls
194 lines (160 loc) · 5.65 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
param(
[ValidateSet("install", "update", "repair", "auto")]
[string]$Action = "auto",
[ValidateSet("portable", "source", "auto")]
[string]$Mode = "auto",
[string]$InstallRoot = "",
[string]$ManifestPath = "",
[string]$ReleaseMetadataPath = "",
[string]$BundlePath = "",
[string]$SourceRepoPath = "",
[string]$RepoUrl = "",
[ValidateSet("Auto", "User", "Machine", "None")]
[string]$PathScope = "Auto",
[switch]$SkipViewer
)
$ErrorActionPreference = "Stop"
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
if ([string]::IsNullOrWhiteSpace($ManifestPath)) {
$ManifestPath = Join-Path $scriptRoot "distribution\version-manifest.json"
}
if (-not (Test-Path $ManifestPath)) {
throw "Version manifest not found: $ManifestPath"
}
$manifest = Get-Content $ManifestPath -Raw | ConvertFrom-Json
$installManifestPath = Join-Path $scriptRoot "distribution\install-manifest.json"
$installManifest = Get-Content $installManifestPath -Raw | ConvertFrom-Json
if ([string]::IsNullOrWhiteSpace($InstallRoot)) {
$InstallRoot = $installManifest.installRoots.windows
}
if ([string]::IsNullOrWhiteSpace($RepoUrl)) {
$RepoUrl = $manifest.repo.publicCloneUrl
}
function Resolve-WindowsAssetKey {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()
$assetKey = switch ($arch) {
"x64" { "win-x64" }
"x86" { "win-x86" }
default { "win-x64" }
}
return $assetKey
}
function Get-ReleaseMetadata {
param(
[psobject]$VersionManifest,
[string]$MetadataPath
)
if (-not [string]::IsNullOrWhiteSpace($MetadataPath)) {
return Get-Content $MetadataPath -Raw | ConvertFrom-Json
}
$headers = @{
"User-Agent" = "ctx-installer"
"Accept" = "application/vnd.github+json"
}
return Invoke-RestMethod -Uri $VersionManifest.repo.latestReleaseApi -Headers $headers -Method Get
}
function Normalize-ReleaseVersion {
param([string]$TagName)
if ([string]::IsNullOrWhiteSpace($TagName)) {
return ""
}
return $TagName.TrimStart('v', 'V')
}
function Resolve-PortableDownloadUrl {
param(
[psobject]$ReleaseMetadata,
[string]$AssetName
)
$asset = $ReleaseMetadata.assets | Where-Object { $_.name -eq $AssetName } | Select-Object -First 1
if ($null -eq $asset) {
throw "Release asset '$AssetName' was not found in GitHub release metadata."
}
return $asset.browser_download_url
}
function Download-ReleaseAsset {
param(
[string]$AssetUrl,
[string]$AssetName
)
$downloadRoot = Join-Path ([System.IO.Path]::GetTempPath()) "ctx-release-downloads"
New-Item -ItemType Directory -Path $downloadRoot -Force | Out-Null
$destination = Join-Path $downloadRoot $AssetName
$headers = @{
"User-Agent" = "ctx-installer"
"Accept" = "application/octet-stream"
}
Invoke-WebRequest -Uri $AssetUrl -Headers $headers -OutFile $destination
return $destination
}
$installMetadataPath = Join-Path $InstallRoot $installManifest.metadataFile
$installedVersion = $null
if (Test-Path $installMetadataPath) {
try {
$installedMetadata = Get-Content $installMetadataPath -Raw | ConvertFrom-Json
$installedVersion = $installedMetadata.version
}
catch {
$installedVersion = $null
}
}
$releaseMetadata = Get-ReleaseMetadata -VersionManifest $manifest -MetadataPath $ReleaseMetadataPath
$releaseVersion = Normalize-ReleaseVersion -TagName $releaseMetadata.tag_name
$effectiveAction = $Action
if ($effectiveAction -eq "auto") {
if (-not (Test-Path $installMetadataPath)) {
$effectiveAction = "install"
}
elseif ($installedVersion -ne $releaseVersion) {
$effectiveAction = "update"
}
else {
$effectiveAction = "repair"
}
}
$effectiveMode = $Mode
if ($effectiveMode -eq "auto") {
$effectiveMode = "portable"
}
if ($effectiveMode -eq "portable" -and [string]::IsNullOrWhiteSpace($BundlePath)) {
$assetKey = Resolve-WindowsAssetKey
$assetName = $manifest.assets.$assetKey
if ([string]::IsNullOrWhiteSpace($assetName)) {
throw "No $assetKey asset declared in version manifest."
}
$assetUrl = Resolve-PortableDownloadUrl -ReleaseMetadata $releaseMetadata -AssetName $assetName
$BundlePath = Download-ReleaseAsset -AssetUrl $assetUrl -AssetName $assetName
}
$statusLine = switch ($effectiveAction) {
"install" { "Installing CTX $releaseVersion..." }
"update" { "Updating CTX from $installedVersion to $releaseVersion..." }
"repair" { "Repairing CTX $releaseVersion..." }
default { "Running CTX bootstrap..." }
}
Write-Host $statusLine
if ($effectiveMode -eq "portable") {
Write-Host "Default bootstrap behavior: install from the published GitHub Release bundle, even when run from a cloned repository."
Write-Host "Use -Mode source only if you intentionally want to build CTX from this checkout."
}
Write-Host "Mode: $effectiveMode"
Write-Host "Install root: $InstallRoot"
Write-Host "Release tag: $($releaseMetadata.tag_name)"
if ($effectiveMode -eq "portable") {
Write-Host "Portable asset: $BundlePath"
}
$installScript = Join-Path $scriptRoot "scripts\install-ctx.ps1"
$params = @{
Mode = $effectiveMode
InstallRoot = $InstallRoot
RepoUrl = $RepoUrl
VersionLabel = $releaseVersion
PathScope = $PathScope
SkipViewer = $SkipViewer
}
if (-not [string]::IsNullOrWhiteSpace($BundlePath)) {
$params.BundlePath = $BundlePath
}
if (-not [string]::IsNullOrWhiteSpace($SourceRepoPath)) {
$params.SourceRepoPath = $SourceRepoPath
}
& $installScript @params
Write-Host "CTX bootstrap complete."