diff --git a/eng/common/mcp/azure-sdk-mcp.ps1 b/eng/common/mcp/azure-sdk-mcp.ps1 index 6a3c23ee6eaf..3026895afe6d 100755 --- a/eng/common/mcp/azure-sdk-mcp.ps1 +++ b/eng/common/mcp/azure-sdk-mcp.ps1 @@ -4,8 +4,6 @@ #Requires -PSEdition Core param( - [string]$FileName = 'Azure.Sdk.Tools.Cli', - [string]$Package = 'azsdk', [string]$Version, # Default to latest [string]$InstallDirectory = '', [string]$Repository = 'Azure/azure-sdk-tools', @@ -15,11 +13,14 @@ param( [switch]$UpdatePathInProfile ) -$ErrorActionPreference = "Stop" +$ErrorActionPreference = 'Stop' . (Join-Path $PSScriptRoot '..' 'scripts' 'Helpers' 'AzSdkTool-Helpers.ps1') $toolInstallDirectory = $InstallDirectory ? $InstallDirectory : (Get-CommonInstallDirectory) +$packageName = 'azsdk' +$packageFileName = 'Azure.Sdk.Tools.Cli' + $mcpMode = $Run # Log to console or MCP client json-rpc @@ -103,14 +104,35 @@ $tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath() $guid = [System.Guid]::NewGuid() $tempInstallDirectory = Join-Path $tmp "azsdk-install-$($guid)" +# If already installed, use first class version mechanism +$azsdkCmd = Get-Command -ErrorAction SilentlyContinue $packageName +if ($azsdkCmd -and !$InstallDirectory) { + $ErrorActionPreference = "Stop" + $upgrade = & $packageName upgrade --check --output json | out-string + if (!$LASTEXITCODE) { + $ErrorActionPreference = 'Ignore' + $localVersion = $upgrade | ConvertFrom-Json -AsHashtable + $ErrorActionPreference = 'Stop' + if ($localVersion.old_version -and $localVersion.old_version -eq ($Version ? $Version : $localVersion.new_version)) { + log "Version up to date at $($localVersion.old_version)" + if ($Run) { + $proc = Start-Process -PassThru -WorkingDirectory $RunDirectory -FilePath $azsdkCmd.Path -ArgumentList 'mcp' -NoNewWindow -Wait + exit $proc.ExitCode + } + exit 0 + } + log "Version not up to date at " + $localVersion.old_version + } +} + if ($mcpMode) { try { # Swallow all output and re-log so we can wrap any # output from the inner function as json-rpc $tempExe = Install-Standalone-Tool ` -Version $Version ` - -FileName $FileName ` - -Package $Package ` + -FileName $packageFileName ` + -Package $packageName ` -Directory $tempInstallDirectory ` -Repository $Repository ` *>&1 @@ -126,8 +148,8 @@ if ($mcpMode) { else { $tempExe = Install-Standalone-Tool ` -Version $Version ` - -FileName $FileName ` - -Package $Package ` + -FileName $packageFileName ` + -Package $packageName ` -Directory $tempInstallDirectory ` -Repository $Repository ` @@ -164,7 +186,7 @@ if (Test-Path $tempInstallDirectory) { } if ($updateSucceeded) { - log "Executable $package is installed at $exeDestination" + log "Executable $packageName is installed at $exeDestination" } if (!$UpdatePathInProfile) { log -warn "To add the tool to PATH for new shell sessions, re-run with -UpdatePathInProfile to modify the shell profile file." @@ -175,5 +197,6 @@ else { } if ($Run) { - Start-Process -WorkingDirectory $RunDirectory -FilePath $exeDestination -ArgumentList 'start' -NoNewWindow -Wait + $proc = Start-Process -PassThru -WorkingDirectory $RunDirectory -FilePath $exeDestination -ArgumentList 'mcp' -NoNewWindow -Wait + exit $proc.ExitCode }