From ead91f5d65440c4ba1d0f1590fa15aee1ae65091 Mon Sep 17 00:00:00 2001 From: Daniel Jurek Date: Tue, 12 Oct 2021 20:55:18 -0700 Subject: [PATCH] GetRelativePath should check if path is already relative before calling [IO.Path]::GetRelativePath --- .../scripts/Save-Package-Properties.ps1 | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/eng/common/scripts/Save-Package-Properties.ps1 b/eng/common/scripts/Save-Package-Properties.ps1 index 10cb4158c01..2d328f389cc 100644 --- a/eng/common/scripts/Save-Package-Properties.ps1 +++ b/eng/common/scripts/Save-Package-Properties.ps1 @@ -28,9 +28,9 @@ Verison property in that file. [CmdletBinding()] Param ( - [Parameter(Mandatory=$True)] + [Parameter(Mandatory = $True)] [string] $serviceDirectory, - [Parameter(Mandatory=$True)] + [Parameter(Mandatory = $True)] [string] $outDirectory, [switch] $addDevVersion ) @@ -40,19 +40,16 @@ Param ( function SetOutput($outputPath, $incomingPackageSpec) { # If there is an exsiting package info json file read that and set that as output object which gets properties updated here. - if (Test-Path $outputPath) - { + if (Test-Path $outputPath) { Write-Host "Found existing package info json." $outputObject = ConvertFrom-Json (Get-Content $outputPath -Raw) } - else - { + else { $outputObject = $incomingPackageSpec } - if ($addDevVersion) - { + if ($addDevVersion) { # Use the "Version" property which was provided by the incoming package spec # as the DevVersion. This may be overridden later. $outputObject.DevVersion = $incomingPackageSpec.Version @@ -73,6 +70,14 @@ function GetRelativePath($path) { if (!$path) { return '' } + + # If the path is already relative return the path. Calling `GetRelativePath` + # on a relative path converts the relative path to an absolute path based on + # the current working directory which can result in unexpected outputs. + if (![IO.Path]::IsPathRooted($path)) { + return $path + } + $relativeTo = Resolve-Path $PSScriptRoot/../../../ # Replace "\" with "/" so the path is valid across other platforms and tools $relativePath = [IO.Path]::GetRelativePath($relativeTo, $path) -replace "\\", '/' @@ -80,35 +85,29 @@ function GetRelativePath($path) { } $allPackageProperties = Get-AllPkgProperties $serviceDirectory -if ($allPackageProperties) -{ - if (-not (Test-Path -Path $outDirectory)) - { - New-Item -ItemType Directory -Force -Path $outDirectory - } - foreach($pkg in $allPackageProperties) - { - if ($pkg.IsNewSdk) - { - Write-Host "Package Name: $($pkg.Name)" - Write-Host "Package Version: $($pkg.Version)" - Write-Host "Package SDK Type: $($pkg.SdkType)" - Write-Host "Artifact Name: $($pkg.ArtifactName)" - Write-Host "Release date: $($pkg.ReleaseStatus)" - $configFilePrefix = $pkg.Name - if ($pkg.ArtifactName) - { - $configFilePrefix = $pkg.ArtifactName - } - $outputPath = Join-Path -Path $outDirectory "$configFilePrefix.json" - SetOutput $outputPath $pkg - } +if ($allPackageProperties) { + if (-not (Test-Path -Path $outDirectory)) { + New-Item -ItemType Directory -Force -Path $outDirectory + } + foreach ($pkg in $allPackageProperties) { + if ($pkg.IsNewSdk) { + Write-Host "Package Name: $($pkg.Name)" + Write-Host "Package Version: $($pkg.Version)" + Write-Host "Package SDK Type: $($pkg.SdkType)" + Write-Host "Artifact Name: $($pkg.ArtifactName)" + Write-Host "Release date: $($pkg.ReleaseStatus)" + $configFilePrefix = $pkg.Name + if ($pkg.ArtifactName) { + $configFilePrefix = $pkg.ArtifactName + } + $outputPath = Join-Path -Path $outDirectory "$configFilePrefix.json" + SetOutput $outputPath $pkg } + } - Get-ChildItem -Path $outDirectory + Get-ChildItem -Path $outDirectory } -else -{ - Write-Error "Package properties are not available for service directory $($serviceDirectory)" - exit 1 +else { + Write-Error "Package properties are not available for service directory $($serviceDirectory)" + exit 1 }