Skip to content

Commit 0865b18

Browse files
authored
Merge pull request connamara#982 from gbirchmeier/ddtool-path-check
DDTool improvements
2 parents 4bae039 + ec72b7d commit 0865b18

File tree

18 files changed

+186
-61
lines changed

18 files changed

+186
-61
lines changed

DDTool/DDTool/DDTool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace DDTool.Generators;
6+
7+
public static class GenCsproj {
8+
private static string ProjFilePath(string outputDir, string ddName) {
9+
return Path.Join(outputDir, "Messages", ddName, $"QuickFix.{ddName}.csproj");
10+
}
11+
12+
public static bool IsExistingCsproj(string outputDir, string ddName) {
13+
return File.Exists(
14+
ProjFilePath(outputDir, ddName));
15+
}
16+
17+
public static string WriteFile(string outputDir, string ddName, string repoRoot) {
18+
string csprojPath = ProjFilePath(outputDir, ddName);
19+
File.WriteAllText(
20+
csprojPath, Generate(ddName, repoRoot));
21+
return csprojPath;
22+
}
23+
24+
private static string Generate(string name, string repoRoot) {
25+
string qfPath = Path.Join(repoRoot, "QuickFIXn", "QuickFix.csproj");
26+
27+
var lines = new List<string>
28+
{
29+
"<Project Sdk=\"Microsoft.NET.Sdk\">",
30+
" <!--",
31+
" This is a generated file. HOWEVER, the generator won't clobber it if it exists.",
32+
" If you want it regenerated, you must delete it.",
33+
" -->",
34+
"",
35+
" <PropertyGroup>",
36+
" <TargetFramework>net8.0</TargetFramework>",
37+
$" <Description>Custom '{name}' build of QF/n message definitions</Description>",
38+
" <Nullable>enable</Nullable>",
39+
" </PropertyGroup>",
40+
"",
41+
" <ItemGroup>",
42+
$" <ProjectReference Include=\"{qfPath}\" />",
43+
" </ItemGroup>",
44+
"</Project>",
45+
""
46+
};
47+
48+
return string.Join(Environment.NewLine, lines);
49+
}
50+
}

DDTool/DDTool/Generators/GenFieldTags.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public static class GenFieldTags {
1212
/// <summary>
1313
/// Returns path of file that is written
1414
/// </summary>
15-
/// <param name="baseDir"></param>
15+
/// <param name="repoRootDir"></param>
1616
/// <param name="fields"></param>
1717
/// <returns></returns>
18-
public static string WriteFile(string baseDir, List<DDField> fields) {
19-
string fieldTagsPath = Path.Join(baseDir, "QuickFIXn", "Fields", "FieldTags.cs");
18+
public static string WriteFile(string repoRootDir, List<DDField> fields) {
19+
string fieldTagsPath = Path.Join(repoRootDir, "QuickFIXn", "Fields", "FieldTags.cs");
2020
File.WriteAllText(fieldTagsPath, Generate(fields));
2121
return fieldTagsPath;
2222
}

DDTool/DDTool/Generators/GenFields.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Text.RegularExpressions;
65
using DDTool.Structures;
76

@@ -14,11 +13,11 @@ public static class GenFields {
1413
/// <summary>
1514
/// Returns path of file that is written
1615
/// </summary>
17-
/// <param name="baseDir"></param>
16+
/// <param name="repoRootDir"></param>
1817
/// <param name="fields"></param>
1918
/// <returns></returns>
20-
public static string WriteFile(string baseDir, List<DDField> fields) {
21-
string fieldsPath = Path.Join(baseDir, "QuickFIXn", "Fields", "Fields.cs");
19+
public static string WriteFile(string repoRootDir, List<DDField> fields) {
20+
string fieldsPath = Path.Join(repoRootDir, "QuickFIXn", "Fields", "Fields.cs");
2221
File.WriteAllText(fieldsPath, Generate(fields));
2322
return fieldsPath;
2423
}

DDTool/DDTool/Generators/GenMessageFactories.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static List<string> WriteFiles(string baseDir, List<DataDictionary> dds)
1717
}
1818

1919
private static string WriteFile(string baseDir, DataDictionary dd) {
20-
string filePath = Path.Join(baseDir, "Messages", dd.IdentifierNoDots, "MessageFactory.cs");
20+
string filePath = Path.Join(baseDir, "Messages", dd.Name, "MessageFactory.cs");
21+
Directory.CreateDirectory(
22+
Path.GetDirectoryName(filePath)!);
2123
File.WriteAllText(filePath, Generate(dd));
2224
return filePath;
2325
}
@@ -30,7 +32,7 @@ private static string Generate(DataDictionary dd) {
3032
"using System.Collections.Generic;",
3133
"using QuickFix.FixValues;",
3234
"",
33-
$"namespace QuickFix.{dd.IdentifierNoDots};",
35+
$"namespace QuickFix.{dd.Name};",
3436
"",
3537
"public class MessageFactory : IMessageFactory",
3638
"{",
@@ -52,7 +54,7 @@ private static string Generate(DataDictionary dd) {
5254

5355
// TODO: foreach order is technically non-deterministic (though not in practice)
5456
foreach (var msg in dd.Messages.Values) {
55-
var fullname = $"QuickFix.{dd.IdentifierNoDots}.{msg.Name}";
57+
var fullname = $"QuickFix.{dd.Name}.{msg.Name}";
5658
lines.Add(new string(' ', 12) + $"{fullname}.MsgType => new {fullname}(),");
5759
}
5860

@@ -75,13 +77,13 @@ private static string Generate(DataDictionary dd) {
7577

7678
List<string> xLines = new();
7779

78-
xLines.Add($"if (QuickFix.{dd.IdentifierNoDots}.{msg.Name}.MsgType.Equals(msgType))");
80+
xLines.Add($"if (QuickFix.{dd.Name}.{msg.Name}.MsgType.Equals(msgType))");
7981
xLines.Add("{");
8082
xLines.Add(" switch (correspondingFieldId)");
8183
xLines.Add(" {");
8284

8385
foreach (var group in groups) {
84-
AppendGroupCases(xLines, group, $"QuickFix.{dd.IdentifierNoDots}.{msg.Name}");
86+
AppendGroupCases(xLines, group, $"QuickFix.{dd.Name}.{msg.Name}");
8587
}
8688

8789
xLines.Add(" }");

DDTool/DDTool/Generators/GenMessages.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public static List<string> WriteFilesForDD(string baseDir, DataDictionary dd) {
2727

2828
private static string WriteBaseMessageFile(string baseDir, DataDictionary dd) {
2929
var beginString = dd.IdentifierNoDots.Contains("FIX50") ? "FIXT11" : dd.IdentifierNoDots;
30-
string filePath = Path.Join($"{baseDir}", "Messages", dd.IdentifierNoDots, "Message.cs");
30+
string filePath = Path.Join($"{baseDir}", "Messages", dd.Name, "Message.cs");
31+
Directory.CreateDirectory(
32+
Path.GetDirectoryName(filePath)!);
3133

3234
var lines = new List<string>
3335
{
3436
"// This is a generated file. Don't edit it directly!",
3537
"",
36-
$"namespace QuickFix.{dd.IdentifierNoDots};",
38+
$"namespace QuickFix.{dd.Name};",
3739
"",
3840
"public abstract class Message : QuickFix.Message",
3941
"{",
@@ -51,7 +53,9 @@ private static string WriteBaseMessageFile(string baseDir, DataDictionary dd) {
5153
}
5254

5355
private static string WriteMessageFile(string baseDir, DDMessage msg, DataDictionary dd) {
54-
string filePath = Path.Join($"{baseDir}", "Messages", dd.IdentifierNoDots, $"{msg.Name}.cs");
56+
string filePath = Path.Join($"{baseDir}", "Messages", dd.Name, $"{msg.Name}.cs");
57+
Directory.CreateDirectory(
58+
Path.GetDirectoryName(filePath)!);
5559

5660
var lines = new List<string>
5761
{
@@ -60,7 +64,7 @@ private static string WriteMessageFile(string baseDir, DDMessage msg, DataDictio
6064
"using System;",
6165
"using QuickFix.Fields;",
6266
"",
63-
$"namespace QuickFix.{dd.IdentifierNoDots};",
67+
$"namespace QuickFix.{dd.Name};",
6468
"",
6569
$"public class {msg.Name} : Message",
6670
"{",

DDTool/DDTool/Options.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace DDTool;
66

77
public class Options {
8-
public string OutputDir { get; }
8+
public string? RepoRoot { get; }
9+
public bool HasRepoRoot => !string.IsNullOrEmpty(RepoRoot);
910

11+
public string? OutputDir { get; }
1012
public bool HasOutputDir => !string.IsNullOrEmpty(OutputDir);
1113

12-
public List<string> DDFiles { get; } = new List<string>();
14+
public List<string> DDFiles { get; } = [];
1315

1416
public Options(string[] args) {
1517
var errors = new List<string>();
@@ -26,6 +28,11 @@ public Options(string[] args) {
2628
}
2729

2830
switch (next) {
31+
case "--reporoot":
32+
RepoRoot = argList.First();
33+
argList.RemoveFirst();
34+
break;
35+
2936
case "--outputdir":
3037
OutputDir = argList.First();
3138
argList.RemoveFirst();
@@ -35,7 +42,7 @@ public Options(string[] args) {
3542
if (next.StartsWith("-"))
3643
errors.Add($"Unrecognized option: {next}");
3744
else {
38-
// All done with cmd line options now,
45+
// All done with cmd-line options now,
3946
// the rest are files.
4047
DDFiles.Add(next);
4148
foreach (var arg in argList) {

DDTool/DDTool/Parsers/DDParser.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public static DataDictionary ParseFile(string path) {
1717
VersionParser.SetVersionInfo(doc, dd);
1818
FieldParser.ParseFields(doc, dd);
1919
MessageParser.ParseMessages(doc, dd);
20-
/*
21-
ParseHeader(RootDoc);
22-
ParseTrailer(RootDoc)
23-
*/
2420
}
2521

2622
return dd;

DDTool/DDTool/Parsers/FieldParser.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ public static void ParseFields(XmlDocument doc, DataDictionary dd) {
1414
}
1515

1616
private static DDField CreateField(XmlNode node) {
17-
string tagstr = node.Attributes["number"].Value;
18-
string name = node.Attributes["name"].Value;
19-
string fldType = node.Attributes["type"].Value;
17+
string tagstr = node.Attributes!["number"]!.Value;
18+
string name = node.Attributes["name"]!.Value;
19+
string fldType = node.Attributes["type"]!.Value;
2020
int tag = int.Parse(tagstr);
2121
List<EnumValue> enums = new();
2222

2323
if (node.HasChildNodes) {
24-
foreach (XmlNode enumEl in node.SelectNodes(".//value")) {
25-
if (enumEl.Attributes["description"] is null)
24+
foreach (XmlNode enumEl in node.SelectNodes(".//value")!) {
25+
if (enumEl.Attributes!["description"] is null)
2626
throw new Exception($"Node {tagstr}/{name} contains a <value> without a 'description' attribute");
2727
if (enumEl.Attributes["enum"] is null)
2828
throw new Exception($"Node {tagstr}/{name} contains a <value> without a 'enum' attribute");
2929

3030
enums.Add(new EnumValue(
31-
enumEl.Attributes["description"].Value,
32-
enumEl.Attributes["enum"].Value));
31+
enumEl.Attributes["description"]!.Value,
32+
enumEl.Attributes["enum"]!.Value));
3333
}
3434
}
3535

DDTool/DDTool/Parsers/MessageParser.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ namespace DDTool.Parsers;
77

88
public static class MessageParser {
99
public static void ParseMessages(XmlDocument doc, DataDictionary dd) {
10-
XmlNodeList nodeList = doc.SelectNodes("//messages/message");
10+
XmlNodeList nodeList = doc.SelectNodes("//messages/message")!;
1111
foreach (XmlNode msgNode in nodeList) {
1212
dd.AddMessage(CreateMessage(msgNode, dd, doc));
1313
}
1414
}
1515

1616
private static DDMessage CreateMessage(XmlNode node, DataDictionary dd, XmlDocument doc) {
1717
var ddMsg = new DDMessage(
18-
node.Attributes["name"].Value,
19-
node.Attributes["msgtype"].Value,
20-
node.Attributes["msgcat"].Value);
18+
node.Attributes!["name"]!.Value,
19+
node.Attributes["msgtype"]!.Value,
20+
node.Attributes["msgcat"]!.Value);
2121

2222
foreach (XmlNode childNode in node.ChildNodes)
2323
ReadChildNode(childNode, ddMsg, dd, doc);
@@ -26,7 +26,7 @@ private static DDMessage CreateMessage(XmlNode node, DataDictionary dd, XmlDocum
2626
}
2727

2828
private static DDGroup CreateGroup(XmlNode node, DataDictionary dd, XmlDocument doc) {
29-
var groupName = node.Attributes["name"].Value;
29+
var groupName = node.Attributes!["name"]!.Value;
3030
var counterField = dd.LookupField(groupName);
3131

3232
if (node.ChildNodes.Count < 1)
@@ -44,20 +44,20 @@ private static DDGroup CreateGroup(XmlNode node, DataDictionary dd, XmlDocument
4444

4545
private static void ReadChildNode(
4646
XmlNode childNode, IElementSequence elSeq, DataDictionary dd, XmlDocument doc, bool overrideReq = false) {
47-
bool req = childNode.Attributes["required"]?.Value == "Y" && !overrideReq;
47+
bool req = childNode.Attributes!["required"]?.Value == "Y" && !overrideReq;
4848

4949
switch (childNode.Name.ToLowerInvariant()) {
5050
case "field":
51-
elSeq.AddElement(dd.LookupField(childNode.Attributes["name"].Value), req);
51+
elSeq.AddElement(dd.LookupField(childNode.Attributes["name"]!.Value), req);
5252
break;
5353

5454
case "group":
5555
elSeq.AddElement(CreateGroup(childNode, dd, doc), req);
5656
break;
5757

5858
case "component":
59-
var componentName = childNode.Attributes["name"].Value;
60-
XmlNode compNode = doc.SelectSingleNode($"//components/component[@name='{componentName}']");
59+
var componentName = childNode.Attributes["name"]!.Value;
60+
XmlNode compNode = doc.SelectSingleNode($"//components/component[@name='{componentName}']")!;
6161

6262
if (compNode == null)
6363
throw new ParsingException($"Can't find component: {componentName}");

0 commit comments

Comments
 (0)