-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2149 from andreaTP/source-gen-version
Source generator for the version of this project
- Loading branch information
Showing
7 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |