Skip to content

Commit

Permalink
Multi-target Roslyn
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDunn committed Dec 27, 2024
1 parent cc5d19c commit 8558e39
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 50 deletions.
23 changes: 19 additions & 4 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ function WriteStage([string]$message)
Write-Output ""
}

function BuildWith([string]$configuration)
{
WriteStage("... building " + $configuration)

exec { & dotnet build Vogen.sln -c $configuration -p Thorough=true --no-restore --verbosity $verbosity}
exec { & dotnet build src/Vogen/Vogen.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.4 --verbosity $verbosity}
exec { & dotnet build src/Vogen/Vogen.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.6 --verbosity $verbosity}
exec { & dotnet build src/Vogen/Vogen.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.8 --verbosity $verbosity}

exec { & dotnet build src/Vogen.CodeFixers/Vogen.CodeFixers.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.4 --verbosity $verbosity}
exec { & dotnet build src/Vogen.CodeFixers/Vogen.CodeFixers.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.6 --verbosity $verbosity}
exec { & dotnet build src/Vogen.CodeFixers/Vogen.CodeFixers.csproj -c $configuration -p Thorough=true -p RoslynVersion=roslyn4.8 --verbosity $verbosity}}

function Get999VersionWithUniquePatch()
{
$date1 = Get-Date("2022-10-17");
Expand Down Expand Up @@ -60,7 +73,7 @@ exec { & dotnet clean Vogen.sln -c Release --verbosity $verbosity}
WriteStage("... restore ...")
exec { & dotnet restore Vogen.sln --no-cache --verbosity $verbosity }

exec { & dotnet build Vogen.sln -c Release -p Thorough=true --no-restore --verbosity $verbosity}
BuildWith("Release");

# run the analyzer tests
WriteStage("Running analyzer tests...")
Expand All @@ -86,7 +99,9 @@ $version = Get999VersionWithUniquePatch
# **NOTE** - we don't want these 999.9.9.x packages ending up in %userprofile%\.nuget\packages because it'll polute it.

exec { & dotnet restore Vogen.sln --packages $localPackages --no-cache --verbosity $verbosity }
exec { & dotnet build Vogen.sln -c Debug --no-restore --verbosity $verbosity}

BuildWith("Debug");

exec { & dotnet pack ./src/Vogen.Pack.csproj -c Debug -o:$localPackages /p:ForceVersion=$version --include-symbols --version-suffix:dev --no-restore --verbosity $verbosity }

WriteStage("Cleaning and building consumers (tests and samples) - verbosity of $verbosity")
Expand All @@ -107,11 +122,11 @@ $releaseTask = Start-Process "dotnet" "build Consumers.sln --configuration Relea
$debugTask.WaitForExit()
$releaseTask.WaitForExit()

if ($debugTask.ExitCode -ne $null -and $debugTask.ExitCode -ne 0) {
if ($null -ne $debugTask.ExitCode -and $debugTask.ExitCode -ne 0) {
Write-Host "debug build returned " + $debugTask.ExitCode
exit -1
}
if ($releaseTask.ExitCode -ne $null -and $releaseTask.ExitCode -ne 0) {
if ($null -ne $releaseTask.ExitCode -and $releaseTask.ExitCode -ne 0) {
Write-Host "release build returned " + $releaseTask.ExitCode
exit -1
}
Expand Down
28 changes: 0 additions & 28 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,6 @@
</PropertyGroup>

<Choose>
<When Condition="$(RoslynVersion) == 'roslyn3.8'">
<ItemGroup >
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="3.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN_3_8</DefineConstants>
<DefineConstants>$(DefineConstants);CSHARP9_OR_GREATER</DefineConstants>
<NoWarn>$(NoWarn);nullable</NoWarn>
</PropertyGroup>
</When>

<When Condition="$(RoslynVersion) == 'roslyn4.2'">
<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.2.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN_4_2;ROSLYN_4_2_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);CSHARP9_OR_GREATER;CSHARP10_OR_GREATER</DefineConstants>
<NoWarn>$(NoWarn);nullable</NoWarn>
</PropertyGroup>
</When>

<When Condition="$(RoslynVersion) == 'roslyn4.4'">
<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
Expand Down
20 changes: 14 additions & 6 deletions src/Vogen.Pack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@
<ItemGroup>
<None Include="..\assets\logo\package_logo_128x128.png" Pack="true" PackagePath="" />
<None Include="..\docs\nuget-readme.md" Pack="true" PackagePath="\" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\netstandard2.0\Vogen.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\Vogen.CodeFixers\bin\$(Configuration)\netstandard2.0\Vogen.CodeFixers.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.4\$(Configuration)\netstandard2.0\Vogen.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.4/cs/" Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\Vogen.CodeFixers\bin\roslyn4.4\$(Configuration)\netstandard2.0\Vogen.CodeFixers.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.4/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net9.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\net9.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net9.0\Vogen.SharedTypes.xml" Pack="true" PackagePath="lib\net9.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.6\$(Configuration)\netstandard2.0\Vogen.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.6/cs/" Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\Vogen.CodeFixers\bin\roslyn4.6\$(Configuration)\netstandard2.0\Vogen.CodeFixers.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.6/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.8\$(Configuration)\netstandard2.0\Vogen.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.8/cs/" Visible="false" />
<None Include="$(MSBuildThisFileDirectory)\Vogen.CodeFixers\bin\roslyn4.8\$(Configuration)\netstandard2.0\Vogen.CodeFixers.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.8/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.8\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.8\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.8\$(Configuration)\net9.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\net9.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\roslyn4.8\$(Configuration)\net9.0\Vogen.SharedTypes.xml" Pack="true" PackagePath="lib\net9.0" Visible="true" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/Vogen/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,9 @@ VOG032 | Usage | Warning | DoNotThrowFromUserCodeAnalyzer
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
VOG033 | Usage | Info | UseReadonlyStructInsteadOfStructAnalyzer
VOG034 | Usage | Error | DoNotCompareWithPrimitivesInEfCoreAnalyzer
VOG034 | Usage | Error | DoNotCompareWithPrimitivesInEfCore





9 changes: 9 additions & 0 deletions src/Vogen/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ namespace Vogen;

internal static class CompilationExtensions
{
public static bool IsAtLeastCSharp12(this Compilation compilation)
{
#if ROSLYN_4_8_OR_GREATER
return (compilation as CSharpCompilation)?.LanguageVersion >= LanguageVersion.CSharp12;
#else
return false;
#endif
}

public static bool IsAtLeastCSharpVersion(this Compilation compilation, LanguageVersion langVersion) =>
(compilation as CSharpCompilation)?.LanguageVersion >= langVersion;

Expand Down
3 changes: 1 addition & 2 deletions src/Vogen/GenerateCodeForBsonSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Vogen.Types;

namespace Vogen;
Expand All @@ -21,7 +20,7 @@ internal class GenerateCodeForBsonSerializers
/// <param name="workItems"></param>
public static void GenerateForApplicableValueObjects(SourceProductionContext context, Compilation compilation, List<VoWorkItem> workItems)
{
if (!compilation.IsAtLeastCSharpVersion(LanguageVersion.CSharp12))
if (!compilation.IsAtLeastCSharp12())
{
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Vogen/GenerateCodeForEfCoreMarkers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Vogen.Generators.Conversions;

Expand All @@ -13,7 +12,7 @@ internal class GenerateCodeForEfCoreMarkers
{
public static void Generate(SourceProductionContext context, Compilation compilation, ImmutableArray<MarkerClassDefinition> markerClasses)
{
if (!compilation.IsAtLeastCSharpVersion(LanguageVersion.CSharp12))
if (!compilation.IsAtLeastCSharp12())
{
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Vogen/GenerateCodeForMessagePack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Vogen.Generators.Conversions;
Expand Down Expand Up @@ -109,7 +108,7 @@ public static void GenerateForApplicableValueObjects(SourceProductionContext con
Compilation compilation,
List<VoWorkItem> valueObjects)
{
if (!compilation.IsAtLeastCSharpVersion(LanguageVersion.CSharp12))
if (!compilation.IsAtLeastCSharp12())
{
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Vogen/Rules/PreferReadonlyStructAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using System.Collections.Immutable;
using System.Linq;
using Vogen.Diagnostics;
// ReSharper disable ArrangeObjectCreationWhenTypeEvident

namespace Vogen.Rules;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class PreferReadonlyStructAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor _rule = new(
private static readonly DiagnosticDescriptor _rule = new DiagnosticDescriptor(
RuleIdentifiers.UseReadonlyStructInsteadOfStruct,
"Use readonly struct instead of struct",
"Type '{0}' should be a readonly struct",
Expand Down
9 changes: 5 additions & 4 deletions src/Vogen/Vogen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<LanguageVersion>latest</LanguageVersion>
<ImplicitUsings>disable</ImplicitUsings>
<nullable>enable</nullable>
<DebugSymbols>True</DebugSymbols>
<DebugType>Embedded</DebugType>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<BaseOutputPath>bin\$(RoslynVersion)\</BaseOutputPath>
<IsRoslynComponent>true</IsRoslynComponent>

<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
Expand Down Expand Up @@ -39,10 +43,7 @@
<None Remove="Generators\Conversions\SystemTextJsonConverters\**" />
<None Remove="Generators\Conversions\TypeConverters\**" />
</ItemGroup>
<ItemGroup>
<None Remove="AnalyzerReleases.Shipped.md" />
<None Remove="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
Expand Down

0 comments on commit 8558e39

Please sign in to comment.