Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve configuration enums directly to fix VS but crashing the source generator #654

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions build/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ 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 \
/p:TargetFrameworks="netstandard2.0" \
/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"
24 changes: 3 additions & 21 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Descriptors;
Expand Down Expand Up @@ -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);
}
}
7 changes: 5 additions & 2 deletions test/Riok.Mapperly.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -117,8 +117,11 @@ params SyntaxTree[] syntaxTrees
return compilation;
}

private static IEnumerable<MethodDeclarationSyntax> ExtractAllMethods(SyntaxNode root)
private static IEnumerable<MethodDeclarationSyntax> ExtractAllMethods(SyntaxNode? root)
{
if (root == null)
yield break;

foreach (var node in root.ChildNodes())
{
// a namespace can contain classes
Expand Down