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

Paste as BicepParams #15897

Merged
merged 10 commits into from
Jan 15, 2025
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