Skip to content

Commit

Permalink
Merge pull request #2149 from andreaTP/source-gen-version
Browse files Browse the repository at this point in the history
Source generator for the version of this project
  • Loading branch information
baywet authored Jan 12, 2023
2 parents f1eb223 + 1f0f89a commit f14ca01
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- The Lock file uses a project version coming from a Source Generator instead of the one looked up with reflection. [#2147](https://github.com/microsoft/kiota/issues/2147)
- Fixed a bug in ruby where file names or paths could be too long to be packaged.
- Fixed a bug where models descriptions would be undeterministic. [#2130](https://github.com/microsoft/kiota/issues/2130)
- Fixed a bug in dotnet where default values for Enum properties with special characters would not match the model. [#2091](https://github.com/microsoft/kiota/issues/2091)
Expand Down
7 changes: 7 additions & 0 deletions kiota.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kiota.Web", "src\Kiota.Web\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kiota.Web.Tests", "tests\Kiota.Web.Tests\Kiota.Web.Tests.csproj", "{A2584ED3-5C92-4BC7-9120-D97F5404B52C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KiotaGenerated", "src\Kiota.Generated\KiotaGenerated.csproj", "{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,10 @@ Global
{A2584ED3-5C92-4BC7-9120-D97F5404B52C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2584ED3-5C92-4BC7-9120-D97F5404B52C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2584ED3-5C92-4BC7-9120-D97F5404B52C}.Release|Any CPU.Build.0 = Release|Any CPU
{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -66,5 +72,6 @@ Global
{E4C108A5-A13F-4C3F-B32A-86210A4EC52A} = {2DF34BB8-B19F-4623-9E3D-9F59A14C0660}
{C27E6F75-8E64-4F3D-8EA0-E632BFAFF600} = {EAAC5CEA-33B8-495D-9CD0-B36794B8AFE7}
{A2584ED3-5C92-4BC7-9120-D97F5404B52C} = {2DF34BB8-B19F-4623-9E3D-9F59A14C0660}
{01ABDF23-60CD-4CE3-8DC7-8654C4BA1EE8} = {EAAC5CEA-33B8-495D-9CD0-B36794B8AFE7}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Kiota.Builder/Kiota.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.4.5" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="YamlDotNet" Version="12.3.1" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Lock/KiotaLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class KiotaLock {
/// <summary>
/// The version of the Kiota generator that generated this client.
/// </summary>
public string KiotaVersion { get; set; } = Assembly.GetEntryAssembly().GetName().Version.ToString();
public string KiotaVersion { get; set; } = Kiota.Generated.KiotaVersion.Current();
/// <summary>
/// The main class name for this client.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Kiota.Generated/KiotaGenerated.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- <TargetFramework>netstandard2.0</TargetFramework> -->
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<SonarQubeExclude>true</SonarQubeExclude>
</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 src/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.Join(directory, "Kiota.Builder.csproj"));

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

string source = $@"// <auto-generated/>
namespace Kiota.Generated
{{
public static class KiotaVersion
{{
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
}
}
25 changes: 25 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaGeneratedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.IO;
using Xunit;

public class KiotaGeneratedTests
{
[Fact]
public void StaticallyGeneratedAssemblyVersion()
{
var topLevelFolder = Directory.GetParent(Path.GetDirectoryName(typeof(KiotaGeneratedTests).Assembly.Location))
.Parent
.Parent
.Parent
.Parent
.FullName;
var csprojFile = Path.Join(topLevelFolder, "src", "Kiota.Builder", "Kiota.Builder.csproj");

var line = Array.Find(File.ReadAllLines(csprojFile), l => l.Contains("<Version>"));
line = line.Trim();
line = line.Replace("<Version>", "");
var version = line.Replace("</Version>", "");

Assert.Equal(version, Kiota.Generated.KiotaVersion.Current());
}
}

0 comments on commit f14ca01

Please sign in to comment.