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

Script to convert library to arm/json #256

Merged
merged 10 commits into from
Oct 26, 2021
Merged
Changes from 6 commits
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
111 changes: 111 additions & 0 deletions utilities/ConvertTo-ARMTemplate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<#
.SYNOPSIS
This script converts the module library from bicep to json based ARM templates.

.DESCRIPTION
The script finds all 'deploy.bicep' files and tries to convert them to json based ARM templates
by using the following steps.
1. Remove existing deploy.json files (to avoid collision)
2.a Convert deploy.bicep to deploy.json using bicep build
2.b Clean up the json file, i.e removing the metadata property
3. Remove bicep files and folders
4. Replace deploy.bicep with deploy.json in workflow files

.PARAMETER Path
Mandatory. The path to the root of the repo.

.PARAMETER CleanUp
Optional. Perform cleanup of bicep files after conversion.

.EXAMPLE
. .\tools\ConvertTo-ARMTemplate.ps1 -Path . -CleanUp

Converts bicep modules to json based ARM template, cleaning up all bicep files after conversion.
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[string] $Path,

[Parameter()]
[switch] $CleanUp
)

$rootPath = Get-Item -Path $Path | Select-Object -ExpandProperty FullName
$armFolderPath = Join-Path -Path $rootPath -ChildPath 'arm'

# Get all bicep files
$bicepFiles = Get-ChildItem -Path $armFolderPath -Filter deploy.bicep -Recurse
Write-Verbose "Convert bicep to json - $($bicepFiles.count) files"
foreach ($bicepFile in $bicepFiles) {
$bicepFilePath = $bicepFile.FullName
Write-Verbose "$bicepFilePath - Processing"
$moduleFolderPath = $bicepFile.Directory.FullName
Write-Verbose "$bicepFilePath - ModuleFolderPath - $moduleFolderPath"
$JSONFilePath = Join-Path -Path $moduleFolderPath -ChildPath 'deploy.json'
Write-Verbose "$bicepFilePath - JSONFilePath - $JSONFilePath"
$bicepFolderPath = Join-Path -Path $moduleFolderPath -ChildPath '.bicep'

# Remove existing json files
Write-Verbose "$bicepFilePath - Removing deploy.json"
if (Test-Path -Path $JSONFilePath) {
if ($PSCmdlet.ShouldProcess("File in path [$bicepFilePath]", 'Remove')) {
Remove-Item -Path $JSONFilePath -Force -Verbose
}
Write-Verbose "$bicepFilePath - Removing deploy.json - Done"
} else {
Write-Verbose "$bicepFilePath - Removing deploy.json - Skipped - Nothing to delete"
}

# Convert bicep to json
Write-Verbose "$bicepFilePath - Convert to json"
if ($PSCmdlet.ShouldProcess("File in path [$bicepFilePath]", 'Convert')) {
az bicep build --file $bicepFilePath --outfile $JSONFilePath
}
Write-Verbose "$bicepFilePath - Convert to json - Done"

# Cleanup json, remove metadate property in json
Write-Verbose "$bicepFilePath - Clean up json"
if (Test-Path -Path $JSONFilePath) {
$JSONFileContent = Get-Content -Path $JSONFilePath
$JSONObj = $JSONFileContent | ConvertFrom-Json
$JSONObj.PSObject.Properties.Remove('metadata')
$JSONFileContent = $JSONObj | ConvertTo-Json -Depth 100
if ($PSCmdlet.ShouldProcess("File in path [$JSONFilePath]", 'Overwrite')) {
Set-Content -Value $JSONFileContent -Path $JSONFilePath
}
Write-Verbose "$bicepFilePath - Clean up json - Done"
} else {
Write-Verbose "$bicepFilePath - Clean up json - Skipped - File not found (deploy.json)"
}

# Remove bicep files and folders
Write-Verbose "$bicepFilePath - Clean up bicep files"
if ($CleanUp) {
if ($PSCmdlet.ShouldProcess("File in path [$bicepFilePath]", 'Remove')) {
Remove-Item -Path $bicepFilePath -Force
}
Write-Verbose "$bicepFilePath - Clean up bicep files - Removed deploy.bicep"
if (Test-Path -Path $bicepFolderPath) {
if ($PSCmdlet.ShouldProcess("File in path [$bicepFolderPath]", 'Remove')) {
Remove-Item -Path $bicepFolderPath -Recurse -Force
}
Write-Verbose "$bicepFilePath - Clean up bicep files - Removed .bicep folder"
}
}
}

# Replace .bicep with .json in workflow files
$workflowFolderPath = Join-Path -Path $rootPath -ChildPath '.github\workflows'
$workflowFiles = Get-ChildItem -Path $workflowFolderPath -Filter 'ms.*.yml' -File
Write-Verbose "Update workflow files - $($workflowFiles.count) files"
foreach ($workflowFile in $workflowFiles) {
Write-Verbose "$workflowFile - Processing"
$content = Get-Content -Path $workflowFile
$content = $content.Replace("deploy.bicep'", "deploy.json'")
if ($PSCmdlet.ShouldProcess("File in path [$workflowFile]", 'Overwrite')) {
Set-Content -Value $content -Path $workflowFile
}
Write-Verbose "$workflowFile - Processing - Done"
}
Write-Verbose "Update workflow files - $($workflowFiles.count) files - Done"