Skip to content

Commit 85c4ebf

Browse files
committed
Made a changes to make the source generater reference a separate project for the attributes. But now all unit tests fail because the unit tests aren't generating any code. In the middle of investigating
1 parent 85ff1ac commit 85c4ebf

15 files changed

+123
-56
lines changed

samples/Samples/Samples.csproj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9-
<ItemGroup>
10-
<ProjectReference Include="..\..\src\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" />
9+
<!-- <ItemGroup>
1110
<ProjectReference Include="..\..\src\PublicInterfaceGenerator\PublicInterfaceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
12-
</ItemGroup>
11+
<ProjectReference Include="..\..\src\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
12+
</ItemGroup>-->
13+
14+
<!--When compiling, generate the nuget so this unit tests project references that instead of the project directly-->
15+
<Target Name="CreateNuget" BeforeTargets="CoreCompile">
16+
<Exec Command="dotnet pack ../../src/PublicInterfaceGenerator/PublicInterfaceGenerator.csproj --configuration Debug" />
17+
</Target>
18+
19+
<ItemGroup>
20+
<!--Reference the nuget we generate so we test that like a normal user-->
21+
<PackageReference Include="ProgrammerAl.SourceGenerators.PublicInterfaceGenerator" Version="*" PrivateAssets="All" />
22+
</ItemGroup>
1323

1424
</Project>

src/PublicInterfaceGenerator.Attributes/GenerateInterfaceAttribute.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public class GenerateInterfaceAttribute : Attribute
3030
/// 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.
3131
/// </summary>
3232
public bool IsIDisposable { get; set; }
33+
34+
public static class Constants
35+
{
36+
public const string GenerateInterfaceAttributeName = nameof(GenerateInterfaceAttribute);
37+
public static string GenerateInterfaceAttributeNameSpace = typeof(GenerateInterfaceAttribute).Namespace;
38+
public static string GenerateInterfaceAttributeFullName = typeof(GenerateInterfaceAttribute).FullName;
39+
40+
public const string ExcludeFromGeneratedInterfaceAttributeName = "ExcludeFromGeneratedInterfaceAttribute";
41+
42+
public const string AttributeProperty_InterfaceName = nameof(InterfaceName);
43+
public const string AttributeProperty_NamespaceName = nameof(Namespace);
44+
public const string AttributeProperty_Interfaces = nameof(Interfaces);
45+
public const string AttributeProperty_IsIDisposable = nameof(IsIDisposable);
46+
}
3347
}

src/PublicInterfaceGenerator.Attributes/PublicInterfaceGenerator.Attributes.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<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>
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>
1212
</PropertyGroup>
1313

1414
</Project>

src/PublicInterfaceGenerator/GeneratorParsers/ClassParser.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Microsoft.CodeAnalysis;
77

8+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
9+
810
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.GeneratorParsers;
911

1012
public static class ClassParser
@@ -29,30 +31,30 @@ public static class ClassParser
2931
{
3032
var attributeClassName = attributeData.AttributeClass?.Name;
3133
if (string.IsNullOrWhiteSpace(attributeClassName)
32-
|| attributeClassName != SourceGenerationHelper.GenerateInterfaceAttributeName
33-
|| attributeData.AttributeClass!.ToDisplayString() != SourceGenerationHelper.GenerateInterfaceAttributeFullName)
34+
|| attributeClassName != GenerateInterfaceAttribute.Constants.GenerateInterfaceAttributeName
35+
|| attributeData.AttributeClass!.ToDisplayString() != GenerateInterfaceAttribute.Constants.GenerateInterfaceAttributeFullName)
3436
{
3537
continue;
3638
}
3739

3840
foreach (KeyValuePair<string, TypedConstant> namedArgument in attributeData.NamedArguments)
3941
{
40-
if (namedArgument.Key == SourceGenerationHelper.AttributeProperty_InterfaceName
42+
if (namedArgument.Key == GenerateInterfaceAttribute.Constants.AttributeProperty_InterfaceName
4143
&& namedArgument.Value.Value?.ToString() is { } infName)
4244
{
4345
interfaceName = infName;
4446
}
45-
else if (namedArgument.Key == SourceGenerationHelper.AttributeProperty_NamespaceName
47+
else if (namedArgument.Key == GenerateInterfaceAttribute.Constants.AttributeProperty_NamespaceName
4648
&& namedArgument.Value.Value?.ToString() is { } nsName)
4749
{
4850
namespaceName = nsName;
4951
}
50-
else if (namedArgument.Key == SourceGenerationHelper.AttributeProperty_Interfaces
52+
else if (namedArgument.Key == GenerateInterfaceAttribute.Constants.AttributeProperty_Interfaces
5153
&& namedArgument.Value.Value?.ToString() is { } interfaces)
5254
{
5355
interfacesNames = interfaces;
5456
}
55-
else if (namedArgument.Key == SourceGenerationHelper.AttributeProperty_IsIDisposable
57+
else if (namedArgument.Key == GenerateInterfaceAttribute.Constants.AttributeProperty_IsIDisposable
5658
&& namedArgument.Value.Value?.ToString() is { } isIDisposable)
5759
{
5860
if (bool.TryParse(isIDisposable, out bool parsedIsIDisposable))

src/PublicInterfaceGenerator/GeneratorParsers/EventParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Microsoft.CodeAnalysis;
77

8+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
9+
810
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.GeneratorParsers;
911

1012
public static class EventParser
@@ -35,7 +37,7 @@ private static bool IsSymbolValid(IEventSymbol symbol)
3537
//Only public
3638
return false;
3739
}
38-
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is SourceGenerationHelper.ExcludeFromGeneratedInterfaceAttributeName))
40+
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is GenerateInterfaceAttribute.Constants.ExcludeFromGeneratedInterfaceAttributeName))
3941
{
4042
//Don't include methods that have the [IgnoreInGeneratedInterface] attribute
4143
return false;

src/PublicInterfaceGenerator/GeneratorParsers/MethodParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.Text;
99

10+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
11+
1012
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.GeneratorParsers;
1113

1214
public static class MethodParser
@@ -82,7 +84,7 @@ private static bool IsSymbolValid(IMethodSymbol symbol, string extraClassInterfa
8284
// Don't include those here because events are handled separately
8385
return false;
8486
}
85-
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is SourceGenerationHelper.ExcludeFromGeneratedInterfaceAttributeName))
87+
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is GenerateInterfaceAttribute.Constants.ExcludeFromGeneratedInterfaceAttributeName))
8688
{
8789
//Don't include methods that have the [IgnoreInGeneratedInterface] attribute
8890
return false;

src/PublicInterfaceGenerator/GeneratorParsers/PropertyParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Microsoft.CodeAnalysis;
77

8+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
9+
810
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.GeneratorParsers;
911

1012
public static class PropertyParser
@@ -45,7 +47,7 @@ private static bool IsSymbolValid(IPropertySymbol symbol)
4547
//Only public properties
4648
return false;
4749
}
48-
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is SourceGenerationHelper.ExcludeFromGeneratedInterfaceAttributeName))
50+
else if (symbol.GetAttributes().Any(x => x.AttributeClass?.Name is GenerateInterfaceAttribute.Constants.ExcludeFromGeneratedInterfaceAttributeName))
4951
{
5052
//Don't include methods that have the [IgnoreInGeneratedInterface] attribute
5153
return false;

src/PublicInterfaceGenerator/PublicInterfaceGenerator.csproj

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@
2727
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
2828
</ItemGroup>
2929

30+
<!-- Reference the attributes from the generator to compile against them -->
31+
<!-- Ensure we specify PrivateAssets so the NuGet doesn't have any dependencies -->
32+
<ItemGroup>
33+
<ProjectReference Include="..\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" PrivateAssets="All" />
34+
</ItemGroup>
35+
3036
<ItemGroup>
3137
<!-- Package the generator in the analyzer directory of the nuget package -->
32-
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
38+
<None Include="$(OutputPath)/$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
3339

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" />
40+
<!-- Pack the attributes dll in the analyzers/dotnet/cs path so the source generator can use it -->
41+
<None Include="$(OutputPath)/ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
3642

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" />
43+
<!-- Pack the attributes dll in the lib\netstandard2.0 path so the library consuming this nuget can use it -->
44+
<None Include="$(OutputPath)/ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes.dll" Pack="true" PackagePath="lib/netstandard2.0" Visible="true" />
3945

40-
<None Include="../../README.md" Pack="true" PackagePath="\" />
41-
</ItemGroup>
42-
<ItemGroup>
43-
<ProjectReference Include="..\PublicInterfaceGenerator.Attributes\PublicInterfaceGenerator.Attributes.csproj" />
46+
<None Include="../../README.md" Pack="true" PackagePath="/" />
4447
</ItemGroup>
4548
</Project>

src/PublicInterfaceGenerator/InterfaceSourceGenerator.cs renamed to src/PublicInterfaceGenerator/PublicInterfaceSourceGenerator.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
using System.Collections.Immutable;
2-
using System.Text;
1+
using System.Text;
32

3+
using System.Linq;
44
using Microsoft.CodeAnalysis;
55
using Microsoft.CodeAnalysis.CSharp.Syntax;
66
using Microsoft.CodeAnalysis.Text;
77

8+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
89
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.GeneratorParsers;
910

1011
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator;
1112

1213
[Generator]
13-
public class InterfaceSourceGenerator : IIncrementalGenerator
14+
public class PublicInterfaceSourceGenerator : IIncrementalGenerator
1415
{
1516
public void Initialize(IncrementalGeneratorInitializationContext context)
1617
{
17-
IncrementalValuesProvider<InterfaceToGenerateInfo?> interfacesToGenerate =
18+
var interfacesToGenerate =
1819
context.SyntaxProvider
1920
.ForAttributeWithMetadataName(
20-
SourceGenerationHelper.GenerateInterfaceAttributeFullName,
21+
GenerateInterfaceAttribute.Constants.GenerateInterfaceAttributeFullName,
2122
predicate: static (node, _) => node is ClassDeclarationSyntax or RecordDeclarationSyntax,
22-
transform: ClassParser.GetTypeToGenerate)
23-
.Where(static m => m is not null);
23+
//predicate: static (node, _) => true,
24+
transform: ClassParser.GetTypeToGenerate);
2425

2526
// Generate source code for each interface
2627
context.RegisterSourceOutput(interfacesToGenerate,

src/PublicInterfaceGenerator/SourceGenerationHelper.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator;
88

99
public static class SourceGenerationHelper
1010
{
11-
public const string GenerateInterfaceAttributeName = "GenerateInterfaceAttribute";
12-
public const string GenerateInterfaceAttributeNameSpace = "ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes";
13-
public const string GenerateInterfaceAttributeFullName = $"{GenerateInterfaceAttributeNameSpace}.{GenerateInterfaceAttributeName}";
14-
15-
public const string ExcludeFromGeneratedInterfaceAttributeName = "ExcludeFromGeneratedInterfaceAttribute";
16-
17-
public const string AttributeProperty_InterfaceName = "InterfaceName";
18-
public const string AttributeProperty_NamespaceName = "Namespace";
19-
public const string AttributeProperty_Interfaces = "Interfaces";
20-
public const string AttributeProperty_IsIDisposable = "IsIDisposable";
21-
2211
public static string GenerateInterface(in InterfaceToGenerateInfo interfaceInfo)
2312
{
2413
var builder = new StringBuilder();
@@ -71,7 +60,5 @@ private static bool ShouldEnableNullableReferences(in InterfaceToGenerateInfo in
7160
|| m.ReturnType.EndsWith("?"))
7261
|| interfaceInfo.Properties.Any(x => x.ReturnType.EndsWith("?"))
7362
|| interfaceInfo.Events.Any(x => x.EventDataType.EndsWith("?"));
74-
75-
7663
}
7764
}

0 commit comments

Comments
 (0)