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 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,40 @@ 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");

[TestMethod]
public void RazorSourceGenerator_HasCorrectPath() =>
RazorSourceGenerator.FullPath
.Should()
.Be(Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
RazorSourceGenerator.FullPath.Should().EndWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

[TestMethod]
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_ReturnsCorrectPath()
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
var latestSdkVersion = Version.Parse(Path.GetFileName(latestSdkFolder));
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

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}.*", SearchOption.TopDirectoryOnly)
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
.Any(x => IsHigherVersion(x, latestSdkVersion))
.Should()
.BeFalse("There should be no SDK folders with a higher version number than the latest SDK folder");
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
}

private static bool IsHigherVersion(string directory, Version referenceVersion) =>
Version.TryParse(new DirectoryInfo(directory).Name, out var version) && version > referenceVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,34 @@ 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())
new(CheckAndReturnRazorSourceGeneratorPath(), new AssemblyLoader())
];

public static string CheckAndReturnRazorSourceGeneratorPath() =>
File.Exists(RazorSourceGeneratorPath) ? RazorSourceGeneratorPath : throw new FileNotFoundException($"Razor sourcegenerator not found: {RazorSourceGeneratorPath}");
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

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}.*", SearchOption.TopDirectoryOnly) is { Length: > 0 } specificMajorVersionSdkDirectories
? specificMajorVersionSdkDirectories.OrderByDescending(x => Version.Parse(new DirectoryInfo(x).Name)).First()
: throw new DirectoryNotFoundException($"SDK directory not found for version {objectAssembly.GetName().Version.Major}");
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
}

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