Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Razor source generators from SDK #9210

Merged
merged 20 commits into from May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -25,20 +25,41 @@ namespace SonarAnalyzer.TestFramework.Test.Common;
[TestClass]
public class SourceGeneratorProviderTest
{
private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));

[TestMethod]
public void SourceGenerators_ContainsRazorSourceGenerator() =>
SourceGeneratorProvider.SourceGenerators.Should().ContainSingle().Which.FullPath.Should().EndWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
SourceGeneratorProvider.SourceGenerators.Should()
.Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll")));

[TestMethod]
public void RazorSourceGenerator_HasCorrectPath() =>
RazorSourceGenerator.FullPath
.Should()
.Be(Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
public void RazorSourceGenerator_ExistsLocally() =>
File.Exists(RazorSourceGenerator.FullPath).Should().BeTrue();

[TestMethod]
public void RazorSourceGenerator_LoadsCorrectAssembly() =>
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators");

private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
[TestMethod]
public void LatestSdkFolder_ReturnsAssemblyMajor()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue($"'{latestSdkFolder}' cannot be parsed to a version number");
latestSdkVersion.Major.Should().Be(typeof(object).Assembly.GetName().Version.Major);
}

[TestMethod]
public void LatestSdkFolder_ReturnLatest()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue($"'{latestSdkFolder}' cannot be parsed to a version number");
var parentDirectory = Directory.GetParent(latestSdkFolder);
parentDirectory.Name.Should().Be("sdk", "Parent directory of the latest SDK should be 'sdk'");
Directory.GetDirectories(parentDirectory.FullName, $"{typeof(object).Assembly.GetName().Version.Major}.*")
.Should().NotContain(x => IsHigherVersion(x, latestSdkVersion), "There should be no SDK folders with a higher version number than the latest SDK folder");
}

private static bool IsHigherVersion(string directory, Version referenceVersion) =>
Version.TryParse(new DirectoryInfo(directory).Name, out var version) && version > referenceVersion;
}
Expand Up @@ -26,13 +26,31 @@ namespace SonarAnalyzer.TestFramework.Common;
public static class SourceGeneratorProvider
{
private static readonly string RazorSourceGeneratorPath =
Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

public static AnalyzerFileReference[] SourceGenerators { get; } =
[
new(RazorSourceGeneratorPath, new AssemblyLoader())
];

public static string LatestSdkFolder()
{
var objectAssembly = typeof(object).Assembly;
var objectAssemblyDirectory = Directory.GetParent(objectAssembly.Location); // C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.4
var dotnetDirectory = objectAssemblyDirectory.Parent.Parent.Parent; // C:\Program Files\dotnet
var sdkDirectory = Path.Combine(dotnetDirectory.FullName, "sdk"); // C:\Program Files\dotnet\sdk

if (!Directory.Exists(sdkDirectory))
{
throw new NotSupportedException($"The directory '{sdkDirectory}' does not exist. " +
$"This may be because you are not using .NET Core. " +
$"Please note that Razor analysis is only supported when using .NET Core.");
}
return Directory.GetDirectories(sdkDirectory, $"{objectAssembly.GetName().Version.Major}.*")
.OrderByDescending(x => Version.Parse(new DirectoryInfo(x).Name))
.First();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about OrderBy(..).Last() to have more positive logic?

}

private sealed class AssemblyLoader : IAnalyzerAssemblyLoader
{
public void AddDependencyLocation(string fullPath) { }
Expand Down
Binary file not shown.