forked from Wish-Org/QuickBooksSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
339 lines (302 loc) · 15.1 KB
/
Copy pathProgram.cs
File metadata and controls
339 lines (302 loc) · 15.1 KB
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Formatting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Schema;
namespace QuickBooksSharp.CodeGen
{
class Program
{
/// <summary>
/// -Download latest minor version XSD from https://developer.intuit.com/app/developer/qbo/docs/develop/explore-the-quickbooks-online-api/minor-versions
/// -Unzip into the xsd/3.{MinorVersion} folder
/// -Update QuickBooksUrl.cs MinorVersion
/// -Update README.md
/// -Run program
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string currentDir = Directory.GetCurrentDirectory();
string solutionPath = currentDir.Substring(0, currentDir.IndexOf("QuickBooksSharp"));
string xsdPath = Path.Combine(solutionPath, $"QuickBooksSharp/QuickBooksSharp.CodeGen/xsd/{QuickBooksUrl.Version}");
string outFilePath = Path.Combine(solutionPath, "QuickBooksSharp/QuickBooksSharp/Entities/Generated.cs");
var schemas = Directory.GetFiles(xsdPath)
.Select(filePath => XmlSchema.Read(new StringReader(File.ReadAllText(filePath)), null))
.ToArray();
var set = new XmlSchemaSet();
foreach (var s in schemas)
set.Add(s);
set.Compile();
var globalTypes = set.GlobalTypes.Values.Cast<XmlSchemaType>()
.ToArray();
var elts = set.GlobalElements.Values.Cast<XmlSchemaElement>().ToArray();
var eltNameToSubstitutedElements = elts.Where(e => !string.IsNullOrEmpty(e.SubstitutionGroup.Name))
.ToLookup(e => e.SubstitutionGroup.Name);
var simpleTypes = globalTypes.OfType<XmlSchemaSimpleType>().ToArray();
var complexTypes = globalTypes.OfType<XmlSchemaComplexType>().ToArray();
var enums = GenerateEnums(simpleTypes);
var classes = GenerateClasses(complexTypes, eltNameToSubstitutedElements);
GenerateOutFile(outFilePath, enums, classes);
FormatOutFile(outFilePath);
}
private static XmlSchemaComplexType GetParentComplexType(XmlSchemaObject o)
{
var p = o.Parent;
while (p != null && p is not XmlSchemaComplexType)
p = p.Parent;
return p as XmlSchemaComplexType;
}
private static IEnumerable<XmlSchemaParticle> GetParticlesRec(XmlSchemaComplexType complexType, XmlSchemaSequence seq)
{
foreach (var item in seq.Items)
{
var parentComplexType = GetParentComplexType(item);
if (parentComplexType != null && parentComplexType != complexType)
continue;
switch (item)
{
case XmlSchemaChoice choice:
yield return choice;
break;
case XmlSchemaElement elt:
yield return elt;
break;
case XmlSchemaSequence innerSeq:
foreach (var p in GetParticlesRec(complexType, innerSeq))
yield return p;
break;
case XmlSchemaAny _:
//IntuitAnyType => no pties
break;
default:
throw new Exception("Unexpected particle");
}
}
}
private static string GetTypeName(string typeName)
{
return typeName switch
{
"id" => "string",
"anyURI" => "string",
"positiveInteger" => "uint",
"boolean" => "bool",
"date" => "DateOnly",
"dateTime" => "DateTimeOffset",
"anyType" => "object",
_ => typeName
};
}
private static PropertyModel GetPropertyFromElt(XmlSchemaElement elt, bool forceIsNullable, bool forceIsArray)
{
return new PropertyModel
{
Name = elt.QualifiedName.Name ?? throw new Exception(),
TypeName = GetTypeName(elt.ElementSchemaType.QualifiedName.Name),
IsNullable = elt.MinOccurs == 0 || forceIsNullable,
IsArray = elt.MaxOccurs == decimal.MaxValue || forceIsArray
};
}
private static PropertyModel GetPropertyFromAttribute(XmlSchemaAttribute attr)
{
return new PropertyModel
{
Name = attr.QualifiedName.Name ?? throw new Exception(),
TypeName = GetTypeName(attr.AttributeSchemaType.QualifiedName.Name),
IsNullable = attr.Use == XmlSchemaUse.Optional,
IsArray = false,
};
}
private static ClassModel[] GenerateClasses(XmlSchemaComplexType[] complexTypes, ILookup<string, XmlSchemaElement> eltNameToSubstitutedElements)
{
return complexTypes.Select(t =>
{
var pties = new List<PropertyModel>();
foreach (var attr in t.Attributes.Cast<XmlSchemaAttribute>())
pties.Add(GetPropertyFromAttribute(attr));
if (t.ContentTypeParticle.GetType().Name != "EmptyParticle")
{
var particles = GetParticlesRec(t, (XmlSchemaSequence)t.ContentTypeParticle);
var elts = particles.SelectMany(p => p switch
{
XmlSchemaChoice choice => choice.Items.Cast<XmlSchemaObject>().SelectMany(o =>
{
if (o is XmlSchemaElement e)
return new[] { (Elt: e, IsChoiceChild: true) };
else if (o is XmlSchemaSequence s)
return GetParticlesRec(t, s).Cast<XmlSchemaElement>().Select(e => (Elt: e, IsChoiceChild: true));
else
throw new Exception();
}),
XmlSchemaElement elt => new[] { (Elt: elt, IsChoiceChild: false) },
_ => throw new Exception()
});
foreach (var i in elts)
{
if (eltNameToSubstitutedElements.Contains(i.Elt.QualifiedName.Name))
{
bool isArray = i.Elt.MaxOccurs == decimal.MaxValue;
bool isNullable = i.Elt.MinOccurs == 0;
var subsitutionsPties = new List<PropertyModel>();
foreach (var subElt in eltNameToSubstitutedElements[i.Elt.QualifiedName.Name]
.Where(i => i.QualifiedName.Name != t.Name))
{
var pty = GetPropertyFromElt(subElt, true, isArray);
pties.Add(pty);
subsitutionsPties.Add(pty);
}
bool isAnyNullabe = isNullable || i.IsChoiceChild;
pties.Add(new PropertyModel
{
Name = i.Elt.QualifiedName.Name + (isArray ? "s" : ""),
TypeName = i.Elt.ElementSchemaType.QualifiedName.Name,
IsArray = isArray,
IsNullable = isAnyNullabe,
Code = "{ get => " + string.Join(" ?? ", subsitutionsPties.Select((p, index) => (index == subsitutionsPties.Count - 1 ? $"({i.Elt.ElementSchemaType.QualifiedName.Name}{(isArray ? "[]" : "")}{(isAnyNullabe ? "?" : "")})" : "") + p.Name)) + (isAnyNullabe ? "" : "!") + "; }"
});
}
else
{
pties.Add(GetPropertyFromElt(i.Elt, i.IsChoiceChild, false));
}
}
}
else
{
var contentModel = (XmlSchemaSimpleContent)t.ContentModel;
var extension = (XmlSchemaSimpleContentExtension)contentModel.Content;
pties.Add(new PropertyModel
{
Name = "value",
TypeName = t.BaseXmlSchemaType.QualifiedName.Name == "id" ? "string" : t.BaseXmlSchemaType.QualifiedName.Name,
IsArray = false,
IsNullable = false,
});
foreach (var attr in extension.Attributes.Cast<XmlSchemaAttribute>())
pties.Add(GetPropertyFromAttribute(attr));
}
bool isReferenceType = t.Name == "ReferenceType";
bool isCustomFieldDefinitionType = t.Name == "CustomFieldDefinition";
return new ClassModel
{
Name = t.Name,
//todo create a custom converter that can deseriaize to the correct CustomFieldDefinition dervived type
//For now, we simply mark the base type as a concrete type, since none of the dervied properties are returned by the API anyway
IsAbstract = isCustomFieldDefinitionType ? false : t.IsAbstract,
BaseName = isReferenceType ? null : t.BaseXmlSchemaType?.Name,
Properties = pties.ToArray()
};
}).Where(c => c.Name != null)
.ToArray();
}
private static EnumModel[] GenerateEnums(XmlSchemaSimpleType[] simpleTypes)
{
return simpleTypes.Select(t => new EnumModel
{
Name = t.Name,
Fields = (t.Content as XmlSchemaSimpleTypeRestriction)
.Facets
.Cast<XmlSchemaFacet>()
.Select(f => f.Value)
.Distinct()
.ToArray()
}).Where(e => e.Fields.Any())
.ToArray();
}
private static void GenerateOutFile(string outFilePath, EnumModel[] enums, ClassModel[] classes)
{
using (var writer = new StreamWriter(outFilePath))
{
writer.WriteLine("using System;");
writer.WriteLine("using System.Runtime.Serialization;");
writer.WriteLine("using System.Text.Json.Serialization;");
writer.WriteLine();
writer.WriteLine("namespace QuickBooksSharp.Entities");
writer.WriteLine("{");
foreach (var e in enums)
{
writer.WriteLine($"public enum {e.Name}");
writer.WriteLine("{");
writer.WriteLine($"Unspecified = 0,");
foreach (var name in e.Fields)
{
string safeName = GetSafePropertyName(name);
if (name != safeName)
writer.WriteLine($"[EnumMember(Value = \"{name}\")]");
writer.WriteLine($"{safeName},");
}
writer.WriteLine("}");
}
foreach (var c in classes)
{
writer.Write($"public {(c.IsAbstract ? "abstract" : "")} class {GetSafeClassName(c.Name)}");
if (c.BaseName != null)
writer.Write($" : {c.BaseName}");
writer.WriteLine();
writer.WriteLine("{");
foreach (var pty in c.Properties)
{
string safeName = GetSafePropertyName(pty.Name);
if (pty.Name != safeName)
writer.WriteLine($"[JsonPropertyName(\"{pty.Name}\")]");
if (c.Name == "BatchItemRequest")
writer.WriteLine(pty.Name == "IntuitObject" ? "[JsonIgnore]" : "[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]");
static string GetPropertyDeclaration(PropertyModel pty, string safeName, string typeName)
{
string ptyDecl = string.Empty;
ptyDecl += $"public {typeName}";
if (pty.IsArray)
ptyDecl += "[]";
if (pty.IsNullable)
ptyDecl += "?";
ptyDecl += $" {safeName} ";
ptyDecl += pty.Code ?? "{ get; set; }";
if (!pty.IsNullable && pty.Code == null)
ptyDecl += " = default!; ";
return ptyDecl;
}
string typeName = GetSafeClassName(pty.TypeName);
if (typeName != nameof(DateOnly))
writer.WriteLine(GetPropertyDeclaration(pty, safeName, typeName));
else
{
//DateOnly type is only support on .NET6+
writer.WriteLine("#if NET6_0_OR_GREATER");
writer.WriteLine(GetPropertyDeclaration(pty, safeName, typeName));
writer.WriteLine("#else");
writer.WriteLine(GetPropertyDeclaration(pty, safeName, "DateTime"));
writer.WriteLine("#endif");
}
}
writer.WriteLine("}");
}
writer.WriteLine("}");
}
}
private static void FormatOutFile(string outFilePath)
{
string rawText = File.ReadAllText(outFilePath);
var ws = new AdhocWorkspace();
var code = CSharpSyntaxTree.ParseText(rawText);
string formattedText = Formatter.Format(code.GetRoot(), ws).ToFullString();
File.WriteAllText(outFilePath, formattedText);
}
private static string GetSafeClassName(string name)
{
//Avoid name conflict with System.Threading.Tasks.Task
return name == "Task" ? "QbTask" : name;
}
private static string GetSafePropertyName(string name)
{
//must escape property names that conflict with keywords
if (new[] { "void" }.Contains(name))
return $"@{name}";
return Regex.Replace(name, "[-%)( ]", string.Empty);
}
}
}