We have a lot of .bicep + .bicepparam files, and sometimes we have evolving ideas on how to structure those. This involves makes changes to many many files, to apply the new approach.
Some of these changes we can just do with text-based find-replace, either regex or normal. However, some changes require a bit more logic.
It would be ideal if we could have a script that can read in a .bicep file, run some logic on it, and then write back a modified version.
Pseudocode:
foreach (filepath in bicep_files)
bicep_model = read_bicep(filepath)
… custom logic here, make changes in model …
write_bicep(bicep_model, filepath)
With ARM / JSON, this is easy - there are many JSON serializers / deserializers out there. For bicep, I know of no such libraries / parsers.
Potential options I have considered:
- Use the Bicep CLI to read in the bicep file. Sadly, it has no commands for deserializing (parsing) a bicep file. It can compile to ARM/JSON, but then things like comments and variable references are lost, meaning we cannot round-trip back to bicep.
https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli - Use Bicep's JSON-RPC interface, which "allows for programmatic interaction with structured output". Sadly, this too does not have any methods to parse a bicep file into a losslessly machine-processable representation.
https://github.com/Azure/bicep/blob/main/src/Bicep.Cli/Rpc/ICliJsonRpcProtocol.cs - The VS Code bicep extension provides rich editing support, so presumably it parses bicep files under the hood into a machine-processable representation. Not sure how it does that though, and not sure how we would use this for scripted editing.
https://github.com/Azure/bicep/tree/main/src/vscode-bicep - The JSON-RPC server talks to the Bicep .NET classes under the hood - perhaps we can do that too, and accomplish the goal that way? Not sure how to get started here though.
https://github.com/Azure/bicep/blob/main/src/Bicep.Cli/Rpc/CliJsonRpcServer.cs