Skip to content

Commit

Permalink
Fix #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Sep 19, 2018
1 parent bd8f057 commit 07c11a9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
7 changes: 6 additions & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void Main(string[] args)
var generateDebuggerStepThroughAttribute = true;
var disableComments = false;
var doNotUseUnderscoreInPrivateMemberNames = false;
var generateDescriptionAttribute = true;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -79,6 +80,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "dst|debuggerStepThrough", "generate DebuggerStepThroughAttribute (default is enabled)", v => generateDebuggerStepThroughAttribute = v != null },
{ "dc|disableComments", "do not include comments from xsd", v => disableComments = v != null },
{ "nu|noUnderscore", "do not generate underscore in private member name (default is false)", v => doNotUseUnderscoreInPrivateMemberNames = v != null },
{ "da|description", "generate DescriptionAttribute (default is true)", v => generateDescriptionAttribute = v != null },
};

var files = options.Parse(args);
Expand Down Expand Up @@ -122,6 +124,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
TextValuePropertyName = textValuePropertyName,
GenerateDebuggerStepThroughAttribute = generateDebuggerStepThroughAttribute,
DisableComments = disableComments,
GenerateDescriptionAttribute = generateDescriptionAttribute,
DoNotUseUnderscoreInPrivateMemberNames = doNotUseUnderscoreInPrivateMemberNames
};

if (pclCompatible)
Expand All @@ -131,8 +135,9 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
generator.GenerateSerializableAttribute = false;
generator.GenerateDebuggerStepThroughAttribute = false;
generator.DataAnnotationMode = DataAnnotationMode.None;
generator.GenerateDescriptionAttribute = false;
}
generator.DoNotUseUnderscoreInPrivateMemberNames = doNotUseUnderscoreInPrivateMemberNames;

if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); }

generator.Generate(files);
Expand Down
4 changes: 3 additions & 1 deletion XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
GenerateDesignerCategoryAttribute = false,
EntityFramework = false,
GenerateInterfaces = true,
NamespacePrefix = name
NamespacePrefix = name,
GenerateDescriptionAttribute = true
};

var output = new FileWatcherOutputWriter(Path.Combine("output", name));
Expand All @@ -78,6 +79,7 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
EntityFramework = generatorPrototype.EntityFramework,
GenerateInterfaces = generatorPrototype.GenerateInterfaces,
MemberVisitor = generatorPrototype.MemberVisitor,
GenerateDescriptionAttribute = generatorPrototype.GenerateDescriptionAttribute
};

gen.Generate(files);
Expand Down
1 change: 1 addition & 0 deletions XmlSchemaClassGenerator.Tests/XsdElsterDatenabholung5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void CanGenerateClasses()
NamingScheme = NamingScheme.Direct,
DataAnnotationMode = DataAnnotationMode.None,
EmitOrder = true,
GenerateDescriptionAttribute = true,
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key =>
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public bool GenerateInterfaces
set { _configuration.GenerateInterfaces = value; }
}

public bool GenerateDescriptionAttribute
{
get { return _configuration.GenerateDescriptionAttribute; }
set { _configuration.GenerateDescriptionAttribute = value; }
}

public CodeTypeReferenceOptions CodeTypeReferenceOptions
{
get { return _configuration.CodeTypeReferenceOptions; }
Expand Down
4 changes: 4 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public GeneratorConfiguration()
/// Generate interfaces for groups and attribute groups
/// </summary>
public bool GenerateInterfaces { get; set; }
/// <summary>
/// Generate <see cref="System.ComponentModel.DescriptionAttribute"/> from XML comments.
/// </summary>
public bool GenerateDescriptionAttribute { get; set; }

/// <summary>
/// Generator Code reference options
Expand Down
31 changes: 30 additions & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
Expand Down Expand Up @@ -77,12 +78,34 @@ public static IEnumerable<CodeCommentStatement> GetComments(IEnumerable<Document
{
var text = doc.Text;
var comment = string.Format(@"<para{0}>{1}</para>",
string.IsNullOrEmpty(doc.Language) ? "" : string.Format(@" xml:lang=""{0}""", doc.Language), CodeUtilities.NormalizeNewlines(text));
string.IsNullOrEmpty(doc.Language) ? "" : string.Format(@" xml:lang=""{0}""", doc.Language), CodeUtilities.NormalizeNewlines(text).Trim());
yield return new CodeCommentStatement(comment, true);
}

yield return new CodeCommentStatement("</summary>", true);
}

public static void AddDescription(CodeAttributeDeclarationCollection attributes, IEnumerable<DocumentationModel> docs, GeneratorConfiguration conf)
{
if (!conf.GenerateDescriptionAttribute || DisableComments || !docs.Any()) return;

var doc = GetSingleDoc(docs);

if (doc != null)
{
var descriptionAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(DescriptionAttribute), conf.CodeTypeReferenceOptions),
new CodeAttributeArgument(new CodePrimitiveExpression(Regex.Replace(doc.Text, @"\s+", " ").Trim())));
attributes.Add(descriptionAttribute);
}
}

private static DocumentationModel GetSingleDoc(IEnumerable<DocumentationModel> docs)
{
if (docs.Count() == 1) return docs.Single();
var englishDoc = docs.Where(d => string.IsNullOrEmpty(d.Language) || d.Language.StartsWith("en", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (englishDoc != null) return englishDoc;
return docs.FirstOrDefault();
}
}

public abstract class TypeModel
Expand Down Expand Up @@ -110,6 +133,8 @@ public virtual CodeTypeDeclaration Generate()

typeDeclaration.Comments.AddRange(DocumentationModel.GetComments(Documentation).ToArray());

DocumentationModel.AddDescription(typeDeclaration.CustomAttributes, Documentation, Configuration);

var generatedAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(GeneratedCodeAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(new CodePrimitiveExpression(Configuration.Version.Title)),
new CodeAttributeArgument(new CodePrimitiveExpression(Configuration.Version.Version)));
Expand Down Expand Up @@ -609,6 +634,8 @@ private void AddDocs(CodeTypeMember member)
{
var docs = new List<DocumentationModel>(Documentation);

DocumentationModel.AddDescription(member.CustomAttributes, docs, Configuration);

if (PropertyType is SimpleModel simpleType)
{
docs.AddRange(simpleType.Documentation);
Expand Down Expand Up @@ -1071,6 +1098,8 @@ public override CodeTypeDeclaration Generate()
var member = new CodeMemberField { Name = val.Name };
var docs = new List<DocumentationModel>(val.Documentation);

DocumentationModel.AddDescription(member.CustomAttributes, docs, Configuration);

if (val.Name != val.Value) // illegal identifier chars in value
{
var enumAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlEnumAttribute), Configuration.CodeTypeReferenceOptions),
Expand Down

0 comments on commit 07c11a9

Please sign in to comment.