diff --git a/eng/common/mcp/azure-sdk-mcp.ps1 b/eng/common/mcp/azure-sdk-mcp.ps1 index b56fb4e96..af6f342a3 100755 --- a/eng/common/mcp/azure-sdk-mcp.ps1 +++ b/eng/common/mcp/azure-sdk-mcp.ps1 @@ -1,5 +1,8 @@ #!/bin/env pwsh +#Requires -Version 7.0 +#Requires -PSEdition Core + param( [string]$FileName = 'Azure.Sdk.Tools.Cli', [string]$Package = 'azsdk', @@ -9,21 +12,13 @@ param( [string]$RunDirectory = (Resolve-Path (Join-Path $PSScriptRoot .. .. ..)), [switch]$Run, [switch]$UpdateVsCodeConfig, - [switch]$Clean + [switch]$UpdatePathInProfile ) $ErrorActionPreference = "Stop" - -if (-not $InstallDirectory) -{ - $homeDir = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE } - $InstallDirectory = (Join-Path $homeDir ".azure-sdk-mcp" "azsdk") -} . (Join-Path $PSScriptRoot '..' 'scripts' 'Helpers' 'AzSdkTool-Helpers.ps1') -if ($Clean) { - Clear-Directory -Path $InstallDirectory -} +$toolInstallDirectory = $InstallDirectory ? $InstallDirectory : (Get-CommonInstallDirectory) if ($UpdateVsCodeConfig) { $vscodeConfigPath = Join-Path $PSScriptRoot ".." ".." ".." ".vscode" "mcp.json" @@ -54,13 +49,30 @@ if ($UpdateVsCodeConfig) { $vscodeConfig | ConvertTo-Json -Depth 10 | Set-Content -Path $vscodeConfigPath -Force } -$exe = Install-Standalone-Tool ` +# Install to a temp directory first so we don't dump out all the other +# release zip contents to one of the users bin directories. +$tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath() +$guid = [System.Guid]::NewGuid() +$tempInstallDirectory = Join-Path $tmp "azsdk-install-$($guid)" +$tempExe = Install-Standalone-Tool ` -Version $Version ` -FileName $FileName ` -Package $Package ` - -Directory $InstallDirectory ` + -Directory $tempInstallDirectory ` -Repository $Repository +Copy-Item -Path $tempExe -Destination $toolInstallDirectory -Force +$exeName = Split-Path $tempExe -Leaf +$exe = Join-Path $toolInstallDirectory $exeName + +Write-Host "Package $package is installed at $exe" +if (!$UpdatePathInProfile) { + Write-Warning "To add the tool to PATH for new shell sessions, re-run with -UpdatePathInProfile to modify the shell profile file." +} else { + Add-InstallDirectoryToPathInProfile -InstallDirectory $toolInstallDirectory + Write-Warning "'$exeName' will be available in PATH for new shell sessions." +} + if ($Run) { Start-Process -WorkingDirectory $RunDirectory -FilePath $exe -ArgumentList 'start' -NoNewWindow -Wait } diff --git a/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 b/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 index 4fa981196..ed71854ec 100644 --- a/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 +++ b/eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1 @@ -192,3 +192,61 @@ function Install-Standalone-Tool ( return $executable_path } + +function Get-CommonInstallDirectory { + $installDirectory = Join-Path $HOME "bin" + if (-not (Test-Path $installDirectory)) { + New-Item -ItemType Directory -Path $installDirectory -Force | Out-Null + } + + # Update PATH in current session + if (-not ($env:PATH -like "*$InstallDirectory*")) { + $env:PATH += ";$InstallDirectory" + } + + return $installDirectory +} + +function Add-InstallDirectoryToPathInProfile( + [Parameter()] + [string]$InstallDirectory = (Get-CommonInstallDirectory) +) { + $powershellProfilePath = $PROFILE + $bashrcPath = Join-Path $HOME ".bashrc" + $zshrcPath = Join-Path $HOME ".zshrc" + $markerComment = " # azsdk install path" + $pathCommand = "" + $configFile = "" + + if ($IsWindows) { # expect powershell for windows, cmd.exe path update is not currently supported + $configFile = $powershellProfilePath + $pathCommand = "if (-not (`$env:PATH -like `'*$InstallDirectory*`')) { `$env:PATH += ';$InstallDirectory`' }" + $markerComment + } + elseif ($IsLinux) { # handle bash or zsh shells for linux + if (Test-Path $zshrcPath) { + $configFile = $zshrcPath + } + else { + $configFile = $bashrcPath + } + $pathCommand = "export PATH=`"`$PATH:$InstallDirectory`"" + $markerComment + } + elseif ($IsMacOS) { # mac os should use zsh by default + $configFile = $zshrcPath + $pathCommand = "export PATH=`"`$PATH:$InstallDirectory`"" + $markerComment + } + else { + throw "Unsupported platform" + } + + if (-not (Test-Path $configFile)) { + New-Item -ItemType File -Path $configFile -Force | Out-Null + } + + $configContent = Get-Content $configFile -Raw + + if (!$configContent -or !$configContent.Contains($markerComment)) { + Write-Host "Adding installation to PATH in shell profile at '$configFile'" + Add-Content -Path $configFile -Value $pathCommand + } +} \ No newline at end of file