-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from max-ieremenko/release/1.4.8
Release/1.4.8
- Loading branch information
Showing
6 changed files
with
322 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...s/ServiceModel.Grpc.DesignTime.Test/Generator/Internal/CSharp/CSharpMessageBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// <copyright> | ||
// Copyright 2022 Max Ieremenko | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using System.Runtime.Serialization; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using NUnit.Framework; | ||
using ServiceModel.Grpc.Channel; | ||
using ServiceModel.Grpc.TestApi; | ||
using Shouldly; | ||
|
||
namespace ServiceModel.Grpc.DesignTime.Generator.Internal.CSharp; | ||
|
||
[TestFixture] | ||
public class CSharpMessageBuilderTest : MessageBuilderTestBase | ||
{ | ||
private AssemblyLoadContext _loadContext = null!; | ||
|
||
[OneTimeSetUp] | ||
public void BeforeAll() | ||
{ | ||
_loadContext = new AssemblyLoadContext(nameof(CSharpMessageBuilderTest), isCollectible: true); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void AfterAll() | ||
{ | ||
_loadContext.Unload(); | ||
} | ||
|
||
protected override Type GetMessageType(Type[] typeArguments) | ||
{ | ||
if (typeArguments.Length == 0) | ||
{ | ||
return typeof(Message); | ||
} | ||
|
||
var genericType = FindOrCompileMessage(typeArguments.Length); | ||
return genericType.MakeGenericType(typeArguments); | ||
} | ||
|
||
private Type FindOrCompileMessage(int propertiesCount) | ||
{ | ||
var ns = typeof(CSharpMessageBuilderTest).Namespace!; | ||
var assemblyName = typeof(CSharpMessageBuilderTest) + propertiesCount.ToString(CultureInfo.InvariantCulture); | ||
|
||
var assembly = _loadContext.Assemblies.FirstOrDefault(i => i.GetName().Name == assemblyName); | ||
if (assembly == null) | ||
{ | ||
assembly = CompileMessage(assemblyName, ns, propertiesCount); | ||
} | ||
|
||
var messageTypeName = ns + ".Message" + "`" + propertiesCount.ToString(CultureInfo.InvariantCulture); | ||
|
||
return assembly.GetType(messageTypeName, true, false)!; | ||
} | ||
|
||
private Assembly CompileMessage(string assemblyName, string ns, int propertiesCount) | ||
{ | ||
var output = new CodeStringBuilder(); | ||
output.Append("namespace ").Append(ns).AppendLine(";"); | ||
|
||
new CSharpMessageBuilder(propertiesCount).GenerateMemberDeclaration(output); | ||
|
||
var syntaxTree = SyntaxFactory.ParseSyntaxTree( | ||
output.AsStringBuilder().ToString(), | ||
CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp10)); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
assemblyName, | ||
syntaxTrees: new[] { syntaxTree }, | ||
references: new[] | ||
{ | ||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location), | ||
MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location)!, "System.Runtime.dll")), | ||
MetadataReference.CreateFromFile(typeof(DataContractAttribute).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(ExcludeFromCodeCoverageAttribute).Assembly.Location) | ||
}, | ||
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
||
var peStream = new MemoryStream(); | ||
var emitResult = compilation.Emit(peStream); | ||
emitResult.Success.ShouldBeTrue(); | ||
|
||
peStream.Position = 0; | ||
return _loadContext.LoadFromStream(peStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.