Skip to content

Commit 360a0d0

Browse files
authored
Don't embed the attributes in the target dll (#160)
1 parent 6ff64d1 commit 360a0d0

File tree

40 files changed

+31
-2394
lines changed

40 files changed

+31
-2394
lines changed

NetEscapades.EnumGenerators.sln

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_build", "build\_build.cspr
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Benchmarks", "tests\NetEscapades.EnumGenerators.Benchmarks\NetEscapades.EnumGenerators.Benchmarks.csproj", "{C7CFC4AC-BC5B-4F07-BE93-4F245D5547D9}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests", "tests\NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests\NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests.csproj", "{E7D66FE9-65BC-4DC8-AC86-5C5BE10D8E61}"
21-
EndProject
2220
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{AC0DDDB0-385D-43B8-A770-C79EE2077D05}"
2321
EndProject
2422
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToStringFastExample", "samples\ToStringFastExample\ToStringFastExample.csproj", "{BDF8C04B-D0E3-4FF5-82C3-E8FDF3916C16}"
@@ -70,8 +68,6 @@ Global
7068
{C7CFC4AC-BC5B-4F07-BE93-4F245D5547D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
7169
{C7CFC4AC-BC5B-4F07-BE93-4F245D5547D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
7270
{C7CFC4AC-BC5B-4F07-BE93-4F245D5547D9}.Release|Any CPU.Build.0 = Release|Any CPU
73-
{E7D66FE9-65BC-4DC8-AC86-5C5BE10D8E61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
74-
{E7D66FE9-65BC-4DC8-AC86-5C5BE10D8E61}.Release|Any CPU.ActiveCfg = Release|Any CPU
7571
{BDF8C04B-D0E3-4FF5-82C3-E8FDF3916C16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7672
{BDF8C04B-D0E3-4FF5-82C3-E8FDF3916C16}.Debug|Any CPU.Build.0 = Debug|Any CPU
7773
{BDF8C04B-D0E3-4FF5-82C3-E8FDF3916C16}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -120,7 +116,6 @@ Global
120116
{F03831B0-630E-4B0E-BD25-AFE975B35E69} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
121117
{328EFEB4-1D15-453E-9929-6FD0FACF3512} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
122118
{C7CFC4AC-BC5B-4F07-BE93-4F245D5547D9} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
123-
{E7D66FE9-65BC-4DC8-AC86-5C5BE10D8E61} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
124119
{BDF8C04B-D0E3-4FF5-82C3-E8FDF3916C16} = {AC0DDDB0-385D-43B8-A770-C79EE2077D05}
125120
{40B2D8D1-7523-498D-9753-CECC3A44071A} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
126121
{7A0F5EA5-DB7B-49E0-827D-946C80175322} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}

build/Build.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ class Build : NukeBuild
107107
var projectFiles = new[]
108108
{
109109
Solution.tests.NetEscapades_EnumGenerators_Nuget_IntegrationTests.Path,
110-
Solution.tests.NetEscapades_EnumGenerators_Nuget_Attributes_IntegrationTests.Path,
111110
Solution.tests.NetEscapades_EnumGenerators_Nuget_Interceptors_IntegrationTests.Path,
112111
Solution.tests.NetEscapades_EnumGenerators_Nuget_NetStandard_Interceptors_IntegrationTests.Path,
113112
};

src/NetEscapades.EnumGenerators.Interceptors/InterceptorGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public class InterceptorGenerator : IIncrementalGenerator
1313
{
1414
public void Initialize(IncrementalGeneratorInitializationContext context)
1515
{
16-
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
17-
"InterceptableAttribute.g.cs", SourceText.From(SourceGenerationHelper.Attribute, Encoding.UTF8)));
18-
1916
IncrementalValuesProvider<EnumToIntercept> enumsToIntercept = context.SyntaxProvider
2017
.ForAttributeWithMetadataName(
2118
Attributes.EnumExtensionsAttribute,

src/NetEscapades.EnumGenerators.Interceptors/SourceGenerationHelper.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,6 @@ public static class SourceGenerationHelper
1818
#nullable enable
1919
""";
2020

21-
public const string Attribute =
22-
$$"""
23-
{{Header}}
24-
25-
#if NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES
26-
namespace NetEscapades.EnumGenerators
27-
{
28-
/// <summary>
29-
/// Add to an assembly to indicate that usages of the enum should
30-
/// be automatically intercepted to use the extension methods
31-
/// generated by EnumExtensionsAttribute in this project.
32-
/// Note that the extension methods must be accessible from this project,
33-
/// otherwise you will receive compilation errors
34-
/// </summary>
35-
[global::System.AttributeUsage(global::System.AttributeTargets.Assembly, AllowMultiple = true)]
36-
[global::System.Diagnostics.Conditional("NETESCAPADES_ENUMGENERATORS_USAGES")]
37-
#if NET5_0_OR_GREATER
38-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Generated by the NetEscapades.EnumGenerators.Interceptors source generator.")]
39-
#else
40-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
41-
#endif
42-
public class InterceptableAttribute<T> : global::System.Attribute
43-
where T: global::System.Enum
44-
{
45-
/// <summary>
46-
/// The namespace generated for the extension class. If not provided,
47-
/// and the referenced enum is in a different project, the namespace
48-
/// of the extension methods are assumed to be the same as the enum.
49-
/// </summary>
50-
public string? ExtensionClassNamespace { get; set; }
51-
52-
/// <summary>
53-
/// The name used for the extension class. If not provided,
54-
/// and the referenced enum is in a different project, the enum name
55-
/// with an <c>Extensions</c> suffix will be assumed. For example for
56-
/// an Enum called StatusCodes, the assumed name will be StatusCodesExtensions.
57-
/// </summary>
58-
public string? ExtensionClassName { get; set; }
59-
}
60-
}
61-
#endif
62-
""";
63-
6421
public static (string Content, string Filename) GenerateInterceptorsClass(MethodToIntercept toIntercept)
6522
{
6623
var sb = new StringBuilder(

src/NetEscapades.EnumGenerators/EnumGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public class EnumGenerator : IIncrementalGenerator
1313
{
1414
public void Initialize(IncrementalGeneratorInitializationContext context)
1515
{
16-
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
17-
"EnumExtensionsAttribute.g.cs", SourceText.From(SourceGenerationHelper.Attribute, Encoding.UTF8)));
18-
1916
var csharp14IsSupported = context.CompilationProvider
2017
.Select((x,_) => x is CSharpCompilation
2118
{

src/NetEscapades.EnumGenerators/README.md

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -349,39 +349,9 @@ public void CantIntercept()
349349
}
350350
```
351351

352-
## Embedding the attributes in your project
353-
354-
By default, the `[EnumExtensions]` attributes referenced in your application are contained in an external dll. It is also possible to embed the attributes directly in your project, so they appear in the dll when your project is built. If you wish to do this, you must do two things:
355-
356-
1. Define the MSBuild constant `NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project
357-
2. Add `compile` to the list of excluded assets in your `<PackageReference>` element. This ensures the attributes in your project are referenced, instead of the _NetEscapades.EnumGenerators.Attributes.dll_ library.
358-
359-
Your project file should look something like this:
360-
361-
```xml
362-
<Project Sdk="Microsoft.NET.Sdk">
363-
364-
<PropertyGroup>
365-
<OutputType>Exe</OutputType>
366-
<TargetFramework>net7.0</TargetFramework>
367-
<!-- Define the MSBuild constant -->
368-
<DefineConstants>$(DefineConstants);NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES</DefineConstants>
369-
</PropertyGroup>
370-
371-
<!-- Add the package -->
372-
<PackageReference Include="NetEscapades.EnumGenerators" Version="1.0.0-beta14"
373-
PrivateAssets="all"
374-
ExcludeAssets="compile;runtime" />
375-
<!-- ☝ Add compile to the list of excluded assets. -->
376-
377-
</Project>
378-
```
379-
380352
## Preserving usages of the `[EnumExtensions]` attribute
381353

382-
The `[EnumExtensions]` attribute is decorated with the `[Conditional]` attribute, [so their usage will not appear in the build output of your project](https://andrewlock.net/conditional-compilation-for-ignoring-method-calls-with-the-conditionalattribute/#applying-the-conditional-attribute-to-classes). If you use reflection at runtime on one of your `enum`s, you will not find `[EnumExtensions]` in the list of custom attributes.
383-
384-
If you wish to preserve these attributes in the build output, you can define the `NETESCAPADES_ENUMGENERATORS_USAGES` MSBuild variable. Note that this means your project will have a runtime-dependency on _NetEscapades.EnumGenerators.Attributes.dll_ so you need to ensure this is included in your build output.
354+
The `[EnumExtensions]` attribute is decorated with the `[Conditional]` attribute, [so their usage will not appear in the build output of your project](https://andrewlock.net/conditional-compilation-for-ignoring-method-calls-with-the-conditionalattribute/#applying-the-conditional-attribute-to-classes). If you use reflection at runtime on one of your `enum`s, you will not find `[EnumExtensions]` in the list of custom attributes. If you wish to preserve these attributes in the build output, you can define the `NETESCAPADES_ENUMGENERATORS_USAGES` MSBuild variable.
385355

386356
```xml
387357
<Project Sdk="Microsoft.NET.Sdk">
@@ -399,15 +369,3 @@ If you wish to preserve these attributes in the build output, you can define the
399369

400370
</Project>
401371
```
402-
403-
## Error CS0436 and [InternalsVisibleTo]
404-
405-
> In the latest version of _NetEscapades.EnumGenerators_, you should not experience error CS0436 by default.
406-
407-
In previous versions of the _NetEscapades.EnumGenerators_ generator, the `[EnumExtensions]` attributes were added to your compilation as `internal` attributes by default. If you added the source generator package to multiple projects, and used the `[InternalsVisibleTo]` attribute, you could experience errors when you build:
408-
409-
```bash
410-
warning CS0436: The type 'EnumExtensionsAttribute' in 'NetEscapades.EnumGenerators\NetEscapades.EnumGenerators\EnumExtensionsAttribute.cs' conflicts with the imported type 'EnumExtensionsAttribute' in 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
411-
```
412-
413-
In the latest version of _NetEscapades.EnumGenerators_, the attributes are not embedded by default, so you should not experience this problem. If you see this error, compare your installation to the examples in the installation guide.

src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -20,92 +20,6 @@ public static class SourceGenerationHelper
2020
2121
""";
2222

23-
public const string AttributeDefinitions =
24-
"""
25-
namespace NetEscapades.EnumGenerators
26-
{
27-
/// <summary>
28-
/// Add to enums to indicate that extension methods should be generated for the type
29-
/// </summary>
30-
[global::System.AttributeUsage(global::System.AttributeTargets.Enum)]
31-
[global::System.Diagnostics.Conditional("NETESCAPADES_ENUMGENERATORS_USAGES")]
32-
#if NET5_0_OR_GREATER
33-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Generated by the NetEscapades.EnumGenerators source generator.")]
34-
#else
35-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
36-
#endif
37-
public class EnumExtensionsAttribute : global::System.Attribute
38-
{
39-
/// <summary>
40-
/// The namespace to generate the extension class.
41-
/// If not provided, the namespace of the enum will be used
42-
/// </summary>
43-
public string? ExtensionClassNamespace { get; set; }
44-
45-
/// <summary>
46-
/// The name to use for the extension class.
47-
/// If not provided, the enum name with "Extensions" will be used.
48-
/// For example for an Enum called StatusCodes, the default name
49-
/// will be StatusCodesExtensions
50-
/// </summary>
51-
public string? ExtensionClassName { get; set; }
52-
53-
/// <summary>
54-
/// By default, when interceptors are enabled for a project, any
55-
/// interceptable usages of the enum will be replaced by usages of
56-
/// the extension method in this project. To disable interception of
57-
/// the enum in this project, set <see cref="IsInterceptable"/> to <see langword="false"/>.
58-
/// </summary>
59-
public bool IsInterceptable { get; set; } = true;
60-
}
61-
62-
/// <summary>
63-
/// Add to enums to indicate that extension methods should be generated for the type
64-
/// </summary>
65-
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple = true)]
66-
[System.Diagnostics.Conditional("NETESCAPADES_ENUMGENERATORS_USAGES")]
67-
#if NET5_0_OR_GREATER
68-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Generated by the NetEscapades.EnumGenerators source generator.")]
69-
#else
70-
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
71-
#endif
72-
public class EnumExtensionsAttribute<T> : System.Attribute
73-
where T: System.Enum
74-
{
75-
/// <summary>
76-
/// The namespace to generate the extension class.
77-
/// If not provided, the namespace of the enum will be used
78-
/// </summary>
79-
public string? ExtensionClassNamespace { get; set; }
80-
81-
/// <summary>
82-
/// The name to use for the extension class.
83-
/// If not provided, the enum name with ""Extensions"" will be used.
84-
/// For example for an Enum called StatusCodes, the default name
85-
/// will be StatusCodesExtensions
86-
/// </summary>
87-
public string? ExtensionClassName { get; set; }
88-
89-
/// <summary>
90-
/// By default, when interceptors are enabled for a project, any
91-
/// interceptable usages of the enum will be replaced by usages of
92-
/// the extension method in this project. To disable interception of
93-
/// the enum in this project, set <see cref="IsInterceptable"/> to <see langword="false"/>.
94-
/// </summary>
95-
public bool IsInterceptable { get; set; } = true;
96-
}
97-
}
98-
""";
99-
100-
public const string Attribute =
101-
$$"""
102-
{{Header}}
103-
#if NETESCAPADES_ENUMGENERATORS_EMBED_ATTRIBUTES
104-
{{AttributeDefinitions}}
105-
#endif
106-
107-
""";
108-
10923
public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate, bool csharp14IsSupported)
11024
{
11125
var constantValues = new HashSet<object>();

tests/NetEscapades.EnumGenerators.IntegrationTests/EnumInFooExtensionEverythingTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace NetEscapades.EnumGenerators.IntegrationTests;
88
namespace NetEscapades.EnumGenerators.NetStandard.IntegrationTests;
99
#elif INTERCEPTOR_TESTS
1010
namespace NetEscapades.EnumGenerators.Interceptors.IntegrationTests;
11-
#elif NUGET_ATTRS_INTEGRATION_TESTS
12-
namespace NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests;
1311
#elif NUGET_INTEGRATION_TESTS
1412
namespace NetEscapades.EnumGenerators.Nuget.IntegrationTests;
1513
#elif NUGET_INTERCEPTOR_TESTS

tests/NetEscapades.EnumGenerators.IntegrationTests/EnumInFooExtensionsTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace NetEscapades.EnumGenerators.IntegrationTests;
88
namespace NetEscapades.EnumGenerators.NetStandard.IntegrationTests;
99
#elif INTERCEPTOR_TESTS
1010
namespace NetEscapades.EnumGenerators.Interceptors.IntegrationTests;
11-
#elif NUGET_ATTRS_INTEGRATION_TESTS
12-
namespace NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests;
1311
#elif NUGET_INTEGRATION_TESTS
1412
namespace NetEscapades.EnumGenerators.Nuget.IntegrationTests;
1513
#elif NUGET_INTERCEPTOR_TESTS

tests/NetEscapades.EnumGenerators.IntegrationTests/EnumInNamespaceExtensionsTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace NetEscapades.EnumGenerators.IntegrationTests;
77
namespace NetEscapades.EnumGenerators.NetStandard.IntegrationTests;
88
#elif INTERCEPTOR_TESTS
99
namespace NetEscapades.EnumGenerators.Interceptors.IntegrationTests;
10-
#elif NUGET_ATTRS_INTEGRATION_TESTS
11-
namespace NetEscapades.EnumGenerators.Nuget.Attributes.IntegrationTests;
1210
#elif NUGET_INTEGRATION_TESTS
1311
namespace NetEscapades.EnumGenerators.Nuget.IntegrationTests;
1412
#elif NUGET_INTERCEPTOR_TESTS

0 commit comments

Comments
 (0)