-
Notifications
You must be signed in to change notification settings - Fork 110
/
JsonConfigurationVariableReplacer.cs
191 lines (166 loc) · 5.4 KB
/
JsonConfigurationVariableReplacer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Octostache;
namespace Calamari.Integration.JsonVariables
{
public class JsonConfigurationVariableReplacer : IJsonConfigurationVariableReplacer
{
public void ModifyJsonFile(string jsonFilePath, VariableDictionary variables)
{
var root = LoadJson(jsonFilePath);
var map = new JsonUpdateMap();
map.Load(root);
map.Update(variables);
SaveJson(jsonFilePath, root);
}
static JObject LoadJson(string jsonFilePath)
{
if (!File.Exists(jsonFilePath))
return new JObject();
if (new FileInfo(jsonFilePath).Length == 0)
return new JObject();
using (var file = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(file))
using (var json = new JsonTextReader(reader))
{
return JObject.Load(json);
}
}
static void SaveJson(string jsonFilePath, JObject root)
{
using (var file = new FileStream(jsonFilePath, FileMode.Create, FileAccess.Write))
using (var writer = new StreamWriter(file))
using (var json = new JsonTextWriter(writer))
{
json.Formatting = Formatting.Indented;
root.WriteTo(json);
}
}
}
public class JsonUpdateMap
{
private readonly IDictionary<string, Action<string>> map = new SortedDictionary<string, Action<string>>(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> path = new Stack<string>();
private string key;
public void Load(JObject json)
{
MapObject(json, true);
}
private void MapObject(JObject j, bool first = false)
{
if (!first)
{
map[key] = t => j.Replace(JToken.Parse(t));
}
foreach (var property in j.Properties())
{
MapProperty(property);
}
}
private void MapToken(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
MapObject(token.Value<JObject>());
break;
case JTokenType.Array:
MapArray(token.Value<JArray>());
break;
case JTokenType.Integer:
case JTokenType.Float:
MapNumber(token);
break;
case JTokenType.Boolean:
MapBool(token);
break;
default:
MapDefault(token);
break;
}
}
private void MapProperty(JProperty property)
{
PushPath(property.Name);
MapToken(property.Value);
PopPath();
}
private void MapDefault(JToken value)
{
map[key] = t => value.Replace(t == null ? null : JToken.FromObject(t));
}
private void MapNumber(JToken value)
{
map[key] = t =>
{
long longvalue;
if (long.TryParse(t, out longvalue))
{
value.Replace(JToken.FromObject(longvalue));
return;
}
double doublevalue;
if (double.TryParse(t, out doublevalue))
{
value.Replace(JToken.FromObject(doublevalue));
return;
}
value.Replace(JToken.FromObject(t));
};
}
private void MapBool(JToken value)
{
map[key] = t =>
{
bool boolvalue;
if (bool.TryParse(t, out boolvalue))
{
value.Replace(JToken.FromObject(boolvalue));
return;
}
value.Replace(JToken.FromObject(t));
};
}
private void MapArray(JContainer array)
{
map[key] = t => array.Replace(JToken.Parse(t));
for (var index = 0; index < array.Count; index++)
{
PushPath(index.ToString());
MapToken(array[index]);
PopPath();
}
}
private void PushPath(string p)
{
path.Push(p);
key = string.Join(":", path.Reverse());
}
private void PopPath()
{
path.Pop();
key = string.Join(":", path.Reverse());
}
public void Update(VariableDictionary variables)
{
foreach (var name in variables.GetNames().Where(x => !x.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase)))
{
if (map.ContainsKey(name))
{
try
{
map[name](variables.Get(name));
}
catch (Exception e)
{
Log.WarnFormat("Unable to set value for {0}. The following error occurred: {1}", name, e.Message);
}
}
}
}
}
}