-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactoring v2] Add source generator for adding enum extensions
- Loading branch information
1 parent
6f7086a
commit 5cebb37
Showing
13 changed files
with
335 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using TraktNET.SourceGenerators; | ||
|
||
namespace TraktNET | ||
{ | ||
[TraktSmartEnum] | ||
public enum TraktAccessScope | ||
{ | ||
Unspecified, | ||
Private, | ||
Friends, | ||
Public | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using TraktNET.SourceGenerators; | ||
|
||
namespace TraktNET | ||
{ | ||
[TraktSmartEnum] | ||
public enum TraktAccessTokenType | ||
{ | ||
Unspecified, | ||
Bearer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
||
<ItemGroup Condition="('$(TargetFramework)' == 'netstandard2.0') Or ('$(TargetFramework)' == 'netstandard2.1')"> | ||
<PackageReference Include="System.Text.Json" Version="8.0.1" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace TraktNET.SourceGenerators | ||
{ | ||
internal static class Constants | ||
{ | ||
internal const string Header = @"//----------------------------------------------------------------------------------------------------- | ||
// <auto-generated> | ||
// This code was generated by the Trakt.NET source generator. | ||
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. | ||
// </auto-generated> | ||
//----------------------------------------------------------------------------------------------------- | ||
#nullable enable | ||
"; | ||
|
||
internal const string SmartEnumAttribute = Header + @" | ||
namespace TraktNET.SourceGenerators | ||
{ | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Enum)] | ||
#if NET5_0_OR_GREATER | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = ""Generated by the Trakt.NET source generator."")] | ||
#else | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
#endif | ||
public class TraktSmartEnumAttribute : global::System.Attribute | ||
{ | ||
} | ||
} | ||
"; | ||
|
||
internal const string FullTraktSmartEnumAttributeName = "TraktNET.SourceGenerators.TraktSmartEnumAttribute"; | ||
|
||
internal const string GeneratedSmartEnumAttributeFilename = "TraktSmartEnumAttribute.g.cs"; | ||
|
||
internal const string GeneratedSmartEnumFileExtension = "EnumExtensions.g.cs"; | ||
|
||
internal const string TraktSmartEnumAttributeName = "TraktSmartEnumAttribute"; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/tools/Trakt.NET.SourceGenerators/SourceGenerationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace TraktNET.SourceGenerators | ||
{ | ||
internal static class SourceGenerationHelper | ||
{ | ||
internal static string GenerateEnumExtensionClass(StringBuilder stringBuilder, in TraktEnumToGenerate enumToGenerate) | ||
{ | ||
string fullyQualifiedName = $"global::{enumToGenerate.FullyQualifiedName}"; | ||
|
||
stringBuilder.Clear(); | ||
stringBuilder.Append(Constants.Header); | ||
stringBuilder.Append(@" | ||
/// <summary>Extension methods for <see cref=""").Append(fullyQualifiedName).Append(@""" />.</summary> | ||
"); | ||
|
||
stringBuilder.Append(@"#if NET5_0_OR_GREATER | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = ""Generated by the Trakt.NET source generator."")] | ||
#else | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
#endif"); | ||
stringBuilder.Append(@" | ||
internal static partial class ").Append(enumToGenerate.CompleteName); | ||
stringBuilder.Append(@" | ||
{"); | ||
|
||
stringBuilder.Append(@" | ||
/// <summary>Returns the Json value for <see cref=""").Append(fullyQualifiedName).Append(@""" />.</summary>"); | ||
stringBuilder.Append(@" | ||
internal static string? ToJson(this ").Append(fullyQualifiedName).Append(" value)"); | ||
|
||
stringBuilder.Append(@" | ||
=> value switch | ||
{"); | ||
|
||
foreach (string member in enumToGenerate.Values) | ||
{ | ||
if (member == "Unspecified") | ||
{ | ||
stringBuilder.Append(@" | ||
").Append(fullyQualifiedName).Append('.').Append(member) | ||
.Append(" => null,"); | ||
} | ||
else | ||
{ | ||
stringBuilder.Append(@" | ||
").Append(fullyQualifiedName).Append('.').Append(member) | ||
.Append(" => ").Append('"').Append(member.ToLower(CultureInfo.InvariantCulture)).Append(@""","); | ||
} | ||
} | ||
|
||
stringBuilder.Append(@" | ||
_ => null,"); | ||
|
||
stringBuilder.Append(@" | ||
};"); | ||
|
||
stringBuilder.Append(@" | ||
/// <summary> | ||
/// Returns a <see cref=""").Append(fullyQualifiedName).Append(@""" /> for the given value, if possible. | ||
/// <para /> | ||
/// If not possible, the value <see cref=""").Append(fullyQualifiedName).Append(".Unspecified").Append(@""" /> will be returned. | ||
/// </summary>"); | ||
stringBuilder.Append(@" | ||
internal static ").Append(fullyQualifiedName).Append(" To").Append(enumToGenerate.Name).Append("(this string? value)"); | ||
|
||
stringBuilder.Append(@" | ||
=> value switch | ||
{"); | ||
|
||
foreach (string member in enumToGenerate.Values) | ||
{ | ||
if (member == "Unspecified") | ||
continue; | ||
|
||
stringBuilder.Append(@" | ||
").Append($"\"{member.ToLower(CultureInfo.InvariantCulture)}\" => {fullyQualifiedName}.{member},"); | ||
|
||
stringBuilder.Append(@" | ||
").Append($"\"{member.ToUpper(CultureInfo.InvariantCulture)}\" => {fullyQualifiedName}.{member},"); | ||
|
||
stringBuilder.Append(@" | ||
").Append($"\"{member}\" => {fullyQualifiedName}.{member},"); | ||
} | ||
|
||
stringBuilder.Append(@" | ||
_ => ").Append(fullyQualifiedName).Append(".Unspecified,"); | ||
|
||
stringBuilder.Append(@" | ||
};"); | ||
|
||
stringBuilder.Append(@" | ||
/// <summary>Returns the display name for <see cref=""").Append(fullyQualifiedName).Append(@""" />.</summary>"); | ||
stringBuilder.Append(@" | ||
internal static string DisplayName(this ").Append(fullyQualifiedName).Append(" value)"); | ||
|
||
stringBuilder.Append(@" | ||
=> value switch | ||
{"); | ||
|
||
foreach (string member in enumToGenerate.Values) | ||
{ | ||
stringBuilder.Append(@" | ||
").Append(fullyQualifiedName).Append('.').Append(member) | ||
.Append(" => ").Append('"').Append(member).Append(@""","); | ||
} | ||
|
||
stringBuilder.Append(@" | ||
_ => value.ToString(),"); | ||
|
||
stringBuilder.Append(@" | ||
};"); | ||
|
||
stringBuilder.Append(@" | ||
} | ||
"); | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/tools/Trakt.NET.SourceGenerators/Trakt.NET.SourceGenerators.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Product>Trakt.NET.SourceGenerators</Product> | ||
<Title>Trakt.NET.SourceGenerators</Title> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
</ItemGroup> | ||
</Project> |
21 changes: 21 additions & 0 deletions
21
src/tools/Trakt.NET.SourceGenerators/TraktEnumToGenerate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace TraktNET | ||
{ | ||
public readonly record struct TraktEnumToGenerate | ||
{ | ||
public readonly string Name; | ||
Check warning on line 5 in src/tools/Trakt.NET.SourceGenerators/TraktEnumToGenerate.cs
|
||
|
||
public readonly string CompleteName; | ||
|
||
public readonly string FullyQualifiedName; | ||
|
||
public readonly IList<string> Values; | ||
|
||
public TraktEnumToGenerate(string name, string completeName, string fullyQualifiedName, IList<string> values) | ||
{ | ||
Name = name; | ||
CompleteName = completeName; | ||
FullyQualifiedName = fullyQualifiedName; | ||
Values = values; | ||
} | ||
} | ||
} |
Oops, something went wrong.