Skip to content

Commit

Permalink
Introduce .NET Core specific code configuration option (fixes #259)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Apr 12, 2021
1 parent 8365b4e commit faf2d6f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
5 changes: 4 additions & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static void Main(string[] args)
var supportedCommentLanguages = new[] { "en", "de" };
var uniqueTypeNamesAcrossNamespaces = false;
var createGeneratedCodeAttributeVersion = true;
var netCoreSpecificCode = false;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -124,6 +125,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
v => commentLanguages = v.Split(',').Select(l => l.Trim()).ToArray() },
{ "un|uniqueTypeNames", "generate type names that are unique across namespaces (default is false)", v => uniqueTypeNamesAcrossNamespaces = v != null },
{ "gc|generatedCodeAttribute", "add version information to GeneratedCodeAttribute (default is true)", v => createGeneratedCodeAttributeVersion = v != null },
{ "nc|netCore", "generate .NET Core specific code that might not work with .NET Framework (default is false)", v => netCoreSpecificCode = v != null },
};

var globsAndUris = options.Parse(args);
Expand Down Expand Up @@ -197,7 +199,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
SeparateSubstitutes = separateSubstitutes,
CompactTypeNames = compactTypeNames,
UniqueTypeNamesAcrossNamespaces = uniqueTypeNamesAcrossNamespaces,
CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion
CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion,
NetCoreSpecificCode = netCoreSpecificCode
};

generator.CommentLanguages.AddRange(commentLanguages);
Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
SeparateSubstitutes = generatorPrototype.SeparateSubstitutes,
CompactTypeNames = generatorPrototype.CompactTypeNames,
UniqueTypeNamesAcrossNamespaces = generatorPrototype.UniqueTypeNamesAcrossNamespaces,
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
};

gen.CommentLanguages.Clear();
Expand Down
6 changes: 4 additions & 2 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ private static IEnumerable<string> ConvertXml(string name, IEnumerable<string> x
MemberVisitor = generatorPrototype.MemberVisitor,
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions,
DoNotForceIsNullable = generatorPrototype.DoNotForceIsNullable,
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
};

gen.CommentLanguages.Clear();
Expand Down Expand Up @@ -310,7 +311,8 @@ public void TestSimple()
GenerateInterfaces = true,
NamespacePrefix = "Simple",
GenerateDescriptionAttribute = true,
CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference
CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference,
NetCoreSpecificCode = true
});
TestSamples("Simple", SimplePattern);
}
Expand Down
1 change: 1 addition & 0 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
{
XmlTypeCode.AnyAtomicType => typeof(string),// union
XmlTypeCode.AnyUri or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string),
XmlTypeCode.Duration => configuration.NetCoreSpecificCode ? type.ValueType : typeof(string),
XmlTypeCode.Time => typeof(DateTime),
XmlTypeCode.Idref => typeof(string),
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonNegativeInteger or XmlTypeCode.NonPositiveInteger or XmlTypeCode.PositiveInteger => GetIntegerDerivedType(type, configuration, restrictions),
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ public bool CreateGeneratedCodeAttributeVersion
set { _configuration.CreateGeneratedCodeAttributeVersion = value; }
}

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

static Generator()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Expand Down
9 changes: 9 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,14 @@ public void WriteLog(string message)
/// Adds version information to <see cref="System.CodeDom.Compiler.GeneratedCodeAttribute"/>. Default is true.
/// </summary>
public bool CreateGeneratedCodeAttributeVersion { get; set; } = true;

/// <summary>
/// Generate code that works with .NET Core but might be incompatible with .NET Framework. Default is false.
/// Specific differences:
/// <list type="bullet">
/// <item>Use <see cref="TimeSpan"/> for duration instead of string <see cref="string"/></item>
/// </list>
/// </summary>
public bool NetCoreSpecificCode { get; set; }
}
}

0 comments on commit faf2d6f

Please sign in to comment.