Skip to content

Commit 7f8eecc

Browse files
committed
Add the actual command that generated the .cs file in the comments
Your colleagues and your future self will be happy to know exactly all the parameters that were used during generation when the C# source code must be regenerated for whatever reason. Before: ```csharp // This code was generated by XmlSchemaClassGenerator version 1.0.0.0. ``` After: ```csharp // This code was generated by XmlSchemaClassGenerator version 1.0.0.0 using the following command: // xscgen --namespace http://microsoft.com/schemas/VisualStudio/TeamTest/2010=vstst --collectionSettersMode=Public --collectionType=System.Array --nullable --pcl --verbose vstst.xsd ```
1 parent fd88711 commit 7f8eecc

4 files changed

Lines changed: 59 additions & 14 deletions

File tree

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: 12 additions & 11 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,46 +1058,47 @@ public void ComplexTypeWithAttributeGroupExtension()
10581058
// </auto-generated>
10591059
//------------------------------------------------------------------------------
10601060
1061-
// This code was generated by Tests.
1061+
// This code was generated by Tests using the following command:
1062+
// {Extensions.GetFormattedCommandLineArgs()}
10621063
namespace Test
1063-
{
1064+
{{
10641065
10651066
10661067
[System.CodeDom.Compiler.GeneratedCodeAttribute(""Tests"", """")]
10671068
[System.SerializableAttribute()]
10681069
[System.Xml.Serialization.XmlTypeAttribute(""group-name"", Namespace="""")]
10691070
[System.ComponentModel.DesignerCategoryAttribute(""code"")]
10701071
public partial class Group_Name
1071-
{
1072+
{{
10721073
10731074
/// <summary>
10741075
/// <para xml:lang=""de"">Ruft den Text ab oder legt diesen fest.</para>
10751076
/// <para xml:lang=""en"">Gets or sets the text value.</para>
10761077
/// </summary>
10771078
[System.Xml.Serialization.XmlTextAttribute()]
1078-
public string Value { get; set; }
1079+
public string Value {{ get; set; }}
10791080
10801081
[System.Xml.Serialization.XmlAttributeAttribute(""justify"")]
1081-
public SimpleType Justify { get; set; }
1082+
public SimpleType Justify {{ get; set; }}
10821083
10831084
/// <summary>
10841085
/// <para xml:lang=""de"">Ruft einen Wert ab, der angibt, ob die Justify-Eigenschaft spezifiziert ist, oder legt diesen fest.</para>
10851086
/// <para xml:lang=""en"">Gets or sets a value indicating whether the Justify property is specified.</para>
10861087
/// </summary>
10871088
[System.Xml.Serialization.XmlIgnoreAttribute()]
1088-
public bool JustifySpecified { get; set; }
1089-
}
1089+
public bool JustifySpecified {{ get; set; }}
1090+
}}
10901091
10911092
[System.CodeDom.Compiler.GeneratedCodeAttribute(""Tests"", """")]
10921093
[System.SerializableAttribute()]
10931094
[System.Xml.Serialization.XmlTypeAttribute(""simpleType"", Namespace="""")]
10941095
public enum SimpleType
1095-
{
1096+
{{
10961097
10971098
[System.Xml.Serialization.XmlEnumAttribute(""foo"")]
10981099
Foo,
1099-
}
1100-
}
1100+
}}
1101+
}}
11011102
", csharp);
11021103
}
11031104

XmlSchemaClassGenerator/Extensions.cs

Lines changed: 22 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,26 @@ 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 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+
52+
public static string QuoteIfNeeded(this string text)
53+
{
54+
if (string.IsNullOrEmpty(text))
55+
{
56+
return text;
57+
}
58+
59+
if (text.Contains(" "))
60+
{
61+
return "\"" + text + "\"";
62+
}
63+
64+
return text;
65+
}
4466
}
4567
}

XmlSchemaClassGenerator/Generator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,14 @@ public void Generate(XmlSchemaSet set)
329329
{
330330
if (Version != null)
331331
{
332-
var comment = $"This code was generated by {Version.Title}"
333-
+ (CreateGeneratedCodeAttributeVersion ? $" version {Version.Version}." : ".");
334-
ns.Comments.Add(new CodeCommentStatement(comment));
332+
var comment = new StringBuilder($"This code was generated by {Version.Title}");
333+
if (CreateGeneratedCodeAttributeVersion)
334+
{
335+
comment.Append($" version {Version.Version}");
336+
}
337+
comment.Append(" using the following command:");
338+
ns.Comments.Add(new CodeCommentStatement(comment.ToString()));
339+
ns.Comments.Add(new CodeCommentStatement(Extensions.GetFormattedCommandLineArgs()));
335340
}
336341

337342
writer.Write(ns);

0 commit comments

Comments
 (0)