Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/TomJamesAtDataSummit-158…
Browse files Browse the repository at this point in the history
…-Output-one-file-per-class'
  • Loading branch information
Michael Ganss committed Apr 23, 2020
2 parents acb5719 + a976d91 commit 52a65e6
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 23 deletions.
5 changes: 4 additions & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static void Main(string[] args)
var enableUpaCheck = true;
var generateComplexTypesForCollections = true;
var useShouldSerialize = false;
var separateClasses = false;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -91,6 +92,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "da|description", "generate DescriptionAttribute (default is true)", v => generateDescriptionAttribute = v != null },
{ "cc|complexTypesForCollections", "generate complex types for collections (default is true)", v => generateComplexTypesForCollections = v != null },
{ "s|useShouldSerialize", "use ShouldSerialize pattern instead of Specified pattern (default is false)", v => useShouldSerialize = v != null },
{ "sf|separateFiles", "generate a separate file for each class (default is false)", v => separateClasses = v != null },
};

var globsAndUris = options.Parse(args);
Expand Down Expand Up @@ -156,7 +158,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
PrivateMemberPrefix = doNotUseUnderscoreInPrivateMemberNames ? "" : "_",
EnableUpaCheck = enableUpaCheck,
GenerateComplexTypesForCollections = generateComplexTypesForCollections,
UseShouldSerializePattern = useShouldSerialize
UseShouldSerializePattern = useShouldSerialize,
SeparateClasses = separateClasses
};

if (pclCompatible)
Expand Down
7 changes: 5 additions & 2 deletions XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
TextValuePropertyName = "Value"
};

var output = new FileWatcherOutputWriter(Path.Combine("output", name));
var output = generatorPrototype.OutputWriter as FileWatcherOutputWriter ?? new FileWatcherOutputWriter(Path.Combine("output", name));

var gen = new Generator
{
Expand All @@ -102,9 +102,12 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
GenerateDescriptionAttribute = generatorPrototype.GenerateDescriptionAttribute,
CodeTypeReferenceOptions = generatorPrototype.CodeTypeReferenceOptions,
TextValuePropertyName = generatorPrototype.TextValuePropertyName,
EmitOrder = generatorPrototype.EmitOrder
EmitOrder = generatorPrototype.EmitOrder,
SeparateClasses = generatorPrototype.SeparateClasses
};

output.Configuration = gen.Configuration;

gen.Generate(files);

return CompileFiles(name, output.Files);
Expand Down
15 changes: 15 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ public void TestTableau()
TestSamples("Tableau", TableauPattern);
}

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestSeparateClasses()
{
var output = new FileWatcherOutputWriter(Path.Combine("output", "Tableau.Separate"));
Compiler.Generate("Tableau.Separate", TableauPattern,
new Generator
{
OutputWriter = output,
SeparateClasses = true,
EnableDataBinding = true
});
TestSamples("Tableau.Separate", TableauPattern);
}

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestDtsx()
Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator.Tests/XsdElsterDatenabholung5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void CanGenerateClasses()
DataAnnotationMode = DataAnnotationMode.None,
EmitOrder = true,
GenerateDescriptionAttribute = true,
SeparateClasses = true,
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key =>
Expand Down Expand Up @@ -76,7 +77,7 @@ public void CanGenerateClasses()
public void CanCompileClasses()
{
var inputPath = GetOutputPath("CanGenerateClasses");
var fileNames = new DirectoryInfo(inputPath).GetFiles("*.cs").Select(x => x.FullName).ToArray();
var fileNames = new DirectoryInfo(inputPath).GetFiles("*.cs", SearchOption.AllDirectories).Select(x => x.FullName).ToArray();
var assembly = Compiler.CompileFiles("Elster.Test", fileNames);

assembly.GetType("Elster.Datenabholung5.Elster", true);
Expand Down
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26403.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.29920.165
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlSchemaClassGenerator", "XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj", "{ECC57F00-DEED-4744-A65A-89222DEA441A}"
EndProject
Expand Down
45 changes: 40 additions & 5 deletions XmlSchemaClassGenerator/FileOutputWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.CodeDom;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace XmlSchemaClassGenerator
{
Expand Down Expand Up @@ -30,10 +33,16 @@ public override void Write(CodeNamespace cn)
var cu = new CodeCompileUnit();
cu.Namespaces.Add(cn);

var path = Path.Combine(OutputDirectory, cn.Name + ".cs");
Configuration?.WriteLog(path);

WriteFile(path, cu);
if (Configuration?.SeparateClasses == true)
{
WriteSeparateFiles(cn);
}
else
{
var path = Path.Combine(OutputDirectory, cn.Name + ".cs");
Configuration?.WriteLog(path);
WriteFile(path, cu);
}
}

protected virtual void WriteFile(string path, CodeCompileUnit cu)
Expand All @@ -56,5 +65,31 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu)
fs.Dispose();
}
}

private void WriteSeparateFiles(CodeNamespace cn)
{
var dirPath = Path.Combine(OutputDirectory, ValidateName(cn.Name));
var ccu = new CodeCompileUnit();
var cns = new CodeNamespace(ValidateName(cn.Name));

Directory.CreateDirectory(dirPath);

cns.Imports.AddRange(cn.Imports.Cast<CodeNamespaceImport>().ToArray());
cns.Comments.AddRange(cn.Comments);
ccu.Namespaces.Add(cns);

foreach (CodeTypeDeclaration ctd in cn.Types)
{
var path = Path.Combine(dirPath, ctd.Name + ".cs");
cns.Types.Clear();
cns.Types.Add(ctd);
Configuration?.WriteLog(path);
WriteFile(path, ccu);
}
}

static readonly Regex InvalidCharacters = new Regex($"[{string.Join("", Path.GetInvalidFileNameChars())}]", RegexOptions.Compiled);

private string ValidateName(string name) => InvalidCharacters.Replace(name, "_");
}
}
20 changes: 14 additions & 6 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Generator
{
private readonly GeneratorConfiguration _configuration = new GeneratorConfiguration();

public GeneratorConfiguration Configuration => _configuration;

public NamespaceProvider NamespaceProvider
{
get { return _configuration.NamespaceProvider; }
Expand Down Expand Up @@ -214,12 +216,18 @@ public string PrivateMemberPrefix
public bool EnableUpaCheck
{
get { return _configuration.EnableUpaCheck; }
set { _configuration.EnableUpaCheck = value; }
}

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
set { _configuration.EnableUpaCheck = value; }
}

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

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
var readers = files.Select(f => XmlReader.Create(f, settings));

Expand Down
15 changes: 10 additions & 5 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ public void WriteLog(string message)
/// }
///
/// public class componentType {}
/// </code>
/// </summary>
public bool GenerateComplexTypesForCollections { get; set; } = true;
}
}
/// </code>
/// </summary>
public bool GenerateComplexTypesForCollections { get; set; } = true;

/// <summary>
/// Separates each class into an individual file
/// </summary>
public bool SeparateClasses { get; set; } = false;
}
}
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public override CodeTypeDeclaration Generate()
if (IsAbstract)
classDeclaration.TypeAttributes |= System.Reflection.TypeAttributes.Abstract;

if (Configuration.EnableDataBinding)
if (Configuration.EnableDataBinding && !(BaseClass is ClassModel))
{
var propertyChangedEvent = new CodeMemberEvent()
{
Expand Down

0 comments on commit 52a65e6

Please sign in to comment.