Skip to content

Commit

Permalink
Encapsulate Save-Package-Properties.ps1 invocation into yaml (#8900)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallipr authored Aug 29, 2024
1 parent 21c1ba4 commit da75627
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 36 deletions.
46 changes: 46 additions & 0 deletions eng/common/pipelines/templates/steps/save-package-properties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
parameters:
- name: ServiceDirectory
type: string
default: ""
- name: DiffDirectory
type: string
default: $(Build.ArtifactStagingDirectory)/diff
- name: PackageInfoDirectory
type: string
default: $(Build.ArtifactStagingDirectory)/PackageInfo
- name: TargetPath
type: string
default: $(Build.SourcesDirectory)
- name: ScriptDirectory
type: string
default: eng/common/scripts

steps:
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
- task: Powershell@2
displayName: Generate PR Diff
inputs:
filePath: ${{ parameters.ScriptDirectory }}/Generate-PR-Diff.ps1
arguments: >
-TargetPath '${{ parameters.TargetPath }}'
-ArtifactPath '${{ parameters.DiffDirectory }}'
pwsh: true

- task: Powershell@2
displayName: Save package properties filtered for PR
inputs:
filePath: ${{ parameters.ScriptDirectory }}/Save-Package-Properties.ps1
arguments: >
-PrDiff ${{ parameters.DiffDirectory }}/diff.json
-OutDirectory ${{ parameters.PackageInfoDirectory }}
pwsh: true
- ${{ else }}:
- task: Powershell@2
displayName: Save package properties
inputs:
filePath: ${{ parameters.ScriptDirectory }}/Save-Package-Properties.ps1
arguments: >
-ServiceDirectory ${{parameters.ServiceDirectory}}
-OutDirectory ${{ parameters.PackageInfoDirectory }}
-AddDevVersion:$${{ eq(variables['SetDevVersion'],'true') }}
pwsh: true
36 changes: 21 additions & 15 deletions eng/common/scripts/Generate-PR-Diff.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ The path under which changes will be detected.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True)]
[Parameter(Mandatory = $True)]
[string] $ArtifactPath,
[Parameter(Mandatory=$True)]
[Parameter(Mandatory = $True)]
[string] $TargetPath
)

. (Join-Path $PSScriptRoot "Helpers" "git-helpers.ps1")

function Get-ChangedServices {
Param (
[Parameter(Mandatory=$True)]
[string[]] $ChangedFiles
)
function Get-ChangedServices
{
Param (
[Parameter(Mandatory = $True)]
[string[]] $ChangedFiles
)

$changedServices = $ChangedFiles | Foreach-Object { if ($_ -match "sdk/([^/]+)") { $matches[1] } } | Sort-Object -Unique
$changedServices = $ChangedFiles | Foreach-Object { if ($_ -match "sdk/([^/]+)") { $matches[1] } } | Sort-Object -Unique

return $changedServices
return $changedServices
}

if (!(Test-Path $ArtifactPath)) {
New-Item -ItemType Directory -Path $ArtifactPath | Out-Null
if (!(Test-Path $ArtifactPath))
{
New-Item -ItemType Directory -Path $ArtifactPath | Out-Null
}

$ArtifactPath = Resolve-Path $ArtifactPath
Expand All @@ -43,9 +45,13 @@ $changedFiles = Get-ChangedFiles -DiffPath $TargetPath
$changedServices = Get-ChangedServices -ChangedFiles $changedFiles

$result = [PSCustomObject]@{
"ChangedFiles" = $changedFiles
"ChangedServices" = $changedServices
"PRNumber" = if ($env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER) { $env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER } else { "-1" }
"ChangedFiles" = $changedFiles
"ChangedServices" = $changedServices
"PRNumber" = if ($env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER) { $env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER } else { "-1" }
}

$result | ConvertTo-Json | Out-File $ArtifactName
$json = $result | ConvertTo-Json
$json | Out-File $ArtifactName

Write-Host "`nGenerated diff.json file at $ArtifactName"
Write-Host " $($json -replace "`n", "`n ")"
58 changes: 37 additions & 21 deletions eng/common/scripts/Save-Package-Properties.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Saves package properties in source of a given service directory to JSON files.
JSON files are named in the form <package name>.json or <artifact name>.json if
an artifact name property is available in the package properties.
Can optionally add a dev version property which can be used logic for daily
Can optionally add a dev version property which can be used logic for daily
builds.
In cases of collisions where track 2 packages (IsNewSdk = true) have the same
In cases of collisions where track 2 packages (IsNewSdk = true) have the same
filename as track 1 packages (e.g. same artifact name or package name), the
track 2 package properties will be written.
Expand All @@ -21,30 +21,31 @@ Service directory in which to search for packages.
A file path leading to a file generated from Generate-PR-Diff.json. This parameter takes precedence over serviceDirectory, do not provide both.
.PARAMETER outDirectory
Output location (generally a package artifact directory in DevOps) for JSON
Output location (generally a package artifact directory in DevOps) for JSON
files
.PARAMETER addDevVersion
Reads the version out of the source and adds a DevVersion property to the
package properties JSON file. If the package properties JSON file already
Reads the version out of the source and adds a DevVersion property to the
package properties JSON file. If the package properties JSON file already
exists, read the Version property from the existing package properties JSON file
and set that as the Version property for the new output. This has the effect of
"adding" a DevVersion property to the file which could be different from the
"adding" a DevVersion property to the file which could be different from the
Verison property in that file.
#>

[CmdletBinding()]
Param (
[string] $serviceDirectory,
[Parameter(Mandatory=$True)]
[Parameter(Mandatory = $True)]
[string] $outDirectory,
[string] $prDiff,
[switch] $addDevVersion
)

. (Join-Path $PSScriptRoot common.ps1)

function SetOutput($outputPath, $incomingPackageSpec) {
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)
Expand All @@ -56,7 +57,7 @@ function SetOutput($outputPath, $incomingPackageSpec) {
{
$outputObject = $incomingPackageSpec
}


if ($addDevVersion)
{
Expand All @@ -75,16 +76,19 @@ function SetOutput($outputPath, $incomingPackageSpec) {
-Value (ConvertTo-Json -InputObject $outputObject -Depth 100)
}

function GetRelativePath($path) {
function GetRelativePath($path)
{
# If the path is empty return an empty string
if (!$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)) {
if (![IO.Path]::IsPathRooted($path))
{
return $path
}

Expand All @@ -98,22 +102,26 @@ $exportedPaths = @{}

$allPackageProperties = @()

if ($prDiff) {
if ($prDiff)
{
Write-Host "Getting package properties for PR diff file: $prDiff"
$allPackageProperties = Get-PrPkgProperties $prDiff

if (!$allPackageProperties) {
if (!$allPackageProperties)
{
Write-Host "No packages found matching PR diff file $prDiff"
Write-Host "Setting NoPackagesChanged variable to true"
Write-Host "##vso[task.setvariable variable=NoPackagesChanged]true"
exit 0
}
}
else {
else
{
Write-Host "Getting package properties for service directory: $serviceDirectory"
$allPackageProperties = Get-AllPkgProperties $serviceDirectory

if (!$allPackageProperties) {
if (!$allPackageProperties)
{
Write-Error "Package properties are not available for service directory $serviceDirectory"
exit 1
}
Expand All @@ -124,21 +132,26 @@ if (-not (Test-Path -Path $outDirectory))
New-Item -ItemType Directory -Force -Path $outDirectory | Out-Null
}

foreach($pkg in $allPackageProperties)
foreach ($pkg in $allPackageProperties)
{
if ($pkg.Name) {
if ($pkg.Name)
{
Write-Host ""
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"
Write-Host "Output path of json file: $outputPath"

$outDir = Split-Path $outputPath -parent
if (-not (Test-Path -path $outDir))
{
Expand All @@ -148,14 +161,17 @@ foreach($pkg in $allPackageProperties)

# If package properties for a track 2 (IsNewSdk = true) package has
# already been written, skip writing to that same path.
if ($exportedPaths.ContainsKey($outputPath) -and $exportedPaths[$outputPath].IsNewSdk -eq $true) {
if ($exportedPaths.ContainsKey($outputPath) -and $exportedPaths[$outputPath].IsNewSdk -eq $true)
{
Write-Host "Track 2 package info with file name $($outputPath) already exported. Skipping export."
continue
}
$exportedPaths[$outputPath] = $pkg

$exportedPaths[$outputPath] = $pkg
SetOutput $outputPath $pkg
}
}

Get-ChildItem -Path $outDirectory
$fileNames = (Get-ChildItem -Path $outDirectory).Name
Write-Host "`nFiles written to $outDirectory`:"
Write-Host " $($fileNames -join "`n ")"

0 comments on commit da75627

Please sign in to comment.