38
38
.\get-aspire-cli.ps1
39
39
40
40
. EXAMPLE
41
- .\get-aspire-cli.ps1 -InstallPath "C:\\ tools\ \aspire"
41
+ .\get-aspire-cli.ps1 -InstallPath "C:\tools\aspire"
42
42
43
43
. EXAMPLE
44
44
.\get-aspire-cli.ps1 -Quality "staging"
@@ -104,6 +104,7 @@ $Script:UserAgent = "get-aspire-cli.ps1/1.0"
104
104
$Script :IsModernPowerShell = $PSVersionTable.PSVersion.Major -ge 6 -and $PSVersionTable.PSEdition -eq " Core"
105
105
$Script :ArchiveDownloadTimeoutSec = 600
106
106
$Script :ChecksumDownloadTimeoutSec = 120
107
+ $Script :HostOS = " unset"
107
108
108
109
# Configuration constants
109
110
$Script :Config = @ {
@@ -125,20 +126,9 @@ $Script:Config = @{
125
126
# False if the body is piped / dot‑sourced / iex’d into the current session.
126
127
$InvokedFromFile = -not [string ]::IsNullOrEmpty($PSCommandPath )
127
128
128
- # Ensure minimum PowerShell version
129
- if ($PSVersionTable.PSVersion.Major -lt $Script :Config.MinimumPowerShellVersion ) {
130
- Write-Message " Error: This script requires PowerShell $ ( $Script :Config.MinimumPowerShellVersion ) .0 or later. Current version: $ ( $PSVersionTable.PSVersion ) " - Level Error
131
- if ($InvokedFromFile ) {
132
- exit 1
133
- }
134
- else {
135
- return 1
136
- }
137
- }
138
-
139
- # =====================
140
- # Shared helpers block
141
- # =====================
129
+ # =============================================================================
130
+ # START: Shared code
131
+ # =============================================================================
142
132
143
133
# Consolidated output function with fallback for platforms that don't support Write-Host
144
134
function Write-Message {
@@ -184,8 +174,6 @@ function Write-Message {
184
174
}
185
175
}
186
176
187
- # # Help is provided via the comment-based help block above; use Get-Help to view.
188
-
189
177
# Helper function for PowerShell version-specific operations
190
178
function Invoke-WithPowerShellVersion {
191
179
[CmdletBinding ()]
@@ -210,6 +198,7 @@ function Get-OperatingSystem {
210
198
[OutputType ([string ])]
211
199
param ()
212
200
201
+ Write-Message " Detecting OS" - Level Verbose
213
202
try {
214
203
return Invoke-WithPowerShellVersion - ModernAction {
215
204
if ($IsWindows ) {
@@ -337,8 +326,6 @@ function Get-CLIArchitectureFromArchitecture {
337
326
[string ]$Architecture
338
327
)
339
328
340
- Write-Message " Converting architecture: $Architecture " - Level Verbose
341
-
342
329
if ($Architecture -eq " <auto>" ) {
343
330
$Architecture = Get-MachineArchitecture
344
331
}
@@ -537,8 +524,11 @@ function New-TempDirectory {
537
524
)
538
525
539
526
if ($PSCmdlet.ShouldProcess (" temporary directory" , " Create temporary directory with prefix '$Prefix '" )) {
527
+ # Create a temporary directory for downloads with conflict resolution
540
528
$tempBaseName = " $Prefix -$ ( [System.Guid ]::NewGuid().ToString(" N" ).Substring(0 , 8 )) "
541
529
$tempDir = Join-Path ([System.IO.Path ]::GetTempPath()) $tempBaseName
530
+
531
+ # Handle potential conflicts
542
532
$attempt = 1
543
533
while (Test-Path $tempDir ) {
544
534
$tempDir = Join-Path ([System.IO.Path ]::GetTempPath()) " $tempBaseName -$attempt "
@@ -558,6 +548,7 @@ function New-TempDirectory {
558
548
}
559
549
}
560
550
else {
551
+ # Return a WhatIf path when -WhatIf is used
561
552
return Join-Path ([System.IO.Path ]::GetTempPath()) " $Prefix -whatif"
562
553
}
563
554
}
@@ -740,10 +731,6 @@ function Invoke-SecureWebRequest {
740
731
}
741
732
}
742
733
743
- # ==========================
744
- # End shared helpers block
745
- # ==========================
746
-
747
734
# Enhanced file download wrapper with validation
748
735
function Invoke-FileDownload {
749
736
[CmdletBinding ()]
@@ -869,21 +856,18 @@ function Install-AspireCli {
869
856
[OutputType ([string ])]
870
857
param (
871
858
[Parameter (Mandatory = $true )]
872
- [string ]$InstallPath ,
859
+ [string ]$CliBinDir ,
873
860
[string ]$Version ,
874
861
[string ]$Quality ,
875
- [string ]$OS ,
876
- [string ]$Architecture
862
+ [string ]$TargetRID
877
863
)
878
864
865
+ $tempDir = $null
879
866
$tempDir = New-TempDirectory - Prefix " aspire-cli-download"
880
867
881
868
try {
882
- # Determine runtime identifier and URLs
883
- $runtimeIdentifier = Get-RuntimeIdentifier - _OS $OS - _Architecture $Architecture
884
- $targetOS = $runtimeIdentifier.Split (' -' )[0 ]
885
- $extension = if ($targetOS -eq " win" ) { " zip" } else { " tar.gz" }
886
- $urls = Get-AspireCliUrl - Version $Version - Quality $Quality - RuntimeIdentifier $runtimeIdentifier - Extension $extension
869
+ $extension = if ($TargetRID.StartsWith (" win-" )) { " zip" } else { " tar.gz" }
870
+ $urls = Get-AspireCliUrl - Version $Version - Quality $Quality - RuntimeIdentifier $TargetRID - Extension $extension
887
871
888
872
$archivePath = Join-Path $tempDir $urls.ArchiveFilename
889
873
$checksumPath = Join-Path $tempDir $urls.ChecksumFilename
@@ -902,35 +886,18 @@ function Install-AspireCli {
902
886
Write-Message " Successfully downloaded and validated: $ ( $urls.ArchiveFilename ) " - Level Verbose
903
887
}
904
888
905
- if ($PSCmdlet.ShouldProcess ($InstallPath , " Install CLI" )) {
889
+ if ($PSCmdlet.ShouldProcess ($CliBinDir , " Install CLI" )) {
906
890
# Unpack the archive
907
- Expand-AspireCliArchive - ArchiveFile $archivePath - DestinationPath $InstallPath
891
+ Expand-AspireCliArchive - ArchiveFile $archivePath - DestinationPath $CliBinDir
908
892
909
- $cliExe = if ($targetOS -eq " win" ) { " aspire.exe" } else { " aspire" }
910
- $cliPath = Join-Path $InstallPath $cliExe
893
+ $cliExe = if ($TargetRID .StartsWith ( " win- " ) ) { " aspire.exe" } else { " aspire" }
894
+ $cliPath = Join-Path $CliBinDir $cliExe
911
895
912
896
Write-Message " Aspire CLI successfully installed to: $cliPath " - Level Success
913
897
}
914
-
915
- # Return the target OS for the caller to use
916
- return $targetOS
917
898
}
918
899
finally {
919
- # Clean up temporary directory and downloaded files
920
- if (Test-Path $tempDir - ErrorAction SilentlyContinue) {
921
- if (-not $KeepArchive ) {
922
- try {
923
- Write-Message " Cleaning up temporary files..." - Level Verbose
924
- Remove-Item $tempDir - Recurse - Force - ErrorAction Stop
925
- }
926
- catch {
927
- Write-Message " Failed to clean up temporary directory: $tempDir - $ ( $_.Exception.Message ) " - Level Warning
928
- }
929
- }
930
- else {
931
- Write-Message " Archive files kept in: $tempDir " - Level Info
932
- }
933
- }
900
+ Remove-TempDirectory - TempDir $tempDir
934
901
}
935
902
}
936
903
@@ -976,15 +943,27 @@ function Start-AspireCliInstallation {
976
943
}
977
944
}
978
945
946
+ $rid = Get-RuntimeIdentifier - _OS $OS - _Architecture $Architecture
947
+
979
948
# Download and install the Aspire CLI
980
- $targetOS = Install-AspireCli - InstallPath $resolvedInstallPath - Version $Version - Quality $Quality - OS $OS - Architecture $Architecture
949
+ Install-AspireCli - CliBinDir $resolvedInstallPath - Version $Version - Quality $Quality - TargetRID $rid
981
950
982
951
# Update PATH environment variables
983
- Update-PathEnvironment - InstallPath $resolvedInstallPath - TargetOS $targetOS
952
+ Update-PathEnvironment - CliBinDir $resolvedInstallPath
984
953
}
985
954
catch {
986
- # Display clean error message without stack trace
987
- Write-Message " Error: $ ( $_.Exception.Message ) " - Level Error
955
+ # Log the full exception details if verbose
956
+ Write-Verbose " Full exception details: $ ( $_.Exception | Out-String ) "
957
+
958
+ # Show clean message to user
959
+ $cleanMessage = switch ($_.Exception.GetType ().Name) {
960
+ ' ArgumentException' { " Invalid argument: $ ( $_.Exception.Message ) " }
961
+ ' UnauthorizedAccessException' { " Access denied: $ ( $_.Exception.Message ) " }
962
+ ' ParameterBindingValidationException' { " Parameter validation failed: $ ( $_.Exception.Message ) " }
963
+ default { $_.Exception.Message }
964
+ }
965
+
966
+ Write-Message " Error: $cleanMessage " - Level Error
988
967
if ($InvokedFromFile ) {
989
968
exit 1
990
969
} else {
@@ -993,13 +972,25 @@ function Start-AspireCliInstallation {
993
972
}
994
973
}
995
974
975
+ # Ensure minimum PowerShell version
976
+ if ($PSVersionTable.PSVersion.Major -lt $Script :Config.MinimumPowerShellVersion ) {
977
+ Write-Message " Error: This script requires PowerShell $ ( $Script :Config.MinimumPowerShellVersion ) .0 or later. Current version: $ ( $PSVersionTable.PSVersion ) " - Level Error
978
+ if ($InvokedFromFile ) {
979
+ exit 1
980
+ }
981
+ else {
982
+ return 1
983
+ }
984
+ }
985
+
996
986
# Run main function and handle exit code
997
987
try {
998
988
# Ensure we're not in strict mode which can cause issues in PowerShell 5.1
999
989
if (-not $Script :IsModernPowerShell ) {
1000
990
Set-StrictMode - Off
1001
991
}
1002
992
993
+ $script :HostOS = Get-OperatingSystem
1003
994
Start-AspireCliInstallation
1004
995
$exitCode = 0
1005
996
}
0 commit comments