-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
317 lines (265 loc) · 9.7 KB
/
install.ps1
File metadata and controls
317 lines (265 loc) · 9.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# dtvem installer for Windows
# Usage:
# Standard (admin required): irm https://raw.githubusercontent.com/CodingWithCalvin/dtvem.cli/main/install.ps1 | iex
# User install (no admin): iex "& { $(irm https://raw.githubusercontent.com/CodingWithCalvin/dtvem.cli/main/install.ps1) } -UserInstall"
param(
[switch]$UserInstall
)
$ErrorActionPreference = "Stop"
$REPO = "CodingWithCalvin/dtvem.cli"
# Get dtvem root directory
# Respects DTVEM_ROOT environment variable if set, XDG_DATA_HOME if set, otherwise uses default
function Get-DtvemRoot {
if ($env:DTVEM_ROOT) {
return $env:DTVEM_ROOT
}
if ($env:XDG_DATA_HOME) {
return "$env:XDG_DATA_HOME\dtvem"
}
return "$env:USERPROFILE\.dtvem"
}
$DTVEM_ROOT = Get-DtvemRoot
$INSTALL_DIR = "$DTVEM_ROOT\bin"
$SHIMS_DIR = "$DTVEM_ROOT\shims"
# This will be replaced with the actual version during release
# Format: $DTVEM_RELEASE_VERSION = "1.0.0"
# Leave empty to fetch latest
$DTVEM_RELEASE_VERSION = ""
function Write-Info {
param([string]$Message)
Write-Host "→ " -ForegroundColor Cyan -NoNewline
Write-Host $Message
}
function Write-Success {
param([string]$Message)
Write-Host "✓ " -ForegroundColor Green -NoNewline
Write-Host $Message
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "✗ " -ForegroundColor Red -NoNewline
Write-Host $Message
}
function Write-Warning-Custom {
param([string]$Message)
Write-Host "⚠ " -ForegroundColor Yellow -NoNewline
Write-Host $Message
}
# Global variable to store release data
$script:ReleaseData = $null
function Get-ReleaseInfo {
param([string]$Version)
try {
if ($Version) {
$apiUrl = "https://api.github.com/repos/$REPO/releases/tags/$Version"
}
else {
$apiUrl = "https://api.github.com/repos/$REPO/releases/latest"
}
$script:ReleaseData = Invoke-RestMethod -Uri $apiUrl
return $script:ReleaseData.tag_name
}
catch {
Write-Error-Custom "Failed to fetch release information: $_"
exit 1
}
}
function Get-AssetDigest {
param([string]$AssetName)
if (-not $script:ReleaseData) {
return $null
}
# Find the asset with matching name
$asset = $script:ReleaseData.assets | Where-Object { $_.name -eq $AssetName }
if (-not $asset) {
return $null
}
# GitHub returns digest in format "sha256:hash"
if ($asset.digest -and $asset.digest.StartsWith("sha256:")) {
return $asset.digest.Substring(7)
}
return $null
}
function Test-Checksum {
param(
[string]$FilePath,
[string]$ExpectedHash
)
if (-not $ExpectedHash) {
Write-Warning-Custom "No checksum available from GitHub API - skipping verification"
return $true
}
# Calculate actual hash
$actualHash = (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash.ToLower()
if ($ExpectedHash.ToLower() -ne $actualHash) {
Write-Error-Custom "Checksum verification failed!"
Write-Error-Custom "Expected: $ExpectedHash"
Write-Error-Custom "Actual: $actualHash"
return $false
}
return $true
}
function Main {
param([switch]$UserInstall)
Write-Host ""
Write-Host "========================================" -ForegroundColor Blue
Write-Host " dtvem installer" -ForegroundColor Blue
Write-Host "========================================" -ForegroundColor Blue
Write-Host ""
# Detect architecture
$PROCESSOR_ARCH = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
$ARCH = switch ($PROCESSOR_ARCH) {
"AMD64" { "amd64" }
"ARM64" { "arm64" }
default { "amd64" }
}
Write-Info "Detected platform: windows-$ARCH"
if ($env:DTVEM_ROOT) {
Write-Info "Using custom DTVEM_ROOT: $DTVEM_ROOT"
}
Write-Info "Install directory: $INSTALL_DIR"
# Determine version to install
$requestedVersion = $null
if ($env:DTVEM_VERSION) {
$requestedVersion = $env:DTVEM_VERSION
Write-Info "Installing user-specified version: $requestedVersion"
}
elseif ($DTVEM_RELEASE_VERSION) {
$requestedVersion = $DTVEM_RELEASE_VERSION
Write-Info "Installing release version: $requestedVersion"
}
else {
Write-Info "Fetching latest release..."
}
# Get release info from GitHub API
$VERSION = Get-ReleaseInfo -Version $requestedVersion
if (-not $VERSION) {
Write-Error-Custom "Failed to determine version"
exit 1
}
if (-not $requestedVersion) {
Write-Success "Latest version: $VERSION"
}
# Strip "v" prefix from version for archive name
$VERSION_NO_V = $VERSION.TrimStart('v')
# Construct download URL
$ARCHIVE_NAME = "dtvem-$VERSION_NO_V-windows-$ARCH.zip"
$DOWNLOAD_URL = "https://github.com/$REPO/releases/download/$VERSION/$ARCHIVE_NAME"
Write-Info "Download URL: $DOWNLOAD_URL"
# Get expected checksum from GitHub API
Write-Info "Fetching checksum from GitHub API..."
$EXPECTED_HASH = Get-AssetDigest -AssetName $ARCHIVE_NAME
if ($EXPECTED_HASH) {
Write-Success "Got checksum: $($EXPECTED_HASH.Substring(0, 16))..."
}
else {
Write-Warning-Custom "Checksum not available from API (may be an older release)"
}
# Create temporary directory
$TMP_DIR = Join-Path $env:TEMP "dtvem-install-$(Get-Random)"
New-Item -ItemType Directory -Path $TMP_DIR -Force | Out-Null
try {
# Download archive
Write-Info "Downloading dtvem..."
$ARCHIVE_PATH = Join-Path $TMP_DIR $ARCHIVE_NAME
try {
Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile $ARCHIVE_PATH -UseBasicParsing
Write-Success "Downloaded successfully"
}
catch {
Write-Error-Custom "Failed to download dtvem: $_"
Write-Error-Custom "URL: $DOWNLOAD_URL"
exit 1
}
# Verify checksum
Write-Info "Verifying checksum..."
if (-not (Test-Checksum -FilePath $ARCHIVE_PATH -ExpectedHash $EXPECTED_HASH)) {
Write-Error-Custom "Archive integrity check failed - aborting installation"
exit 1
}
Write-Success "Checksum verified"
# Extract archive
Write-Info "Extracting archive..."
Expand-Archive -Path $ARCHIVE_PATH -DestinationPath $TMP_DIR -Force
Write-Success "Extracted successfully"
# Create install directory
Write-Info "Installing to $INSTALL_DIR..."
New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null
# Install binaries
$dtvemExe = Join-Path $TMP_DIR "dtvem.exe"
$shimExe = Join-Path $TMP_DIR "dtvem-shim.exe"
if (Test-Path $dtvemExe) {
Copy-Item $dtvemExe -Destination $INSTALL_DIR -Force
}
else {
Write-Error-Custom "dtvem.exe not found in archive"
exit 1
}
if (Test-Path $shimExe) {
Copy-Item $shimExe -Destination $INSTALL_DIR -Force
}
else {
Write-Warning-Custom "dtvem-shim.exe not found in archive"
}
Write-Success "Installation complete!"
# Add install directory to PATH
Write-Host ""
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$INSTALL_DIR*") {
Write-Info "Adding $INSTALL_DIR to PATH..."
try {
# Add to BEGINNING of PATH for priority over system versions
$newPath = if ($userPath) { "$INSTALL_DIR;$userPath" } else { $INSTALL_DIR }
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
Write-Success "Added to PATH (at beginning for priority)"
Write-Warning-Custom "Please restart your terminal for PATH changes to take effect"
}
catch {
Write-Warning-Custom "Failed to add to PATH automatically: $_"
Write-Info "Please add manually: $INSTALL_DIR"
}
}
else {
Write-Info "$INSTALL_DIR is already in PATH"
}
# Run init to add shims directory to PATH
Write-Host ""
Write-Info "Running dtvem init to add shims directory to PATH..."
$dtvemPath = Join-Path $INSTALL_DIR "dtvem.exe"
try {
# Temporarily add to PATH for this session
$env:Path = "$INSTALL_DIR;$env:Path"
if ($UserInstall) {
Write-Info "Using user-level PATH (no admin required)"
& $dtvemPath init --user -y
}
else {
& $dtvemPath init
}
Write-Success "dtvem is ready to use!"
Write-Info "Both $INSTALL_DIR and $SHIMS_DIR have been added to PATH"
}
catch {
Write-Warning-Custom "dtvem init failed - you may need to run it manually after restarting your terminal"
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " Installation successful!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Info "Next steps:"
Write-Host " 1. Restart your terminal"
Write-Host " 2. Run: dtvem install python 3.11.0"
Write-Host " 3. Run: dtvem global python 3.11.0"
Write-Host ""
Write-Info "For help, run: dtvem help"
Write-Host ""
}
finally {
# Cleanup
if (Test-Path $TMP_DIR) {
Remove-Item -Path $TMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
Main -UserInstall:$UserInstall