Skip to content

Commit

Permalink
Refactor PackageProps to make the Yaml loading and processing methods…
Browse files Browse the repository at this point in the history
… common (#9236)

* Refactor PackageProps to make the Yaml loading and processing methods common

* Updates for feedback
  • Loading branch information
JimSuplizio authored Oct 24, 2024
1 parent f4965c2 commit 8b2d384
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 32 deletions.
86 changes: 86 additions & 0 deletions eng/common/scripts/Helpers/Package-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,90 @@ function CompatibleConvertFrom-Yaml {
else {
return ConvertFrom-Yaml $content
}
}

<#
.SYNOPSIS
Common function that will verify that the YmlFile being loaded exists, load the raw file and
return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
there's a problem loading the yml file. The return is the PowerShell HashTable object.
.DESCRIPTION
Common function that will verify that the YmlFile being loaded exists, load the raw file and
return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
there's a problem loading the yml file. This is just to save anyone needing to load yml from
having to deal with checking the file's existence and ensure that the CompatibleConvertFrom-Yaml
is made within a try/catch. The return is the PowerShell HashTable object from the
CompatibleConvertFrom-Yaml call or $null if there was an issue with the convert.
.PARAMETER YmlFile
The full path of the yml file to load.
.EXAMPLE
LoadFrom-Yaml -YmlFile path/to/file.yml
#>
function LoadFrom-Yaml {
param(
[Parameter(Mandatory=$true)]
[string]$YmlFile
)
if (Test-Path -Path $YmlFile) {
try {
return Get-Content -Raw -Path $YmlFile | CompatibleConvertFrom-Yaml
}
catch {
Write-Host "LoadFrom-Yaml::Exception while parsing yml file $($YmlFile): $_"
}
}
else {
Write-Host "LoadFrom-Yaml::YmlFile '$YmlFile' does not exist."
}
return $null
}

<#
.SYNOPSIS
Given the Hashtable contents of a Yml file and an array of strings representing the keys
return the value if it exist or null if it doesn't.
.DESCRIPTION
The Yaml file needs to be loaded via CompatibleConvertFrom-Yaml which returns the file as
as hashtable. The Keys are basically the path in the yaml file whose value to return, or
null if it doesn't exist. This function safely traverses the path, outputting an error
if there's an issue or returning the object representing the result if successful. This
function loops through the Keys safely trying to get values, checking each piece of the
path to ensure it exists. Normally one would just do
$Yml["extends"]["parameters"]["artifacts"]
but if something was off it would throw. Doing it this way allows more succinct error
reporting if a piece of the path didn't exist
.PARAMETER YamlContentAsHashtable
The hashtable representing the yaml file contents loaded through LoadFrom-Yaml
or CompatibleConvertFrom-Yaml, which is what LoadFrom-Yaml calls.
.PARAMETER Keys
String array representation of the path in the yaml file whose value we're trying to retrieve.
.EXAMPLE
GetValueSafelyFrom-Yaml -YamlContentAsHashtable $YmlFileContent -Keys @("extends", "parameters", "Artifacts")
#>
function GetValueSafelyFrom-Yaml {
param(
[Parameter(Mandatory=$true)]
$YamlContentAsHashtable,
[Parameter(Mandatory=$true)]
[string[]]$Keys
)
$current = $YamlContentAsHashtable
foreach ($key in $Keys) {
if ($current.ContainsKey($key) -or $current[$key]) {
$current = $current[$key]
}
else {
Write-Host "The '$key' part of the path $($Keys -join "/") doesn't exist or is null."
return $null
}
}

return [object]$current
}
44 changes: 12 additions & 32 deletions eng/common/scripts/Package-Properties.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class PackageProps
[string]$ReleaseStatus
# was this package purely included because other packages included it as an AdditionalValidationPackage?
[boolean]$IncludedForValidation
# does this package include other packages that we should trigger validation for?
# does this package include other packages that we should trigger validation for or
# additional packages required for validation of this one
[string[]]$AdditionalValidationPackages
[HashTable]$ArtifactDetails

Expand Down Expand Up @@ -83,42 +84,21 @@ class PackageProps
$this.Group = $group
}

hidden [object]GetValueSafely($hashtable, [string[]]$keys) {
$current = $hashtable
foreach ($key in $keys) {
if ($current.ContainsKey($key) -or $current[$key]) {
$current = $current[$key]
}
else {
return $null
}
}
hidden [HashTable]ParseYmlForArtifact([string]$ymlPath) {

return $current
}
$content = LoadFrom-Yaml $ymlPath
if ($content) {
$artifacts = GetValueSafelyFrom-Yaml $content @("extends", "parameters", "Artifacts")
$artifactForCurrentPackage = $null

hidden [HashTable]ParseYmlForArtifact([string]$ymlPath) {
if (Test-Path -Path $ymlPath) {
try {
$content = Get-Content -Raw -Path $ymlPath | CompatibleConvertFrom-Yaml
if ($content) {
$artifacts = $this.GetValueSafely($content, @("extends", "parameters", "Artifacts"))
$artifactForCurrentPackage = $null

if ($artifacts) {
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
}

if ($artifactForCurrentPackage) {
return [HashTable]$artifactForCurrentPackage
}
}
if ($artifacts) {
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
}
catch {
Write-Host "Exception while parsing yml file $($ymlPath): $_"

if ($artifactForCurrentPackage) {
return [HashTable]$artifactForCurrentPackage
}
}

return $null
}

Expand Down

0 comments on commit 8b2d384

Please sign in to comment.