Skip to content

Commit 717ddb1

Browse files
committed
Add support and showcase determining install time
This is achieved by adding the analyzer file itself as an additional file and checking its last write time.
1 parent 49c9a38 commit 717ddb1

4 files changed

Lines changed: 51 additions & 8 deletions

File tree

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1-
using System.Collections.Immutable;
1+
using System;
2+
using System.Collections.Immutable;
3+
using System.IO;
4+
using System.Linq;
25
using Devlooped.Sponsors;
6+
using Humanizer;
37
using Microsoft.CodeAnalysis;
48
using Microsoft.CodeAnalysis.Diagnostics;
59
using static Devlooped.Sponsors.SponsorLink;
610

711
namespace Analyzer;
812

9-
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
1014
public class StatusReportingAnalyzer : DiagnosticAnalyzer
1115
{
12-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray<DiagnosticDescriptor>.Empty;
16+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(new DiagnosticDescriptor(
17+
"SL001", "Report Sponsoring Status", "Reports sponsoring status determined by SponsorLink", "Sponsors",
18+
DiagnosticSeverity.Warning, true));
1319

1420
public override void Initialize(AnalysisContext context)
1521
{
1622
context.EnableConcurrentExecution();
1723
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
18-
19-
context.RegisterCodeBlockAction(c =>
24+
25+
context.RegisterCompilationAction(c =>
2026
{
27+
var installed = c.Options.AdditionalFiles.Where(x =>
28+
{
29+
var options = c.Options.AnalyzerConfigOptionsProvider.GetOptions(x);
30+
// In release builds, we'll have a single such item, since we IL-merge the analyzer.
31+
return options.TryGetValue("build_metadata.Analyzer.ItemType", out var itemType) &&
32+
options.TryGetValue("build_metadata.Analyzer.NuGetPackageId", out var packageId) &&
33+
itemType == "Analyzer" &&
34+
packageId == "SponsorableLib";
35+
}).Select(x => File.GetLastWriteTime(x.Path)).OrderByDescending(x => x).FirstOrDefault();
36+
2137
var status = Diagnostics.GetStatus(Funding.Product);
22-
Tracing.Trace($"Status: {status}");
38+
if (installed != default)
39+
Tracing.Trace($"Status: {status}, Installed: {(DateTime.Now - installed).Humanize()} ago");
40+
else
41+
Tracing.Trace($"Status: {status}, unknown install time");
2342
});
2443
}
2544
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<Import Project="Devlooped.Sponsors.targets"/>
3+
<ItemGroup>
4+
<!-- Brings in the analyzer file to report installation time -->
5+
<SponsorablePackageId Include="SponsorableLib" />
6+
</ItemGroup>
37
</Project>

src/SponsorLink/SponsorLink/buildTransitive/Devlooped.Sponsors.targets

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
<SponsorManifest Include="$(SponsorLinkHome)/github/*.jwt" />
2222

2323
<CompilerVisibleItemMetadata Include="SponsorManifest" MetadataName="ItemType" />
24+
<CompilerVisibleItemMetadata Include="Analyzer" MetadataName="ItemType" />
25+
<CompilerVisibleItemMetadata Include="Analyzer" MetadataName="NuGetPackageId" />
2426

2527
<!-- To quickly exit if true -->
2628
<CompilerVisibleProperty Include="DesignTimeBuild" />
2729
</ItemGroup>
2830

2931
<PropertyGroup>
30-
<SLDependsOn>SL_CollectDependencies</SLDependsOn>
32+
<SLDependsOn>SL_CollectDependencies;SL_CollectSponsorableAnalyzer</SLDependsOn>
3133
<SLDependsOn Condition="'$(BuildingInsideVisualStudio)' == 'true' and '$(DesignTimeBuild)' != 'true'">$(SLDependsOn);SL_CheckAutoSync;SL_ReadAutoSyncEnabled;SL_SyncSponsors</SLDependsOn>
3234
</PropertyGroup>
3335

@@ -40,6 +42,7 @@
4042

4143
<Target Name="SL_CollectDependencies" DependsOnTargets="_GenerateRestoreGraph">
4244
<!-- Makes direct dependencies visible to the compiler, so that sponsoring checks can selectively skip indirectly referenced analyzer -->
45+
<!-- An analyzer can check for the presense of a global property named after the package id (which will contain the instaled version) -->
4346
<ItemGroup>
4447
<CompilerVisibleProperty Include="$([MSBuild]::ValueOrDefault('%(_RestoreGraphEntry.Id)', '').Replace('.', '_'))" />
4548
</ItemGroup>
@@ -48,6 +51,18 @@
4851
</CreateProperty>
4952
</Target>
5053

54+
<Target Name="SL_CollectSponsorableAnalyzer" Inputs="@(SponsorablePackageId)" Outputs="|%(SponsorablePackageId.Identity)|">
55+
<PropertyGroup>
56+
<SponsorablePackageId>%(SponsorablePackageId.Identity)</SponsorablePackageId>
57+
</PropertyGroup>
58+
<ItemGroup>
59+
<!--Used to determine installation time, for example, by looking up the analyzer assembly in additional files with:
60+
build_metadata.Analyzer.ItemType = Analyzer
61+
build_metadata.Analyzer.NuGetPackageId = [PackageId] -->
62+
<AdditionalFiles Include="@(Analyzer -> WithMetadataValue('NuGetPackageId', '$(SponsorablePackageId)'))" />
63+
</ItemGroup>
64+
</Target>
65+
5166
<Target Name="SL_Clean" AfterTargets="Clean">
5267
<!-- Cleanup of stamp files -->
5368
<ItemGroup>

src/SponsorLink/Tests/Tests.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<ItemGroup>
2020
<!-- This project reference allows debugging the source generator/analyzer project -->
2121
<ProjectReference Include="..\Analyzer\Analyzer.csproj" Aliases="Analyzer" />
22-
<Analyzer Include="..\Analyzer\bin\$(Configuration)\netstandard2.0\*.dll" />
22+
<Analyzer Include="..\Analyzer\bin\$(Configuration)\netstandard2.0\*.dll" NuGetPackageId="SponsorableLib" />
2323
</ItemGroup>
2424

2525
<ItemGroup>
@@ -51,6 +51,11 @@
5151
</ItemGroup>
5252
</Target>
5353

54+
<!-- Simulates importing SponsorableLib.targets -->
5455
<Import Project="..\SponsorLink\buildTransitive\Devlooped.Sponsors.targets" />
56+
<ItemGroup>
57+
<!-- Bring in the analyzer file to report installation time -->
58+
<AdditionalFiles Include="@(Analyzer -> WithMetadataValue('NuGetPackageId', 'SponsorableLib'))" />
59+
</ItemGroup>
5560

5661
</Project>

0 commit comments

Comments
 (0)