diff --git a/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultCSharpExtensions.cs b/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultCSharpExtensions.cs new file mode 100644 index 0000000000..6343345b24 --- /dev/null +++ b/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultCSharpExtensions.cs @@ -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; + } + } +} diff --git a/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultExtensions.cs b/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultExtensions.cs index 7bd650b39d..4ffdba6ba9 100644 --- a/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultExtensions.cs +++ b/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultExtensions.cs @@ -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 { @@ -34,26 +31,6 @@ public static IEnumerable 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()); @@ -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, @@ -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) @@ -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)) {