Skip to content

Commit

Permalink
Merge pull request #418 from microsoft/dependabot/nuget/Microsoft.Cod…
Browse files Browse the repository at this point in the history
…eAnalysis.Analyzers-3.11.0

chore(deps): bump Microsoft.CodeAnalysis.Analyzers from 3.3.4 to 3.11.0
  • Loading branch information
github-actions[bot] authored Oct 7, 2024
2 parents 3b1f96c + 8360b57 commit ec056c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/generated/KiotaGenerated.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" ExcludeAssets="runtime" />
</ItemGroup>

<Target Name="DoSthAfterPublish1" AfterTargets="Publish">
Expand Down
46 changes: 21 additions & 25 deletions src/generated/KiotaVersionGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace KiotaGenerated;

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

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

var version = "unknown";
try
{
XmlDocument csproj = new XmlDocument();
projectDirectory = Path.Combine(projectDirectory, "..", "..", "..", "..", "Directory.Build.props");
csproj.Load(projectDirectory);
version = csproj.GetElementsByTagName("VersionPrefix")[0].InnerText;
}
catch(Exception e)
context.RegisterSourceOutput(context.CompilationProvider, (spc, compilation) =>
{
throw new FileNotFoundException($"KiotaVersionGenerator expanded in an invalid project, missing 'Directory.Build.props' file in the following directory {projectDirectory}", e);
}
var projectDirectory = Path.GetDirectoryName(compilation.SyntaxTrees.First().FilePath);

string source = $@"// <auto-generated/>
try
{
XmlDocument csproj = new XmlDocument();
projectDirectory = Path.Combine(projectDirectory, "..", "..", "..", "..", "Directory.Build.props");
csproj.Load(projectDirectory);
var version = csproj.GetElementsByTagName("VersionPrefix")[0].InnerText;
string source = $@"// <auto-generated/>
namespace Microsoft.Kiota.Http.Generated
{{
/// <summary>
Expand All @@ -48,12 +42,14 @@ public static string Current()
}}
";

// Add the source code to the compilation
context.AddSource($"KiotaVersion.g.cs", source);
// Add the source code to the compilation
spc.AddSource($"KiotaVersion.g.cs", SourceText.From(source, Encoding.UTF8));
}
catch(Exception e)
{
throw new FileNotFoundException($"KiotaVersionGenerator expanded in an invalid project, missing 'Directory.Build.props' file in the following directory {projectDirectory}", e);
}
});
}

public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this one
}
}

0 comments on commit ec056c5

Please sign in to comment.