Skip to content

Commit c45c090

Browse files
committed
ps1: cleanup, and fix up after the shared code move
1 parent 5f9f67d commit c45c090

File tree

1 file changed

+48
-57
lines changed

1 file changed

+48
-57
lines changed

eng/scripts/get-aspire-cli.ps1

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
.\get-aspire-cli.ps1
3939
4040
.EXAMPLE
41-
.\get-aspire-cli.ps1 -InstallPath "C:\\tools\\aspire"
41+
.\get-aspire-cli.ps1 -InstallPath "C:\tools\aspire"
4242
4343
.EXAMPLE
4444
.\get-aspire-cli.ps1 -Quality "staging"
@@ -104,6 +104,7 @@ $Script:UserAgent = "get-aspire-cli.ps1/1.0"
104104
$Script:IsModernPowerShell = $PSVersionTable.PSVersion.Major -ge 6 -and $PSVersionTable.PSEdition -eq "Core"
105105
$Script:ArchiveDownloadTimeoutSec = 600
106106
$Script:ChecksumDownloadTimeoutSec = 120
107+
$Script:HostOS = "unset"
107108

108109
# Configuration constants
109110
$Script:Config = @{
@@ -125,20 +126,9 @@ $Script:Config = @{
125126
# False if the body is piped / dot‑sourced / iex’d into the current session.
126127
$InvokedFromFile = -not [string]::IsNullOrEmpty($PSCommandPath)
127128

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+
# =============================================================================
142132

143133
# Consolidated output function with fallback for platforms that don't support Write-Host
144134
function Write-Message {
@@ -184,8 +174,6 @@ function Write-Message {
184174
}
185175
}
186176

187-
## Help is provided via the comment-based help block above; use Get-Help to view.
188-
189177
# Helper function for PowerShell version-specific operations
190178
function Invoke-WithPowerShellVersion {
191179
[CmdletBinding()]
@@ -210,6 +198,7 @@ function Get-OperatingSystem {
210198
[OutputType([string])]
211199
param()
212200

201+
Write-Message "Detecting OS" -Level Verbose
213202
try {
214203
return Invoke-WithPowerShellVersion -ModernAction {
215204
if ($IsWindows) {
@@ -337,8 +326,6 @@ function Get-CLIArchitectureFromArchitecture {
337326
[string]$Architecture
338327
)
339328

340-
Write-Message "Converting architecture: $Architecture" -Level Verbose
341-
342329
if ($Architecture -eq "<auto>") {
343330
$Architecture = Get-MachineArchitecture
344331
}
@@ -537,8 +524,11 @@ function New-TempDirectory {
537524
)
538525

539526
if ($PSCmdlet.ShouldProcess("temporary directory", "Create temporary directory with prefix '$Prefix'")) {
527+
# Create a temporary directory for downloads with conflict resolution
540528
$tempBaseName = "$Prefix-$([System.Guid]::NewGuid().ToString("N").Substring(0, 8))"
541529
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) $tempBaseName
530+
531+
# Handle potential conflicts
542532
$attempt = 1
543533
while (Test-Path $tempDir) {
544534
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "$tempBaseName-$attempt"
@@ -558,6 +548,7 @@ function New-TempDirectory {
558548
}
559549
}
560550
else {
551+
# Return a WhatIf path when -WhatIf is used
561552
return Join-Path ([System.IO.Path]::GetTempPath()) "$Prefix-whatif"
562553
}
563554
}
@@ -740,10 +731,6 @@ function Invoke-SecureWebRequest {
740731
}
741732
}
742733

743-
# ==========================
744-
# End shared helpers block
745-
# ==========================
746-
747734
# Enhanced file download wrapper with validation
748735
function Invoke-FileDownload {
749736
[CmdletBinding()]
@@ -869,21 +856,18 @@ function Install-AspireCli {
869856
[OutputType([string])]
870857
param(
871858
[Parameter(Mandatory = $true)]
872-
[string]$InstallPath,
859+
[string]$CliBinDir,
873860
[string]$Version,
874861
[string]$Quality,
875-
[string]$OS,
876-
[string]$Architecture
862+
[string]$TargetRID
877863
)
878864

865+
$tempDir = $null
879866
$tempDir = New-TempDirectory -Prefix "aspire-cli-download"
880867

881868
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
887871

888872
$archivePath = Join-Path $tempDir $urls.ArchiveFilename
889873
$checksumPath = Join-Path $tempDir $urls.ChecksumFilename
@@ -902,35 +886,18 @@ function Install-AspireCli {
902886
Write-Message "Successfully downloaded and validated: $($urls.ArchiveFilename)" -Level Verbose
903887
}
904888

905-
if ($PSCmdlet.ShouldProcess($InstallPath, "Install CLI")) {
889+
if ($PSCmdlet.ShouldProcess($CliBinDir, "Install CLI")) {
906890
# Unpack the archive
907-
Expand-AspireCliArchive -ArchiveFile $archivePath -DestinationPath $InstallPath
891+
Expand-AspireCliArchive -ArchiveFile $archivePath -DestinationPath $CliBinDir
908892

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
911895

912896
Write-Message "Aspire CLI successfully installed to: $cliPath" -Level Success
913897
}
914-
915-
# Return the target OS for the caller to use
916-
return $targetOS
917898
}
918899
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
934901
}
935902
}
936903

@@ -976,15 +943,27 @@ function Start-AspireCliInstallation {
976943
}
977944
}
978945

946+
$rid = Get-RuntimeIdentifier -_OS $OS -_Architecture $Architecture
947+
979948
# 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
981950

982951
# Update PATH environment variables
983-
Update-PathEnvironment -InstallPath $resolvedInstallPath -TargetOS $targetOS
952+
Update-PathEnvironment -CliBinDir $resolvedInstallPath
984953
}
985954
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
988967
if ($InvokedFromFile) {
989968
exit 1
990969
} else {
@@ -993,13 +972,25 @@ function Start-AspireCliInstallation {
993972
}
994973
}
995974

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+
996986
# Run main function and handle exit code
997987
try {
998988
# Ensure we're not in strict mode which can cause issues in PowerShell 5.1
999989
if (-not $Script:IsModernPowerShell) {
1000990
Set-StrictMode -Off
1001991
}
1002992

993+
$script:HostOS = Get-OperatingSystem
1003994
Start-AspireCliInstallation
1004995
$exitCode = 0
1005996
}

0 commit comments

Comments
 (0)