Skip to content

Commit fe3b230

Browse files
committed
Add a flag (ca|commandArgs) to disable the command line args comment
By default, a comment with the exact command line arguments that were used to generate the source code is generated, only when run from `xcsgen` or `XmlSchemaClassGenerator.Console`.
1 parent 7f8eecc commit fe3b230

6 files changed

Lines changed: 65 additions & 21 deletions

File tree

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void Main(string[] args)
5454
var uniqueTypeNamesAcrossNamespaces = false;
5555
var createGeneratedCodeAttributeVersion = true;
5656
var netCoreSpecificCode = false;
57+
var generateCommandLineArgs = true;
5758

5859
var options = new OptionSet {
5960
{ "h|help", "show this message and exit", v => showHelp = v != null },
@@ -126,6 +127,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
126127
{ "un|uniqueTypeNames", "generate type names that are unique across namespaces (default is false)", v => uniqueTypeNamesAcrossNamespaces = v != null },
127128
{ "gc|generatedCodeAttribute", "add version information to GeneratedCodeAttribute (default is true)", v => createGeneratedCodeAttributeVersion = v != null },
128129
{ "nc|netCore", "generate .NET Core specific code that might not work with .NET Framework (default is false)", v => netCoreSpecificCode = v != null },
130+
{ "ca|commandArgs", "generate a comment with the exact command line arguments that were used to generate the source code (default is true)", v => generateCommandLineArgs = v != null },
129131
};
130132

131133
var globsAndUris = options.Parse(args);
@@ -200,7 +202,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
200202
CompactTypeNames = compactTypeNames,
201203
UniqueTypeNamesAcrossNamespaces = uniqueTypeNamesAcrossNamespaces,
202204
CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion,
203-
NetCoreSpecificCode = netCoreSpecificCode
205+
NetCoreSpecificCode = netCoreSpecificCode,
206+
GenerateCommandLineArgumentsComment = generateCommandLineArgs,
204207
};
205208

206209
generator.CommentLanguages.AddRange(commentLanguages);

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ public void ComplexTypeWithAttributeGroupExtension()
10491049
var csharp = Assert.Single(contents);
10501050

10511051
CompareOutput(
1052-
$@"//------------------------------------------------------------------------------
1052+
@"//------------------------------------------------------------------------------
10531053
// <auto-generated>
10541054
// This code was generated by a tool.
10551055
//
@@ -1058,47 +1058,46 @@ public void ComplexTypeWithAttributeGroupExtension()
10581058
// </auto-generated>
10591059
//------------------------------------------------------------------------------
10601060
1061-
// This code was generated by Tests using the following command:
1062-
// {Extensions.GetFormattedCommandLineArgs()}
1061+
// This code was generated by Tests
10631062
namespace Test
1064-
{{
1063+
{
10651064
10661065
10671066
[System.CodeDom.Compiler.GeneratedCodeAttribute(""Tests"", """")]
10681067
[System.SerializableAttribute()]
10691068
[System.Xml.Serialization.XmlTypeAttribute(""group-name"", Namespace="""")]
10701069
[System.ComponentModel.DesignerCategoryAttribute(""code"")]
10711070
public partial class Group_Name
1072-
{{
1071+
{
10731072
10741073
/// <summary>
10751074
/// <para xml:lang=""de"">Ruft den Text ab oder legt diesen fest.</para>
10761075
/// <para xml:lang=""en"">Gets or sets the text value.</para>
10771076
/// </summary>
10781077
[System.Xml.Serialization.XmlTextAttribute()]
1079-
public string Value {{ get; set; }}
1078+
public string Value { get; set; }
10801079
10811080
[System.Xml.Serialization.XmlAttributeAttribute(""justify"")]
1082-
public SimpleType Justify {{ get; set; }}
1081+
public SimpleType Justify { get; set; }
10831082
10841083
/// <summary>
10851084
/// <para xml:lang=""de"">Ruft einen Wert ab, der angibt, ob die Justify-Eigenschaft spezifiziert ist, oder legt diesen fest.</para>
10861085
/// <para xml:lang=""en"">Gets or sets a value indicating whether the Justify property is specified.</para>
10871086
/// </summary>
10881087
[System.Xml.Serialization.XmlIgnoreAttribute()]
1089-
public bool JustifySpecified {{ get; set; }}
1090-
}}
1088+
public bool JustifySpecified { get; set; }
1089+
}
10911090
10921091
[System.CodeDom.Compiler.GeneratedCodeAttribute(""Tests"", """")]
10931092
[System.SerializableAttribute()]
10941093
[System.Xml.Serialization.XmlTypeAttribute(""simpleType"", Namespace="""")]
10951094
public enum SimpleType
1096-
{{
1095+
{
10971096
10981097
[System.Xml.Serialization.XmlEnumAttribute(""foo"")]
10991098
Foo,
1100-
}}
1101-
}}
1099+
}
1100+
}
11021101
", csharp);
11031102
}
11041103

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace XmlSchemaClassGenerator
6+
{
7+
public class CommandLineArgumentsProvider
8+
{
9+
public virtual string CommandLineArguments
10+
{
11+
get
12+
{
13+
var args = Environment.GetCommandLineArgs();
14+
return string.Join(" ", args.Take(1).Select(Path.GetFileNameWithoutExtension).Concat(args.Skip(1)).Select(Extensions.QuoteIfNeeded));
15+
}
16+
}
17+
}
18+
}

XmlSchemaClassGenerator/Extensions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
4343
return source.GroupBy(propertySelector).Select(x => x.First());
4444
}
4545

46-
public static string GetFormattedCommandLineArgs()
47-
{
48-
var args = Environment.GetCommandLineArgs();
49-
return string.Join(" ", args.Take(1).Select(Path.GetFileNameWithoutExtension).Concat(args.Skip(1)).Select(QuoteIfNeeded));
50-
}
51-
5246
public static string QuoteIfNeeded(this string text)
5347
{
5448
if (string.IsNullOrEmpty(text))

XmlSchemaClassGenerator/Generator.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ public bool NetCoreSpecificCode
288288
set { _configuration.NetCoreSpecificCode = value; }
289289
}
290290

291+
public bool GenerateCommandLineArgumentsComment
292+
{
293+
get { return _configuration.GenerateCommandLineArgumentsComment; }
294+
set { _configuration.GenerateCommandLineArgumentsComment = value; }
295+
}
296+
297+
public CommandLineArgumentsProvider CommandLineArgumentsProvider
298+
{
299+
get { return _configuration.CommandLineArgumentsProvider; }
300+
set { _configuration.CommandLineArgumentsProvider = value; }
301+
}
302+
291303
static Generator()
292304
{
293305
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
@@ -334,9 +346,15 @@ public void Generate(XmlSchemaSet set)
334346
{
335347
comment.Append($" version {Version.Version}");
336348
}
337-
comment.Append(" using the following command:");
349+
if (GenerateCommandLineArgumentsComment)
350+
{
351+
comment.Append(" using the following command:");
352+
}
338353
ns.Comments.Add(new CodeCommentStatement(comment.ToString()));
339-
ns.Comments.Add(new CodeCommentStatement(Extensions.GetFormattedCommandLineArgs()));
354+
if (GenerateCommandLineArgumentsComment)
355+
{
356+
ns.Comments.Add(new CodeCommentStatement(CommandLineArgumentsProvider?.CommandLineArguments ?? "N/A"));
357+
}
340358
}
341359

342360
writer.Write(ns);

XmlSchemaClassGenerator/GeneratorConfiguration.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public GeneratorConfiguration()
3838
NamingProvider = new NamingProvider(NamingScheme);
3939
Version = VersionProvider.CreateFromAssembly();
4040
EnableUpaCheck = true;
41+
CommandLineArgumentsProvider = new CommandLineArgumentsProvider();
4142
}
4243

4344
/// <summary>
@@ -304,5 +305,16 @@ public void WriteLog(string message)
304305
/// </list>
305306
/// </summary>
306307
public bool NetCoreSpecificCode { get; set; }
308+
309+
/// <summary>
310+
/// Adds a comment with the exact command line arguments that were used to generate the
311+
/// source code using the <see cref="CommandLineArgumentsProvider"/>. Default is false.
312+
/// </summary>
313+
public bool GenerateCommandLineArgumentsComment { get; set; }
314+
315+
/// <summary>
316+
/// A provider to obtain the command line arguments of the tool.
317+
/// </summary>
318+
public CommandLineArgumentsProvider CommandLineArgumentsProvider { get; set; }
307319
}
308320
}

0 commit comments

Comments
 (0)