Skip to content

Commit

Permalink
update to Roslyn 4.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 7, 2024
1 parent d8c31dd commit 08b9671
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ updates:
time: "02:00"
timezone: "America/Chicago"
open-pull-requests-limit: 10
ignore:
- dependency-name: "Microsoft.CodeAnalysis.CSharp"
groups:
Azure:
patterns:
Expand Down
3 changes: 2 additions & 1 deletion AssemblyMetadata.Generators.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
# 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyMetadata.Generators", "src\AssemblyMetadata.Generators\AssemblyMetadata.Generators.csproj", "{57C67863-03B3-4E43-8881-7B19A5657FE0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{1BA030F4-6549-4F95-ACEA-80E992C519BD}"
ProjectSection(SolutionItems) = preProject
coverlet.runsettings = coverlet.runsettings
.github\dependabot.yml = .github\dependabot.yml
src\Directory.Build.props = src\Directory.Build.props
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
README.md = README.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>true</IsPackable>

<IsPackable>true</IsPackable>
<IsRoslynComponent>true</IsRoslynComponent>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
<NoPackageAnalysis>true</NoPackageAnalysis>
<DevelopmentDependency>true</DevelopmentDependency>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
<!-- https://learn.microsoft.com/en-us/visualstudio/extensibility/roslyn-version-support -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.4.0]" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\AssemblyMetadata.Generators.dll" PackagePath="analyzers/dotnet/roslyn4.3/cs" Pack="true" Visible="false" />
<None Include="$(OutputPath)\$(AssemblyName).dll" PackagePath="analyzers/dotnet/roslyn4.4/cs" Pack="true" Visible="false" />
<None Include="$(AssemblyName).targets" PackagePath="build" Pack="true" Visible="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup>
<CompilerVisibleProperty Include="ThisAssemblyNamespace" />
</ItemGroup>
</Project>
22 changes: 18 additions & 4 deletions src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.Reflection;
using System.Resources;
using System.Runtime.Versioning;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class AssemblyMetadataGenerator : IIncrementalGenerator

public void Initialize(IncrementalGeneratorInitializationContext context)
{

var provider = context.SyntaxProvider
.ForAttributeWithMetadataName(
fullyQualifiedMetadataName: "System.Reflection.AssemblyVersionAttribute",
Expand All @@ -50,11 +52,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterSourceOutput(diagnostics, ReportDiagnostic);

IncrementalValuesProvider<EquatableArray<AssemblyConstant>> constants = provider
var constants = provider
.Select(static (item, _) => item.Constants)
.Where(static item => item.Count > 0);

context.RegisterSourceOutput(constants, GenerateOutput);
var assemblyName = context.CompilationProvider
.Select(static (c, _) => c.AssemblyName);

var thisNamespace = context.AnalyzerConfigOptionsProvider
.Select(static (c, _) =>
{
c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var methodName);
return methodName;
});

var options = assemblyName.Combine(thisNamespace);

context.RegisterSourceOutput(constants.Combine(options), GenerateOutput);
}

private static bool SyntacticPredicate(SyntaxNode syntaxNode, CancellationToken cancellationToken)
Expand Down Expand Up @@ -122,9 +136,9 @@ private static void ReportDiagnostic(SourceProductionContext context, EquatableA
context.ReportDiagnostic(diagnostic);
}

private void GenerateOutput(SourceProductionContext context, EquatableArray<AssemblyConstant> constants)
private void GenerateOutput(SourceProductionContext context, (EquatableArray<AssemblyConstant> constants, (string? assemblyName, string? thisNamespace) options) parameters)
{
var source = AssemblyMetadataWriter.Generate(constants);
var source = AssemblyMetadataWriter.Generate(parameters.constants, parameters.options.assemblyName, parameters.options.thisNamespace);

context.AddSource("AssemblyMetadata.g.cs", source);
}
Expand Down
27 changes: 26 additions & 1 deletion src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class AssemblyMetadataWriter
return attribute?.InformationalVersion ?? "1.0.0.0";
});

public static string Generate(EquatableArray<AssemblyConstant> constants)
public static string Generate(EquatableArray<AssemblyConstant> constants, string? assemblyName = null, string? thisNamespace = null)
{
if (constants == null)
throw new ArgumentNullException(nameof(constants));
Expand All @@ -21,6 +21,15 @@ public static string Generate(EquatableArray<AssemblyConstant> constants)
.AppendLine("// <auto-generated />")
.AppendLine();

if (!string.IsNullOrEmpty(thisNamespace))
{
codeBuilder
.Append("namespace ")
.AppendLine(thisNamespace!)
.AppendLine("{")
.IncrementIndent();
}

codeBuilder
.AppendLine("/// <summary>")
.AppendLine("/// Assembly attributes exposed as public constants")
Expand All @@ -41,6 +50,15 @@ public static string Generate(EquatableArray<AssemblyConstant> constants)
.IncrementIndent()
.AppendLine();

if (!string.IsNullOrEmpty(assemblyName))
{
codeBuilder
.Append("public const string AssemblyName = \"")
.Append(assemblyName)
.AppendLine("\";")
.AppendLine();
}

foreach (var constant in constants)
{
var name = SafeName(constant.Name);
Expand All @@ -58,6 +76,13 @@ public static string Generate(EquatableArray<AssemblyConstant> constants)
.DecrementIndent()
.AppendLine("}"); // class

if (!string.IsNullOrEmpty(thisNamespace))
{
codeBuilder
.DecrementIndent()
.AppendLine("}"); // namespace
}

return codeBuilder.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<PropertyGroup>
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -43,7 +43,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.4.0]" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="All" />
<PackageReference Include="Verify.Xunit" Version="25.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
internal static partial class ThisAssembly
{

public const string AssemblyName = "Test.Generator";

public const string HardKey = "HardValue";

public const string VerifyTargetFrameworks = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
internal static partial class ThisAssembly
{

public const string AssemblyName = "Test.Generator";

public const string TargetFramework = ".NETCoreApp,Version=v8.0";

public const string Serviceable = "True";
Expand Down

0 comments on commit 08b9671

Please sign in to comment.