Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Use a Source Generator to extract the Version information #73

Merged
merged 1 commit into from
Jan 16, 2023
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
17 changes: 17 additions & 0 deletions Kiota.Generated/KiotaGenerated.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" ExcludeAssets="runtime" />
</ItemGroup>

<Target Name="DoSthAfterPublish1" AfterTargets="Publish">
<Copy SourceFiles="..\Kiota.Builder\Kiota.Builder.csproj" />
</Target>

</Project>
49 changes: 49 additions & 0 deletions Kiota.Generated/KiotaVersionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.CodeAnalysis;

[Generator]
public class KiotaVersionGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
var mainSyntaxTree = context.Compilation.SyntaxTrees
.First(static x => x.HasCompilationUnitRoot);

var directory = Path.GetDirectoryName(mainSyntaxTree.FilePath);

var version = "unknown";
try {
XmlDocument csproj = new XmlDocument();
csproj.Load(Path.Combine(directory, "..", "Microsoft.Kiota.Http.HttpClientLibrary.csproj"));

version = csproj.GetElementsByTagName("VersionPrefix")[0].InnerText;
} catch (Exception e)
{
throw new SystemException("KiotaVersionGenerator expanded in an invalid project, missing 'Microsoft.Kiota.Http.HttpClientLibrary.csproj' file.", e);
}

string source = $@"// <auto-generated/>
namespace Microsoft.Kiota.Http.Generated
{{
public static class Version
{{
public static string Current()
{{
return ""{version}"";
}}
}}
}}
";

// Add the source code to the compilation
context.AddSource($"KiotaVersion.g.cs", source);
}

public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this one
}
}
14 changes: 14 additions & 0 deletions Microsoft.Kiota.Http.HttpClientLibrary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Kiota.Http.HttpClientLibrary.Tests", "Microsoft.Kiota.Http.HttpClientLibrary.Tests\Microsoft.Kiota.Http.HttpClientLibrary.Tests.csproj", "{CCD20728-D593-48F8-8627-6E7A57B31A43}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KiotaGenerated", "Kiota.Generated\KiotaGenerated.csproj", "{6667C82F-EFF1-4D7C-828D-F981629C8256}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -46,6 +48,18 @@ Global
{CCD20728-D593-48F8-8627-6E7A57B31A43}.Release|x64.Build.0 = Release|Any CPU
{CCD20728-D593-48F8-8627-6E7A57B31A43}.Release|x86.ActiveCfg = Release|Any CPU
{CCD20728-D593-48F8-8627-6E7A57B31A43}.Release|x86.Build.0 = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|x64.ActiveCfg = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|x64.Build.0 = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|x86.ActiveCfg = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Debug|x86.Build.0 = Debug|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|Any CPU.Build.0 = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|x64.ActiveCfg = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|x64.Build.0 = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|x86.ActiveCfg = Release|Any CPU
{6667C82F-EFF1-4D7C-828D-F981629C8256}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-rc.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.1" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/Options/UserAgentHandlerOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public class UserAgentHandlerOption : IRequestOption
/// <summary>
/// The product version to append to the user agent header
/// </summary>
public string ProductVersion { get; set; } = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion.Split('+').First();
public string ProductVersion { get; set; } = Microsoft.Kiota.Http.Generated.Version.Current();
}
}