|
1 | 1 | using GitVersion.Core.Tests.Helpers; |
2 | 2 | using GitVersion.Extensions; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
3 | 5 | using Microsoft.Extensions.DependencyInjection; |
4 | | -using Mono.Cecil; |
5 | 6 |
|
6 | 7 | namespace GitVersion.App.Tests; |
7 | 8 |
|
@@ -46,17 +47,16 @@ public void WriteVersionShouldWriteFileVersionWithPrereleaseTag() |
46 | 47 |
|
47 | 48 | private static Assembly GenerateAssembly(Version fileVersion, string prereleaseInfo) |
48 | 49 | { |
49 | | - var definition = new AssemblyNameDefinition("test-asm", fileVersion); |
50 | | - |
51 | | - var asmDef = AssemblyDefinition.CreateAssembly(definition, "test-asm", ModuleKind.Dll); |
52 | | - var constructor = typeof(AssemblyInformationalVersionAttribute).GetConstructor(new[] { typeof(string) }); |
53 | | - var methodReference = asmDef.MainModule.ImportReference(constructor); |
54 | | - var customAttribute = new CustomAttribute(methodReference); |
55 | | - customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(asmDef.MainModule.TypeSystem.String, fileVersion + prereleaseInfo)); |
56 | | - asmDef.CustomAttributes.Add(customAttribute); |
| 50 | + var attribute = typeof(AssemblyInformationalVersionAttribute); |
| 51 | + var csharpCode = $@"[assembly: {attribute.FullName}(""{fileVersion + prereleaseInfo}"")]"; |
| 52 | + var compilation = CSharpCompilation.Create("test-asm") |
| 53 | + .WithOptions(new(OutputKind.DynamicallyLinkedLibrary)) |
| 54 | + .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) |
| 55 | + .AddReferences(MetadataReference.CreateFromFile(attribute.Assembly.Location)) |
| 56 | + .AddSyntaxTrees(CSharpSyntaxTree.ParseText(csharpCode)); |
57 | 57 |
|
58 | 58 | using var memoryStream = new MemoryStream(); |
59 | | - asmDef.Write(memoryStream); |
| 59 | + compilation.Emit(memoryStream); |
60 | 60 |
|
61 | 61 | return Assembly.Load(memoryStream.ToArray()); |
62 | 62 | } |
|
0 commit comments