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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SimpleJson;
namespace TablesAndTuples
{
class Program
{
static readonly XNamespace ns = "http://wixtoolset.org/schemas/v4/wi/tables";
static readonly XName TableDefinition = ns + "tableDefinition";
static readonly XName ColumnDefinition = ns + "columnDefinition";
static readonly XName Name = "name";
static readonly XName Type = "type";
static void Main(string[] args)
{
if (args.Length == 0)
{
return;
}
else if (Path.GetExtension(args[0]) == ".xml")
{
if (args.Length < 2)
{
Console.WriteLine("Need to specify output json file as well.");
}
ReadXmlWriteJson(Path.GetFullPath(args[0]), Path.GetFullPath(args[1]));
}
else if (Path.GetExtension(args[0]) == ".json")
{
string prefix = null;
if (args.Length < 2)
{
Console.WriteLine("Need to specify output folder.");
}
else if (args.Length > 2)
{
prefix = args[2];
}
ReadJsonWriteCs(Path.GetFullPath(args[0]), Path.GetFullPath(args[1]), prefix);
}
}
private static void ReadXmlWriteJson(string inputPath, string outputPath)
{
var doc = XDocument.Load(inputPath);
var array = new JsonArray();
foreach (var tableDefinition in doc.Descendants(TableDefinition))
{
var tupleType = tableDefinition.Attribute(Name).Value;
var fields = new JsonArray();
foreach (var columnDefinition in tableDefinition.Elements(ColumnDefinition))
{
var fieldName = columnDefinition.Attribute(Name).Value;
var type = columnDefinition.Attribute(Type).Value;
if (type == "localized")
{
type = "string";
}
else if (type == "object")
{
type = "path";
}
var field = new JsonObject
{
{ fieldName, type }
};
fields.Add(field);
}
var obj = new JsonObject
{
{ tupleType, fields }
};
array.Add(obj);
}
array.Sort(CompareTupleDefinitions);
var strat = new PocoJsonSerializerStrategy();
var json = SimpleJson.SimpleJson.SerializeObject(array, strat);
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
File.WriteAllText(outputPath, json);
}
private static void ReadJsonWriteCs(string inputPath, string outputFolder, string prefix)
{
var json = File.ReadAllText(inputPath);
var tuples = SimpleJson.SimpleJson.DeserializeObject(json) as JsonArray;
var tupleNames = new List<string>();
foreach (var tupleDefinition in tuples.Cast<JsonObject>())
{
var tupleName = tupleDefinition.Keys.Single();
var fields = tupleDefinition.Values.Single() as JsonArray;
var list = GetFields(fields).ToList();
tupleNames.Add(tupleName);
var text = GenerateTupleFileText(prefix, tupleName, list);
var pathTuple = Path.Combine(outputFolder, tupleName + "Tuple.cs");
Console.WriteLine("Writing: {0}", pathTuple);
File.WriteAllText(pathTuple, text);
}
var content = TupleNamesFileContent(prefix, tupleNames);
var pathNames = Path.Combine(outputFolder, String.Concat(prefix, "TupleDefinitions.cs"));
Console.WriteLine("Writing: {0}", pathNames);
File.WriteAllText(pathNames, content);
}
private static IEnumerable<(string Name, string Type, string ClrType, string AsFunction)> GetFields(JsonArray fields)
{
foreach (var field in fields.Cast<JsonObject>())
{
var fieldName = field.Keys.Single();
var fieldType = field.Values.Single() as string;
var clrType = ConvertToClrType(fieldType);
fieldType = ConvertToFieldType(fieldType);
var asFunction = $"As{fieldType}()";
yield return (Name: fieldName, Type: fieldType, ClrType: clrType, AsFunction: asFunction);
}
}
private static string GenerateTupleFileText(string prefix, string tupleName, List<(string Name, string Type, string ClrType, string AsFunction)> tupleFields)
{
var ns = prefix ?? "Data";
var toString = String.IsNullOrEmpty(prefix) ? null : ".ToString()";
var startTupleDef = String.Join(Environment.NewLine,
"// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.",
"",
"namespace WixToolset.{2}",
"{",
" using WixToolset.Data;",
" using WixToolset.{2}.Tuples;",
"",
" public static partial class {3}TupleDefinitions",
" {",
" public static readonly IntermediateTupleDefinition {1} = new IntermediateTupleDefinition(",
" {3}TupleDefinitionType.{1}{4},",
" new[]",
" {");
var fieldDef =
" new IntermediateFieldDefinition(nameof({1}TupleFields.{2}), IntermediateFieldType.{3}),";
var endTupleDef = String.Join(Environment.NewLine,
" },",
" typeof({1}Tuple));",
" }",
"}",
"",
"namespace WixToolset.{2}.Tuples",
"{",
" using WixToolset.Data;",
"",
" public enum {1}TupleFields",
" {");
var fieldEnum =
" {2},";
var startTuple = String.Join(Environment.NewLine,
" }",
"",
" public class {1}Tuple : IntermediateTuple",
" {",
" public {1}Tuple() : base({3}TupleDefinitions.{1}, null, null)",
" {",
" }",
"",
" public {1}Tuple(SourceLineNumber sourceLineNumber, Identifier id = null) : base({3}TupleDefinitions.{1}, sourceLineNumber, id)",
" {",
" }",
"",
" public IntermediateField this[{1}TupleFields index] => this.Fields[(int)index];");
var fieldProp = String.Join(Environment.NewLine,
"",
" public {4} {2}",
" {",
" get => this.Fields[(int){1}TupleFields.{2}].{5};",
" set => this.Set((int){1}TupleFields.{2}, value);",
" }");
var endTuple = String.Join(Environment.NewLine,
" }",
"}");
var sb = new StringBuilder();
sb.AppendLine(startTupleDef.Replace("{1}", tupleName).Replace("{2}", ns).Replace("{3}", prefix).Replace("{4}", toString));
foreach (var field in tupleFields)
{
sb.AppendLine(fieldDef.Replace("{1}", tupleName).Replace("{2}", field.Name).Replace("{3}", field.Type).Replace("{4}", field.ClrType).Replace("{5}", field.AsFunction));
}
sb.AppendLine(endTupleDef.Replace("{1}", tupleName).Replace("{2}", ns).Replace("{3}", prefix));
foreach (var field in tupleFields)
{
sb.AppendLine(fieldEnum.Replace("{1}", tupleName).Replace("{2}", field.Name).Replace("{3}", field.Type).Replace("{4}", field.ClrType).Replace("{5}", field.AsFunction));
}
sb.AppendLine(startTuple.Replace("{1}", tupleName).Replace("{2}", ns).Replace("{3}", prefix));
foreach (var field in tupleFields)
{
sb.AppendLine(fieldProp.Replace("{1}", tupleName).Replace("{2}", field.Name).Replace("{3}", field.Type).Replace("{4}", field.ClrType).Replace("{5}", field.AsFunction));
}
sb.Append(endTuple);
return sb.ToString();
}
private static string TupleNamesFileContent(string prefix, List<string> tupleNames)
{
var ns = prefix ?? "Data";
var header = String.Join(Environment.NewLine,
"// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.",
"",
"namespace WixToolset.{2}",
"{",
" using System;",
" using WixToolset.Data;",
"",
" public enum {3}TupleDefinitionType",
" {");
var namesFormat =
" {1},";
var midpoint = String.Join(Environment.NewLine,
" }",
"",
" public static partial class {3}TupleDefinitions",
" {",
" public static readonly Version Version = new Version(\"4.0.0\");",
"",
" public static IntermediateTupleDefinition ByName(string name)",
" {",
" if (!Enum.TryParse(name, out {3}TupleDefinitionType type))",
" {",
" return null;",
" }",
"",
" return ByType(type);",
" }",
"",
" public static IntermediateTupleDefinition ByType({3}TupleDefinitionType type)",
" {",
" switch (type)",
" {");
var caseFormat = String.Join(Environment.NewLine,
" case {3}TupleDefinitionType.{1}:",
" return {3}TupleDefinitions.{1};",
"");
var footer = String.Join(Environment.NewLine,
" default:",
" throw new ArgumentOutOfRangeException(nameof(type));",
" }",
" }",
" }",
"}");
var sb = new StringBuilder();
sb.AppendLine(header.Replace("{2}", ns).Replace("{3}", prefix));
foreach (var tupleName in tupleNames)
{
sb.AppendLine(namesFormat.Replace("{1}", tupleName).Replace("{2}", ns).Replace("{3}", prefix));
}
sb.AppendLine(midpoint.Replace("{2}", ns).Replace("{3}", prefix));
foreach (var tupleName in tupleNames)
{
sb.AppendLine(caseFormat.Replace("{1}", tupleName).Replace("{2}", ns).Replace("{3}", prefix));
}
sb.AppendLine(footer);
return sb.ToString();
}
private static string ConvertToFieldType(string fieldType)
{
switch (fieldType.ToLowerInvariant())
{
case "bool":
return "Bool";
case "string":
case "preserved":
return "String";
case "number":
return "Number";
case "path":
return "Path";
}
throw new ArgumentException(fieldType);
}
private static string ConvertToClrType(string fieldType)
{
switch (fieldType.ToLowerInvariant())
{
case "bool":
return "bool";
case "string":
case "preserved":
return "string";
case "number":
return "int";
case "path":
return "string";
}
throw new ArgumentException(fieldType);
}
private static int CompareTupleDefinitions(object x, object y)
{
var first = (JsonObject)x;
var second = (JsonObject)y;
var firstType = first.Keys.Single();
var secondType = second.Keys.Single();
return firstType.CompareTo(secondType);
}
}
}
|