From f3f67cff3115a52895c0529e7e56d78a27e5de68 Mon Sep 17 00:00:00 2001 From: latonz Date: Thu, 17 Aug 2023 16:32:10 +0200 Subject: [PATCH] fix: resolve configuration enums directly to fix VS but crashing the source generator --- build/package.sh | 16 ++++++++++--- .../Configuration/AttributeDataAccessor.cs | 24 +++---------------- test/Riok.Mapperly.Tests/TestHelper.cs | 7 ++++-- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/build/package.sh b/build/package.sh index 7beb381571..ded6ae2dfa 100755 --- a/build/package.sh +++ b/build/package.sh @@ -7,14 +7,23 @@ set -Eeuo pipefail roslyn_versions=('4.0' '4.4' '4.5') -RELEASE_VERSION=${RELEASE_VERSION:-'0.0.1-dev'} +RELEASE_VERSION=${RELEASE_VERSION:-"0.0.1-dev.$(date +%s)"} RELEASE_NOTES=${RELEASE_NOTES:-''} # https://stackoverflow.com/a/246128/3302887 script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) artifacts_dir="${script_dir}/../artifacts" +echo "building Mapperly v${RELEASE_VERSION}" +echo "cleaning artifacts dir" + +mkdir -p "${artifacts_dir}" +rm -rf "${artifacts_dir:?}"/* + +artifacts_dir="$(realpath "$artifacts_dir")" + for roslyn_version in "${roslyn_versions[@]}"; do + echo "building for Roslyn ${roslyn_version}" dotnet pack \ "${script_dir}/../src/Riok.Mapperly" \ -c Release \ @@ -22,8 +31,9 @@ for roslyn_version in "${roslyn_versions[@]}"; do /p:ROSLYN_VERSION="${roslyn_version}" \ -o "${artifacts_dir}/roslyn-${roslyn_version}" \ /p:Version="${RELEASE_VERSION}" \ - /p:PackageReleaseNotes=\""${RELEASE_NOTES}"\" + /p:PackageReleaseNotes=\""${RELEASE_NOTES}"\" >/dev/null done -echo merging multi targets to one nupkg +echo "merging multi targets to a single nupkg" zipmerge "${artifacts_dir}/Riok.Mapperly.${RELEASE_VERSION}.nupkg" "${artifacts_dir}"/*/*.nupkg +echo "built ${artifacts_dir}/Riok.Mapperly.${RELEASE_VERSION}.nupkg" diff --git a/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs b/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs index 85e85e56c6..64e7ddb3f8 100644 --- a/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs +++ b/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs @@ -1,4 +1,3 @@ -using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Riok.Mapperly.Descriptors; @@ -177,25 +176,8 @@ is InvocationExpressionSyntax return null; var enumRoslynType = arg.Type ?? throw new InvalidOperationException("Type is null"); - if (targetType == typeof(IFieldSymbol)) - return enumRoslynType.GetFields().First(f => Equals(f.ConstantValue, arg.Value)); - - var enumReflectionType = GetReflectionType(enumRoslynType); - return enumReflectionType == null - ? throw new InvalidOperationException( - $"Could not resolve enum reflection type of {enumRoslynType.Name} or {targetType} is not supported" - ) - : Enum.ToObject(enumReflectionType, arg.Value); - } - - private static Type? GetReflectionType(ITypeSymbol type) - { - // other special types not yet supported since they are not used yet. - if (type.SpecialType == SpecialType.System_String) - return typeof(string); - - var assemblyName = type.ContainingAssembly.Name; - var qualifiedTypeName = Assembly.CreateQualifiedName(assemblyName, type.ToDisplayString()); - return Type.GetType(qualifiedTypeName); + return targetType == typeof(IFieldSymbol) + ? enumRoslynType.GetFields().First(f => Equals(f.ConstantValue, arg.Value)) + : Enum.ToObject(targetType, arg.Value); } } diff --git a/test/Riok.Mapperly.Tests/TestHelper.cs b/test/Riok.Mapperly.Tests/TestHelper.cs index 12efff216b..aa449867f8 100644 --- a/test/Riok.Mapperly.Tests/TestHelper.cs +++ b/test/Riok.Mapperly.Tests/TestHelper.cs @@ -33,7 +33,7 @@ public static MapperGenerationResult GenerateMapper( var result = Generate(source, options, additionalAssemblies).GetRunResult(); - var methods = ExtractAllMethods(result.GeneratedTrees.Single().GetRoot()) + var methods = ExtractAllMethods(result.GeneratedTrees.SingleOrDefault()?.GetRoot()) .Select(x => new GeneratedMethod(x)) .ToDictionary(x => x.Name); @@ -117,8 +117,11 @@ params SyntaxTree[] syntaxTrees return compilation; } - private static IEnumerable ExtractAllMethods(SyntaxNode root) + private static IEnumerable ExtractAllMethods(SyntaxNode? root) { + if (root == null) + yield break; + foreach (var node in root.ChildNodes()) { // a namespace can contain classes