diff options
Diffstat (limited to 'src/TablesAndTuples/Program.cs')
| -rw-r--r-- | src/TablesAndTuples/Program.cs | 142 |
1 files changed, 139 insertions, 3 deletions
diff --git a/src/TablesAndTuples/Program.cs b/src/TablesAndTuples/Program.cs index c15c3c6d..bcec604c 100644 --- a/src/TablesAndTuples/Program.cs +++ b/src/TablesAndTuples/Program.cs | |||
| @@ -1,8 +1,9 @@ | |||
| 1 | using System; | 1 | using System; |
| 2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
| 3 | using System.IO; | 3 | using System.IO; |
| 4 | using System.Linq; | 4 | using System.Linq; |
| 5 | using System.Text; | 5 | using System.Text; |
| 6 | using System.Text.RegularExpressions; | ||
| 6 | using System.Xml.Linq; | 7 | using System.Xml.Linq; |
| 7 | using SimpleJson; | 8 | using SimpleJson; |
| 8 | 9 | ||
| @@ -27,9 +28,23 @@ namespace TablesAndTuples | |||
| 27 | if (args.Length < 2) | 28 | if (args.Length < 2) |
| 28 | { | 29 | { |
| 29 | Console.WriteLine("Need to specify output json file as well."); | 30 | Console.WriteLine("Need to specify output json file as well."); |
| 31 | return; | ||
| 30 | } | 32 | } |
| 33 | if (Path.GetExtension(args[1]) != ".json") | ||
| 34 | { | ||
| 35 | Console.WriteLine("Output needs to be .json"); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | string prefix = null; | ||
| 40 | if (args.Length > 2) | ||
| 41 | { | ||
| 42 | prefix = args[2]; | ||
| 43 | } | ||
| 44 | |||
| 45 | var csFile = Path.Combine(Path.GetDirectoryName(args[1]), String.Concat(prefix ?? "WindowsInstaller", "TableDefinitions.cs")); | ||
| 31 | 46 | ||
| 32 | ReadXmlWriteJson(Path.GetFullPath(args[0]), Path.GetFullPath(args[1])); | 47 | ReadXmlWriteJson(Path.GetFullPath(args[0]), Path.GetFullPath(args[1]), Path.GetFullPath(csFile), prefix); |
| 33 | } | 48 | } |
| 34 | else if (Path.GetExtension(args[0]) == ".json") | 49 | else if (Path.GetExtension(args[0]) == ".json") |
| 35 | { | 50 | { |
| @@ -37,6 +52,7 @@ namespace TablesAndTuples | |||
| 37 | if (args.Length < 2) | 52 | if (args.Length < 2) |
| 38 | { | 53 | { |
| 39 | Console.WriteLine("Need to specify output folder."); | 54 | Console.WriteLine("Need to specify output folder."); |
| 55 | return; | ||
| 40 | } | 56 | } |
| 41 | else if (args.Length > 2) | 57 | else if (args.Length > 2) |
| 42 | { | 58 | { |
| @@ -47,8 +63,10 @@ namespace TablesAndTuples | |||
| 47 | } | 63 | } |
| 48 | } | 64 | } |
| 49 | 65 | ||
| 50 | private static void ReadXmlWriteJson(string inputPath, string outputPath) | 66 | private static void ReadXmlWriteJson(string inputPath, string outputPath, string csOutputPath, string prefix) |
| 51 | { | 67 | { |
| 68 | ReadXmlWriteCs(inputPath, csOutputPath, prefix); | ||
| 69 | |||
| 52 | var doc = XDocument.Load(inputPath); | 70 | var doc = XDocument.Load(inputPath); |
| 53 | 71 | ||
| 54 | var array = new JsonArray(); | 72 | var array = new JsonArray(); |
| @@ -97,6 +115,14 @@ namespace TablesAndTuples | |||
| 97 | File.WriteAllText(outputPath, json); | 115 | File.WriteAllText(outputPath, json); |
| 98 | } | 116 | } |
| 99 | 117 | ||
| 118 | private static void ReadXmlWriteCs(string inputPath, string outputPath, string prefix) | ||
| 119 | { | ||
| 120 | var tableDefinitions = WixTableDefinition.LoadCollection(inputPath); | ||
| 121 | var text = GenerateCsTableDefinitionsFileText(prefix, tableDefinitions); | ||
| 122 | Console.WriteLine("Writing: {0}", outputPath); | ||
| 123 | File.WriteAllText(outputPath, text); | ||
| 124 | } | ||
| 125 | |||
| 100 | private static void ReadJsonWriteCs(string inputPath, string outputFolder, string prefix) | 126 | private static void ReadJsonWriteCs(string inputPath, string outputFolder, string prefix) |
| 101 | { | 127 | { |
| 102 | var json = File.ReadAllText(inputPath); | 128 | var json = File.ReadAllText(inputPath); |
| @@ -142,6 +168,116 @@ namespace TablesAndTuples | |||
| 142 | } | 168 | } |
| 143 | } | 169 | } |
| 144 | 170 | ||
| 171 | private static string GenerateCsTableDefinitionsFileText(string prefix, List<WixTableDefinition> tableDefinitions) | ||
| 172 | { | ||
| 173 | var ns = prefix ?? "Data"; | ||
| 174 | |||
| 175 | var startClassDef = String.Join(Environment.NewLine, | ||
| 176 | "// 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.", | ||
| 177 | "", | ||
| 178 | "namespace WixToolset.{1}", | ||
| 179 | "{", | ||
| 180 | " using WixToolset.Data.WindowsInstaller;", | ||
| 181 | "", | ||
| 182 | " public static class {2}TableDefinitions", | ||
| 183 | " {"); | ||
| 184 | var startTableDef = String.Join(Environment.NewLine, | ||
| 185 | " public static readonly TableDefinition {1} = new TableDefinition(", | ||
| 186 | " \"{1}\",", | ||
| 187 | " new[]", | ||
| 188 | " {"); | ||
| 189 | var columnDef = | ||
| 190 | " new ColumnDefinition(\"{1}\", ColumnType.{2}, {3}, primaryKey: {4}, nullable: {5}, ColumnCategory.{6}"; | ||
| 191 | var endColumnsDef = String.Join(Environment.NewLine, | ||
| 192 | " },"); | ||
| 193 | var unrealDef = | ||
| 194 | " unreal: true,"; | ||
| 195 | var endTableDef = String.Join(Environment.NewLine, | ||
| 196 | " tupleDefinitionName: \"{1}\",", | ||
| 197 | " tupleIdIsPrimaryKey: {2}", | ||
| 198 | " );", | ||
| 199 | ""); | ||
| 200 | var startAllTablesDef = String.Join(Environment.NewLine, | ||
| 201 | " public static readonly TableDefinition[] All = new[]", | ||
| 202 | " {"); | ||
| 203 | var allTableDef = | ||
| 204 | " {1},"; | ||
| 205 | var endAllTablesDef = | ||
| 206 | " };"; | ||
| 207 | var endClassDef = String.Join(Environment.NewLine, | ||
| 208 | " }", | ||
| 209 | "}"); | ||
| 210 | |||
| 211 | var sb = new StringBuilder(); | ||
| 212 | |||
| 213 | sb.AppendLine(startClassDef.Replace("{1}", ns).Replace("{2}", prefix)); | ||
| 214 | foreach (var tableDefinition in tableDefinitions) | ||
| 215 | { | ||
| 216 | sb.AppendLine(startTableDef.Replace("{1}", tableDefinition.Name)); | ||
| 217 | foreach (var columnDefinition in tableDefinition.Columns) | ||
| 218 | { | ||
| 219 | sb.Append(columnDef.Replace("{1}", columnDefinition.Name).Replace("{2}", columnDefinition.Type.ToString()).Replace("{3}", columnDefinition.Length.ToString()) | ||
| 220 | .Replace("{4}", columnDefinition.PrimaryKey.ToString().ToLower()).Replace("{5}", columnDefinition.Nullable.ToString().ToLower()).Replace("{6}", columnDefinition.Category.ToString())); | ||
| 221 | if (columnDefinition.MinValue.HasValue) | ||
| 222 | { | ||
| 223 | sb.AppendFormat(", minValue: {0}", columnDefinition.MinValue.Value); | ||
| 224 | } | ||
| 225 | if (columnDefinition.MaxValue.HasValue) | ||
| 226 | { | ||
| 227 | sb.AppendFormat(", maxValue: {0}", columnDefinition.MaxValue.Value); | ||
| 228 | } | ||
| 229 | if (!String.IsNullOrEmpty(columnDefinition.KeyTable)) | ||
| 230 | { | ||
| 231 | sb.AppendFormat(", keyTable: \"{0}\"", columnDefinition.KeyTable); | ||
| 232 | } | ||
| 233 | if (columnDefinition.KeyColumn.HasValue) | ||
| 234 | { | ||
| 235 | sb.AppendFormat(", keyColumn: {0}", columnDefinition.KeyColumn.Value); | ||
| 236 | } | ||
| 237 | if (!String.IsNullOrEmpty(columnDefinition.Possibilities)) | ||
| 238 | { | ||
| 239 | sb.AppendFormat(", possibilities: \"{0}\"", columnDefinition.Possibilities); | ||
| 240 | } | ||
| 241 | if (!String.IsNullOrEmpty(columnDefinition.Description)) | ||
| 242 | { | ||
| 243 | sb.AppendFormat(", description: \"{0}\"", columnDefinition.Description); | ||
| 244 | } | ||
| 245 | if (columnDefinition.ModularizeType.HasValue && columnDefinition.ModularizeType.Value != ColumnModularizeType.None) | ||
| 246 | { | ||
| 247 | sb.AppendFormat(", modularizeType: ColumnModularizeType.{0}", columnDefinition.ModularizeType.ToString()); | ||
| 248 | } | ||
| 249 | if (columnDefinition.ForceLocalizable) | ||
| 250 | { | ||
| 251 | sb.Append(", forceLocalizable: true"); | ||
| 252 | } | ||
| 253 | if (columnDefinition.UseCData) | ||
| 254 | { | ||
| 255 | sb.Append(", useCData: true"); | ||
| 256 | } | ||
| 257 | if (columnDefinition.Unreal) | ||
| 258 | { | ||
| 259 | sb.Append(", unreal: true"); | ||
| 260 | } | ||
| 261 | sb.AppendLine("),"); | ||
| 262 | } | ||
| 263 | sb.AppendLine(endColumnsDef); | ||
| 264 | if (tableDefinition.Unreal) | ||
| 265 | { | ||
| 266 | sb.AppendLine(unrealDef); | ||
| 267 | } | ||
| 268 | sb.AppendLine(endTableDef.Replace("{1}", tableDefinition.TupleDefinitionName).Replace("{2}", tableDefinition.TupleIdIsPrimaryKey.ToString().ToLower())); | ||
| 269 | } | ||
| 270 | sb.AppendLine(startAllTablesDef); | ||
| 271 | foreach (var tableDefinition in tableDefinitions) | ||
| 272 | { | ||
| 273 | sb.AppendLine(allTableDef.Replace("{1}", tableDefinition.Name)); | ||
| 274 | } | ||
| 275 | sb.AppendLine(endAllTablesDef); | ||
| 276 | sb.AppendLine(endClassDef); | ||
| 277 | |||
| 278 | return sb.ToString(); | ||
| 279 | } | ||
| 280 | |||
| 145 | private static string GenerateTupleFileText(string prefix, string tupleName, List<(string Name, string Type, string ClrType, string AsFunction)> tupleFields) | 281 | private static string GenerateTupleFileText(string prefix, string tupleName, List<(string Name, string Type, string ClrType, string AsFunction)> tupleFields) |
| 146 | { | 282 | { |
| 147 | var ns = prefix ?? "Data"; | 283 | var ns = prefix ?? "Data"; |
