diff --git a/XmlSchemaClassGenerator.SourceGenerator.Tests/Samples/simple_schema.xsd b/XmlSchemaClassGenerator.SourceGenerator.Tests/Samples/simple_schema.xsd new file mode 100644 index 00000000..f96efc29 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator.Tests/Samples/simple_schema.xsd @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/XmlSchemaClassGenerator.SourceGenerator.Tests/SimpleSchemaTests.cs b/XmlSchemaClassGenerator.SourceGenerator.Tests/SimpleSchemaTests.cs new file mode 100644 index 00000000..c8780310 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator.Tests/SimpleSchemaTests.cs @@ -0,0 +1,18 @@ +using System; +using Xunit; + +namespace XmlSchemaClassGenerator.SourceGenerator.Tests +{ + public class SimpleSchemaTests + { + [Fact] + public void Compiles() + { + new Sample.Generated.MyRootElement + { + Child1 = true, + Child2 = "foo" + }; + } + } +} diff --git a/XmlSchemaClassGenerator.SourceGenerator.Tests/XmlSchemaClassGenerator.SourceGenerator.Tests.csproj b/XmlSchemaClassGenerator.SourceGenerator.Tests/XmlSchemaClassGenerator.SourceGenerator.Tests.csproj new file mode 100644 index 00000000..52123717 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator.Tests/XmlSchemaClassGenerator.SourceGenerator.Tests.csproj @@ -0,0 +1,42 @@ + + + + netcoreapp3.1 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + Sample.Generated + + + + + PreserveNewest + + + + diff --git a/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.csproj b/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.csproj new file mode 100644 index 00000000..b4cba198 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.csproj @@ -0,0 +1,69 @@ + + + + netstandard2.0 + false + true + + XmlSchemaClassGenerator.SourceGenerator + Sven Hübner + Source generator for POCOs from XSD schema files, based on XmlSchemaClassGenerator + xml;xsd;Schema;Source Generator;poco;XmlSchemaClassGenerator + https://github.com/mganss/XmlSchemaClassGenerator + Apache-2.0 + git + git://github.com/mganss/XmlSchemaClassGenerator + XmlSchemaClassGenerator + true + true + true + snupkg + + + + + + + + + + + + + + + + + + + + + + + + + $(GetTargetPathDependsOn);GetDependencyTargetPaths + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.props b/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.props new file mode 100644 index 00000000..b8d5f063 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator/XmlSchemaClassGenerator.SourceGenerator.props @@ -0,0 +1,5 @@ + + + + + diff --git a/XmlSchemaClassGenerator.SourceGenerator/XsdSourceGenerator.cs b/XmlSchemaClassGenerator.SourceGenerator/XsdSourceGenerator.cs new file mode 100644 index 00000000..126d1c09 --- /dev/null +++ b/XmlSchemaClassGenerator.SourceGenerator/XsdSourceGenerator.cs @@ -0,0 +1,81 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Xml; +using System.Xml.Schema; + +namespace XmlSchemaClassGenerator +{ + [Generator] + public class XsdSourceGenerator : ISourceGenerator + { + internal class MemoryOutputWriter : OutputWriter + { + public string Content { get; set; } + + public override void Write(CodeNamespace cn) + { + var cu = new CodeCompileUnit(); + cu.Namespaces.Add(cn); + + using (var writer = new StringWriter()) + { + Write(writer, cu); + Content = writer.ToString(); + } + } + } + + public void Execute(GeneratorExecutionContext context) + { +#if DEBUG + if (!Debugger.IsAttached) + { + //Debugger.Launch(); + } +#endif + var configurations = GetConfigurations(context); + + foreach (var (schemaFile, @namespace) in configurations) + { + var schemaStr = schemaFile.GetText().ToString(); + var stringReader = new StringReader(schemaStr); + + var schemaSet = new XmlSchemaSet(); + schemaSet.Add(null, XmlReader.Create(stringReader)); + + var generator = new Generator(); + generator.NamespaceProvider.Add(new NamespaceKey(), @namespace); + MemoryOutputWriter memoryOutputWriter = new MemoryOutputWriter(); + generator.OutputWriter = memoryOutputWriter; + generator.Generate(schemaSet); + context.AddSource("Pocos", memoryOutputWriter.Content); + } + } + + public void Initialize(GeneratorInitializationContext context) + { + // do nothing + } + + static IEnumerable<(AdditionalText SchemaFile, string Namespace)> GetConfigurations(GeneratorExecutionContext context) + { + if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.xscgen_Namespace", out var @namespace)) + { + @namespace = "Generated"; + } + + foreach (AdditionalText file in context.AdditionalFiles) + { + if (Path.GetExtension(file.Path).Equals(".xsd", StringComparison.OrdinalIgnoreCase)) + { + yield return (file, @namespace); + } + } + } + } +} diff --git a/XmlSchemaClassGenerator.sln b/XmlSchemaClassGenerator.sln index 23fb93ad..9e36bfda 100644 --- a/XmlSchemaClassGenerator.sln +++ b/XmlSchemaClassGenerator.sln @@ -20,6 +20,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xscgen-proj", "xscgen-proj\xscgen-proj.csproj", "{2F20FF02-12AB-448B-BC78-DD76DD4C9E66}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlSchemaClassGenerator.SourceGenerator", "XmlSchemaClassGenerator.SourceGenerator\XmlSchemaClassGenerator.SourceGenerator.csproj", "{1A4760D7-7618-41E8-BC97-C68566B7A16C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlSchemaClassGenerator.SourceGenerator.Tests", "XmlSchemaClassGenerator.SourceGenerator.Tests\XmlSchemaClassGenerator.SourceGenerator.Tests.csproj", "{333F3108-2714-4706-ABA7-4298EFEBFCA7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -50,6 +54,14 @@ Global {2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Debug|Any CPU.Build.0 = Debug|Any CPU {2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Release|Any CPU.ActiveCfg = Release|Any CPU {2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Release|Any CPU.Build.0 = Release|Any CPU + {1A4760D7-7618-41E8-BC97-C68566B7A16C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A4760D7-7618-41E8-BC97-C68566B7A16C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A4760D7-7618-41E8-BC97-C68566B7A16C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A4760D7-7618-41E8-BC97-C68566B7A16C}.Release|Any CPU.Build.0 = Release|Any CPU + {333F3108-2714-4706-ABA7-4298EFEBFCA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {333F3108-2714-4706-ABA7-4298EFEBFCA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {333F3108-2714-4706-ABA7-4298EFEBFCA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {333F3108-2714-4706-ABA7-4298EFEBFCA7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE