Skip to content

Commit edf7011

Browse files
committed
feat: Reeanble generators
1 parent 75796aa commit edf7011

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jobs:
7575
dotnet pack src/bunit/ -c Release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
7676
dotnet pack src/bunit.template/ -c Release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
7777
dotnet pack src/bunit.web.query/ -c release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
78+
dotnet pack src/bunit.generators/ -c release --output ${{ env.NUGET_DIRECTORY }} -p:ContinuousIntegrationBuild=true -p:publicrelease=true
7879
7980
# Publish the NuGet package as an artifact, so they can be used in the following jobs
8081
- uses: actions/upload-artifact@v4

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
dotnet pack src/bunit/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
100100
dotnet pack src/bunit.template/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
101101
dotnet pack src/bunit.web.query/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
102+
dotnet pack src/bunit.generators/ -c Release --property:PackageOutputPath=${GITHUB_WORKSPACE}/packages -p:ContinuousIntegrationBuild=true -p:publicrelease=true
102103
103104
- name: 🛠️ Upload library to NuGet.org repository
104105
run: |

Directory.Packages.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@
101101
</ItemGroup>
102102

103103
<ItemGroup Label="Source Code Generators">
104-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
105-
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.12.0"/>
106-
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.12.0"/>
107-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0"/>
104+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0"/>
105+
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.14.0"/>
106+
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.14.0"/>
107+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0"/>
108108
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.2" />
109109
</ItemGroup>
110110
</Project>

bunit.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.generators", "src\bun
6060
EndProject
6161
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bunit.tests", "tests\bunit.tests\bunit.tests.csproj", "{56889DE7-5E66-4E9C-815B-CBCFC9961612}"
6262
EndProject
63+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bunit.generators.tests", "tests\bunit.generators.tests\bunit.generators.tests.csproj", "{D08F7F1D-74B1-4A76-86A2-94918863740C}"
64+
EndProject
6365
Global
6466
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6567
Debug|Any CPU = Debug|Any CPU
@@ -96,6 +98,10 @@ Global
9698
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Debug|Any CPU.Build.0 = Debug|Any CPU
9799
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Release|Any CPU.ActiveCfg = Release|Any CPU
98100
{56889DE7-5E66-4E9C-815B-CBCFC9961612}.Release|Any CPU.Build.0 = Release|Any CPU
101+
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
102+
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Debug|Any CPU.Build.0 = Debug|Any CPU
103+
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Release|Any CPU.ActiveCfg = Release|Any CPU
104+
{D08F7F1D-74B1-4A76-86A2-94918863740C}.Release|Any CPU.Build.0 = Release|Any CPU
99105
EndGlobalSection
100106
GlobalSection(SolutionProperties) = preSolution
101107
HideSolutionNode = FALSE
@@ -109,6 +115,7 @@ Global
109115
{AE3DFB52-2BF4-4806-AD82-7FB7B38AC17F} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
110116
{A7C6A2AA-FF8F-4ED1-8590-5324FC566059} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
111117
{56889DE7-5E66-4E9C-815B-CBCFC9961612} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
118+
{D08F7F1D-74B1-4A76-86A2-94918863740C} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
112119
EndGlobalSection
113120
GlobalSection(ExtensibilityGlobals) = postSolution
114121
SolutionGuid = {24106918-1C86-4769-BDA6-9C80E64CD260}

src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text;
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Microsoft.CodeAnalysis.CSharp; // For GetInterceptableLocation API and InterceptableLocation type
56

67
namespace Bunit.Web.Stubs.AddStubMethodStubGenerator;
78

@@ -50,10 +51,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5051
return null;
5152
}
5253

53-
var path = GetInterceptorFilePath(context.Node.SyntaxTree, context.SemanticModel.Compilation);
54-
var lineSpan = context.SemanticModel.SyntaxTree.GetLineSpan(context.Node.Span);
55-
var line = lineSpan.StartLinePosition.Line + 1;
56-
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
54+
var interceptableLocation = context.SemanticModel.GetInterceptableLocation(invocation);
55+
if (interceptableLocation is null)
56+
{
57+
return null;
58+
}
5759

5860
var properties = symbol.GetAllMembersRecursively()
5961
.OfType<IPropertySymbol>()
@@ -67,9 +69,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6769
TargetTypeNamespace = symbol.ContainingNamespace.ToDisplayString(),
6870
TargetTypeName = symbol.ToDisplayString(),
6971
Properties = properties,
70-
Path = path,
71-
Line = line,
72-
Column = column,
72+
Location = interceptableLocation,
7373
};
7474

7575
static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation, SemanticModel semanticModel)
@@ -88,11 +88,6 @@ static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation,
8888
return semanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol { IsExtensionMethod: true, ReceiverType.Name: "ComponentFactoryCollection" };
8989
}
9090

91-
static string GetInterceptorFilePath(SyntaxTree tree, Compilation compilation)
92-
{
93-
return compilation.Options.SourceReferenceResolver?.NormalizePath(tree.FilePath, baseFilePath: null) ?? tree.FilePath;
94-
}
95-
9691
static bool IsParameterOrCascadingParameter(ISymbol member)
9792
{
9893
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
@@ -124,27 +119,21 @@ private static void Execute(ImmutableArray<AddStubClassInfo> classInfos, SourceP
124119

125120
private static void GenerateInterceptorCode(AddStubClassInfo stubbedComponentGroup, IEnumerable<AddStubClassInfo> stubClassGrouped, SourceProductionContext context)
126121
{
127-
// Generate the attribute
128-
const string attribute = """
129-
namespace System.Runtime.CompilerServices
130-
{
131-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
132-
sealed file class InterceptsLocationAttribute : Attribute
133-
{
134-
public InterceptsLocationAttribute(string filePath, int line, int column)
135-
{
136-
_ = filePath;
137-
_ = line;
138-
_ = column;
139-
}
140-
}
141-
}
142-
""";
143-
144122
// Generate the interceptor
145123
var interceptorSource = new StringBuilder(1000);
146124
interceptorSource.AppendLine(HeaderProvider.Header);
147-
interceptorSource.AppendLine(attribute);
125+
interceptorSource.AppendLine("namespace System.Runtime.CompilerServices");
126+
interceptorSource.AppendLine("{");
127+
interceptorSource.AppendLine("\t[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)]");
128+
interceptorSource.AppendLine("\tsealed file class InterceptsLocationAttribute : global::System.Attribute");
129+
interceptorSource.AppendLine("\t{");
130+
interceptorSource.AppendLine("\t\tpublic InterceptsLocationAttribute(int version, string data)");
131+
interceptorSource.AppendLine("\t\t{");
132+
interceptorSource.AppendLine("\t\t\t_ = version;");
133+
interceptorSource.AppendLine("\t\t\t_ = data;");
134+
interceptorSource.AppendLine("\t\t}");
135+
interceptorSource.AppendLine("\t}");
136+
interceptorSource.AppendLine("}");
148137
interceptorSource.AppendLine();
149138
interceptorSource.AppendLine("namespace Bunit");
150139
interceptorSource.AppendLine("{");
@@ -153,8 +142,7 @@ public InterceptsLocationAttribute(string filePath, int line, int column)
153142

154143
foreach (var hit in stubClassGrouped)
155144
{
156-
interceptorSource.AppendLine(
157-
$"\t\t[global::System.Runtime.CompilerServices.InterceptsLocationAttribute(@\"{hit.Path}\", {hit.Line}, {hit.Column})]");
145+
interceptorSource.AppendLine($"\t\t{hit.Location.GetInterceptsLocationAttributeSyntax()}");
158146
}
159147

160148
interceptorSource.AppendLine(
@@ -213,9 +201,7 @@ internal sealed record AddStubClassInfo
213201
public required string TargetTypeName { get; set; }
214202
public string UniqueQualifier => $"{TargetTypeNamespace}.{StubClassName}";
215203
public ImmutableArray<StubPropertyInfo> Properties { get; set; } = ImmutableArray<StubPropertyInfo>.Empty;
216-
public required string Path { get; set; }
217-
public int Line { get; set; }
218-
public int Column { get; set; }
204+
public required InterceptableLocation Location { get; set; }
219205
}
220206

221207
internal sealed record StubPropertyInfo

0 commit comments

Comments
 (0)