Skip to content

Commit

Permalink
Exit with nonzero status if xsd is invalid (fixes #485)
Browse files Browse the repository at this point in the history
VS code suggestions
  • Loading branch information
mganss committed Feb 9, 2024
1 parent 7e9a91d commit 2369145
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
23 changes: 17 additions & 6 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace XmlSchemaClassGenerator.Console
{
static class Program
{
static void Main(string[] args)
static int Main(string[] args)
{
var showHelp = args.Length == 0;
var namespaces = new List<string>();
Expand Down Expand Up @@ -165,7 +165,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
if (showHelp)
{
ShowHelp(options);
return;
return 0;
}

var uris = new List<string>();
Expand All @@ -181,7 +181,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
if (expandedGlob.Count == 0)
{
System.Console.WriteLine($"No files found for '{globOrUri}'");
Environment.Exit(1);
Environment.Exit((int)ExitCodes.FileNotFound);
}

uris.AddRange(expandedGlob);
Expand Down Expand Up @@ -267,6 +267,17 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); }

generator.Generate(uris);

return !generator.ValidationError ? (int)ExitCodes.Ok : (int)ExitCodes.ValidationError;
}

enum ExitCodes
{
Ok = 0,
FileNotFound = 1,
InvalidNameSubstitutionFile = 2,
InvalidNamespaceFile = 3,
ValidationError = 4
}

private static void ParseNamespaceFiles(List<string> namespaces, List<string> namespaceFiles)
Expand All @@ -280,12 +291,12 @@ private static void ParseNamespaceFiles(List<string> namespaces, List<string> na
var parts = line.Split('=');

if (parts.Length == 1)
parts = new[] { string.Empty, parts[0] };
parts = [string.Empty, parts[0]];

if (parts.Length != 2)
{
System.Console.WriteLine($"{namespaceFile}:{number}: Line format is XML namespace = C# namespace [file name]");
Environment.Exit(1);
Environment.Exit((int)ExitCodes.InvalidNamespaceFile);
}

var xmlns = parts[0].Trim();
Expand All @@ -312,7 +323,7 @@ private static void ParseNameSubstituteFiles(List<string> nameSubstitutes, List<
if (parts.Length != 2)
{
System.Console.WriteLine($"{nameSubstituteFile}:{number}: Line format is prefixed type/member name = substitute name");
Environment.Exit(2);
Environment.Exit((int)ExitCodes.InvalidNameSubstitutionFile);
}

var generatedName = parts[0].Trim();
Expand Down
3 changes: 1 addition & 2 deletions XmlSchemaClassGenerator/FileOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu)
}
finally
{
if (fs != null)
fs.Dispose();
fs?.Dispose();
}
}

Expand Down
5 changes: 5 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ public bool SeparateNamespaceHierarchy
set { _configuration.SeparateNamespaceHierarchy = value; }
}

public bool ValidationError { get; private set; }

static Generator()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Expand All @@ -347,12 +349,15 @@ public void Generate(IEnumerable<string> files)
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
var readers = files.Select(f => XmlReader.Create(f, settings));

ValidationError = false;

set.XmlResolver = new XmlUrlResolver();
set.ValidationEventHandler += (s, e) =>
{
var ex = e.Exception as Exception;
while (ex != null)
{
ValidationError = true;
Log?.Invoke(ex.Message);
ex = ex.InnerException;
}
Expand Down
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray, TypeMo
var qualifiedName = xmlSchemaType.GetQualifiedName();

if ((qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType") &&
(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && configuration.DateTimeWithTimeZone) == false)
(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && Configuration.DateTimeWithTimeZone) == false)
{
args.Add(new("DataType", new CodePrimitiveExpression(qualifiedName.Name)));
break;
Expand Down

0 comments on commit 2369145

Please sign in to comment.