Skip to content

Commit 9848a8b

Browse files
committed
Moved attributese to a separate assembly and packed into the nuget
1 parent 4d23623 commit 9848a8b

File tree

8 files changed

+73
-47
lines changed

8 files changed

+73
-47
lines changed

samples/Samples/Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<ProjectReference Include="..\..\src\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" />
1011
<ProjectReference Include="..\..\src\PublicInterfaceGenerator\PublicInterfaceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
1112
</ItemGroup>
1213

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
2+
3+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)]
4+
public class ExcludeFromGeneratedInterfaceAttribute : Attribute
5+
{
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
6+
7+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
8+
public class GenerateInterfaceAttribute : Attribute
9+
{
10+
/// <summary>
11+
/// Set this to override the default interface name. Or leave it null to use the class name with an 'I' prepended to it.
12+
/// </summary>
13+
public string? InterfaceName { get; set; }
14+
15+
/// <summary>
16+
/// Set this to override the namespace to generate the interface in. By default, it will be the same as the class.
17+
/// </summary>
18+
public string? Namespace { get; set; }
19+
20+
/// <summary>
21+
/// Set this to specify the interfaces the generated interface will inherit from. For example, IDisposable.
22+
/// This should be a syntax-valid list as you would type it out normally because it will be concatenated directly into the interface definition.
23+
/// For example: ""MyNamespace.MyInterface1, MyNamespace.MyInterface2""
24+
/// </summary>
25+
public string? Interfaces { get; set; }
26+
27+
/// <summary>
28+
/// Set this to specify the generates interface inherits from System.IDisposable.
29+
/// This will be appended to the list of interfaces.
30+
/// If you are also specifying interfaces with the ""{AttributeProperty_Interfaces}"" property, either set this to false and include ""System.IDisposable"" in the ""{AttributeProperty_Interfaces}"" property string, or set this to true and don't include ""System.IDisposable"" in the ""{AttributeProperty_Interfaces}"" property string.
31+
/// </summary>
32+
public bool IsIDisposable { get; set; }
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>Latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes</RootNamespace>
9+
<AssemblyName>ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes</AssemblyName>
10+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
11+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
12+
</PropertyGroup>
13+
14+
</Project>

src/PublicInterfaceGenerator.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\Unit
1313
EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{7235A503-C7CD-4028-88F4-15DBD7371471}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PublicInterfaceGenerator.Attributes", "PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj", "{59953E27-6190-44CE-9244-DBA1566811F5}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +33,10 @@ Global
3133
{F014C6CC-0A1E-4E2D-A9DC-8F3CFFF7D87A}.Debug|Any CPU.Build.0 = Debug|Any CPU
3234
{F014C6CC-0A1E-4E2D-A9DC-8F3CFFF7D87A}.Release|Any CPU.ActiveCfg = Release|Any CPU
3335
{F014C6CC-0A1E-4E2D-A9DC-8F3CFFF7D87A}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{59953E27-6190-44CE-9244-DBA1566811F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{59953E27-6190-44CE-9244-DBA1566811F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{59953E27-6190-44CE-9244-DBA1566811F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{59953E27-6190-44CE-9244-DBA1566811F5}.Release|Any CPU.Build.0 = Release|Any CPU
3440
EndGlobalSection
3541
GlobalSection(SolutionProperties) = preSolution
3642
HideSolutionNode = FALSE

src/PublicInterfaceGenerator/InterfaceSourceGenerator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ public class InterfaceSourceGenerator : IIncrementalGenerator
1414
{
1515
public void Initialize(IncrementalGeneratorInitializationContext context)
1616
{
17-
// Add the marker attribute
18-
context.RegisterPostInitializationOutput(static ctx => ctx.AddSource(
19-
$"{SourceGenerationHelper.GenerateInterfaceAttributeName}.g.cs", SourceText.From(SourceGenerationHelper.AttributeClassCode, Encoding.UTF8)));
20-
2117
IncrementalValuesProvider<InterfaceToGenerateInfo?> interfacesToGenerate =
2218
context.SyntaxProvider
2319
.ForAttributeWithMetadataName(

src/PublicInterfaceGenerator/PublicInterfaceGenerator.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@
1818

1919
<PropertyGroup>
2020
<Version>0.1.0</Version>
21+
<!--Mark the nuget as a dev dependency, so any projects using this nuget don't auto pull in this project-->
22+
<DevelopmentDependency>true</DevelopmentDependency>
2123
</PropertyGroup>
2224

2325
<ItemGroup>
2426
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
2527
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
2628
</ItemGroup>
2729

28-
<!-- Package the generator in the analyzer directory of the nuget package -->
2930
<ItemGroup>
31+
<!-- Package the generator in the analyzer directory of the nuget package -->
3032
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
33+
34+
<!-- Pack the attributes dll in the analyzers/dotnet/cs path -->
35+
<None Include="$(OutputPath)\PublicInterfaceGenerator.Attributes.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
36+
37+
<!-- Pack the attributes dll in the lib\netstandard2.0 path -->
38+
<None Include="$(OutputPath)\PublicInterfaceGenerator.Attributes.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
39+
3140
<None Include="../../README.md" Pack="true" PackagePath="\" />
3241
</ItemGroup>
42+
<ItemGroup>
43+
<ProjectReference Include="..\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" />
44+
</ItemGroup>
3345
</Project>

src/PublicInterfaceGenerator/SourceGenerationHelper.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,11 @@ public static class SourceGenerationHelper
1414

1515
public const string ExcludeFromGeneratedInterfaceAttributeName = "ExcludeFromGeneratedInterfaceAttribute";
1616

17-
1817
public const string AttributeProperty_InterfaceName = "InterfaceName";
1918
public const string AttributeProperty_NamespaceName = "Namespace";
2019
public const string AttributeProperty_Interfaces = "Interfaces";
2120
public const string AttributeProperty_IsIDisposable = "IsIDisposable";
2221

23-
public const string AttributeClassCode =
24-
@$"
25-
#nullable enable
26-
using System;
27-
namespace {GenerateInterfaceAttributeNameSpace}
28-
{{
29-
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
30-
public class {GenerateInterfaceAttributeName} : Attribute
31-
{{
32-
/// <summary>
33-
/// Set this to override the default interface name. Or leave it null to use the class name with an 'I' prepended to it.
34-
/// </summary>
35-
public string? {AttributeProperty_InterfaceName} {{ get; set; }}
36-
37-
/// <summary>
38-
/// Set this to override the namespace to generate the interface in. By default, it will be the same as the class.
39-
/// </summary>
40-
public string? {AttributeProperty_NamespaceName} {{ get; set; }}
41-
42-
/// <summary>
43-
/// Set this to specify the interfaces the generated interface will inherit from. For example, IDisposable.
44-
/// This should be a syntax-valid list as you would type it out normally because it will be concatenated directly into the interface definition.
45-
/// For example: ""MyNamespace.MyInterface1, MyNamespace.MyInterface2""
46-
/// </summary>
47-
public string? {AttributeProperty_Interfaces} {{ get; set; }}
48-
49-
/// <summary>
50-
/// Set this to specify the generates interface inherits from System.IDisposable.
51-
/// This will be appended to the list of interfaces.
52-
/// If you are also specifying interfaces with the ""{AttributeProperty_Interfaces}"" property, either set this to false and include ""System.IDisposable"" in the ""{AttributeProperty_Interfaces}"" property string, or set this to true and don't include ""System.IDisposable"" in the ""{AttributeProperty_Interfaces}"" property string.
53-
/// </summary>
54-
public bool {AttributeProperty_IsIDisposable} {{ get; set; }} = false;
55-
}}
56-
57-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)]
58-
public class {ExcludeFromGeneratedInterfaceAttributeName} : Attribute
59-
{{
60-
}}
61-
}}
62-
";
63-
6422
public static string GenerateInterface(in InterfaceToGenerateInfo interfaceInfo)
6523
{
6624
var builder = new StringBuilder();

0 commit comments

Comments
 (0)