Skip to content

Commit

Permalink
chore: Splitting some C# related extensions in a separate file (#2697)
Browse files Browse the repository at this point in the history
Splitting some C# related extensions in a separate file

Co-authored-by: Rouke Broersma <[email protected]>
  • Loading branch information
psfinaki and rouke-broersma authored Oct 13, 2023
1 parent b4f22e5 commit bb1f129
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Buildalyzer;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Stryker.Core.Options;

namespace Stryker.Core.Initialisation.Buildalyzer
{
public static class IAnalyzerResultCSharpExtensions
{
public static CSharpCompilationOptions GetCompilationOptions(this IAnalyzerResult analyzerResult)
{
var compilationOptions = new CSharpCompilationOptions(analyzerResult.GetOutputKind())
.WithNullableContextOptions(analyzerResult.GetNullableContextOptions())
.WithAllowUnsafe(analyzerResult.GetPropertyOrDefault("AllowUnsafeBlocks", true))
.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)
.WithConcurrentBuild(true)
.WithModuleName(analyzerResult.GetAssemblyName())
.WithOverflowChecks(analyzerResult.GetPropertyOrDefault("CheckForOverflowUnderflow", false));

if (analyzerResult.IsSignedAssembly() && analyzerResult.GetAssemblyOriginatorKeyFile() is var keyFile && keyFile is not null)
{
compilationOptions = compilationOptions.WithCryptoKeyFile(keyFile)
.WithStrongNameProvider(new DesktopStrongNameProvider());
}
return compilationOptions;
}

public static CSharpParseOptions GetParseOptions(this IAnalyzerResult analyzerResult, StrykerOptions options) => new CSharpParseOptions(options.LanguageVersion, DocumentationMode.None, preprocessorSymbols: analyzerResult.PreprocessorSymbols);

private static NullableContextOptions GetNullableContextOptions(this IAnalyzerResult analyzerResult)
{
if (!Enum.TryParse(analyzerResult.GetPropertyOrDefault("Nullable", "enable"), true, out NullableContextOptions nullableOptions))
{
nullableOptions = NullableContextOptions.Enable;
}

return nullableOptions;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using Buildalyzer;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Extensions.Logging;
using NuGet.Frameworks;
using Stryker.Core.Exceptions;
using Stryker.Core.Options;

namespace Stryker.Core.Initialisation.Buildalyzer
{
Expand All @@ -34,26 +31,6 @@ public static IEnumerable<ResourceDescription> GetResources(this IAnalyzerResult
embeddedResources);
}

public static CSharpCompilationOptions GetCompilationOptions(this IAnalyzerResult analyzerResult)
{
var compilationOptions = new CSharpCompilationOptions(analyzerResult.GetOutputKind())
.WithNullableContextOptions(analyzerResult.GetNullableContextOptions())
.WithAllowUnsafe(analyzerResult.GetPropertyOrDefault("AllowUnsafeBlocks", true))
.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)
.WithConcurrentBuild(true)
.WithModuleName(analyzerResult.GetAssemblyName())
.WithOverflowChecks(analyzerResult.GetPropertyOrDefault("CheckForOverflowUnderflow", false));

if (analyzerResult.IsSignedAssembly() && analyzerResult.GetAssemblyOriginatorKeyFile() is var keyFile && keyFile is not null)
{
compilationOptions = compilationOptions.WithCryptoKeyFile(keyFile)
.WithStrongNameProvider(new DesktopStrongNameProvider());
}
return compilationOptions;
}

public static CSharpParseOptions GetParseOptions(this IAnalyzerResult analyzerResult, StrykerOptions options) => new CSharpParseOptions(options.LanguageVersion, DocumentationMode.None, preprocessorSymbols: analyzerResult.PreprocessorSymbols);

public static string AssemblyAttributeFileName(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("GeneratedAssemblyInfoFile",
(Path.GetFileNameWithoutExtension(analyzerResult.ProjectFilePath) + ".AssemblyInfo.cs")
.ToLowerInvariant());
Expand Down Expand Up @@ -153,7 +130,7 @@ public static bool IsTestProject(this IAnalyzerResult analyzerResult)
return isTestProject || hasTestProjectTypeGuid;
}

private static OutputKind GetOutputKind(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("OutputType") switch
internal static OutputKind GetOutputKind(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("OutputType") switch
{
"Exe" => OutputKind.ConsoleApplication,
"WinExe" => OutputKind.WindowsApplication,
Expand All @@ -163,19 +140,9 @@ public static bool IsTestProject(this IAnalyzerResult analyzerResult)
_ => OutputKind.DynamicallyLinkedLibrary
};

private static NullableContextOptions GetNullableContextOptions(this IAnalyzerResult analyzerResult)
{
if (!Enum.TryParse(analyzerResult.GetPropertyOrDefault("Nullable", "enable"), true, out NullableContextOptions nullableOptions))
{
nullableOptions = NullableContextOptions.Enable;
}

return nullableOptions;
}

private static bool IsSignedAssembly(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("SignAssembly", false);
internal static bool IsSignedAssembly(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("SignAssembly", false);

private static string GetAssemblyOriginatorKeyFile(this IAnalyzerResult analyzerResult)
internal static string GetAssemblyOriginatorKeyFile(this IAnalyzerResult analyzerResult)
{
var assemblyKeyFileProp = analyzerResult.GetPropertyOrDefault("AssemblyOriginatorKeyFile");
if (assemblyKeyFileProp is null)
Expand All @@ -188,9 +155,9 @@ private static string GetAssemblyOriginatorKeyFile(this IAnalyzerResult analyzer

private static string GetRootNamespace(this IAnalyzerResult analyzerResult) => analyzerResult.GetPropertyOrDefault("RootNamespace") ?? analyzerResult.GetAssemblyName();

private static bool GetPropertyOrDefault(this IAnalyzerResult analyzerResult, string name, bool defaultBoolean) => bool.Parse(GetPropertyOrDefault(analyzerResult, name, defaultBoolean.ToString()));
internal static bool GetPropertyOrDefault(this IAnalyzerResult analyzerResult, string name, bool defaultBoolean) => bool.Parse(GetPropertyOrDefault(analyzerResult, name, defaultBoolean.ToString()));

private static string GetPropertyOrDefault(this IAnalyzerResult analyzerResult, string name, string defaultValue = default)
internal static string GetPropertyOrDefault(this IAnalyzerResult analyzerResult, string name, string defaultValue = default)
{
if (analyzerResult.Properties is null || !analyzerResult.Properties.TryGetValue(name, out var property))
{
Expand Down

0 comments on commit bb1f129

Please sign in to comment.