Skip to content

Commit

Permalink
Add CLI option
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
mganss committed Apr 10, 2020
1 parent bdb2344 commit a04312a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 43 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
55 changes: 17 additions & 38 deletions XmlSchemaClassGenerator/FileOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void Write(CodeNamespace cn)
var cu = new CodeCompileUnit();
cu.Namespaces.Add(cn);

if (Configuration.SeparateClasses == true)
if (Configuration?.SeparateClasses == true)
{
WriteSeparateFiles(cn);
}
Expand Down Expand Up @@ -68,49 +68,28 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu)

private void WriteSeparateFiles(CodeNamespace cn)
{
try
{
string dirPath = Path.Combine(OutputDirectory, ValidateName(cn.Name));
DirectoryInfo di = null;
//Create directory to hold the output files
if (Directory.Exists(dirPath))
{
di = Directory.GetParent(dirPath);
}
else
{
di = Directory.CreateDirectory(dirPath);
}

var dirPath = Path.Combine(OutputDirectory, ValidateName(cn.Name));
var ccu = new CodeCompileUnit();
var cns = new CodeNamespace(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);
cns.Imports.AddRange(cn.Imports.Cast<CodeNamespaceImport>().ToArray());
cns.Comments.AddRange(cn.Comments);
ccu.Namespaces.Add(cns);

foreach (CodeTypeDeclaration ctd in cn.Types)
{
string path = Path.Combine(dirPath, ctd.Name + ".cs");
cns.Types.Clear();
cns.Types.Add(ctd);
Configuration?.WriteLog(path);
WriteFile(path, ccu);
}
}
catch (System.Exception ae)
foreach (CodeTypeDeclaration ctd in cn.Types)
{
string s = ae.ToString();
if (ae.InnerException != null) s = ae.InnerException.Message;
var path = Path.Combine(dirPath, ctd.Name + ".cs");
cns.Types.Clear();
cns.Types.Add(ctd);
Configuration?.WriteLog(path);
WriteFile(path, ccu);
}
}

private string ValidateName(string name)
{
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
string validName = Regex.Replace(name, regexSearch, "_");
return validName.Replace(".", "_");
}
static readonly Regex InvalidCharacters = new Regex($"[{string.Join("", Path.GetInvalidFileNameChars())}]", RegexOptions.Compiled);

private string ValidateName(string name) => InvalidCharacters.Replace(name, "_");
}
}
2 changes: 2 additions & 0 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
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void WriteLog(string message)
public bool GenerateComplexTypesForCollections { get; set; } = true;

/// <summary>
/// Separates each classes into an individual C# file
/// Separates each class into an individual file
/// </summary>
public bool SeparateClasses { get; set; } = false;
}
Expand Down

0 comments on commit a04312a

Please sign in to comment.