Skip to content

Commit b82f77a

Browse files
committed
get-aspire-cli.ps1: add helper functions (New-TempDirectory, Remove-TempDirectory, Get-RuntimeIdentifier) and use them in Install-AspireCli
1 parent f2fb695 commit b82f77a

File tree

1 file changed

+82
-35
lines changed

1 file changed

+82
-35
lines changed

eng/scripts/get-aspire-cli.ps1

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,84 @@ function Get-CLIArchitectureFromArchitecture {
356356
}
357357
}
358358

359+
function Get-RuntimeIdentifier {
360+
[CmdletBinding()]
361+
[OutputType([object])]
362+
param(
363+
[string]$_OS,
364+
[string]$_Architecture
365+
)
366+
367+
$computedTargetOS = if ([string]::IsNullOrWhiteSpace($_OS)) { Get-OperatingSystem } else { $_OS }
368+
if ($computedTargetOS -eq "unsupported") {
369+
throw "Unsupported operating system. Current platform: $([System.Environment]::OSVersion.Platform)"
370+
}
371+
$computedTargetArch = if ([string]::IsNullOrWhiteSpace($_Architecture)) { Get-CLIArchitectureFromArchitecture "<auto>" } else { Get-CLIArchitectureFromArchitecture $_Architecture }
372+
return "${computedTargetOS}-${computedTargetArch}"
373+
}
374+
375+
# Function to create a temporary directory with conflict resolution
376+
function New-TempDirectory {
377+
[CmdletBinding(SupportsShouldProcess)]
378+
[OutputType([string])]
379+
param(
380+
[Parameter(Mandatory = $true)]
381+
[ValidateNotNullOrEmpty()]
382+
[string]$Prefix
383+
)
384+
385+
if ($PSCmdlet.ShouldProcess("temporary directory", "Create temporary directory with prefix '$Prefix'")) {
386+
$tempBaseName = "$Prefix-$([System.Guid]::NewGuid().ToString("N").Substring(0, 8))"
387+
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) $tempBaseName
388+
$attempt = 1
389+
while (Test-Path $tempDir) {
390+
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "$tempBaseName-$attempt"
391+
$attempt++
392+
if ($attempt -gt 10) {
393+
throw "Unable to create temporary directory after 10 attempts"
394+
}
395+
}
396+
397+
Write-Message "Creating temporary directory: $tempDir" -Level Verbose
398+
try {
399+
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
400+
return $tempDir
401+
}
402+
catch {
403+
throw "Failed to create temporary directory: $tempDir - $($_.Exception.Message)"
404+
}
405+
}
406+
else {
407+
return Join-Path ([System.IO.Path]::GetTempPath()) "$Prefix-whatif"
408+
}
409+
}
410+
411+
# Cleanup function for temporary directory
412+
function Remove-TempDirectory {
413+
[CmdletBinding(SupportsShouldProcess)]
414+
param(
415+
[Parameter()]
416+
[string]$TempDir
417+
)
418+
419+
if (-not [string]::IsNullOrWhiteSpace($TempDir) -and (Test-Path $TempDir)) {
420+
if (-not $KeepArchive) {
421+
Write-Message "Cleaning up temporary files..." -Level Verbose
422+
try {
423+
if ($PSCmdlet.ShouldProcess($TempDir, "Remove temporary directory")) {
424+
Remove-Item $TempDir -Recurse -Force
425+
}
426+
}
427+
catch {
428+
Write-Message "Failed to clean up temporary directory: $TempDir - $($_.Exception.Message)" -Level Warning
429+
}
430+
}
431+
else {
432+
Write-Message "Archive files kept in: $TempDir" -Level Info
433+
}
434+
}
435+
}
436+
359437
function Get-ContentTypeFromUri {
360438
[CmdletBinding()]
361439
[OutputType([string])]
@@ -769,43 +847,12 @@ function Install-AspireCli {
769847
[string]$Architecture
770848
)
771849

772-
# Create a temporary directory for downloads with conflict resolution
773-
$tempBaseName = "aspire-cli-download-$([System.Guid]::NewGuid().ToString("N").Substring(0, 8))"
774-
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) $tempBaseName
775-
776-
# Handle potential conflicts
777-
$attempt = 1
778-
while (Test-Path $tempDir) {
779-
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "$tempBaseName-$attempt"
780-
$attempt++
781-
if ($attempt -gt 10) {
782-
throw "Unable to create temporary directory after 10 attempts"
783-
}
784-
}
785-
786-
if ($PSCmdlet.ShouldProcess($InstallPath, "Create temporary directory")) {
787-
Write-Message "Creating temporary directory: $tempDir" -Level Verbose
788-
try {
789-
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
790-
}
791-
catch {
792-
throw "Failed to create temporary directory: $tempDir - $($_.Exception.Message)"
793-
}
794-
}
850+
$tempDir = New-TempDirectory -Prefix "aspire-cli-download"
795851

796852
try {
797-
# Determine OS and architecture (either detected or user-specified)
798-
$targetOS = if ([string]::IsNullOrWhiteSpace($OS)) { Get-OperatingSystem } else { $OS }
799-
800-
# Check for unsupported OS
801-
if ($targetOS -eq "unsupported") {
802-
throw "Unsupported operating system. Current platform: $([System.Environment]::OSVersion.Platform)"
803-
}
804-
805-
$targetArch = if ([string]::IsNullOrWhiteSpace($Architecture)) { Get-CLIArchitectureFromArchitecture "<auto>" } else { Get-CLIArchitectureFromArchitecture $Architecture }
806-
807-
# Construct the runtime identifier and URLs
808-
$runtimeIdentifier = "$targetOS-$targetArch"
853+
# Determine runtime identifier and URLs
854+
$runtimeIdentifier = Get-RuntimeIdentifier -_OS $OS -_Architecture $Architecture
855+
$targetOS = $runtimeIdentifier.Split('-')[0]
809856
$extension = if ($targetOS -eq "win") { "zip" } else { "tar.gz" }
810857
$urls = Get-AspireCliUrl -Version $Version -Quality $Quality -RuntimeIdentifier $runtimeIdentifier -Extension $extension
811858

0 commit comments

Comments
 (0)