summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs
blob: 79f840c5f9948ac76d27696635242efbc0063f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;

using Xamarin.Forms.Build.Tasks;
using Mono.Cecil;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public static class MockCompiler
	{
		public static void Compile(Type type)
		{
			MethodDefinition _;
			Compile(type, out _);
		}

		public static void Compile(Type type, out MethodDefinition methdoDefinition)
		{
			methdoDefinition = null;
			var assembly = type.Assembly.Location;
			var refs = from an in type.Assembly.GetReferencedAssemblies()
					   let a = System.Reflection.Assembly.Load(an)
					   select a.Location;

			var xamlc = new XamlCTask {
				Assembly = assembly,
				ReferencePath = string.Join(";", refs),
				KeepXamlResources = true,
				OptimizeIL = true,
				DebugSymbols = false,
				ReadOnly = true,
				Type = type.FullName
			};

			IList<Exception> exceptions;
			if (xamlc.Execute(out exceptions) || exceptions == null || !exceptions.Any()) {
				methdoDefinition = xamlc.InitCompForType;
				return;
			}
			if (exceptions.Count > 1)
				throw new AggregateException(exceptions);
			throw exceptions[0];
		}
	}
}