diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd347089c..d05a9f9bc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/kiota.sln b/kiota.sln index 0295922a43..daeef864da 100644 --- a/kiota.sln +++ b/kiota.sln @@ -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 @@ -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 @@ -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 diff --git a/src/Kiota.Builder/Kiota.Builder.csproj b/src/Kiota.Builder/Kiota.Builder.csproj index 98da2d2220..580a72cc5a 100644 --- a/src/Kiota.Builder/Kiota.Builder.csproj +++ b/src/Kiota.Builder/Kiota.Builder.csproj @@ -36,6 +36,7 @@ + diff --git a/src/Kiota.Builder/Lock/KiotaLock.cs b/src/Kiota.Builder/Lock/KiotaLock.cs index 1c47506212..f6517dc480 100644 --- a/src/Kiota.Builder/Lock/KiotaLock.cs +++ b/src/Kiota.Builder/Lock/KiotaLock.cs @@ -24,7 +24,7 @@ public class KiotaLock { /// /// The version of the Kiota generator that generated this client. /// - public string KiotaVersion { get; set; } = Assembly.GetEntryAssembly().GetName().Version.ToString(); + public string KiotaVersion { get; set; } = Kiota.Generated.KiotaVersion.Current(); /// /// The main class name for this client. /// diff --git a/src/Kiota.Generated/KiotaGenerated.csproj b/src/Kiota.Generated/KiotaGenerated.csproj new file mode 100644 index 0000000000..0b28d442d3 --- /dev/null +++ b/src/Kiota.Generated/KiotaGenerated.csproj @@ -0,0 +1,19 @@ + + + + + net7.0 + latest + true + + + + + + + + + + + + diff --git a/src/Kiota.Generated/KiotaVersionGenerator.cs b/src/Kiota.Generated/KiotaVersionGenerator.cs new file mode 100644 index 0000000000..ac4d1cc7da --- /dev/null +++ b/src/Kiota.Generated/KiotaVersionGenerator.cs @@ -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 = $@"// +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 + } +} diff --git a/tests/Kiota.Builder.Tests/KiotaGeneratedTests.cs b/tests/Kiota.Builder.Tests/KiotaGeneratedTests.cs new file mode 100644 index 0000000000..23178153c9 --- /dev/null +++ b/tests/Kiota.Builder.Tests/KiotaGeneratedTests.cs @@ -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("")); + line = line.Trim(); + line = line.Replace("", ""); + var version = line.Replace("", ""); + + Assert.Equal(version, Kiota.Generated.KiotaVersion.Current()); + } +}