Skip to content

Commit 5f9f67d

Browse files
committed
More shared functions, and a new Get-DEfaultInstallPath
1 parent 4e51fff commit 5f9f67d

File tree

1 file changed

+179
-161
lines changed

1 file changed

+179
-161
lines changed

eng/scripts/get-aspire-cli.ps1

Lines changed: 179 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,164 @@ function Get-RuntimeIdentifier {
368368
[string]$_Architecture
369369
)
370370

371-
$computedTargetOS = if ([string]::IsNullOrWhiteSpace($_OS)) { Get-OperatingSystem } else { $_OS }
371+
# Determine OS and architecture (either detected or user-specified)
372+
$computedTargetOS = if ([string]::IsNullOrWhiteSpace($_OS)) { $Script:HostOS } else { $_OS }
373+
374+
# Check for unsupported OS
372375
if ($computedTargetOS -eq "unsupported") {
373376
throw "Unsupported operating system. Current platform: $([System.Environment]::OSVersion.Platform)"
374377
}
378+
375379
$computedTargetArch = if ([string]::IsNullOrWhiteSpace($_Architecture)) { Get-CLIArchitectureFromArchitecture "<auto>" } else { Get-CLIArchitectureFromArchitecture $_Architecture }
380+
376381
return "${computedTargetOS}-${computedTargetArch}"
377382
}
378383

384+
function Expand-AspireCliArchive {
385+
param(
386+
[string]$ArchiveFile,
387+
[string]$DestinationPath
388+
)
389+
390+
Write-Message "Unpacking archive to: $DestinationPath" -Level Verbose
391+
392+
# Create destination directory if it doesn't exist
393+
if (-not (Test-Path $DestinationPath)) {
394+
Write-Message "Creating destination directory: $DestinationPath" -Level Verbose
395+
New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
396+
}
397+
398+
# Check archive format based on file extension and extract accordingly
399+
if ($ArchiveFile -match "\.zip$") {
400+
# Use Expand-Archive for ZIP files
401+
if (-not (Get-Command Expand-Archive -ErrorAction SilentlyContinue)) {
402+
throw "Expand-Archive cmdlet not found. Please use PowerShell 5.0 or later to extract ZIP files."
403+
}
404+
405+
try {
406+
Expand-Archive -Path $ArchiveFile -DestinationPath $DestinationPath -Force
407+
}
408+
catch {
409+
throw "Failed to unpack archive: $($_.Exception.Message)"
410+
}
411+
}
412+
elseif ($ArchiveFile -match "\.tar\.gz$") {
413+
# Use tar for tar.gz files
414+
if (-not (Get-Command tar -ErrorAction SilentlyContinue)) {
415+
throw "tar command not found. Please install tar to extract tar.gz files."
416+
}
417+
418+
$currentLocation = Get-Location
419+
try {
420+
Set-Location $DestinationPath
421+
& tar -xzf $ArchiveFile
422+
if ($LASTEXITCODE -ne 0) {
423+
throw "Failed to extract tar.gz archive: $ArchiveFile. tar command returned exit code $LASTEXITCODE"
424+
}
425+
}
426+
finally {
427+
Set-Location $currentLocation
428+
}
429+
}
430+
else {
431+
throw "Unsupported archive format: $ArchiveFile. Only .zip and .tar.gz files are supported."
432+
}
433+
434+
Write-Message "Successfully unpacked archive" -Level Verbose
435+
}
436+
437+
# Simplified installation path determination
438+
function Get-DefaultInstallPrefix {
439+
[CmdletBinding()]
440+
[OutputType([string])]
441+
param()
442+
443+
# Get home directory cross-platform
444+
$homeDirectory = Invoke-WithPowerShellVersion -ModernAction {
445+
if ($env:HOME) {
446+
$env:HOME
447+
} elseif ($IsWindows -and $env:USERPROFILE) {
448+
$env:USERPROFILE
449+
} elseif ($env:USERPROFILE) {
450+
$env:USERPROFILE
451+
} else {
452+
$null
453+
}
454+
} -LegacyAction {
455+
if ($env:USERPROFILE) {
456+
$env:USERPROFILE
457+
} elseif ($env:HOME) {
458+
$env:HOME
459+
} else {
460+
$null
461+
}
462+
}
463+
464+
if ([string]::IsNullOrWhiteSpace($homeDirectory)) {
465+
throw "Unable to determine user home directory. Please specify -InstallPath parameter."
466+
}
467+
468+
$defaultPath = Join-Path $homeDirectory ".aspire"
469+
return [System.IO.Path]::GetFullPath($defaultPath)
470+
}
471+
472+
# Simplified PATH environment update
473+
function Update-PathEnvironment {
474+
[CmdletBinding(SupportsShouldProcess)]
475+
param(
476+
[Parameter(Mandatory = $true)]
477+
[ValidateNotNullOrEmpty()]
478+
[string]$CliBinDir
479+
)
480+
481+
$pathSeparator = [System.IO.Path]::PathSeparator
482+
483+
# Update current session PATH
484+
$currentPathArray = $env:PATH.Split($pathSeparator, [StringSplitOptions]::RemoveEmptyEntries)
485+
if ($currentPathArray -notcontains $CliBinDir) {
486+
if ($PSCmdlet.ShouldProcess("PATH environment variable", "Add $CliBinDir to current session")) {
487+
$env:PATH = (@($CliBinDir) + $currentPathArray) -join $pathSeparator
488+
Write-Message "Added $CliBinDir to PATH for current session" -Level Info
489+
}
490+
}
491+
492+
# Update persistent PATH for Windows
493+
if ($Script:HostOS -eq "win") {
494+
try {
495+
$userPath = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User)
496+
if (-not $userPath) { $userPath = "" }
497+
$userPathArray = if ($userPath) { $userPath.Split($pathSeparator, [StringSplitOptions]::RemoveEmptyEntries) } else { @() }
498+
if ($userPathArray -notcontains $CliBinDir) {
499+
if ($PSCmdlet.ShouldProcess("User PATH environment variable", "Add $CliBinDir")) {
500+
$newUserPath = (@($CliBinDir) + $userPathArray) -join $pathSeparator
501+
[Environment]::SetEnvironmentVariable("PATH", $newUserPath, [EnvironmentVariableTarget]::User)
502+
Write-Message "Added $CliBinDir to user PATH environment variable" -Level Info
503+
}
504+
}
505+
506+
Write-Message "" -Level Info
507+
Write-Message "The aspire cli is now available for use in this and new sessions." -Level Success
508+
}
509+
catch {
510+
Write-Message "Failed to update persistent PATH environment variable: $($_.Exception.Message)" -Level Warning
511+
Write-Message "You may need to manually add $CliBinDir to your PATH environment variable" -Level Info
512+
}
513+
}
514+
515+
# GitHub Actions support
516+
if ($env:GITHUB_ACTIONS -eq "true" -and $env:GITHUB_PATH) {
517+
try {
518+
if ($PSCmdlet.ShouldProcess("GITHUB_PATH environment variable", "Add $CliBinDir to GITHUB_PATH")) {
519+
Add-Content -Path $env:GITHUB_PATH -Value $CliBinDir
520+
Write-Message "Added $CliBinDir to GITHUB_PATH for GitHub Actions" -Level Success
521+
}
522+
}
523+
catch {
524+
Write-Message "Failed to update GITHUB_PATH: $($_.Exception.Message)" -Level Warning
525+
}
526+
}
527+
}
528+
379529
# Function to create a temporary directory with conflict resolution
380530
function New-TempDirectory {
381531
[CmdletBinding(SupportsShouldProcess)]
@@ -438,6 +588,34 @@ function Remove-TempDirectory {
438588
}
439589
}
440590

591+
# =============================================================================
592+
# END: Shared code
593+
# =============================================================================
594+
595+
# Simplified installation path determination
596+
function Get-InstallPath {
597+
[CmdletBinding()]
598+
[OutputType([string])]
599+
param(
600+
[Parameter()]
601+
[string]$InstallPath
602+
)
603+
604+
if (-not [string]::IsNullOrWhiteSpace($InstallPath)) {
605+
# Validate that the path is not just whitespace and can be created
606+
try {
607+
$resolvedPath = [System.IO.Path]::GetFullPath($InstallPath)
608+
return $resolvedPath
609+
}
610+
catch {
611+
throw "Invalid installation path: $InstallPath - $($_.Exception.Message)"
612+
}
613+
}
614+
615+
$defaultPath = Join-Path (Get-DefaultInstallPrefix) "bin"
616+
return [System.IO.Path]::GetFullPath($defaultPath)
617+
}
618+
441619
function Get-ContentTypeFromUri {
442620
[CmdletBinding()]
443621
[OutputType([string])]
@@ -631,166 +809,6 @@ function Test-FileChecksum {
631809
}
632810
}
633811

634-
function Expand-AspireCliArchive {
635-
param(
636-
[string]$ArchiveFile,
637-
[string]$DestinationPath
638-
)
639-
640-
Write-Message "Unpacking archive to: $DestinationPath" -Level Verbose
641-
642-
try {
643-
# Create destination directory if it doesn't exist
644-
if (-not (Test-Path $DestinationPath)) {
645-
Write-Message "Creating destination directory: $DestinationPath" -Level Verbose
646-
New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
647-
}
648-
649-
if ($ArchiveFile -match "\.zip$") {
650-
if (-not (Get-Command Expand-Archive -ErrorAction SilentlyContinue)) {
651-
throw "Expand-Archive cmdlet not found. Please use PowerShell 5.0 or later to extract ZIP files."
652-
}
653-
654-
Expand-Archive -Path $ArchiveFile -DestinationPath $DestinationPath -Force
655-
}
656-
elseif ($ArchiveFile -match "\.tar\.gz$") {
657-
if (-not (Get-Command tar -ErrorAction SilentlyContinue)) {
658-
throw "tar command not found. Please install tar to extract tar.gz files."
659-
}
660-
661-
$currentLocation = Get-Location
662-
try {
663-
Set-Location $DestinationPath
664-
& tar -xzf $ArchiveFile
665-
if ($LASTEXITCODE -ne 0) {
666-
throw "Failed to extract tar.gz archive: $ArchiveFile. tar command returned exit code $LASTEXITCODE"
667-
}
668-
}
669-
finally {
670-
Set-Location $currentLocation
671-
}
672-
}
673-
else {
674-
throw "Unsupported archive format: $ArchiveFile. Only .zip and .tar.gz files are supported."
675-
}
676-
677-
Write-Message "Successfully unpacked archive" -Level Verbose
678-
}
679-
catch {
680-
throw "Failed to unpack archive: $($_.Exception.Message)"
681-
}
682-
}
683-
684-
# Simplified installation path determination
685-
function Get-InstallPath {
686-
[CmdletBinding()]
687-
[OutputType([string])]
688-
param(
689-
[Parameter()]
690-
[string]$InstallPath
691-
)
692-
693-
if (-not [string]::IsNullOrWhiteSpace($InstallPath)) {
694-
# Validate that the path is not just whitespace and can be created
695-
try {
696-
$resolvedPath = [System.IO.Path]::GetFullPath($InstallPath)
697-
return $resolvedPath
698-
}
699-
catch {
700-
throw "Invalid installation path: $InstallPath - $($_.Exception.Message)"
701-
}
702-
}
703-
704-
# Get home directory cross-platform
705-
$homeDirectory = Invoke-WithPowerShellVersion -ModernAction {
706-
if ($env:HOME) {
707-
$env:HOME
708-
} elseif ($IsWindows -and $env:USERPROFILE) {
709-
$env:USERPROFILE
710-
} elseif ($env:USERPROFILE) {
711-
$env:USERPROFILE
712-
} else {
713-
$null
714-
}
715-
} -LegacyAction {
716-
if ($env:USERPROFILE) {
717-
$env:USERPROFILE
718-
} elseif ($env:HOME) {
719-
$env:HOME
720-
} else {
721-
$null
722-
}
723-
}
724-
725-
if ([string]::IsNullOrWhiteSpace($homeDirectory)) {
726-
throw "Unable to determine user home directory. Please specify -InstallPath parameter."
727-
}
728-
729-
$defaultPath = Join-Path (Join-Path $homeDirectory ".aspire") "bin"
730-
return [System.IO.Path]::GetFullPath($defaultPath)
731-
}
732-
733-
# Simplified PATH environment update
734-
function Update-PathEnvironment {
735-
[CmdletBinding(SupportsShouldProcess)]
736-
param(
737-
[Parameter(Mandatory = $true)]
738-
[ValidateNotNullOrEmpty()]
739-
[string]$InstallPath,
740-
741-
[Parameter(Mandatory = $true)]
742-
[ValidateSet("win", "linux", "linux-musl", "osx")]
743-
[string]$TargetOS
744-
)
745-
746-
$pathSeparator = [System.IO.Path]::PathSeparator
747-
748-
# Update current session PATH
749-
$currentPathArray = $env:PATH.Split($pathSeparator, [StringSplitOptions]::RemoveEmptyEntries)
750-
if ($currentPathArray -notcontains $InstallPath) {
751-
if ($PSCmdlet.ShouldProcess("PATH environment variable", "Add $InstallPath to current session")) {
752-
$env:PATH = (@($InstallPath) + $currentPathArray) -join $pathSeparator
753-
Write-Message "Added $InstallPath to PATH for current session" -Level Info
754-
}
755-
}
756-
757-
# Update persistent PATH for Windows
758-
if ($TargetOS -eq "win") {
759-
try {
760-
$userPath = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User)
761-
if (-not $userPath) { $userPath = "" }
762-
$userPathArray = if ($userPath) { $userPath.Split($pathSeparator, [StringSplitOptions]::RemoveEmptyEntries) } else { @() }
763-
if ($userPathArray -notcontains $InstallPath) {
764-
if ($PSCmdlet.ShouldProcess("User PATH environment variable", "Add $InstallPath")) {
765-
$newUserPath = (@($InstallPath) + $userPathArray) -join $pathSeparator
766-
[Environment]::SetEnvironmentVariable("PATH", $newUserPath, [EnvironmentVariableTarget]::User)
767-
Write-Message "Added $InstallPath to user PATH environment variable" -Level Info
768-
}
769-
}
770-
771-
Write-Message "" -Level Info
772-
Write-Message "The aspire cli is now available for use in this and new sessions." -Level Success
773-
}
774-
catch {
775-
Write-Message "Failed to update persistent PATH environment variable: $($_.Exception.Message)" -Level Warning
776-
Write-Message "You may need to manually add $InstallPath to your PATH environment variable" -Level Info
777-
}
778-
}
779-
780-
# GitHub Actions support
781-
if ($env:GITHUB_ACTIONS -eq "true" -and $env:GITHUB_PATH) {
782-
try {
783-
if ($PSCmdlet.ShouldProcess("GITHUB_PATH environment variable", "Add $InstallPath to GITHUB_PATH")) {
784-
Add-Content -Path $env:GITHUB_PATH -Value $InstallPath
785-
Write-Message "Added $InstallPath to GITHUB_PATH for GitHub Actions" -Level Success
786-
}
787-
}
788-
catch {
789-
Write-Message "Failed to update GITHUB_PATH: $($_.Exception.Message)" -Level Warning
790-
}
791-
}
792-
}
793-
794812
# Enhanced URL construction function with configuration-based URLs
795813
function Get-AspireCliUrl {
796814
[CmdletBinding()]

0 commit comments

Comments
 (0)