Skip to content

Commit 877487a

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 11333 (#46173)
* Install azsdk cli to a place in PATH * Only add to path if user manually approves via parameter * Also update shell profile for linux --------- Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent 3964531 commit 877487a

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

eng/common/mcp/azure-sdk-mcp.ps1

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/env pwsh
22

3+
#Requires -Version 7.0
4+
#Requires -PSEdition Core
5+
36
param(
47
[string]$FileName = 'Azure.Sdk.Tools.Cli',
58
[string]$Package = 'azsdk',
@@ -9,21 +12,13 @@ param(
912
[string]$RunDirectory = (Resolve-Path (Join-Path $PSScriptRoot .. .. ..)),
1013
[switch]$Run,
1114
[switch]$UpdateVsCodeConfig,
12-
[switch]$Clean
15+
[switch]$UpdatePathInProfile
1316
)
1417

1518
$ErrorActionPreference = "Stop"
16-
17-
if (-not $InstallDirectory)
18-
{
19-
$homeDir = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE }
20-
$InstallDirectory = (Join-Path $homeDir ".azure-sdk-mcp" "azsdk")
21-
}
2219
. (Join-Path $PSScriptRoot '..' 'scripts' 'Helpers' 'AzSdkTool-Helpers.ps1')
2320

24-
if ($Clean) {
25-
Clear-Directory -Path $InstallDirectory
26-
}
21+
$toolInstallDirectory = $InstallDirectory ? $InstallDirectory : (Get-CommonInstallDirectory)
2722

2823
if ($UpdateVsCodeConfig) {
2924
$vscodeConfigPath = Join-Path $PSScriptRoot ".." ".." ".." ".vscode" "mcp.json"
@@ -54,13 +49,30 @@ if ($UpdateVsCodeConfig) {
5449
$vscodeConfig | ConvertTo-Json -Depth 10 | Set-Content -Path $vscodeConfigPath -Force
5550
}
5651

57-
$exe = Install-Standalone-Tool `
52+
# Install to a temp directory first so we don't dump out all the other
53+
# release zip contents to one of the users bin directories.
54+
$tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath()
55+
$guid = [System.Guid]::NewGuid()
56+
$tempInstallDirectory = Join-Path $tmp "azsdk-install-$($guid)"
57+
$tempExe = Install-Standalone-Tool `
5858
-Version $Version `
5959
-FileName $FileName `
6060
-Package $Package `
61-
-Directory $InstallDirectory `
61+
-Directory $tempInstallDirectory `
6262
-Repository $Repository
6363

64+
Copy-Item -Path $tempExe -Destination $toolInstallDirectory -Force
65+
$exeName = Split-Path $tempExe -Leaf
66+
$exe = Join-Path $toolInstallDirectory $exeName
67+
68+
Write-Host "Package $package is installed at $exe"
69+
if (!$UpdatePathInProfile) {
70+
Write-Warning "To add the tool to PATH for new shell sessions, re-run with -UpdatePathInProfile to modify the shell profile file."
71+
} else {
72+
Add-InstallDirectoryToPathInProfile -InstallDirectory $toolInstallDirectory
73+
Write-Warning "'$exeName' will be available in PATH for new shell sessions."
74+
}
75+
6476
if ($Run) {
6577
Start-Process -WorkingDirectory $RunDirectory -FilePath $exe -ArgumentList 'start' -NoNewWindow -Wait
6678
}

eng/common/scripts/Helpers/AzSdkTool-Helpers.ps1

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,61 @@ function Install-Standalone-Tool (
192192

193193
return $executable_path
194194
}
195+
196+
function Get-CommonInstallDirectory {
197+
$installDirectory = Join-Path $HOME "bin"
198+
if (-not (Test-Path $installDirectory)) {
199+
New-Item -ItemType Directory -Path $installDirectory -Force | Out-Null
200+
}
201+
202+
# Update PATH in current session
203+
if (-not ($env:PATH -like "*$InstallDirectory*")) {
204+
$env:PATH += ";$InstallDirectory"
205+
}
206+
207+
return $installDirectory
208+
}
209+
210+
function Add-InstallDirectoryToPathInProfile(
211+
[Parameter()]
212+
[string]$InstallDirectory = (Get-CommonInstallDirectory)
213+
) {
214+
$powershellProfilePath = $PROFILE
215+
$bashrcPath = Join-Path $HOME ".bashrc"
216+
$zshrcPath = Join-Path $HOME ".zshrc"
217+
$markerComment = " # azsdk install path"
218+
$pathCommand = ""
219+
$configFile = ""
220+
221+
if ($IsWindows) { # expect powershell for windows, cmd.exe path update is not currently supported
222+
$configFile = $powershellProfilePath
223+
$pathCommand = "if (-not (`$env:PATH -like `'*$InstallDirectory*`')) { `$env:PATH += ';$InstallDirectory`' }" + $markerComment
224+
}
225+
elseif ($IsLinux) { # handle bash or zsh shells for linux
226+
if (Test-Path $zshrcPath) {
227+
$configFile = $zshrcPath
228+
}
229+
else {
230+
$configFile = $bashrcPath
231+
}
232+
$pathCommand = "export PATH=`"`$PATH:$InstallDirectory`"" + $markerComment
233+
}
234+
elseif ($IsMacOS) { # mac os should use zsh by default
235+
$configFile = $zshrcPath
236+
$pathCommand = "export PATH=`"`$PATH:$InstallDirectory`"" + $markerComment
237+
}
238+
else {
239+
throw "Unsupported platform"
240+
}
241+
242+
if (-not (Test-Path $configFile)) {
243+
New-Item -ItemType File -Path $configFile -Force | Out-Null
244+
}
245+
246+
$configContent = Get-Content $configFile -Raw
247+
248+
if (!$configContent -or !$configContent.Contains($markerComment)) {
249+
Write-Host "Adding installation to PATH in shell profile at '$configFile'"
250+
Add-Content -Path $configFile -Value $pathCommand
251+
}
252+
}

0 commit comments

Comments
 (0)