Skip to content

Instantly share code, notes, and snippets.

@scbedd
Created February 8, 2024 01:56
Show Gist options
  • Save scbedd/9569b0b8c4035516ca25256c15a3d2bf to your computer and use it in GitHub Desktop.
Save scbedd/9569b0b8c4035516ca25256c15a3d2bf to your computer and use it in GitHub Desktop.
Invokes template conversion tool on set of files
# example usage:
#
# ./parse_template_calls.ps1 -InputFile C:\repo\sdk-for-python\eng\pipelines\templates\stages\archetype-sdk-client.yml -RepoRoot C:\repo\sdk-for-python
# We could make the thing ascend to repo root for us, but I didn't want to fit that in this script
param(
$InputFile,
$RepoRoot = "C:/repo/sdk-for-python/"
)
function Evaluate-Line {
param(
[string]$Line,
[string]$Pattern
)
if ($Line -match $Pattern) {
return $matches[1]
}
}
function Resolve-Template-Ref {
param(
[string]$File,
[string]$TemplateRef,
[string]$TemplateRepoRoot
)
$containingDir = $File | Split-Path -Parent
if ($TemplateRef.EndsWith("@self")) {
$TemplateRef = $TemplateRef.Substring(0, $TemplateRef.Length - 5)
}
# this is an absolute reference from beginning of repo
if ($TemplateRef.StartsWith("/")) {
$TemplateRef = $TemplateRef.Substring(1)
$resolvedTemplate = Join-Path -Path $TemplateRepoRoot -ChildPath $TemplateRef
}
else {
Write-Host "The containing dir is $containingDir and the template ref is $TemplateRef"
$resolvedTemplate = Join-Path -Path $containingDir -ChildPath $TemplateRef
}
return Resolve-Path -Path $resolvedTemplate
}
function Get-Yaml-Files {
param(
[string]$YmlPath,
[string]$RepoRoot
)
$templateReferences = @()
$lines = Get-Content -Path $YmlPath
foreach ($line in $lines) {
$result = Evaluate-Line -Line $line -Pattern "template:\s*(.+)"
if ($result) {
if ($result.EndsWith(".yml") -or $result.EndsWith("@self")){
$templateReferences += Resolve-Template-Ref -File $YmlPath -TemplateRef $result -TemplateRepoRoot $RepoRoot
}
}
}
$dependentTemplateReferences = @()
if ($templateReferences.Count -eq 0) {
# Read-Host "Stop! This is processing $YmlPath with NO RECURSION"
return $YmlPath
}
else {
# Read-Host "Stop! This is processing $YmlPath and recursing on: `n $(($templateReferences | % { "`t$_"}) -join "`n")"
foreach($ref in $templateReferences){
Write-Host "Calling Get-Yaml-Files with $ref"
$dependentTemplateReferences += Get-Yaml-Files -YmlPath $ref -RepoRoot $RepoRoot
}
return @($YmlPath) + $dependentTemplateReferences | Select-Object -Unique
}
}
$involvedYml = Get-Yaml-Files -YmlPath $InputFile -RepoRoot $RepoRoot
foreach ($resolvedTemplate in $involvedYml) {
dotnet run --project C:\repo\azure-sdk-tools\tools\pipeline-template-converter\Azure.Sdk.PipelineTemplateConverter\ -- -p $resolvedTemplate --overwrite
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to convert template $resolvedTemplate"
exit 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment