Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PackageProps to make the Yaml loading and processing methods common #9236

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
CompatibleLoadAndConvertFrom-Yaml -YmlFile path/to/file.yml
#>
function LoadAndCompatibleConvertFrom-Yaml {
JimSuplizio marked this conversation as resolved.
Show resolved Hide resolved
param(
[Parameter(Mandatory=$true)]
[string]$YmlFile
)
if (Test-Path -Path $YmlFile) {
try {
return Get-Content -Raw -Path $YmlFile | CompatibleConvertFrom-Yaml
}
catch {
Write-Host "CompatibleLoadAndConvertFrom-Yaml::Exception while parsing yml file $($YmlFile): $_"
}
}
else {
Write-Host "CompatibleLoadAndConvertFrom-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 does is safely traverses the path, outputting an
JimSuplizio marked this conversation as resolved.
Show resolved Hide resolved
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 LoadAndCompatibleConvertFrom-Yaml
or CompatibleConvertFrom-Yaml, which is what LoadAndCompatibleConvertFrom-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 = LoadAndCompatibleConvertFrom-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