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
|
// 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 WixBuildTools.MsgGen
{
using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Schema;
/// <summary>
/// The main entry point for MsgGen.
/// </summary>
public class MsgGen
{
/// <summary>
/// The main entry point for MsgGen.
/// </summary>
/// <param name="args">Commandline arguments for the application.</param>
/// <returns>Returns the application error code.</returns>
[STAThread]
public static int Main(string[] args)
{
try
{
MsgGenMain msgGen = new MsgGenMain(args);
}
catch (Exception e)
{
Console.WriteLine("MsgGen.exe : fatal error MSGG0000: {0}\r\n\r\nStack Trace:\r\n{1}", e.Message, e.StackTrace);
if (e is NullReferenceException || e is SEHException)
{
throw;
}
return 2;
}
return 0;
}
/// <summary>
/// Main class for MsgGen.
/// </summary>
private class MsgGenMain
{
private bool showLogo;
private bool showHelp;
private string sourceFile;
private string destClassFile;
private string destResourcesFile;
/// <summary>
/// Main method for the MsgGen application within the MsgGenMain class.
/// </summary>
/// <param name="args">Commandline arguments to the application.</param>
public MsgGenMain(string[] args)
{
this.showLogo = true;
this.showHelp = false;
this.sourceFile = null;
this.destClassFile = null;
this.destResourcesFile = null;
// parse the command line
this.ParseCommandLine(args);
if (null == this.sourceFile || null == this.destClassFile)
{
this.showHelp = true;
}
if (null == this.destResourcesFile)
{
this.destResourcesFile = Path.ChangeExtension(this.destClassFile, ".resources");
}
// get the assemblies
Assembly msgGenAssembly = Assembly.GetExecutingAssembly();
if (this.showLogo)
{
Console.WriteLine("Microsoft (R) Message Generation Tool version {0}", msgGenAssembly.GetName().Version.ToString());
Console.WriteLine("Copyright (C) Microsoft Corporation 2004. All rights reserved.");
Console.WriteLine();
}
if (this.showHelp)
{
Console.WriteLine(" usage: MsgGen.exe [-?] [-nologo] sourceFile destClassFile [destResourcesFile]");
Console.WriteLine();
Console.WriteLine(" -? this help information");
Console.WriteLine();
Console.WriteLine("For more information see: http://wix.sourceforge.net");
return; // exit
}
// load the schema
XmlReader reader = null;
XmlSchemaCollection schemaCollection = null;
try
{
reader = new XmlTextReader(msgGenAssembly.GetManifestResourceStream("WixBuildTools.MsgGen.Xsd.messages.xsd"));
schemaCollection = new XmlSchemaCollection();
schemaCollection.Add("http://schemas.microsoft.com/genmsgs/2004/07/messages", reader);
}
finally
{
reader.Close();
}
// load the source file and process it
using (StreamReader sr = new StreamReader(this.sourceFile))
{
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None);
XmlValidatingReader validatingReader = new XmlValidatingReader(sr.BaseStream, XmlNodeType.Document, context);
validatingReader.Schemas.Add(schemaCollection);
XmlDocument errorsDoc = new XmlDocument();
errorsDoc.Load(validatingReader);
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
using (ResourceWriter resourceWriter = new ResourceWriter(this.destResourcesFile))
{
GenerateMessageFiles.Generate(errorsDoc, codeCompileUnit, resourceWriter);
GenerateCSharpCode(codeCompileUnit, this.destClassFile);
}
}
}
/// <summary>
/// Generate the actual C# code.
/// </summary>
/// <param name="codeCompileUnit">The code DOM.</param>
/// <param name="destClassFile">Destination C# source file.</param>
public static void GenerateCSharpCode(CodeCompileUnit codeCompileUnit, string destClassFile)
{
// generate the code with the C# code provider
CSharpCodeProvider provider = new CSharpCodeProvider();
// obtain an ICodeGenerator from the CodeDomProvider class
ICodeGenerator gen = provider.CreateGenerator();
// create a TextWriter to a StreamWriter to the output file
using (StreamWriter sw = new StreamWriter(destClassFile))
{
using (IndentedTextWriter tw = new IndentedTextWriter(sw, " "))
{
CodeGeneratorOptions options = new CodeGeneratorOptions();
// code generation options
options.BlankLinesBetweenMembers = true;
options.BracingStyle = "C";
// generate source code using the code generator
gen.GenerateCodeFromCompileUnit(codeCompileUnit, tw, options);
}
}
}
/// <summary>
/// Parse the commandline arguments.
/// </summary>
/// <param name="args">Commandline arguments.</param>
private void ParseCommandLine(string[] args)
{
for (int i = 0; i < args.Length; ++i)
{
string arg = args[i];
if (null == arg || "" == arg) // skip blank arguments
{
continue;
}
if ('-' == arg[0] || '/' == arg[0])
{
string parameter = arg.Substring(1);
if ("nologo" == parameter)
{
this.showLogo = false;
}
else if ("?" == parameter || "help" == parameter)
{
this.showHelp = true;
}
}
else if ('@' == arg[0])
{
using (StreamReader reader = new StreamReader(arg.Substring(1)))
{
string line;
ArrayList newArgs = new ArrayList();
while (null != (line = reader.ReadLine()))
{
string newArg = "";
bool betweenQuotes = false;
for (int j = 0; j < line.Length; ++j)
{
// skip whitespace
if (!betweenQuotes && (' ' == line[j] || '\t' == line[j]))
{
if ("" != newArg)
{
newArgs.Add(newArg);
newArg = null;
}
continue;
}
// if we're escaping a quote
if ('\\' == line[j] && '"' == line[j])
{
++j;
}
else if ('"' == line[j]) // if we've hit a new quote
{
betweenQuotes = !betweenQuotes;
continue;
}
newArg = String.Concat(newArg, line[j]);
}
if ("" != newArg)
{
newArgs.Add(newArg);
}
}
string[] ar = (string[])newArgs.ToArray(typeof(string));
this.ParseCommandLine(ar);
}
}
else if (null == this.sourceFile)
{
this.sourceFile = arg;
}
else if (null == this.destClassFile)
{
this.destClassFile = arg;
}
else if (null == this.destResourcesFile)
{
this.destResourcesFile = arg;
}
else
{
throw new ArgumentException(String.Format("Unknown argument '{0}'.", arg));
}
}
}
}
}
}
|