-
Notifications
You must be signed in to change notification settings - Fork 727
/
PlaceholderParametersJsonWriter.cs
132 lines (110 loc) · 5.22 KB
/
PlaceholderParametersJsonWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using Bicep.Core.Emit.Options;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Microsoft.WindowsAzure.ResourceStack.Common.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Bicep.Core.Emit
{
public class PlaceholderParametersJsonWriter
{
public PlaceholderParametersJsonWriter(SemanticModel semanticModel, IncludeParamsOption includeParams)
{
this.Context = new EmitterContext(semanticModel);
this.IncludeParams = includeParams;
}
private EmitterContext Context { get; }
private IncludeParamsOption IncludeParams { get; }
public void Write(JsonTextWriter writer, string existingContent)
{
var existingParamsContent = null as JToken;
var existingContentVersion = "1.0.0.0";
if (!string.IsNullOrWhiteSpace(existingContent))
{
try
{
existingParamsContent = JToken.Parse(existingContent);
existingContentVersion = existingParamsContent.Value<string>("contentVersion") ?? existingContentVersion;
}
catch
{
// content of the existing file is not valid json, ignore merging it
}
}
// Template is used for calcualting template hash, template jtoken is used for writing to file.
var templateJToken = GenerateTemplate(existingContentVersion);
if (existingParamsContent != null)
{
var existingParameters = existingParamsContent?.SelectToken("parameters")?.ToObject<JObject>();
if (existingParameters is not null)
{
var mergeSettings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union };
foreach (var existingParameter in existingParameters)
{
var existingParameterName = existingParameter.Key;
var existingParameterValue = existingParameter.Value;
if (existingParameterValue is not null)
{
if (templateJToken.SelectToken($"parameters.{existingParameterName}") is JObject existingParameterObject)
{
existingParameterObject.Merge(existingParameterValue, mergeSettings);
}
}
}
}
}
templateJToken.WriteTo(writer);
}
private JToken GenerateTemplate(string contentVersion)
{
using var stringWriter = new StringWriter();
using var jsonWriter = new PositionTrackingJsonTextWriter(stringWriter);
var emitter = new ExpressionEmitter(jsonWriter, this.Context);
jsonWriter.WriteStartObject();
emitter.EmitProperty("$schema", "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#");
emitter.EmitProperty("contentVersion", contentVersion);
var allParameterDeclarations = this.Context.SemanticModel.Root.ParameterDeclarations;
var filteredParameterDeclarations = this.IncludeParams == IncludeParamsOption.All
? allParameterDeclarations
: allParameterDeclarations
.Where(e => e.DeclaringParameter.Modifier is not ParameterDefaultValueSyntax)
.ToImmutableArray();
if (filteredParameterDeclarations.Length > 0)
{
jsonWriter.WritePropertyName("parameters");
jsonWriter.WriteStartObject();
foreach (var parameterSymbol in filteredParameterDeclarations)
{
jsonWriter.WritePropertyName(parameterSymbol.Name);
jsonWriter.WriteStartObject();
switch (parameterSymbol.Type.Name)
{
case "string":
emitter.EmitProperty("value", "");
break;
case "int":
emitter.EmitProperty("value", () => jsonWriter.WriteValue(0));
break;
case "bool":
emitter.EmitProperty("value", () => jsonWriter.WriteValue(false));
break;
case "object":
emitter.EmitProperty("value", () => { jsonWriter.WriteStartObject(); jsonWriter.WriteEndObject(); });
break;
case "array":
emitter.EmitProperty("value", () => { jsonWriter.WriteStartArray(); jsonWriter.WriteEndArray(); });
break;
}
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndObject();
var content = stringWriter.ToString();
return content.FromJson<JToken>();
}
}
}