Skip to content

Commit 85d7532

Browse files
Sync eng/common directory with azure-sdk-tools for PR 9236 (#46818)
* Refactor PackageProps to make the Yaml loading and processing methods common * Updates for feedback --------- Co-authored-by: James Suplizio <jasupliz@microsoft.com>
1 parent cf787a1 commit 85d7532

File tree

2 files changed

+98
-32
lines changed

2 files changed

+98
-32
lines changed

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,90 @@ function CompatibleConvertFrom-Yaml {
9090
else {
9191
return ConvertFrom-Yaml $content
9292
}
93+
}
94+
95+
<#
96+
.SYNOPSIS
97+
Common function that will verify that the YmlFile being loaded exists, load the raw file and
98+
return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
99+
there's a problem loading the yml file. The return is the PowerShell HashTable object.
100+
101+
.DESCRIPTION
102+
Common function that will verify that the YmlFile being loaded exists, load the raw file and
103+
return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
104+
there's a problem loading the yml file. This is just to save anyone needing to load yml from
105+
having to deal with checking the file's existence and ensure that the CompatibleConvertFrom-Yaml
106+
is made within a try/catch. The return is the PowerShell HashTable object from the
107+
CompatibleConvertFrom-Yaml call or $null if there was an issue with the convert.
108+
109+
.PARAMETER YmlFile
110+
The full path of the yml file to load.
111+
112+
.EXAMPLE
113+
LoadFrom-Yaml -YmlFile path/to/file.yml
114+
#>
115+
function LoadFrom-Yaml {
116+
param(
117+
[Parameter(Mandatory=$true)]
118+
[string]$YmlFile
119+
)
120+
if (Test-Path -Path $YmlFile) {
121+
try {
122+
return Get-Content -Raw -Path $YmlFile | CompatibleConvertFrom-Yaml
123+
}
124+
catch {
125+
Write-Host "LoadFrom-Yaml::Exception while parsing yml file $($YmlFile): $_"
126+
}
127+
}
128+
else {
129+
Write-Host "LoadFrom-Yaml::YmlFile '$YmlFile' does not exist."
130+
}
131+
return $null
132+
}
133+
134+
<#
135+
.SYNOPSIS
136+
Given the Hashtable contents of a Yml file and an array of strings representing the keys
137+
return the value if it exist or null if it doesn't.
138+
139+
.DESCRIPTION
140+
The Yaml file needs to be loaded via CompatibleConvertFrom-Yaml which returns the file as
141+
as hashtable. The Keys are basically the path in the yaml file whose value to return, or
142+
null if it doesn't exist. This function safely traverses the path, outputting an error
143+
if there's an issue or returning the object representing the result if successful. This
144+
function loops through the Keys safely trying to get values, checking each piece of the
145+
path to ensure it exists. Normally one would just do
146+
$Yml["extends"]["parameters"]["artifacts"]
147+
but if something was off it would throw. Doing it this way allows more succinct error
148+
reporting if a piece of the path didn't exist
149+
150+
.PARAMETER YamlContentAsHashtable
151+
The hashtable representing the yaml file contents loaded through LoadFrom-Yaml
152+
or CompatibleConvertFrom-Yaml, which is what LoadFrom-Yaml calls.
153+
154+
.PARAMETER Keys
155+
String array representation of the path in the yaml file whose value we're trying to retrieve.
156+
157+
.EXAMPLE
158+
GetValueSafelyFrom-Yaml -YamlContentAsHashtable $YmlFileContent -Keys @("extends", "parameters", "Artifacts")
159+
#>
160+
function GetValueSafelyFrom-Yaml {
161+
param(
162+
[Parameter(Mandatory=$true)]
163+
$YamlContentAsHashtable,
164+
[Parameter(Mandatory=$true)]
165+
[string[]]$Keys
166+
)
167+
$current = $YamlContentAsHashtable
168+
foreach ($key in $Keys) {
169+
if ($current.ContainsKey($key) -or $current[$key]) {
170+
$current = $current[$key]
171+
}
172+
else {
173+
Write-Host "The '$key' part of the path $($Keys -join "/") doesn't exist or is null."
174+
return $null
175+
}
176+
}
177+
178+
return [object]$current
93179
}

eng/common/scripts/Package-Properties.ps1

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class PackageProps
1717
[string]$ReleaseStatus
1818
# was this package purely included because other packages included it as an AdditionalValidationPackage?
1919
[boolean]$IncludedForValidation
20-
# does this package include other packages that we should trigger validation for?
20+
# does this package include other packages that we should trigger validation for or
21+
# additional packages required for validation of this one
2122
[string[]]$AdditionalValidationPackages
2223
[HashTable]$ArtifactDetails
2324

@@ -83,42 +84,21 @@ class PackageProps
8384
$this.Group = $group
8485
}
8586

86-
hidden [object]GetValueSafely($hashtable, [string[]]$keys) {
87-
$current = $hashtable
88-
foreach ($key in $keys) {
89-
if ($current.ContainsKey($key) -or $current[$key]) {
90-
$current = $current[$key]
91-
}
92-
else {
93-
return $null
94-
}
95-
}
87+
hidden [HashTable]ParseYmlForArtifact([string]$ymlPath) {
9688

97-
return $current
98-
}
89+
$content = LoadFrom-Yaml $ymlPath
90+
if ($content) {
91+
$artifacts = GetValueSafelyFrom-Yaml $content @("extends", "parameters", "Artifacts")
92+
$artifactForCurrentPackage = $null
9993

100-
hidden [HashTable]ParseYmlForArtifact([string]$ymlPath) {
101-
if (Test-Path -Path $ymlPath) {
102-
try {
103-
$content = Get-Content -Raw -Path $ymlPath | CompatibleConvertFrom-Yaml
104-
if ($content) {
105-
$artifacts = $this.GetValueSafely($content, @("extends", "parameters", "Artifacts"))
106-
$artifactForCurrentPackage = $null
107-
108-
if ($artifacts) {
109-
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
110-
}
111-
112-
if ($artifactForCurrentPackage) {
113-
return [HashTable]$artifactForCurrentPackage
114-
}
115-
}
94+
if ($artifacts) {
95+
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
11696
}
117-
catch {
118-
Write-Host "Exception while parsing yml file $($ymlPath): $_"
97+
98+
if ($artifactForCurrentPackage) {
99+
return [HashTable]$artifactForCurrentPackage
119100
}
120101
}
121-
122102
return $null
123103
}
124104

0 commit comments

Comments
 (0)