Skip to content

Commit 85ead03

Browse files
authored
Merge pull request #269 from 0xced/command-comment
Add the actual command that generated the .cs file in the comments
2 parents fd88711 + 62681c0 commit 85ead03

8 files changed

Lines changed: 166 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ Options:
148148
(default is true)
149149
--nc, --netCore generate .NET Core specific code that might not
150150
work with .NET Framework (default is false)
151+
--ca, --commandArgs generate a comment with the exact command line
152+
arguments that were used to generate the source
153+
code (default is true)
151154
```
152155

153156
For use from code use the [library NuGet package](https://www.nuget.org/packages/XmlSchemaClassGenerator-beta/):
@@ -362,4 +365,4 @@ Contrbutions are welcome. Here are some guidelines:
362365

363366
- If it's not a trivial fix, please submit an issue first
364367
- Try and blend new code with the existing code's style
365-
- Add unit tests
368+
- Add unit tests

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);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Xunit;
2+
3+
namespace XmlSchemaClassGenerator.Tests
4+
{
5+
public class ExtensionsTests
6+
{
7+
[Theory]
8+
[InlineData(null, null)]
9+
[InlineData("", "")]
10+
[InlineData("MyText", "MyText")]
11+
[InlineData("My Text", "\"My Text\"")]
12+
public void QuoteEmptyOrNull(string input, string expected)
13+
{
14+
Assert.Equal(expected, input.QuoteIfNeeded());
15+
}
16+
}
17+
}

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
5656
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions,
5757
DoNotForceIsNullable = generatorPrototype.DoNotForceIsNullable,
5858
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
59-
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
59+
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode,
60+
GenerateCommandLineArgumentsComment = generatorPrototype.GenerateCommandLineArgumentsComment,
61+
CommandLineArgumentsProvider = generatorPrototype.CommandLineArgumentsProvider,
6062
};
6163

6264
gen.CommentLanguages.Clear();
@@ -1058,7 +1060,7 @@ public void ComplexTypeWithAttributeGroupExtension()
10581060
// </auto-generated>
10591061
//------------------------------------------------------------------------------
10601062
1061-
// This code was generated by Tests.
1063+
// This code was generated by Tests
10621064
namespace Test
10631065
{
10641066
@@ -2388,5 +2390,64 @@ void UnknownAttributeHandler(object sender, XmlAttributeEventArgs e)
23882390
AssertEx.Equal(deserializedObject, deserializedXml);
23892391
}
23902392
}
2393+
2394+
[Theory]
2395+
[InlineData("fake command line arguments", "fake command line arguments")]
2396+
[InlineData(null, "N/A")]
2397+
public void IncludeCommandLineArguments(string commandLineArguments, string expectedCommandLineArguments)
2398+
{
2399+
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
2400+
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://local.none"" xmlns:l=""http://local.none"">
2401+
<xs:element name=""document"" type=""l:elem"" />
2402+
<xs:complexType name=""elem"">
2403+
<xs:attribute name=""Text"" type=""xs:string""/>
2404+
</xs:complexType>
2405+
</xs:schema>";
2406+
2407+
var generator = new Generator
2408+
{
2409+
GenerateInterfaces = false,
2410+
NamespaceProvider = new NamespaceProvider
2411+
{
2412+
GenerateNamespace = key => "Test"
2413+
},
2414+
GenerateCommandLineArgumentsComment = true,
2415+
CommandLineArgumentsProvider = new CommandLineArgumentsProvider(commandLineArguments)
2416+
};
2417+
2418+
var contents = ConvertXml(nameof(IncludeCommandLineArguments), xsd, generator);
2419+
2420+
var csharp = Assert.Single(contents);
2421+
2422+
CompareOutput(
2423+
$@"//------------------------------------------------------------------------------
2424+
// <auto-generated>
2425+
// This code was generated by a tool.
2426+
//
2427+
// Changes to this file may cause incorrect behavior and will be lost if
2428+
// the code is regenerated.
2429+
// </auto-generated>
2430+
//------------------------------------------------------------------------------
2431+
2432+
// This code was generated by Tests version 1.0.0.1 using the following command:
2433+
// {expectedCommandLineArguments}
2434+
namespace Test
2435+
{{
2436+
2437+
2438+
[System.CodeDom.Compiler.GeneratedCodeAttribute(""Tests"", ""1.0.0.1"")]
2439+
[System.SerializableAttribute()]
2440+
[System.Xml.Serialization.XmlTypeAttribute(""elem"", Namespace=""http://local.none"")]
2441+
[System.ComponentModel.DesignerCategoryAttribute(""code"")]
2442+
[System.Xml.Serialization.XmlRootAttribute(""document"", Namespace=""http://local.none"")]
2443+
public partial class Elem
2444+
{{
2445+
2446+
[System.Xml.Serialization.XmlAttributeAttribute(""Text"")]
2447+
public string Text {{ get; set; }}
2448+
}}
2449+
}}
2450+
", csharp);
2451+
}
23912452
}
23922453
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace XmlSchemaClassGenerator
7+
{
8+
public class CommandLineArgumentsProvider
9+
{
10+
public CommandLineArgumentsProvider(string commandLineArguments)
11+
{
12+
CommandLineArguments = commandLineArguments;
13+
}
14+
15+
public string CommandLineArguments { get; }
16+
17+
public static CommandLineArgumentsProvider CreateFromEnvironment()
18+
{
19+
var args = Environment.GetCommandLineArgs();
20+
var commandLineArguments = string.Join(" ", args.Take(1).Select(Path.GetFileNameWithoutExtension).Concat(args.Skip(1)).Select(Extensions.QuoteIfNeeded));
21+
return new CommandLineArgumentsProvider(commandLineArguments);
22+
}
23+
}
24+
}

XmlSchemaClassGenerator/Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -41,5 +42,20 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
4142
{
4243
return source.GroupBy(propertySelector).Select(x => x.First());
4344
}
45+
46+
public static string QuoteIfNeeded(this string text)
47+
{
48+
if (string.IsNullOrEmpty(text))
49+
{
50+
return text;
51+
}
52+
53+
if (text.Contains(" "))
54+
{
55+
return "\"" + text + "\"";
56+
}
57+
58+
return text;
59+
}
4460
}
4561
}

XmlSchemaClassGenerator/Generator.cs

Lines changed: 26 additions & 3 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);
@@ -329,9 +341,20 @@ public void Generate(XmlSchemaSet set)
329341
{
330342
if (Version != null)
331343
{
332-
var comment = $"This code was generated by {Version.Title}"
333-
+ (CreateGeneratedCodeAttributeVersion ? $" version {Version.Version}." : ".");
334-
ns.Comments.Add(new CodeCommentStatement(comment));
344+
var comment = new StringBuilder($"This code was generated by {Version.Title}");
345+
if (CreateGeneratedCodeAttributeVersion)
346+
{
347+
comment.Append($" version {Version.Version}");
348+
}
349+
if (GenerateCommandLineArgumentsComment)
350+
{
351+
comment.Append(" using the following command:");
352+
}
353+
ns.Comments.Add(new CodeCommentStatement(comment.ToString()));
354+
if (GenerateCommandLineArgumentsComment)
355+
{
356+
ns.Comments.Add(new CodeCommentStatement(CommandLineArgumentsProvider?.CommandLineArguments ?? "N/A"));
357+
}
335358
}
336359

337360
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 = CommandLineArgumentsProvider.CreateFromEnvironment();
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)