Skip to content

Commit 076c4ed

Browse files
committed
add source generator with global namespace setting
1 parent 0141ec3 commit 076c4ed

File tree

7 files changed

+252
-0
lines changed

7 files changed

+252
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema id="XMLSchema1"
3+
targetNamespace="mysamplenamespace"
4+
elementFormDefault="qualified"
5+
xmlns="http://tempuri.org/XMLSchema1.xsd"
6+
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
7+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
8+
>
9+
<xs:element name="MyRootElement">
10+
<xs:complexType id="RootType">
11+
<xs:all>
12+
<xs:element name="Child1">
13+
<xs:simpleType id="Child1Type">
14+
<xs:restriction base="xs:boolean"/>
15+
</xs:simpleType>
16+
</xs:element>
17+
<xs:element name="Child2">
18+
<xs:simpleType id="Child2Type">
19+
<xs:restriction base="xs:string"/>
20+
</xs:simpleType>
21+
</xs:element>
22+
</xs:all>
23+
</xs:complexType>
24+
</xs:element>
25+
</xs:schema>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace XmlSchemaClassGenerator.SourceGenerator.Tests
5+
{
6+
public class SimpleSchemaTests
7+
{
8+
[Fact]
9+
public void Compiles()
10+
{
11+
new Sample.Generated.MyRootElement
12+
{
13+
Child1 = true,
14+
Child2 = "foo"
15+
};
16+
}
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="3.1.0">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
<Import Project="..\XmlSchemaClassGenerator.SourceGenerator\XmlSchemaClassGenerator.SourceGenerator.props"/>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\XmlSchemaClassGenerator.SourceGenerator\XmlSchemaClassGenerator.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<AdditionalFiles Include="Samples\simple_schema.xsd" />
30+
</ItemGroup>
31+
32+
<PropertyGroup>
33+
<xscgen_Namespace>Sample.Generated</xscgen_Namespace>
34+
</PropertyGroup>
35+
36+
<ItemGroup>
37+
<Content Update="Samples\simple_schema.xsd">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</Content>
40+
</ItemGroup>
41+
42+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<IncludeBuildOutput>false</IncludeBuildOutput>
6+
<NoPackageAnalysis>true</NoPackageAnalysis>
7+
8+
<PackageId>XmlSchemaClassGenerator.SourceGenerator</PackageId>
9+
<Authors>Sven Hübner</Authors>
10+
<Description>Source generator for POCOs from XSD schema files, based on XmlSchemaClassGenerator</Description>
11+
<PackageTags>xml;xsd;Schema;Source Generator;poco;XmlSchemaClassGenerator</PackageTags>
12+
<PackageProjectUrl>https://github.com/mganss/XmlSchemaClassGenerator</PackageProjectUrl>
13+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
14+
<RepositoryType>git</RepositoryType>
15+
<RepositoryUrl>git://github.com/mganss/XmlSchemaClassGenerator</RepositoryUrl>
16+
<RootNamespace>XmlSchemaClassGenerator</RootNamespace>
17+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
18+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
19+
<IncludeSymbols>true</IncludeSymbols>
20+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" PrivateAssets="all" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
33+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<PackageReference Include="System.CodeDom" Version="5.0.0" PrivateAssets="all" GeneratePathProperty="true" />
38+
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" PrivateAssets="all" GeneratePathProperty="true" />
39+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" PrivateAssets="all" GeneratePathProperty="true" />
40+
<PackageReference Include="System.ValueTuple" Version="4.5.0" PrivateAssets="all" GeneratePathProperty="true" />
41+
<PackageReference Include="System.ComponentModel.Annotations" Version="4.4.0" GeneratePathProperty="true"/>
42+
</ItemGroup>
43+
44+
<PropertyGroup>
45+
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
46+
</PropertyGroup>
47+
48+
<Target Name="GetDependencyTargetPaths">
49+
<ItemGroup>
50+
<TargetPathWithTargetPlatformMoniker Include="..\XmlSchemaClassGenerator\bin\$(Configuration)\$(TargetFramework)\XmlSchemaClassGenerator.dll" IncludeRuntimeDependency="false" />
51+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_CodeDom)\lib\netstandard2.0\System.CodeDom.dll" IncludeRuntimeDependency="false" />
52+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Encoding_CodePages)\lib\netstandard2.0\System.Text.Encoding.CodePages.dll" IncludeRuntimeDependency="false" />
53+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_ValueTuple)\lib\netstandard1.0\System.ValueTuple.dll" IncludeRuntimeDependency="false" />
54+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_ComponentModel_Annotations)\lib\netstandard2.0\System.ComponentModel.Annotations.dll" IncludeRuntimeDependency="false" />
55+
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Runtime_CompilerServices_Unsafe)\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll" IncludeRuntimeDependency="false" />
56+
</ItemGroup>
57+
</Target>
58+
59+
<ItemGroup>
60+
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
61+
<None Include="..\XmlSchemaClassGenerator\bin\$(Configuration)\$(TargetFramework)\XmlSchemaClassGenerator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
62+
<None Include="$(PKGSystem_CodeDom)\lib\netstandard2.0\System.CodeDom.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
63+
<None Include="$(PKGSystem_Text_Encoding_CodePages)\lib\netstandard2.0\System.Text.Encoding.CodePages.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
64+
<None Include="$(PKGSystem_ValueTuple)\lib\netstandard1.0\System.ValueTuple.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
65+
<None Include="$(PKGSystem_ComponentModel_Annotations)\lib\netstandard2.0\System.ComponentModel.Annotations.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
66+
<None Include="$(PKGSystem_Runtime_CompilerServices_Unsafe)\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
67+
</ItemGroup>
68+
69+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<ItemGroup>
3+
<CompilerVisibleProperty Include="xscgen_Namespace" />
4+
</ItemGroup>
5+
</Project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Diagnostics;
3+
using System;
4+
using System.CodeDom;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Xml;
9+
using System.Xml.Schema;
10+
11+
namespace XmlSchemaClassGenerator
12+
{
13+
[Generator]
14+
public class XsdSourceGenerator : ISourceGenerator
15+
{
16+
internal class MemoryOutputWriter : OutputWriter
17+
{
18+
public string Content { get; set; }
19+
20+
public override void Write(CodeNamespace cn)
21+
{
22+
var cu = new CodeCompileUnit();
23+
cu.Namespaces.Add(cn);
24+
25+
using (var writer = new StringWriter())
26+
{
27+
Write(writer, cu);
28+
Content = writer.ToString();
29+
}
30+
}
31+
}
32+
33+
public void Execute(GeneratorExecutionContext context)
34+
{
35+
#if DEBUG
36+
if (!Debugger.IsAttached)
37+
{
38+
//Debugger.Launch();
39+
}
40+
#endif
41+
var configurations = GetConfigurations(context);
42+
43+
foreach (var (schemaFile, @namespace) in configurations)
44+
{
45+
var schemaStr = schemaFile.GetText().ToString();
46+
var stringReader = new StringReader(schemaStr);
47+
48+
var schemaSet = new XmlSchemaSet();
49+
schemaSet.Add(null, XmlReader.Create(stringReader));
50+
51+
var generator = new Generator();
52+
generator.NamespaceProvider.Add(new NamespaceKey(), @namespace);
53+
MemoryOutputWriter memoryOutputWriter = new MemoryOutputWriter();
54+
generator.OutputWriter = memoryOutputWriter;
55+
generator.Generate(schemaSet);
56+
context.AddSource("Pocos", memoryOutputWriter.Content);
57+
}
58+
}
59+
60+
public void Initialize(GeneratorInitializationContext context)
61+
{
62+
// do nothing
63+
}
64+
65+
static IEnumerable<(AdditionalText SchemaFile, string Namespace)> GetConfigurations(GeneratorExecutionContext context)
66+
{
67+
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.xscgen_Namespace", out var @namespace))
68+
{
69+
@namespace = "Generated";
70+
}
71+
72+
foreach (AdditionalText file in context.AdditionalFiles)
73+
{
74+
if (Path.GetExtension(file.Path).Equals(".xsd", StringComparison.OrdinalIgnoreCase))
75+
{
76+
yield return (file, @namespace);
77+
}
78+
}
79+
}
80+
}
81+
}

XmlSchemaClassGenerator.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2020
EndProject
2121
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xscgen-proj", "xscgen-proj\xscgen-proj.csproj", "{2F20FF02-12AB-448B-BC78-DD76DD4C9E66}"
2222
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlSchemaClassGenerator.SourceGenerator", "XmlSchemaClassGenerator.SourceGenerator\XmlSchemaClassGenerator.SourceGenerator.csproj", "{1A4760D7-7618-41E8-BC97-C68566B7A16C}"
24+
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlSchemaClassGenerator.SourceGenerator.Tests", "XmlSchemaClassGenerator.SourceGenerator.Tests\XmlSchemaClassGenerator.SourceGenerator.Tests.csproj", "{333F3108-2714-4706-ABA7-4298EFEBFCA7}"
26+
EndProject
2327
Global
2428
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2529
Debug|Any CPU = Debug|Any CPU
@@ -50,6 +54,14 @@ Global
5054
{2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Debug|Any CPU.Build.0 = Debug|Any CPU
5155
{2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Release|Any CPU.ActiveCfg = Release|Any CPU
5256
{2F20FF02-12AB-448B-BC78-DD76DD4C9E66}.Release|Any CPU.Build.0 = Release|Any CPU
57+
{1A4760D7-7618-41E8-BC97-C68566B7A16C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58+
{1A4760D7-7618-41E8-BC97-C68566B7A16C}.Debug|Any CPU.Build.0 = Debug|Any CPU
59+
{1A4760D7-7618-41E8-BC97-C68566B7A16C}.Release|Any CPU.ActiveCfg = Release|Any CPU
60+
{1A4760D7-7618-41E8-BC97-C68566B7A16C}.Release|Any CPU.Build.0 = Release|Any CPU
61+
{333F3108-2714-4706-ABA7-4298EFEBFCA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
62+
{333F3108-2714-4706-ABA7-4298EFEBFCA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
63+
{333F3108-2714-4706-ABA7-4298EFEBFCA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
64+
{333F3108-2714-4706-ABA7-4298EFEBFCA7}.Release|Any CPU.Build.0 = Release|Any CPU
5365
EndGlobalSection
5466
GlobalSection(SolutionProperties) = preSolution
5567
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)