Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion Bicep.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution
src\Directory.Build.targets = src\Directory.Build.targets
global.json = global.json
LICENSE = LICENSE
NuGet.config = NuGet.config
README.md = README.md
SECURITY.md = SECURITY.md
EndProjectSection
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core/LanguageConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static class LanguageConstants
public const string DisableNextLineDiagnosticsKeyword = "disable-next-line";

public static readonly Regex ArmTemplateSchemaRegex = new(@"https?:\/\/schema\.management\.azure\.com\/schemas\/([^""\/]+\/[a-zA-Z]*[dD]eploymentTemplate\.json)#?");
public static readonly Regex ArmParametersSchemaRegex = new(@"https?:\/\/schema\.management\.azure\.com\/schemas\/([^""\/]+\/[dD]eploymentParameters\.json)#?");

public static readonly ImmutableSortedSet<string> DeclarationKeywords = ImmutableSortedSet.Create(
StringComparer.Ordinal,
Expand Down
33 changes: 19 additions & 14 deletions src/Bicep.Decompiler/BicepDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,46 @@ public async Task<DecompileResult> Decompile(Uri bicepUri, string jsonContent, D
bicepUri,
this.PrintFiles(workspace));
}
public DecompileResult DecompileParameters(string contents, Uri entryBicepparamUri, Uri? bicepFileUri)
public DecompileResult DecompileParameters(string contents, Uri entryBicepparamUri, Uri? bicepFileUri, DecompileParamOptions? options = null)
{
options ??= new();

var workspace = new Workspace();

var program = DecompileParametersFile(contents, entryBicepparamUri, bicepFileUri);
var program = DecompileParametersFile(contents, entryBicepparamUri, bicepFileUri, options);

var bicepparamFile = SourceFileFactory.CreateBicepParamFile(entryBicepparamUri, program.ToString());

workspace.UpsertSourceFile(bicepparamFile);

return new DecompileResult(entryBicepparamUri, this.PrintFiles(workspace));
return new(entryBicepparamUri, this.PrintFiles(workspace));
}

private ProgramSyntax DecompileParametersFile(string jsonInput, Uri entryBicepparamUri, Uri? bicepFileUri)
private ProgramSyntax DecompileParametersFile(string jsonInput, Uri entryBicepparamUri, Uri? bicepFileUri, DecompileParamOptions options)
{
var statements = new List<SyntaxBase>();

var jsonObject = JTokenHelpers.LoadJson(jsonInput, JObject.Load, ignoreTrailingContent: false);
var bicepPath = bicepFileUri is { } ? PathHelper.GetRelativePath(entryBicepparamUri, bicepFileUri) : null;
var jsonObject = JTokenHelpers.LoadJson(jsonInput, JObject.Load, ignoreTrailingContent: options.IgnoreTrailingInput);

statements.Add(new UsingDeclarationSyntax(
SyntaxFactory.UsingKeywordToken,
bicepPath is { } ?
SyntaxFactory.CreateStringLiteral(bicepPath) :
SyntaxFactory.CreateStringLiteralWithComment("", "TODO: Provide a path to a bicep template")));
if (options.IncludeUsingDeclaration)
{
var bicepPath = bicepFileUri is not null ? PathHelper.GetRelativePath(entryBicepparamUri, bicepFileUri) : null;
statements.Add(new UsingDeclarationSyntax(
SyntaxFactory.UsingKeywordToken,
bicepPath is not null
? SyntaxFactory.CreateStringLiteral(bicepPath)
: SyntaxFactory.CreateStringLiteralWithComment("", "TODO: Provide a path to a bicep template")));

statements.Add(SyntaxFactory.DoubleNewlineToken);
statements.Add(SyntaxFactory.DoubleNewlineToken);
}

var parameters = (TemplateHelpers.GetProperty(jsonObject, "parameters")?.Value as JObject ?? new JObject()).Properties();

foreach (var parameter in parameters)
{
var metadata = parameter.Value?["metadata"];
var metadata = parameter.Value["metadata"];

if (metadata is { })
if (metadata is not null)
{
statements.Add(ParseParameterWithComment(metadata));
statements.Add(SyntaxFactory.NewlineToken);
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Decompiler/DecompileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ public record DecompileOptions(
bool AllowMissingParamsAndVarsInNestedTemplates = false,
bool IgnoreTrailingInput = true
);

public record DecompileParamOptions(
bool IgnoreTrailingInput = true,
bool IncludeUsingDeclaration = true
);
Loading
Loading