Skip to content
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Consumers on net48 or net8+ built with VS 2022 17.14+ or .NET 10+ SDK are fully supported. -->
<RoslynVersion>4.14.0</RoslynVersion>
<TUnitVersion>1.65.51</TUnitVersion>
<PurviewSGFVersion>1.0.0-prerelease.33</PurviewSGFVersion>
<PurviewSGFVersion>1.0.0-prerelease.36</PurviewSGFVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Purview.SourceGeneratorFramework" Version="$(PurviewSGFVersion)" />
Expand Down
5 changes: 2 additions & 3 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ set quiet
root_folder := "./src/"
solution_file := root_folder + "Telemetry.SourceGenerator.slnx"
test_solution := solution_file
build_configuration := "Release"
build_configuration := "Debug"

pipeline_version := "0.2.1"
pipeline_feed := "https://api.nuget.org/v3/index.json"
pipeline_tool := ".tools/purview-build/purview-build"

Expand All @@ -23,7 +22,7 @@ default:
[private]
ensure-pipeline-tool:
if [ ! -x "{{ pipeline_tool }}" ]; then \
dotnet tool install Purview.Build --tool-path .tools/purview-build --add-source "{{ pipeline_feed }}" --version "{{ pipeline_version }}"; \
dotnet tool install Purview.Build --tool-path .tools/purview-build --add-source "{{ pipeline_feed }}"; \
fi

# Run the PR pipeline (restore, build, lint, tests)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "purview-telemetry-sourcegenerator",
"version": "5.0.0-prerelease.4",
"version": "5.0.0-prerelease.5",
"description": "Generates [`ActivitySource`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.activitysource), [`ILogger`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger), and [`Metrics`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics) based on interface methods.",
"readme": "README.md",
"repository": {
Expand Down
28 changes: 17 additions & 11 deletions src/src/SourceGenerator/Emitters/DependencyInjectionClassEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ GenerationContext<TelemetryCapabilities> generationContext
}

var classNameToGenerate = attribute.DependencyInjectionClassName;
var includeGeneratedAttributes = false;
if (string.IsNullOrWhiteSpace(classNameToGenerate))
{
classNameToGenerate = implementationClassName + "DIExtension";
includeGeneratedAttributes = true;
}

var classAccessibility = attribute.DependencyInjectionClassIsPublic
? TypeDeclarationAccessibility.Public
Expand Down Expand Up @@ -62,7 +66,11 @@ GenerationContext<TelemetryCapabilities> generationContext
new(classNameToGenerate!, classAccessibility)
{
IsStatic = true,
IncludeGeneratedAttributes = false,
// When the class name is auto-generated, include the <c>GeneratedCodeAttribute</c> to indicate that it is generated code.
// When the class name is explicitly specified, we don't know if it's pre-existing, and generated by someone else
// so if we apply the generated code attributes it can cause duplicate attribute errors if the user has already applied them to the class
// as AllowMultiple = false on the GeneratedCodeAttribute and others...!
IncludeGeneratedAttributes = includeGeneratedAttributes,
Attributes = [EmitterHelpers.EditorBrowsableAttribute()],
}
)
Expand Down Expand Up @@ -102,15 +110,16 @@ CancellationToken token

generationContext.Debug($"Emitting DI method for {interfaceName}.");

writer.XmlSummary(
$"Registers the generated {XmlSee("global::" + interfaceType.RenderFullName)} implementation with the service collection."
);
writer.XmlParam("services", "The service collection to register the telemetry implementation with.");
writer.XmlReturn("The service collection, for chaining.");
writer
.XmlSummary(
$"Registers the generated {XmlSee(interfaceType.RenderFullName)} implementation with the service collection."
)
.XmlParam("services", "The service collection to register the telemetry implementation with.")
.XmlReturn("The service collection, for chaining.");

using (
writer.MethodScope(
new MethodDeclarationOptions(
new(
"Add" + methodName,
TypeLibrary.DependencyInjection.IServiceCollection,
TypeDeclarationAccessibility.Public
Expand All @@ -119,10 +128,7 @@ CancellationToken token
IsStatic = true,
Parameters =
[
new ParameterDeclarationOptions("services", TypeLibrary.DependencyInjection.IServiceCollection)
{
IsThis = true,
},
new("services", TypeLibrary.DependencyInjection.IServiceCollection) { IsThis = true },
],
IncludeGeneratedAttributes = false,
}
Expand Down
2 changes: 1 addition & 1 deletion src/src/SourceGenerator/Emitters/GeneratedTypesEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static void EmitAll(IncrementalGeneratorPostInitializationContext context

emitter.Emitter(writer, emitter.Type);

context.AddSource($"{emitter.Type.MetadataFullName}.g.cs", writer);
context.AddSource($"{emitter.Type.Name}.g.cs", writer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Testing;
var generationResult = await GenerateAsync(empty, cancellationToken: cancellationToken);

// Assert
var autoCounterAttribute = generationResult.GetSource("Purview.Telemetry.AutoCounterAttribute.g.cs");
var autoCounterAttribute = generationResult.GetSource("AutoCounterAttribute.g.cs");
await Assert
.That(autoCounterAttribute)
.ContainsGeneratedCode("#pragma warning disable CS8625")
Expand All @@ -67,7 +67,7 @@ await Assert
.IsFalse()
.Because("missing-documentation warnings must be resolved with XML summaries, not pragmas");

var targetsEnum = generationResult.GetSource("Purview.Telemetry.Targets.g.cs");
var targetsEnum = generationResult.GetSource("Targets.g.cs");
await Assert
.That(targetsEnum)
.ContainsGeneratedCode("/// <summary>Excludes logging targets.</summary>")
Expand Down