Skip to content

Update module references #3467

@AlexanderSehr

Description

@AlexanderSehr

Update child & cross-module references in each module after the folder paths are updated.

You can use the following script to automate this step (note: It does NOT update the READMEs but focuses on the templates)

$repoRootPath = 'C:\dev\ip\Azure-ResourceModules\ResourceModules'
$mappings = ConvertFrom-Json (Get-Content -Path (Join-Path $repoRootPath 'rtMapping.jsonc') -Raw) -AsHashtable

$templateFilePaths = (Get-ChildItem -Path (Join-Path $repoRootPath 'modules') -File -Recurse -Include 'main.bicep').FullName | Sort-Object


$foreachIndex = 0
foreach ($templateFilePath in $templateFilePaths) {

    $percentageComplete = [Math]::Round(($foreachIndex / $templateFilePaths.count) * 100)


    $templateFileContent = Get-Content -Path $templateFilePath
    $updateApplied = $false

    for ($index = 0; $index -lt $templateFileContent.Count; $index++) {

        $line = $templateFileContent[$index]

        if ($line -match "(.+')(.+\/main.bicep)('.+)") {

            $referencePrefix = $Matches[1]
            $reference = $Matches[2]
            $referenceSuffix = $Matches[3]

            $updatedReference = (( $reference -split '\/') | ForEach-Object {
                    $mappings.ContainsKey($_) ? $mappings[$_] : $_
                }) -join '/'

            $templateFileContent[$index] = '{0}{1}{2}' -f $referencePrefix, $updatedReference, $referenceSuffix
            $updateApplied = $true
        }
    }

    $shortPath = $templateFilePath -replace [Regex]::Escape("$repoRootPath\modules\")
    if ($updateApplied) {
        $null = Set-Content -Path $templateFilePath -Value $templateFileContent
        Write-Verbose "Updated file [$shortPath]" -Verbose
    } else {
        Write-Verbose "Skipped file [$shortPath]" -Verbose
    }

    Write-Progress -Activity ('Handled file [{0}/{1}]' -f ($foreachIndex + 1 ), $templateFilePaths.Count) -Status "$percentageComplete% Complete:" -PercentComplete $percentageComplete
    $foreachIndex++
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions