Skip to content

Commit fb9b7a2

Browse files
azure-sdkscbedd
andauthored
Sync eng/common directory with azure-sdk-tools for PR 9102 (Azure#23564)
* Add CompatibleConvertFrom-Yaml to common Package-Helpers * Add CI artifact parsing to Save-Package-Properties, now each individual packageinfo.json file contains the relevant ci artifact lines for the package. This short circuits needing to re-parse this information in common checks. --------- Co-authored-by: Scott Beddall <[email protected]>
1 parent bb7200c commit fb9b7a2

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function GetPackageKey($pkg) {
1212

1313
return $pkgKey
1414
}
15-
15+
1616
# Different language needs a different way to index the package. Build a map in convienice to lookup the package.
1717
# E.g. <groupId>:<packageName> is the package key in java.
1818
function GetPackageLookup($packageList) {
@@ -44,4 +44,50 @@ function GetDocsTocDisplayName($pkg) {
4444
$displayName += " (deprecated)"
4545
}
4646
return $displayName
47+
}
48+
49+
50+
<#
51+
.SYNOPSIS
52+
This function is a safe wrapper around `yq` and `ConvertFrom-Yaml` to convert YAML content to a PowerShell HashTable object
53+
54+
.DESCRIPTION
55+
This function wraps `yq` and `ConvertFrom-Yaml` to convert YAML content to a PowerShell HashTable object. The reason this function exists is
56+
because while on a local user's machine, installing a module from the powershell gallery is an easy task, in pipelines we often have failures
57+
to install modules from the gallery. This function will attempt to use the `yq` command if it is available on the machine, and only will install
58+
the yaml module if `yq` is not available. This means that for the majority of runs on CI machines, the yaml module will not be installed.
59+
60+
.PARAMETER Content
61+
The content to convert from YAML to a PowerShell HashTable object. Accepted as named argument or from the pipeline.
62+
63+
.EXAMPLE
64+
CompatibleConvertFrom-Yaml -Content (Get-Content -Raw path/to/file.yml)
65+
66+
.EXAMPLE
67+
Get-Content -Raw path/to/file.yml | CompatibleConvertFrom-Yaml
68+
#>
69+
function CompatibleConvertFrom-Yaml {
70+
param(
71+
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
72+
[string]$Content
73+
)
74+
75+
if (!($Content)) {
76+
throw "Content to parse is a required input."
77+
}
78+
79+
# Initialize any variables or checks that need to be done once
80+
$yqPresent = Get-Command 'yq' -ErrorAction SilentlyContinue
81+
if (-not $yqPresent) {
82+
. (Join-Path $PSScriptRoot PSModule-Helpers.ps1)
83+
Install-ModuleIfNotInstalled -WhatIf:$false "powershell-yaml" "0.4.1" | Import-Module
84+
}
85+
86+
# Process the content (for example, you could convert from YAML here)
87+
if ($yqPresent) {
88+
return ($content | yq -o=json | ConvertFrom-Json -AsHashTable)
89+
}
90+
else {
91+
return ConvertFrom-Yaml $content
92+
}
4793
}

eng/common/scripts/Package-Properties.ps1

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Helper functions for retireving useful information from azure-sdk-for-* repo
1+
# Helper functions for retrieving useful information from azure-sdk-for-* repo
22
. "${PSScriptRoot}\logging.ps1"
3-
3+
. "${PSScriptRoot}\Helpers\Package-Helpers.ps1"
44
class PackageProps
55
{
66
[string]$Name
@@ -19,6 +19,7 @@ class PackageProps
1919
[boolean]$IncludedForValidation
2020
# does this package include other packages that we should trigger validation for?
2121
[string[]]$AdditionalValidationPackages
22+
[HashTable]$ArtifactDetails
2223

2324
PackageProps([string]$name, [string]$version, [string]$directoryPath, [string]$serviceDirectory)
2425
{
@@ -66,6 +67,8 @@ class PackageProps
6667
{
6768
$this.ChangeLogPath = $null
6869
}
70+
71+
$this.InitializeCIArtifacts()
6972
}
7073

7174
hidden [void]Initialize(
@@ -79,6 +82,59 @@ class PackageProps
7982
$this.Initialize($name, $version, $directoryPath, $serviceDirectory)
8083
$this.Group = $group
8184
}
85+
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+
}
96+
97+
return $current
98+
}
99+
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+
107+
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
108+
109+
if ($artifactForCurrentPackage) {
110+
return [HashTable]$artifactForCurrentPackage
111+
}
112+
}
113+
}
114+
catch {
115+
Write-Host "Exception while parsing yml file $($ymlPath): $_"
116+
}
117+
}
118+
119+
return $null
120+
}
121+
122+
[void]InitializeCIArtifacts(){
123+
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot ".." ".." "..")
124+
125+
$ciFolderPath = Join-Path -Path $RepoRoot -ChildPath (Join-Path "sdk" $this.ServiceDirectory)
126+
$ciFiles = Get-ChildItem -Path $ciFolderPath -Filter "ci*.yml" -File
127+
128+
if (-not $this.ArtifactDetails) {
129+
foreach($ciFile in $ciFiles) {
130+
$ciArtifactResult = $this.ParseYmlForArtifact($ciFile.FullName)
131+
if ($ciArtifactResult) {
132+
$this.ArtifactDetails = [Hashtable]$ciArtifactResult
133+
break
134+
}
135+
}
136+
}
137+
}
82138
}
83139

84140
# Takes package name and service Name

0 commit comments

Comments
 (0)