Skip to content

Commit

Permalink
Merge pull request #420 from Dimsday/master
Browse files Browse the repository at this point in the history
Add option for separate namespace hierarchy by folder
  • Loading branch information
mganss authored Oct 2, 2023
2 parents 3b6810c + 094fbe2 commit aebd9a3
Show file tree
Hide file tree
Showing 6 changed files with 54 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 @@ -63,6 +63,7 @@ static void Main(string[] args)
var namespaceFiles = new List<string>();
var nameSubstituteFiles = new List<string>();
var unionCommonType = false;
var separateNamespaceHierarchy = false;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -138,6 +139,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "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 },
{ "nh|namespaceHierarchy", "generate a separate folder for namespace hierarchy. Accepted only if \"separateFiles\" is true (default is false)", v=> separateNamespaceHierarchy = v != null },
{ "sg|separateSubstitutes", "generate a separate property for each element of a substitution group (default is false)", v => separateSubstitutes = v != null },
{ "dnfin|doNotForceIsNullable", "do not force generator to emit IsNullable = true in XmlElement annotation for nillable elements when element is nullable (minOccurs < 1 or parent element is choice) (default is false)", v => doNotForceIsNullable = v != null },
{ "cn|compactTypeNames", "use type names without namespace qualifier for types in the using list (default is false)", v => compactTypeNames = v != null },
Expand Down Expand Up @@ -235,7 +237,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
GenerateCommandLineArgumentsComment = generateCommandLineArgs,
UseArrayItemAttribute = useArrayItemAttribute,
EnumAsString = enumAsString,
MapUnionToWidestCommonType = unionCommonType
MapUnionToWidestCommonType = unionCommonType,
SeparateNamespaceHierarchy = separateNamespaceHierarchy
};

if (nameSubstituteMap.Any())
Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
NamingScheme = generatorPrototype.NamingScheme,
UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute,
EnumAsString = generatorPrototype.EnumAsString,
MapUnionToWidestCommonType = generatorPrototype.MapUnionToWidestCommonType
MapUnionToWidestCommonType = generatorPrototype.MapUnionToWidestCommonType,
SeparateNamespaceHierarchy = generatorPrototype.SeparateNamespaceHierarchy
};

gen.CommentLanguages.Clear();
Expand Down
26 changes: 26 additions & 0 deletions XmlSchemaClassGenerator.Tests/FileOutputWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,39 @@ public void TestSeparateDefaultProviderGeneratorPrefix()
EnableDataBinding = true,
SeparateClasses = true,
NamespacePrefix = "Generator.Prefix",
SeparateNamespaceHierarchy = false,
});

SharedTestFunctions.TestSamples(_output, "SeparateDefaultProviderGeneratorPrefix", PrefixPattern);
Assert.Equal(2, outputWriter.Files.Count());
Assert.Equal(Path.Combine(directory, "Generator.Prefix", "PurchaseOrderType.cs"), outputWriter.Files.First());
}

[Fact]
[TestPriority(1)]
[UseCulture("en-US")]
public void TestSeparateWithNamespaceProviderGeneratorPrefix()
{
var directory = Path.Combine("output", "FileOutputWriterTests", "SeparateWithNamespaceProviderGeneratorPrefix");
var outputWriter = new FileWatcherOutputWriter(directory);

Compiler.Generate(
"SeparateWithNamespaceProviderGeneratorPrefix",
PrefixPattern,
new Generator
{
OutputWriter = outputWriter,
EnableDataBinding = true,
SeparateClasses = true,
NamespacePrefix = "Generator.Prefix",
SeparateNamespaceHierarchy = true,
});

SharedTestFunctions.TestSamples(_output, "SeparateWithNamespaceProviderGeneratorPrefix", PrefixPattern);
Assert.Equal(2, outputWriter.Files.Count());
Assert.Equal(Path.Combine(directory, "Generator", "Prefix", "PurchaseOrderType.cs"), outputWriter.Files.First());
}

[Fact]
[TestPriority(1)]
[UseCulture("en-US")]
Expand Down
13 changes: 11 additions & 2 deletions XmlSchemaClassGenerator/FileOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu)

private void WriteSeparateFiles(CodeNamespace cn)
{
var dirPath = Path.Combine(OutputDirectory, ValidateName(cn.Name));
var validatedNamespaceName = ValidateName(cn.Name);

var namespacePath = validatedNamespaceName;

if (Configuration?.SeparateNamespaceHierarchy == true)
{
namespacePath = Path.Combine(validatedNamespaceName.Split('.'));
}

var dirPath = Path.Combine(OutputDirectory, namespacePath);
var ccu = new CodeCompileUnit();
var cns = new CodeNamespace(ValidateName(cn.Name));
var cns = new CodeNamespace(validatedNamespaceName);

Directory.CreateDirectory(dirPath);

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

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

static Generator()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Expand Down
5 changes: 5 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,10 @@ public void WriteLog(string message)
/// a numeric C# type is generated. If this is disabled, a union's type will default to string. Default is false.
/// </summary>
public bool MapUnionToWidestCommonType { get; set; }

/// <summary>
/// Separates namespace hierarchy by folder. Default is false.
/// </summary>
public bool SeparateNamespaceHierarchy { get; set; } = false;
}
}

0 comments on commit aebd9a3

Please sign in to comment.