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

Feature/external enum generation #6

Merged
merged 5 commits into from
Jun 9, 2023
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
27 changes: 27 additions & 0 deletions EnumClass.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnumClass.JsonConverter.Gen
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnumClass.JsonSerialization", "samples\EnumClass.JsonSerialization\EnumClass.JsonSerialization.csproj", "{7BE2B7EC-E2E7-4FC5-9403-0CB536C8528E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HelperProjects", "HelperProjects", "{52329321-E2A3-468D-8434-49B941DC1431}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleEnums", "tests\Generator\SampleEnums\SampleEnums.csproj", "{E948745F-D6D2-4455-9066-D128E846D711}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FromExternalAssembly", "FromExternalAssembly", "{2E55B6B6-0217-45DB-B097-28589F21767C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnumClass.FromAnotherAssembly", "samples\EnumClass.FromAnotherAssembly\EnumClass.FromAnotherAssembly.csproj", "{7ACE470B-C303-4F55-AFAF-12178860A48E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnumClass.EnumOnly", "samples\EnumClass.EnumOnly\EnumClass.EnumOnly.csproj", "{7932ED00-F088-4227-8BAB-22FB30966395}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,6 +98,18 @@ Global
{7BE2B7EC-E2E7-4FC5-9403-0CB536C8528E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BE2B7EC-E2E7-4FC5-9403-0CB536C8528E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BE2B7EC-E2E7-4FC5-9403-0CB536C8528E}.Release|Any CPU.Build.0 = Release|Any CPU
{E948745F-D6D2-4455-9066-D128E846D711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E948745F-D6D2-4455-9066-D128E846D711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E948745F-D6D2-4455-9066-D128E846D711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E948745F-D6D2-4455-9066-D128E846D711}.Release|Any CPU.Build.0 = Release|Any CPU
{7ACE470B-C303-4F55-AFAF-12178860A48E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7ACE470B-C303-4F55-AFAF-12178860A48E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7ACE470B-C303-4F55-AFAF-12178860A48E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7ACE470B-C303-4F55-AFAF-12178860A48E}.Release|Any CPU.Build.0 = Release|Any CPU
{7932ED00-F088-4227-8BAB-22FB30966395}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7932ED00-F088-4227-8BAB-22FB30966395}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7932ED00-F088-4227-8BAB-22FB30966395}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7932ED00-F088-4227-8BAB-22FB30966395}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2B03E852-A29E-4881-A013-BDC01C5021FF} = {EEBC4D9F-FCE9-4F37-8C48-7D9512302129}
Expand All @@ -103,5 +125,10 @@ Global
{2115BF90-CF61-40FF-840C-C7ED37CFF373} = {26FD7445-A8B3-45A5-85C1-FAAAF2C00335}
{8C00EDB7-C432-4348-A8D4-E435DF85C891} = {2115BF90-CF61-40FF-840C-C7ED37CFF373}
{7BE2B7EC-E2E7-4FC5-9403-0CB536C8528E} = {06B1141F-5E62-4F18-9ADC-3D8205E1BA8A}
{52329321-E2A3-468D-8434-49B941DC1431} = {05C15E8B-5714-4CB9-96BB-5A2BA63EDF91}
{E948745F-D6D2-4455-9066-D128E846D711} = {52329321-E2A3-468D-8434-49B941DC1431}
{2E55B6B6-0217-45DB-B097-28589F21767C} = {06B1141F-5E62-4F18-9ADC-3D8205E1BA8A}
{7ACE470B-C303-4F55-AFAF-12178860A48E} = {2E55B6B6-0217-45DB-B097-28589F21767C}
{7932ED00-F088-4227-8BAB-22FB30966395} = {2E55B6B6-0217-45DB-B097-28589F21767C}
EndGlobalSection
EndGlobal
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,54 @@ using Domain;
Console.WriteLine(SampleEnum.First);
```

### Generate `enum class` for enum from another assembly

If you do not have access to enum source code directly, you can generate `enum class` for enum in external assembly.
For this use `[ExternalEnumClass]` attribute.

```csharp
// External assembly
namespace Logic;

public enum Word
{
Single,
Double,
Triple
}
```

```csharp
// Our assembly
using EnumClass.Attributes;
using Logic;

[assembly: ExternalEnumClass(typeof(Word), Namespace = "Another")]
namespace Another;

public partial class Word
{
public abstract int WordsCount { get; }

public partial class SingleEnumValue
{
public override int WordsCount => 1;
}


public partial class DoubleEnumValue
{
public override int WordsCount => 2;
}


public partial class TripleEnumValue
{
public override int WordsCount => 3;
}
}
```

## Known limitations

### Same name of member and enum
Expand Down
19 changes: 19 additions & 0 deletions docs/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

## Release 1.2.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ECG001 | Usage | Warning | Diagnostics
ECG002 | Usage | Warning | Diagnostics

## Release 1.3.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ECG003 | Usage | Warning | Diagnostics
6 changes: 0 additions & 6 deletions docs/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ECG001 | Usage | Warning | Diagnostics
ECG002 | Usage | Warning | Diagnostics
9 changes: 9 additions & 0 deletions samples/EnumClass.EnumOnly/EnumClass.EnumOnly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions samples/EnumClass.EnumOnly/Toy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EnumClass.EnumOnly;

public enum Toy
{
Car,
Doll,
Ball
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EnumClass.Attributes\EnumClass.Attributes.csproj" />
<ProjectReference Include="..\..\src\EnumClass.Core\EnumClass.Core.csproj" OutputItemType="Analyzer" />
<ProjectReference Include="..\..\src\EnumClass.Generator\EnumClass.Generator.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\EnumClass.EnumOnly\EnumClass.EnumOnly.csproj" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions samples/EnumClass.FromAnotherAssembly/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Channels;
using EnumClass.FromAnotherAssembly;

foreach (var member in Toy.GetAllMembers())
{
Console.WriteLine($"Playing with {member}:");
member.Play();
Console.WriteLine();
}
33 changes: 33 additions & 0 deletions samples/EnumClass.FromAnotherAssembly/Toy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EnumClass.Attributes;
using Toy = EnumClass.EnumOnly.Toy;

[assembly: ExternalEnumClass(typeof(Toy), Namespace = "EnumClass.FromAnotherAssembly")]
namespace EnumClass.FromAnotherAssembly;

public partial class Toy
{
public abstract void Play();
public partial class BallEnumValue
{
public override void Play()
{
Console.WriteLine($"Ball jumps high");
}
}

public partial class CarEnumValue
{
public override void Play()
{
Console.WriteLine($"Car is going fast");
}
}

public partial class DollEnumValue
{
public override void Play()
{
Console.WriteLine($"Doll is dressed smartly");
}
}
}
4 changes: 4 additions & 0 deletions samples/EnumClass.RawEnumComparison/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using EnumClass.Attributes;
using EnumClass.SimpleEnum.EnumClass;

[assembly: ExternalEnumClass(typeof(PetKind))]
// ReSharper disable UnusedParameter.Local

PrintEnumClassComparison("Dog", PetKind.Dog, EnumClass.SimpleEnum.PetKind.Dog);
Expand All @@ -20,6 +23,7 @@ void PrintEnumClassComparison(string representationName, PetKind enumClassKind,
Console.WriteLine($"Equals: {enumClassKind.Equals(enumKind)}");
Console.WriteLine($"==: {enumClassKind == enumKind}");
Console.WriteLine($"!=: {enumClassKind == enumKind}");
// Console.WriteLine($"{enumClassKind == enumClassKind}");

var result = enumClassKind.Switch(2, 2,
static (dog, i, j) => i + j * 2,
Expand Down
30 changes: 30 additions & 0 deletions src/EnumClass.Attributes/ExternalEnumClassAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace EnumClass.Attributes;

/// <summary>
/// Marker attribute for EnumClassGenerator
/// used for generating EnumClass for enums in external assemblies
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class ExternalEnumClassAttribute: Attribute
{
/// <summary>
/// Primary constructor
/// </summary>
/// <param name="enum">Type of enum for generating</param>
public ExternalEnumClassAttribute(Type @enum)
{ }

/// <summary>
/// Namespace where generated class will be contained.
/// Defaults to namespace of original enum + "".EnumClass""
/// </summary>
public string Namespace { get; set; } = null!;

/// <summary>
/// Name of class that will be generated.
/// Defaults to the same name of enum
/// </summary>
public string ClassName { get; set; } = null!;
}
7 changes: 2 additions & 5 deletions src/EnumClass.Core/EnumClass.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="../../docs/AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="../../docs/AnalyzerReleases.Unshipped.md" />
<None Include="..\..\docs\AnalyzerReleases.Shipped.md" Pack="true" PackagePath="\" />
<None Include="..\..\docs\AnalyzerReleases.Unshipped.md" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
Loading