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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Ast;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using Xamarin.Forms.Xaml;
namespace Xamarin.Forms.Build.Tasks
{
public class XamlCTask : XamlTask
{
bool hasCompiledXamlResources;
public bool KeepXamlResources { get; set; }
public bool OptimizeIL { get; set; }
public bool OutputGeneratedILAsCode { get; set; }
internal string Type { get; set; }
public override bool Execute(IList<Exception> thrownExceptions)
{
Logger = Logger ?? new Logger(null, Verbosity);
Logger.LogLine(1, "Compiling Xaml");
Logger.LogLine(1, "\nAssembly: {0}", Assembly);
if (!string.IsNullOrEmpty(DependencyPaths))
Logger.LogLine(1, "DependencyPaths: \t{0}", DependencyPaths);
if (!string.IsNullOrEmpty(ReferencePath))
Logger.LogLine(1, "ReferencePath: \t{0}", ReferencePath.Replace("//", "/"));
Logger.LogLine(3, "DebugSymbols:\"{0}\"", DebugSymbols);
var skipassembly = true; //change this to false to enable XamlC by default
bool success = true;
if (!File.Exists(Assembly))
{
Logger.LogLine(1, "Assembly file not found. Skipping XamlC.");
return true;
}
var resolver = new XamlCAssemblyResolver();
if (!string.IsNullOrEmpty(DependencyPaths))
{
foreach (var dep in DependencyPaths.Split(';'))
{
Logger.LogLine(3, "Adding searchpath {0}", dep);
resolver.AddSearchDirectory(dep);
}
}
if (!string.IsNullOrEmpty(ReferencePath))
{
var paths = ReferencePath.Replace("//", "/").Split(';');
foreach (var p in paths)
{
var searchpath = Path.GetDirectoryName(p);
Logger.LogLine(3, "Adding searchpath {0}", searchpath);
resolver.AddSearchDirectory(searchpath);
}
}
var assemblyDefinition = AssemblyDefinition.ReadAssembly(Path.GetFullPath(Assembly), new ReaderParameters
{
AssemblyResolver = resolver,
ReadSymbols = DebugSymbols
});
CustomAttribute xamlcAttr;
if (assemblyDefinition.HasCustomAttributes &&
(xamlcAttr =
assemblyDefinition.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null)
{
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipassembly = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipassembly = false;
}
foreach (var module in assemblyDefinition.Modules)
{
var skipmodule = skipassembly;
if (module.HasCustomAttributes &&
(xamlcAttr =
module.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null)
{
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipmodule = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipmodule = false;
}
Logger.LogLine(2, " Module: {0}", module.Name);
var resourcesToPrune = new List<EmbeddedResource>();
foreach (var resource in module.Resources.OfType<EmbeddedResource>())
{
Logger.LogString(2, " Resource: {0}... ", resource.Name);
string classname;
if (!resource.IsXaml(out classname))
{
Logger.LogLine(2, "skipped.");
continue;
}
TypeDefinition typeDef = module.GetType(classname);
if (typeDef == null)
{
Logger.LogLine(2, "no type found... skipped.");
continue;
}
var skiptype = skipmodule;
if (typeDef.HasCustomAttributes &&
(xamlcAttr =
typeDef.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null)
{
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skiptype = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skiptype = false;
}
if (Type != null)
skiptype = !(Type == classname);
if (skiptype)
{
Logger.LogLine(2, "Has XamlCompilationAttribute set to Skip and not Compile... skipped");
continue;
}
var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent");
if (initComp == null)
{
Logger.LogLine(2, "no InitializeComponent found... skipped.");
continue;
}
Logger.LogLine(2, "");
CustomAttribute xamlFilePathAttr;
var xamlFilePath = typeDef.HasCustomAttributes && (xamlFilePathAttr = typeDef.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlFilePathAttribute")) != null ?
(string)xamlFilePathAttr.ConstructorArguments [0].Value :
resource.Name;
var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime");
if (initCompRuntime != null)
Logger.LogLine(2, " __InitComponentRuntime already exists... not duplicating");
else {
Logger.LogString(2, " Duplicating {0}.InitializeComponent () into {0}.__InitComponentRuntime ... ", typeDef.Name);
initCompRuntime = DuplicateMethodDef(typeDef, initComp, "__InitComponentRuntime");
Logger.LogLine(2, "done.");
}
Logger.LogString(2, " Parsing Xaml... ");
var rootnode = ParseXaml(resource.GetResourceStream(), typeDef);
if (rootnode == null)
{
Logger.LogLine(2, "failed.");
continue;
}
Logger.LogLine(2, "done.");
hasCompiledXamlResources = true;
Logger.LogString(2, " Replacing {0}.InitializeComponent ()... ", typeDef.Name);
Exception e;
if (!TryCoreCompile(initComp, initCompRuntime, rootnode, out e)) {
success = false;
Logger.LogLine(2, "failed.");
thrownExceptions?.Add(e);
Logger.LogException(null, null, null, xamlFilePath, e);
Logger.LogLine(4, e.StackTrace);
continue;
}
Logger.LogLine(2, "done.");
if (OptimizeIL)
{
Logger.LogString(2, " Optimizing IL... ");
initComp.Body.OptimizeMacros();
Logger.LogLine(2, "done");
}
if (OutputGeneratedILAsCode)
{
var filepath = Path.Combine(Path.GetDirectoryName(Assembly), typeDef.FullName + ".decompiled.cs");
Logger.LogString(2, " Decompiling {0} into {1}...", typeDef.FullName, filepath);
var decompilerContext = new DecompilerContext(module);
using (var writer = new StreamWriter(filepath))
{
var output = new PlainTextOutput(writer);
var codeDomBuilder = new AstBuilder(decompilerContext);
codeDomBuilder.AddType(typeDef);
codeDomBuilder.GenerateCode(output);
}
Logger.LogLine(2, "done");
}
resourcesToPrune.Add(resource);
}
if (!KeepXamlResources)
{
if (resourcesToPrune.Any())
Logger.LogLine(2, " Removing compiled xaml resources");
foreach (var resource in resourcesToPrune)
{
Logger.LogString(2, " Removing {0}... ", resource.Name);
module.Resources.Remove(resource);
Logger.LogLine(2, "done");
}
}
Logger.LogLine(2, "");
}
if (!hasCompiledXamlResources)
{
Logger.LogLine(1, "No compiled resources. Skipping writing assembly.");
return success;
}
Logger.LogString(1, "Writing the assembly... ");
try
{
assemblyDefinition.Write(Assembly, new WriterParameters
{
WriteSymbols = DebugSymbols
});
Logger.LogLine(1, "done.");
}
catch (Exception e)
{
Logger.LogLine(1, "failed.");
Logger.LogException(null, null, null, null, e);
thrownExceptions?.Add(e);
Logger.LogLine(4, e.StackTrace);
success = false;
}
return success;
}
bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
{
try {
var body = new MethodBody(initComp);
var il = body.GetILProcessor();
il.Emit(OpCodes.Nop);
if (initCompRuntime != null) {
// Generating branching code for the Previewer
// IL_0007: call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
// IL_000c: brfalse IL_0031
// IL_0011: call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
// IL_0016: ldarg.0
// IL_0017: call instance class [mscorlib]System.Type object::GetType()
// IL_001c: callvirt instance !1 class [mscorlib]System.Func`2<class [mscorlib]System.Type, string>::Invoke(!0)
// IL_0021: brfalse IL_0031
// IL_0026: ldarg.0
// IL_0027: call instance void class Xamarin.Forms.Xaml.UnitTests.XamlLoaderGetXamlForTypeTests::__InitComponentRuntime()
// IL_002c: ret
// IL_0031: nop
var nop = Instruction.Create(OpCodes.Nop);
var getXamlFileProvider = body.Method.Module.Import(body.Method.Module.Import(typeof(Xamarin.Forms.Xaml.Internals.XamlLoader))
.Resolve()
.Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider")
.GetMethod);
il.Emit(OpCodes.Call, getXamlFileProvider);
il.Emit(OpCodes.Brfalse, nop);
il.Emit(OpCodes.Call, getXamlFileProvider);
il.Emit(OpCodes.Ldarg_0);
var getType = body.Method.Module.Import(body.Method.Module.Import(typeof(object))
.Resolve()
.Methods.FirstOrDefault(md => md.Name == "GetType"));
il.Emit(OpCodes.Call, getType);
var func = body.Method.Module.Import(body.Method.Module.Import(typeof(Func<Type, string>))
.Resolve()
.Methods.FirstOrDefault(md => md.Name == "Invoke"));
func = func.ResolveGenericParameters(body.Method.Module.Import(typeof(Func<Type, string>)), body.Method.Module);
il.Emit(OpCodes.Callvirt, func);
il.Emit(OpCodes.Brfalse, nop);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, initCompRuntime);
il.Emit(OpCodes.Ret);
il.Append(nop);
}
var visitorContext = new ILContext(il, body, body.Method.Module);
rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null);
rootnode.Accept(new PruneIgnoredNodesVisitor(), null);
rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
rootnode.Accept(new SetFieldVisitor(visitorContext), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);
il.Emit(OpCodes.Ret);
initComp.Body = body;
exception = null;
return true;
} catch (Exception e) {
exception = e;
return false;
}
}
}
}
|