Skip to content

Commit 1aa9d22

Browse files
committed
Add simple code generator to get the right version of the library from version.json for use in the agent header.
Bump version.json version
1 parent 2470c47 commit 1aa9d22

File tree

10 files changed

+190
-2
lines changed

10 files changed

+190
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (c) Barry Dorrans. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
Console.WriteLine(idunno.Versioning.JsonVersion);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<!--<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
<CompilerGeneratedFilesOutputPath>generated</CompilerGeneratedFilesOutputPath>-->
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<AdditionalFiles Include="../../version.json" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\idunno.VersionGenerator\idunno.VersionGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"Debug Generator": {
4+
"commandName": "DebugRoslynComponent",
5+
"targetProject": "..\\idunno.VersionGenerator.Debug\\idunno.VersionGenerator.Debug.csproj"
6+
}
7+
}
8+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Barry Dorrans. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.Text;
9+
10+
namespace idunno.VersionGenerator
11+
{
12+
[Generator]
13+
internal sealed class VersioningSourceGenerator : IIncrementalGenerator
14+
{
15+
const string VersionFileName = "version.json";
16+
17+
const string Pattern = @"""version"":(?:\s*)""(?<version>.*)""";
18+
19+
private static readonly Regex s_versionExtractorRegex = new(
20+
pattern: Pattern,
21+
RegexOptions.Compiled | RegexOptions.CultureInvariant,
22+
matchTimeout: new TimeSpan(0, 0, 30));
23+
24+
/// <inheritdoc/>
25+
public void Initialize(IncrementalGeneratorInitializationContext context)
26+
{
27+
IncrementalValuesProvider <(string name, string code)> pipeline = context.AdditionalTextsProvider
28+
.Where(static file => Path.GetFileName(file.Path).Equals(VersionFileName, StringComparison.Ordinal))
29+
.Select(static (text, cancellationToken) =>
30+
{
31+
string name = Path.GetFileName(text.Path);
32+
SourceText? jsonText = text.GetText(cancellationToken);
33+
34+
if (jsonText is not null)
35+
{
36+
Match capture = s_versionExtractorRegex.Match(jsonText.ToString());
37+
38+
if (capture.Success)
39+
{
40+
string extractedVersion = capture.Groups["version"].ToString(); ;
41+
42+
string code = GenerateVersionClass(extractedVersion);
43+
44+
return (name, code);
45+
}
46+
else
47+
{
48+
return (name, string.Empty);
49+
}
50+
}
51+
else
52+
{
53+
return (name, string.Empty);
54+
}
55+
});
56+
57+
context.RegisterSourceOutput(
58+
pipeline,
59+
static (context, pair) =>
60+
context.AddSource($"idunno.VersionGenerator.g.cs", SourceText.From(pair.code, Encoding.UTF8)));
61+
62+
}
63+
64+
private static string GenerateVersionClass(string version)
65+
{
66+
if (string.IsNullOrEmpty(version))
67+
{
68+
return $@"
69+
namespace idunno
70+
{{
71+
internal static class Versioning
72+
{{
73+
#warning Version attribute not found in {VersionFileName}
74+
public const string JsonVersion = ""NotFound"";
75+
}}
76+
}}";
77+
}
78+
else
79+
{
80+
return $@"
81+
namespace idunno
82+
{{
83+
internal static class Versioning
84+
{{
85+
public const string JsonVersion = ""{version}"";
86+
}}
87+
}}";
88+
}
89+
}
90+
}
91+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>13.0</LangVersion>
8+
<IsRoslynComponent>true</IsRoslynComponent>
9+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
10+
<IsTrimmable>false</IsTrimmable>
11+
<IsAotCompatible>false</IsAotCompatible>
12+
</PropertyGroup>
13+
14+
<PropertyGroup>
15+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
23+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
24+
</ItemGroup>
25+
26+
<PropertyGroup>
27+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Generates a package at build -->
28+
<IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency -->
29+
<NoWarn>$(NoWarn);NU5128</NoWarn>
30+
</PropertyGroup>
31+
32+
<ItemGroup>
33+
<!-- Package the generator in the analyzer directory of the nuget package -->
34+
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
35+
</ItemGroup>
36+
37+
</Project>

idunno.Bluesky.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step4", "docs\docs\bots\cod
233233
EndProject
234234
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step5", "docs\docs\bots\code\WatcherBot\Step5\Step5.csproj", "{061FF07D-DC46-5649-2A78-94B86652F4E8}"
235235
EndProject
236+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "codeGen", "codeGen", "{20879E4B-C3E0-480E-9EA0-CD13C2F888C4}"
237+
EndProject
238+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "idunno.VersionGenerator", "codeGen\idunno.VersionGenerator\idunno.VersionGenerator.csproj", "{21925E4E-5085-C86E-0777-BF70500268FE}"
239+
EndProject
240+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "idunno.VersionGenerator.Debug", "codeGen\idunno.VersionGenerator.Debug\idunno.VersionGenerator.Debug.csproj", "{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}"
241+
EndProject
236242
Global
237243
GlobalSection(SolutionConfigurationPlatforms) = preSolution
238244
CodeQL|Any CPU = CodeQL|Any CPU
@@ -386,6 +392,18 @@ Global
386392
{061FF07D-DC46-5649-2A78-94B86652F4E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
387393
{061FF07D-DC46-5649-2A78-94B86652F4E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
388394
{061FF07D-DC46-5649-2A78-94B86652F4E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
395+
{21925E4E-5085-C86E-0777-BF70500268FE}.CodeQL|Any CPU.ActiveCfg = Debug|Any CPU
396+
{21925E4E-5085-C86E-0777-BF70500268FE}.CodeQL|Any CPU.Build.0 = Debug|Any CPU
397+
{21925E4E-5085-C86E-0777-BF70500268FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
398+
{21925E4E-5085-C86E-0777-BF70500268FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
399+
{21925E4E-5085-C86E-0777-BF70500268FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
400+
{21925E4E-5085-C86E-0777-BF70500268FE}.Release|Any CPU.Build.0 = Release|Any CPU
401+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.CodeQL|Any CPU.ActiveCfg = Debug|Any CPU
402+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.CodeQL|Any CPU.Build.0 = Debug|Any CPU
403+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
404+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
405+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
406+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7}.Release|Any CPU.Build.0 = Release|Any CPU
389407
EndGlobalSection
390408
GlobalSection(SolutionProperties) = preSolution
391409
HideSolutionNode = FALSE
@@ -437,6 +455,8 @@ Global
437455
{3CD72755-37E5-D821-DE3A-378D01FDD4E9} = {8EF4CCE5-0328-4ED0-9D28-F5E9CC2F2EB4}
438456
{E2D3A4A9-5FD9-AE9B-0C33-E08069CA710C} = {8EF4CCE5-0328-4ED0-9D28-F5E9CC2F2EB4}
439457
{061FF07D-DC46-5649-2A78-94B86652F4E8} = {8EF4CCE5-0328-4ED0-9D28-F5E9CC2F2EB4}
458+
{21925E4E-5085-C86E-0777-BF70500268FE} = {20879E4B-C3E0-480E-9EA0-CD13C2F888C4}
459+
{5DA63169-F9BB-49C6-A43A-AFD5992B1CB7} = {20879E4B-C3E0-480E-9EA0-CD13C2F888C4}
440460
EndGlobalSection
441461
GlobalSection(ExtensibilityGlobals) = postSolution
442462
SolutionGuid = {B2B9F8C2-975D-4E3A-B05E-48FDF4BCE3E6}

src/Directory.Build.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<EventSourceSupport>true</EventSourceSupport>
2424
</PropertyGroup>
2525

26+
<ItemGroup>
27+
<AdditionalFiles Include="../../version.json" />
28+
</ItemGroup>
29+
2630
<!-- Trimming -->
2731
<!-- Limit trimming to .NET9.0 or higher due to https://github.com/dotnet/runtime/issues/114307 -->
2832
<PropertyGroup Condition="'$(TargetFramework)' == 'net9.0'">

src/idunno.AtProto/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private static void InternalConfigureHttpClient(HttpClient client, string? httpU
190190

191191
if (httpUserAgent is null)
192192
{
193-
client.DefaultRequestHeaders.UserAgent.ParseAdd("idunno.AtProto/" + typeof(Agent).Assembly.GetName().Version);
193+
client.DefaultRequestHeaders.UserAgent.ParseAdd("idunno.AtProto/" + Versioning.JsonVersion);
194194
}
195195
else
196196
{

src/idunno.AtProto/idunno.AtProto.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
<PackageReference Include="ZstdSharp.Port" />
6565
</ItemGroup>
6666

67+
<ItemGroup>
68+
<ProjectReference Include="..\..\codeGen\idunno.VersionGenerator\idunno.VersionGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
69+
</ItemGroup>
70+
6771
<ItemGroup>
6872
<Compile Update="Resource.Designer.cs">
6973
<DesignTime>True</DesignTime>

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "0.9.7-prerelease",
3+
"version": "0.9.8-prerelease",
44
"publicReleaseRefSpec": [
55
"^refs\/heads\/rel\/.*$"
66
],

0 commit comments

Comments
 (0)