From 732c17668ed9774a29e246d199e4c81d1428ba72 Mon Sep 17 00:00:00 2001 From: MrMikeJJ Date: Thu, 27 Jan 2022 20:06:53 +0000 Subject: [PATCH] Added option to prevent Prefix being added to output path --- XmlSchemaClassGenerator.Console/Program.cs | 4 ++++ XmlSchemaClassGenerator/FileOutputWriter.cs | 16 ++++++++++++++-- XmlSchemaClassGenerator/Generator.cs | 6 ++++++ .../GeneratorConfiguration.cs | 7 +++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index 15374ebf..ec03cf7f 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -55,6 +55,7 @@ static void Main(string[] args) var createGeneratedCodeAttributeVersion = true; var netCoreSpecificCode = false; var generateCommandLineArgs = true; + var addPrefixToOutputPath = true; var options = new OptionSet { { "h|help", "show this message and exit", v => showHelp = v != null }, @@ -87,6 +88,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l { "r|order", "emit order for all class members stored as XML element", v => emitOrder = v != null }, { "c|pcl", "PCL compatible output", v => pclCompatible = v != null }, { "p|prefix=", "the {PREFIX} to prepend to auto-generated namespace names", v => namespacePrefix = v }, + { "po|prefixOnOutput", "include the prefix on the path (default is enabled)", v => addPrefixToOutputPath = v != null }, { "v|verbose", "print generated file names on stdout", v => verbose = v != null }, { "0|nullable", "generate nullable adapter properties for optional elements/attributes w/o default values", v => nullables = v != null }, { "f|ef", "generate Entity Framework Code First compatible classes", v => entityFramework = v != null }, @@ -204,6 +206,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l CreateGeneratedCodeAttributeVersion = createGeneratedCodeAttributeVersion, NetCoreSpecificCode = netCoreSpecificCode, GenerateCommandLineArgumentsComment = generateCommandLineArgs, + NamespacePrefix = namespacePrefix, + AddPrefixToOutputPath = addPrefixToOutputPath, }; generator.CommentLanguages.AddRange(commentLanguages); diff --git a/XmlSchemaClassGenerator/FileOutputWriter.cs b/XmlSchemaClassGenerator/FileOutputWriter.cs index 81699983..63d6be1e 100644 --- a/XmlSchemaClassGenerator/FileOutputWriter.cs +++ b/XmlSchemaClassGenerator/FileOutputWriter.cs @@ -39,7 +39,13 @@ public override void Write(CodeNamespace cn) } else { - var path = Path.Combine(OutputDirectory, cn.Name + ".cs"); + var fileName = cn.Name; + if (Configuration != null && !Configuration.AddPrefixToOutputPath && !string.IsNullOrEmpty(Configuration.NamespacePrefix)) + { + fileName = cn.Name.Replace($"{Configuration.NamespacePrefix}.", string.Empty); + } + + var path = Path.Combine(OutputDirectory, fileName + ".cs"); Configuration?.WriteLog(path); WriteFile(path, cu); } @@ -68,7 +74,13 @@ protected virtual void WriteFile(string path, CodeCompileUnit cu) private void WriteSeparateFiles(CodeNamespace cn) { - var dirPath = Path.Combine(OutputDirectory, ValidateName(cn.Name)); + var dirName = cn.Name; + if (Configuration != null && !Configuration.AddPrefixToOutputPath && !string.IsNullOrEmpty(Configuration.NamespacePrefix)) + { + dirName = cn.Name.Replace($"{Configuration.NamespacePrefix}.", string.Empty); + } + + var dirPath = Path.Combine(OutputDirectory, ValidateName(dirName)); var ccu = new CodeCompileUnit(); var cns = new CodeNamespace(ValidateName(cn.Name)); diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index 0e790717..cad1e307 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -300,6 +300,12 @@ public CommandLineArgumentsProvider CommandLineArgumentsProvider set { _configuration.CommandLineArgumentsProvider = value; } } + public bool AddPrefixToOutputPath + { + get { return _configuration.AddPrefixToOutputPath; } + set { _configuration.AddPrefixToOutputPath = value; } + } + static Generator() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); diff --git a/XmlSchemaClassGenerator/GeneratorConfiguration.cs b/XmlSchemaClassGenerator/GeneratorConfiguration.cs index fa71cfd0..e04ca6df 100644 --- a/XmlSchemaClassGenerator/GeneratorConfiguration.cs +++ b/XmlSchemaClassGenerator/GeneratorConfiguration.cs @@ -39,6 +39,7 @@ public GeneratorConfiguration() Version = VersionProvider.CreateFromAssembly(); EnableUpaCheck = true; CommandLineArgumentsProvider = CommandLineArgumentsProvider.CreateFromEnvironment(); + AddPrefixToOutputPath = true; } /// @@ -316,5 +317,11 @@ public void WriteLog(string message) /// A provider to obtain the command line arguments of the tool. /// public CommandLineArgumentsProvider CommandLineArgumentsProvider { get; set; } + + /// + /// Adds the Namespace prefix to the Output path + /// Default is true. + /// + public bool AddPrefixToOutputPath { get; set; } } }