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

Update module references #3467

Closed
Tracked by #3452
AlexanderSehr opened this issue Jul 30, 2023 · 0 comments · Fixed by #3570
Closed
Tracked by #3452

Update module references #3467

AlexanderSehr opened this issue Jul 30, 2023 · 0 comments · Fixed by #3570
Assignees
Labels
[cat] modules category: modules

Comments

@AlexanderSehr
Copy link
Contributor

AlexanderSehr commented Jul 30, 2023

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++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[cat] modules category: modules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants