Skip to content

Commit

Permalink
fix: resolve configuration enums directly to fix VS but crashing the …
Browse files Browse the repository at this point in the history
…source generator (#654)
  • Loading branch information
latonz authored Aug 18, 2023
1 parent c103cde commit a512ee9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
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

0 comments on commit a512ee9

Please sign in to comment.