Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support netstandard2.0 #35

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions N.SourceGenerators.UnionTypes.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BusinessLogic", "examples\B
EndProject
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LegacyFramework", "examples\LegacyFramework\LegacyFramework.csproj", "{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -55,6 +57,7 @@ Global
{3B72811B-AA6E-4263-B0E9-931648D636BC} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
{007DEABA-55E6-4AF8-95D7-B869D6B46126} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
{5DD23F15-E950-4538-BF17-8BC5A7F4F720} = {08535BFD-5C2B-4D8B-90B7-520C578BA7E4}
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B} = {432FABD2-1B70-4E29-935B-AEE6D93559E2}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2C39796B-5B3A-42F9-84DF-0501A44030B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -85,5 +88,9 @@ Global
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DD23F15-E950-4538-BF17-8BC5A7F4F720}.Release|Any CPU.Build.0 = Release|Any CPU
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E90A05B8-3085-4DC6-857A-5D87ECEBC99B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion examples/DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\src\N.SourceGenerators.UnionTypes\N.SourceGenerators.UnionTypes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions examples/LegacyFramework/LegacyFramework.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!-- <LangVersion>8.0</LangVersion>-->
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\N.SourceGenerators.UnionTypes\N.SourceGenerators.UnionTypes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<None Update="Properties\launchSettings.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions examples/LegacyFramework/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Debug Generators": {
"commandName": "DebugRoslynComponent",
"targetProject": "LegacyFramework.csproj"
}
}
}
25 changes: 25 additions & 0 deletions examples/LegacyFramework/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using N.SourceGenerators.UnionTypes;

namespace LegacyFramework
{
public class Foo
{
}

public class Bar
{
}


[UnionType(typeof(Foo))]
[UnionType(typeof(Bar))]
public partial class Result
{
}

[UnionType(typeof(Foo))]
[UnionType(typeof(Bar))]
public partial struct StructResult
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@ public static ParameterSyntax AddAttributeListsWhen(
? syntax.AddAttributeLists(items)
: syntax;
}

public static SyntaxTriviaList AddWhen(
this SyntaxTriviaList syntax,
bool condition,
SyntaxTrivia trivia)
{
return condition
? syntax.Add(trivia)
: syntax;
}
}
8 changes: 8 additions & 0 deletions src/N.SourceGenerators.UnionTypes/Helpers/RoslynUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public static ThrowStatementSyntax ThrowInvalidOperationException(ExpressionSynt
SyntaxFactory.Argument(expression)
);
}

public static ThrowStatementSyntax ThrowArgumentNullException(ExpressionSyntax expression)
{
return ThrowException(
"System.ArgumentNullException",
SyntaxFactory.Argument(expression)
);
}

public static ThrowStatementSyntax ThrowException(string type, params ArgumentSyntax[] arguments)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace N.SourceGenerators.UnionTypes.Models;

internal record CompilationContext(
bool SupportsNotNullWhenAttribute,
bool SupportsThrowIfNull,
bool NullableContextEnabled,
bool SupportsAutoDefaultField);
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<!-- Generates a package at build -->
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<!-- Do not include the generator as a lib dependency -->
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsRoslynComponent>true</IsRoslynComponent>
<IsPackable>true</IsPackable>
<PackageOutputPath>./nupkg</PackageOutputPath>
<VersionPrefix>0.26.0</VersionPrefix>
<VersionPrefix>0.27.0</VersionPrefix>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public partial class UnionTypesGenerator

private const string UnionTypeAttributeText = $$"""
{{AutoGeneratedComment}}
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -25,12 +27,24 @@ namespace N.SourceGenerators.UnionTypes
internal sealed class UnionTypeAttribute : Attribute
{
public Type Type { get; }
#if NETCOREAPP3_1_OR_GREATER
public string? Alias { get; }
#else
public string Alias { get; }
#endif
public int Order { get; }
public bool AllowNull { get; set; }
#if NETCOREAPP3_1_OR_GREATER
public object? TypeDiscriminator { get; set; }

#else
public object TypeDiscriminator { get; set; }
#endif

#if NETCOREAPP3_1_OR_GREATER
public UnionTypeAttribute(Type type, string? alias = null, [CallerLineNumber] int order = 0)
#else
public UnionTypeAttribute(Type type, string alias = null, [CallerLineNumber] int order = 0)
#endif
{
Type = type;
Alias = alias;
Expand All @@ -42,7 +56,9 @@ public UnionTypeAttribute(Type type, string? alias = null, [CallerLineNumber] in

private const string GenericUnionTypeAttributeText = $$"""
{{AutoGeneratedComment}}
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -51,16 +67,24 @@ namespace N.SourceGenerators.UnionTypes
[AttributeUsage(AttributeTargets.GenericParameter, Inherited = false, AllowMultiple = false)]
internal sealed class GenericUnionTypeAttribute : Attribute
{
#if NETCOREAPP3_1_OR_GREATER
public string? Alias { get; set; }
public bool AllowNull { get; set; }
public object? TypeDiscriminator { get; set; }
#else
public string Alias { get; set; }
public bool AllowNull { get; set; }
public object TypeDiscriminator { get; set; }
#endif
}
}
""";

private const string JsonPolymorphicUnionAttributeText = $$"""
{{AutoGeneratedComment}}
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -69,14 +93,20 @@ namespace N.SourceGenerators.UnionTypes
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
internal sealed class JsonPolymorphicUnionAttribute : Attribute
{
#if NETCOREAPP3_1_OR_GREATER
public string? TypeDiscriminatorPropertyName { get; set; }
#else
public string TypeDiscriminatorPropertyName { get; set; }
#endif
}
}
""";

private const string UnionConverterFromAttributeText = $$"""
{{AutoGeneratedComment}}
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -97,7 +127,9 @@ public UnionConverterFromAttribute(Type fromType)

private const string UnionConverterToAttributeText = $$"""
{{AutoGeneratedComment}}
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -117,7 +149,9 @@ public UnionConverterToAttribute(Type toType)
""";

private const string UnionConverterAttributeText = """
#if NETCOREAPP3_1_OR_GREATER
#nullable enable
#endif
using System;
using System.Runtime.CompilerServices;

Expand All @@ -128,9 +162,17 @@ sealed class UnionConverterAttribute : Attribute
{
public Type FromType { get; }
public Type ToType { get; }
#if NETCOREAPP3_1_OR_GREATER
public string? MethodName { get; }
#else
public string MethodName { get; }
#endif

#if NETCOREAPP3_1_OR_GREATER
public UnionConverterAttribute(Type fromType, Type toType, string? methodName = null)
#else
public UnionConverterAttribute(Type fromType, Type toType, string methodName = null)
#endif
{
FromType = fromType;
ToType = toType;
Expand Down
Loading
Loading