Skip to content

Commit 8390907

Browse files
azure-sdkhallipr
andauthored
Sync eng/common directory with azure-sdk-tools for PR 8900 (Azure#23386)
* Add save-package-properties yaml * Encapsulate Save-Package-Properties.ps1 invocation into yaml * Fix output formatting * Fix code style --------- Co-authored-by: Patrick Hallisey <[email protected]>
1 parent 113ebb0 commit 8390907

File tree

3 files changed

+104
-36
lines changed

3 files changed

+104
-36
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
parameters:
2+
- name: ServiceDirectory
3+
type: string
4+
default: ""
5+
- name: DiffDirectory
6+
type: string
7+
default: $(Build.ArtifactStagingDirectory)/diff
8+
- name: PackageInfoDirectory
9+
type: string
10+
default: $(Build.ArtifactStagingDirectory)/PackageInfo
11+
- name: TargetPath
12+
type: string
13+
default: $(Build.SourcesDirectory)
14+
- name: ScriptDirectory
15+
type: string
16+
default: eng/common/scripts
17+
18+
steps:
19+
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
20+
- task: Powershell@2
21+
displayName: Generate PR Diff
22+
inputs:
23+
filePath: ${{ parameters.ScriptDirectory }}/Generate-PR-Diff.ps1
24+
arguments: >
25+
-TargetPath '${{ parameters.TargetPath }}'
26+
-ArtifactPath '${{ parameters.DiffDirectory }}'
27+
pwsh: true
28+
29+
- task: Powershell@2
30+
displayName: Save package properties filtered for PR
31+
inputs:
32+
filePath: ${{ parameters.ScriptDirectory }}/Save-Package-Properties.ps1
33+
arguments: >
34+
-PrDiff ${{ parameters.DiffDirectory }}/diff.json
35+
-OutDirectory ${{ parameters.PackageInfoDirectory }}
36+
pwsh: true
37+
- ${{ else }}:
38+
- task: Powershell@2
39+
displayName: Save package properties
40+
inputs:
41+
filePath: ${{ parameters.ScriptDirectory }}/Save-Package-Properties.ps1
42+
arguments: >
43+
-ServiceDirectory ${{parameters.ServiceDirectory}}
44+
-OutDirectory ${{ parameters.PackageInfoDirectory }}
45+
-AddDevVersion:$${{ eq(variables['SetDevVersion'],'true') }}
46+
pwsh: true

eng/common/scripts/Generate-PR-Diff.ps1

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,29 @@ The path under which changes will be detected.
1313
#>
1414
[CmdletBinding()]
1515
Param (
16-
[Parameter(Mandatory=$True)]
16+
[Parameter(Mandatory = $True)]
1717
[string] $ArtifactPath,
18-
[Parameter(Mandatory=$True)]
18+
[Parameter(Mandatory = $True)]
1919
[string] $TargetPath
2020
)
2121

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

24-
function Get-ChangedServices {
25-
Param (
26-
[Parameter(Mandatory=$True)]
27-
[string[]] $ChangedFiles
28-
)
24+
function Get-ChangedServices
25+
{
26+
Param (
27+
[Parameter(Mandatory = $True)]
28+
[string[]] $ChangedFiles
29+
)
2930

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

32-
return $changedServices
33+
return $changedServices
3334
}
3435

35-
if (!(Test-Path $ArtifactPath)) {
36-
New-Item -ItemType Directory -Path $ArtifactPath | Out-Null
36+
if (!(Test-Path $ArtifactPath))
37+
{
38+
New-Item -ItemType Directory -Path $ArtifactPath | Out-Null
3739
}
3840

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

4547
$result = [PSCustomObject]@{
46-
"ChangedFiles" = $changedFiles
47-
"ChangedServices" = $changedServices
48-
"PRNumber" = if ($env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER) { $env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER } else { "-1" }
48+
"ChangedFiles" = $changedFiles
49+
"ChangedServices" = $changedServices
50+
"PRNumber" = if ($env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER) { $env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER } else { "-1" }
4951
}
5052

51-
$result | ConvertTo-Json | Out-File $ArtifactName
53+
$json = $result | ConvertTo-Json
54+
$json | Out-File $ArtifactName
55+
56+
Write-Host "`nGenerated diff.json file at $ArtifactName"
57+
Write-Host " $($json -replace "`n", "`n ")"

eng/common/scripts/Save-Package-Properties.ps1

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Saves package properties in source of a given service directory to JSON files.
77
JSON files are named in the form <package name>.json or <artifact name>.json if
88
an artifact name property is available in the package properties.
99
10-
Can optionally add a dev version property which can be used logic for daily
10+
Can optionally add a dev version property which can be used logic for daily
1111
builds.
1212
13-
In cases of collisions where track 2 packages (IsNewSdk = true) have the same
13+
In cases of collisions where track 2 packages (IsNewSdk = true) have the same
1414
filename as track 1 packages (e.g. same artifact name or package name), the
1515
track 2 package properties will be written.
1616
@@ -21,30 +21,31 @@ Service directory in which to search for packages.
2121
A file path leading to a file generated from Generate-PR-Diff.json. This parameter takes precedence over serviceDirectory, do not provide both.
2222
2323
.PARAMETER outDirectory
24-
Output location (generally a package artifact directory in DevOps) for JSON
24+
Output location (generally a package artifact directory in DevOps) for JSON
2525
files
2626
2727
.PARAMETER addDevVersion
28-
Reads the version out of the source and adds a DevVersion property to the
29-
package properties JSON file. If the package properties JSON file already
28+
Reads the version out of the source and adds a DevVersion property to the
29+
package properties JSON file. If the package properties JSON file already
3030
exists, read the Version property from the existing package properties JSON file
3131
and set that as the Version property for the new output. This has the effect of
32-
"adding" a DevVersion property to the file which could be different from the
32+
"adding" a DevVersion property to the file which could be different from the
3333
Verison property in that file.
3434
#>
3535

3636
[CmdletBinding()]
3737
Param (
3838
[string] $serviceDirectory,
39-
[Parameter(Mandatory=$True)]
39+
[Parameter(Mandatory = $True)]
4040
[string] $outDirectory,
4141
[string] $prDiff,
4242
[switch] $addDevVersion
4343
)
4444

4545
. (Join-Path $PSScriptRoot common.ps1)
4646

47-
function SetOutput($outputPath, $incomingPackageSpec) {
47+
function SetOutput($outputPath, $incomingPackageSpec)
48+
{
4849

4950
# If there is an exsiting package info json file read that and set that as output object which gets properties updated here.
5051
if (Test-Path $outputPath)
@@ -56,7 +57,7 @@ function SetOutput($outputPath, $incomingPackageSpec) {
5657
{
5758
$outputObject = $incomingPackageSpec
5859
}
59-
60+
6061

6162
if ($addDevVersion)
6263
{
@@ -75,16 +76,19 @@ function SetOutput($outputPath, $incomingPackageSpec) {
7576
-Value (ConvertTo-Json -InputObject $outputObject -Depth 100)
7677
}
7778

78-
function GetRelativePath($path) {
79+
function GetRelativePath($path)
80+
{
7981
# If the path is empty return an empty string
80-
if (!$path) {
82+
if (!$path)
83+
{
8184
return ''
8285
}
8386

8487
# If the path is already relative return the path. Calling `GetRelativePath`
8588
# on a relative path converts the relative path to an absolute path based on
8689
# the current working directory which can result in unexpected outputs.
87-
if (![IO.Path]::IsPathRooted($path)) {
90+
if (![IO.Path]::IsPathRooted($path))
91+
{
8892
return $path
8993
}
9094

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

99103
$allPackageProperties = @()
100104

101-
if ($prDiff) {
105+
if ($prDiff)
106+
{
102107
Write-Host "Getting package properties for PR diff file: $prDiff"
103108
$allPackageProperties = Get-PrPkgProperties $prDiff
104109

105-
if (!$allPackageProperties) {
110+
if (!$allPackageProperties)
111+
{
106112
Write-Host "No packages found matching PR diff file $prDiff"
107113
Write-Host "Setting NoPackagesChanged variable to true"
108114
Write-Host "##vso[task.setvariable variable=NoPackagesChanged]true"
109115
exit 0
110116
}
111117
}
112-
else {
118+
else
119+
{
113120
Write-Host "Getting package properties for service directory: $serviceDirectory"
114121
$allPackageProperties = Get-AllPkgProperties $serviceDirectory
115122

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

127-
foreach($pkg in $allPackageProperties)
135+
foreach ($pkg in $allPackageProperties)
128136
{
129-
if ($pkg.Name) {
137+
if ($pkg.Name)
138+
{
139+
Write-Host ""
130140
Write-Host "Package Name: $($pkg.Name)"
131141
Write-Host "Package Version: $($pkg.Version)"
132142
Write-Host "Package SDK Type: $($pkg.SdkType)"
133143
Write-Host "Artifact Name: $($pkg.ArtifactName)"
134144
Write-Host "Release date: $($pkg.ReleaseStatus)"
135145
$configFilePrefix = $pkg.Name
146+
136147
if ($pkg.ArtifactName)
137148
{
138149
$configFilePrefix = $pkg.ArtifactName
139150
}
151+
140152
$outputPath = Join-Path -Path $outDirectory "$configFilePrefix.json"
141153
Write-Host "Output path of json file: $outputPath"
154+
142155
$outDir = Split-Path $outputPath -parent
143156
if (-not (Test-Path -path $outDir))
144157
{
@@ -148,14 +161,17 @@ foreach($pkg in $allPackageProperties)
148161

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

170+
$exportedPaths[$outputPath] = $pkg
157171
SetOutput $outputPath $pkg
158172
}
159173
}
160174

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

0 commit comments

Comments
 (0)