Skip to content

Commit 2369145

Browse files
committed
Exit with nonzero status if xsd is invalid (fixes #485)
VS code suggestions
1 parent 7e9a91d commit 2369145

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace XmlSchemaClassGenerator.Console
1717
{
1818
static class Program
1919
{
20-
static void Main(string[] args)
20+
static int Main(string[] args)
2121
{
2222
var showHelp = args.Length == 0;
2323
var namespaces = new List<string>();
@@ -165,7 +165,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
165165
if (showHelp)
166166
{
167167
ShowHelp(options);
168-
return;
168+
return 0;
169169
}
170170

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

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

269269
generator.Generate(uris);
270+
271+
return !generator.ValidationError ? (int)ExitCodes.Ok : (int)ExitCodes.ValidationError;
272+
}
273+
274+
enum ExitCodes
275+
{
276+
Ok = 0,
277+
FileNotFound = 1,
278+
InvalidNameSubstitutionFile = 2,
279+
InvalidNamespaceFile = 3,
280+
ValidationError = 4
270281
}
271282

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

282293
if (parts.Length == 1)
283-
parts = new[] { string.Empty, parts[0] };
294+
parts = [string.Empty, parts[0]];
284295

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

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

318329
var generatedName = parts[0].Trim();

XmlSchemaClassGenerator/FileOutputWriter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu)
6161
}
6262
finally
6363
{
64-
if (fs != null)
65-
fs.Dispose();
64+
fs?.Dispose();
6665
}
6766
}
6867

XmlSchemaClassGenerator/Generator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ public bool SeparateNamespaceHierarchy
336336
set { _configuration.SeparateNamespaceHierarchy = value; }
337337
}
338338

339+
public bool ValidationError { get; private set; }
340+
339341
static Generator()
340342
{
341343
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
@@ -347,12 +349,15 @@ public void Generate(IEnumerable<string> files)
347349
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
348350
var readers = files.Select(f => XmlReader.Create(f, settings));
349351

352+
ValidationError = false;
353+
350354
set.XmlResolver = new XmlUrlResolver();
351355
set.ValidationEventHandler += (s, e) =>
352356
{
353357
var ex = e.Exception as Exception;
354358
while (ex != null)
355359
{
360+
ValidationError = true;
356361
Log?.Invoke(ex.Message);
357362
ex = ex.InnerException;
358363
}

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray, TypeMo
10941094
var qualifiedName = xmlSchemaType.GetQualifiedName();
10951095

10961096
if ((qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType") &&
1097-
(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && configuration.DateTimeWithTimeZone) == false)
1097+
(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && Configuration.DateTimeWithTimeZone) == false)
10981098
{
10991099
args.Add(new("DataType", new CodePrimitiveExpression(qualifiedName.Name)));
11001100
break;

0 commit comments

Comments
 (0)