Skip to content

Commit 820c205

Browse files
authored
feat: support netstandard2.0 (#35)
* feat: support netstandard2.0 * fix: struct union types for netstandard2.0
1 parent f1f8ac9 commit 820c205

File tree

217 files changed

+2825
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+2825
-85
lines changed

N.SourceGenerators.UnionTypes.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BusinessLogic", "examples\B
3838
EndProject
3939
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N.SourceGenerators.UnionTypes.BehaviorTests", "tests\N.SourceGenerators.UnionTypes.BehaviorTests\N.SourceGenerators.UnionTypes.BehaviorTests.csproj", "{5DD23F15-E950-4538-BF17-8BC5A7F4F720}"
4040
EndProject
41+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LegacyFramework", "examples\LegacyFramework\LegacyFramework.csproj", "{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}"
42+
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4345
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,7 @@ Global
5557
{3B72811B-AA6E-4263-B0E9-931648D636BC} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
5658
{007DEABA-55E6-4AF8-95D7-B869D6B46126} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
5759
{5DD23F15-E950-4538-BF17-8BC5A7F4F720} = {08535BFD-5C2B-4D8B-90B7-520C578BA7E4}
60+
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
5861
EndGlobalSection
5962
GlobalSection(ProjectConfigurationPlatforms) = postSolution
6063
{2C39796B-5B3A-42F9-84DF-0501A44030B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -85,5 +88,9 @@ Global
8588
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Debug|Any CPU.Build.0 = Debug|Any CPU
8689
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Release|Any CPU.ActiveCfg = Release|Any CPU
8790
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Release|Any CPU.Build.0 = Release|Any CPU
91+
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
92+
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Debug|Any CPU.Build.0 = Debug|Any CPU
93+
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Release|Any CPU.ActiveCfg = Release|Any CPU
94+
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Release|Any CPU.Build.0 = Release|Any CPU
8895
EndGlobalSection
8996
EndGlobal

examples/DataAccess/DataAccess.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
33
<ItemGroup>
44
<ProjectReference Include="..\..\src\N.SourceGenerators.UnionTypes\N.SourceGenerators.UnionTypes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
55
</ItemGroup>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<!-- <LangVersion>8.0</LangVersion>-->
6+
<ImplicitUsings>disable</ImplicitUsings>
7+
<Nullable>disable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\N.SourceGenerators.UnionTypes\N.SourceGenerators.UnionTypes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Properties\launchSettings.json">
16+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
17+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
18+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
19+
</None>
20+
</ItemGroup>
21+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"Debug Generators": {
4+
"commandName": "DebugRoslynComponent",
5+
"targetProject": "LegacyFramework.csproj"
6+
}
7+
}
8+
}

examples/LegacyFramework/Result.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using N.SourceGenerators.UnionTypes;
2+
3+
namespace LegacyFramework
4+
{
5+
public class Foo
6+
{
7+
}
8+
9+
public class Bar
10+
{
11+
}
12+
13+
14+
[UnionType(typeof(Foo))]
15+
[UnionType(typeof(Bar))]
16+
public partial class Result
17+
{
18+
}
19+
20+
[UnionType(typeof(Foo))]
21+
[UnionType(typeof(Bar))]
22+
public partial struct StructResult
23+
{
24+
}
25+
}

src/N.SourceGenerators.UnionTypes/Extensions/ConditionalSyntaxExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,14 @@ public static ParameterSyntax AddAttributeListsWhen(
125125
? syntax.AddAttributeLists(items)
126126
: syntax;
127127
}
128+
129+
public static SyntaxTriviaList AddWhen(
130+
this SyntaxTriviaList syntax,
131+
bool condition,
132+
SyntaxTrivia trivia)
133+
{
134+
return condition
135+
? syntax.Add(trivia)
136+
: syntax;
137+
}
128138
}

src/N.SourceGenerators.UnionTypes/Helpers/RoslynUtils.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public static ThrowStatementSyntax ThrowInvalidOperationException(ExpressionSynt
7070
SyntaxFactory.Argument(expression)
7171
);
7272
}
73+
74+
public static ThrowStatementSyntax ThrowArgumentNullException(ExpressionSyntax expression)
75+
{
76+
return ThrowException(
77+
"System.ArgumentNullException",
78+
SyntaxFactory.Argument(expression)
79+
);
80+
}
7381

7482
public static ThrowStatementSyntax ThrowException(string type, params ArgumentSyntax[] arguments)
7583
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace N.SourceGenerators.UnionTypes.Models;
2+
3+
internal record CompilationContext(
4+
bool SupportsNotNullWhenAttribute,
5+
bool SupportsThrowIfNull,
6+
bool NullableContextEnabled,
7+
bool SupportsAutoDefaultField);

src/N.SourceGenerators.UnionTypes/N.SourceGenerators.UnionTypes.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>11</LangVersion>
5+
<LangVersion>12</LangVersion>
66
<!-- Generates a package at build -->
77
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
88
<!-- Do not include the generator as a lib dependency -->
99
<IncludeBuildOutput>false</IncludeBuildOutput>
1010
<IsRoslynComponent>true</IsRoslynComponent>
1111
<IsPackable>true</IsPackable>
1212
<PackageOutputPath>./nupkg</PackageOutputPath>
13-
<VersionPrefix>0.26.0</VersionPrefix>
13+
<VersionPrefix>0.27.0</VersionPrefix>
1414
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1515
<NoWarn>$(NoWarn);NU5128</NoWarn>
1616
</PropertyGroup>

src/N.SourceGenerators.UnionTypes/UnionTypesGenerator.Attributes.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public partial class UnionTypesGenerator
1515

1616
private const string UnionTypeAttributeText = $$"""
1717
{{AutoGeneratedComment}}
18+
#if NETCOREAPP3_1_OR_GREATER
1819
#nullable enable
20+
#endif
1921
using System;
2022
using System.Runtime.CompilerServices;
2123
@@ -25,12 +27,24 @@ namespace N.SourceGenerators.UnionTypes
2527
internal sealed class UnionTypeAttribute : Attribute
2628
{
2729
public Type Type { get; }
30+
#if NETCOREAPP3_1_OR_GREATER
2831
public string? Alias { get; }
32+
#else
33+
public string Alias { get; }
34+
#endif
2935
public int Order { get; }
3036
public bool AllowNull { get; set; }
37+
#if NETCOREAPP3_1_OR_GREATER
3138
public object? TypeDiscriminator { get; set; }
32-
39+
#else
40+
public object TypeDiscriminator { get; set; }
41+
#endif
42+
43+
#if NETCOREAPP3_1_OR_GREATER
3344
public UnionTypeAttribute(Type type, string? alias = null, [CallerLineNumber] int order = 0)
45+
#else
46+
public UnionTypeAttribute(Type type, string alias = null, [CallerLineNumber] int order = 0)
47+
#endif
3448
{
3549
Type = type;
3650
Alias = alias;
@@ -42,7 +56,9 @@ public UnionTypeAttribute(Type type, string? alias = null, [CallerLineNumber] in
4256

4357
private const string GenericUnionTypeAttributeText = $$"""
4458
{{AutoGeneratedComment}}
59+
#if NETCOREAPP3_1_OR_GREATER
4560
#nullable enable
61+
#endif
4662
using System;
4763
using System.Runtime.CompilerServices;
4864
@@ -51,16 +67,24 @@ namespace N.SourceGenerators.UnionTypes
5167
[AttributeUsage(AttributeTargets.GenericParameter, Inherited = false, AllowMultiple = false)]
5268
internal sealed class GenericUnionTypeAttribute : Attribute
5369
{
70+
#if NETCOREAPP3_1_OR_GREATER
5471
public string? Alias { get; set; }
5572
public bool AllowNull { get; set; }
5673
public object? TypeDiscriminator { get; set; }
74+
#else
75+
public string Alias { get; set; }
76+
public bool AllowNull { get; set; }
77+
public object TypeDiscriminator { get; set; }
78+
#endif
5779
}
5880
}
5981
""";
6082

6183
private const string JsonPolymorphicUnionAttributeText = $$"""
6284
{{AutoGeneratedComment}}
85+
#if NETCOREAPP3_1_OR_GREATER
6386
#nullable enable
87+
#endif
6488
using System;
6589
using System.Runtime.CompilerServices;
6690
@@ -69,14 +93,20 @@ namespace N.SourceGenerators.UnionTypes
6993
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
7094
internal sealed class JsonPolymorphicUnionAttribute : Attribute
7195
{
96+
#if NETCOREAPP3_1_OR_GREATER
7297
public string? TypeDiscriminatorPropertyName { get; set; }
98+
#else
99+
public string TypeDiscriminatorPropertyName { get; set; }
100+
#endif
73101
}
74102
}
75103
""";
76104

77105
private const string UnionConverterFromAttributeText = $$"""
78106
{{AutoGeneratedComment}}
107+
#if NETCOREAPP3_1_OR_GREATER
79108
#nullable enable
109+
#endif
80110
using System;
81111
using System.Runtime.CompilerServices;
82112
@@ -97,7 +127,9 @@ public UnionConverterFromAttribute(Type fromType)
97127

98128
private const string UnionConverterToAttributeText = $$"""
99129
{{AutoGeneratedComment}}
130+
#if NETCOREAPP3_1_OR_GREATER
100131
#nullable enable
132+
#endif
101133
using System;
102134
using System.Runtime.CompilerServices;
103135
@@ -117,7 +149,9 @@ public UnionConverterToAttribute(Type toType)
117149
""";
118150

119151
private const string UnionConverterAttributeText = """
152+
#if NETCOREAPP3_1_OR_GREATER
120153
#nullable enable
154+
#endif
121155
using System;
122156
using System.Runtime.CompilerServices;
123157
@@ -128,9 +162,17 @@ sealed class UnionConverterAttribute : Attribute
128162
{
129163
public Type FromType { get; }
130164
public Type ToType { get; }
165+
#if NETCOREAPP3_1_OR_GREATER
131166
public string? MethodName { get; }
167+
#else
168+
public string MethodName { get; }
169+
#endif
132170
171+
#if NETCOREAPP3_1_OR_GREATER
133172
public UnionConverterAttribute(Type fromType, Type toType, string? methodName = null)
173+
#else
174+
public UnionConverterAttribute(Type fromType, Type toType, string methodName = null)
175+
#endif
134176
{
135177
FromType = fromType;
136178
ToType = toType;

0 commit comments

Comments
 (0)