XamlCompilationVerifier.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Linq;
  3. using Mono.Cecil;
  4. using Nuke.Common.Tooling;
  5. using Serilog;
  6. internal static class XamlCompilationVerifier
  7. {
  8. public static void VerifyAssemblyCompiledXaml(string assemblyPath)
  9. {
  10. const string avaloniaResourcesTypeName = "CompiledAvaloniaXaml.!AvaloniaResources";
  11. const string mainViewTypeName = "BuildTests.MainView";
  12. const string populateMethodName = "!XamlIlPopulate";
  13. using var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
  14. if (assembly.MainModule.GetType(avaloniaResourcesTypeName) is null)
  15. {
  16. throw new InvalidOperationException(
  17. $"Assembly {assemblyPath} is missing type {avaloniaResourcesTypeName}");
  18. }
  19. if (assembly.MainModule.GetType(mainViewTypeName) is not { } mainViewType)
  20. {
  21. throw new InvalidOperationException(
  22. $"Assembly {assemblyPath} is missing type {mainViewTypeName}");
  23. }
  24. if (!mainViewType.Methods.Any(method => method.Name == populateMethodName))
  25. {
  26. throw new InvalidOperationException(
  27. $"Assembly {assemblyPath} is missing method {populateMethodName} on {mainViewTypeName}");
  28. }
  29. Log.Information($"Assembly {assemblyPath} correctly has compiled XAML");
  30. }
  31. public static void VerifyNativeAot(string programPath)
  32. {
  33. const string expectedOutput = "Hello from AOT";
  34. using var process = ProcessTasks.StartProcess(programPath, string.Empty);
  35. process.WaitForExit();
  36. process.AssertZeroExitCode();
  37. var output = process.Output.Select(o => o.Text).FirstOrDefault();
  38. if (output != expectedOutput)
  39. {
  40. throw new InvalidOperationException(
  41. $"{programPath} returned text \"{output}\", expected \"{expectedOutput}\"");
  42. }
  43. Log.Information($"Native program {programPath} correctly has compiled XAML");
  44. }
  45. }