This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathXamlGenerator.cs
457 lines (395 loc) · 15.5 KB
/
XamlGenerator.cs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CSharp;
using Xamarin.Forms.Xaml;
using Xamarin.Forms.Internals;
using Mono.Cecil;
using IOPath = System.IO.Path;
namespace Xamarin.Forms.Build.Tasks
{
class XamlGenerator
{
internal XamlGenerator()
{
}
public XamlGenerator(
ITaskItem taskItem,
string language,
string assemblyName,
string outputFile,
string references,
TaskLoggingHelper logger)
: this(
taskItem.ItemSpec,
language,
taskItem.GetMetadata("ManifestResourceName"),
taskItem.GetMetadata("TargetPath"),
assemblyName,
outputFile,
references,
logger)
{
}
List<XmlnsDefinitionAttribute> _xmlnsDefinitions;
Dictionary<string, ModuleDefinition> _xmlnsModules;
internal static CodeDomProvider Provider = new CSharpCodeProvider();
public string XamlFile { get; }
public string Language { get; }
public string ResourceId { get; }
public string TargetPath { get; }
public string AssemblyName { get; }
public string OutputFile { get; }
public TaskLoggingHelper Logger { get; }
public string RootClrNamespace { get; private set; }
public string RootType { get; private set; }
public string References { get; }
bool GenerateDefaultCtor { get; set; }
bool AddXamlCompilationAttribute { get; set; }
bool HideFromIntellisense { get; set; }
bool XamlResourceIdOnly { get; set; }
internal IEnumerable<CodeMemberField> NamedFields { get; set; }
internal CodeTypeReference BaseType { get; set; }
public XamlGenerator(
string xamlFile,
string language,
string resourceId,
string targetPath,
string assemblyName,
string outputFile,
string references,
TaskLoggingHelper logger = null)
{
XamlFile = xamlFile;
Language = language;
ResourceId = resourceId;
TargetPath = targetPath;
AssemblyName = assemblyName;
OutputFile = outputFile;
References = references;
Logger = logger;
}
//returns true if a file is generated
public bool Execute()
{
Logger?.LogMessage(MessageImportance.Low, "Source: {0}", XamlFile);
Logger?.LogMessage(MessageImportance.Low, " Language: {0}", Language);
Logger?.LogMessage(MessageImportance.Low, " ResourceID: {0}", ResourceId);
Logger?.LogMessage(MessageImportance.Low, " TargetPath: {0}", TargetPath);
Logger?.LogMessage(MessageImportance.Low, " AssemblyName: {0}", AssemblyName);
Logger?.LogMessage(MessageImportance.Low, " OutputFile {0}", OutputFile);
using (StreamReader reader = File.OpenText(XamlFile))
if (!ParseXaml(reader))
return false;
try
{
GenerateCode();
}
finally
{
CleanupXmlnsAssemblyData();
}
return true;
}
internal bool ParseXaml(TextReader xaml)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(xaml);
// if the following xml processing instruction is present
//
// <?xaml-comp compile="true" ?>
//
// we will generate a xaml.g.cs file with the default ctor calling InitializeComponent, and a XamlCompilation attribute
var hasXamlCompilationProcessingInstruction = GetXamlCompilationProcessingInstruction(xmlDoc);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("__f__", XamlParser.XFUri);
var root = xmlDoc.SelectSingleNode("/*", nsmgr);
if (root == null) {
Logger?.LogMessage(MessageImportance.Low, " No root node found");
return false;
}
foreach (XmlAttribute attr in root.Attributes) {
if (attr.Name == "xmlns")
nsmgr.AddNamespace("", attr.Value); //Add default xmlns
if (attr.Prefix != "xmlns")
continue;
nsmgr.AddNamespace(attr.LocalName, attr.Value);
}
var rootClass = root.Attributes["Class", XamlParser.X2006Uri]
?? root.Attributes["Class", XamlParser.X2009Uri];
if (rootClass != null) {
string rootType, rootNs, rootAsm, targetPlatform;
XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootNs, out rootAsm, out targetPlatform);
RootType = rootType;
RootClrNamespace = rootNs;
}
else if (hasXamlCompilationProcessingInstruction) {
RootClrNamespace = "__XamlGeneratedCode__";
RootType = $"__Type{Guid.NewGuid().ToString("N")}";
GenerateDefaultCtor = true;
AddXamlCompilationAttribute = true;
HideFromIntellisense = true;
}
else { // rootClass == null && !hasXamlCompilationProcessingInstruction) {
XamlResourceIdOnly = true; //only generate the XamlResourceId assembly attribute
return true;
}
NamedFields = GetCodeMemberFields(root, nsmgr);
var typeArguments = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri);
var xmlType = new XmlType(root.NamespaceURI, root.LocalName, typeArguments != null ? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null) : null);
BaseType = GetType(xmlType, root.GetNamespaceOfPrefix);
return true;
}
static Version version = typeof(XamlGenerator).Assembly.GetName().Version;
static CodeAttributeDeclaration GeneratedCodeAttrDecl =>
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(GeneratedCodeAttribute).FullName}"),
new CodeAttributeArgument(new CodePrimitiveExpression("Xamarin.Forms.Build.Tasks.XamlG")),
new CodeAttributeArgument(new CodePrimitiveExpression($"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}")));
void GenerateCode()
{
//Create the target directory if required
Directory.CreateDirectory(IOPath.GetDirectoryName(OutputFile));
var ccu = new CodeCompileUnit();
ccu.AssemblyCustomAttributes.Add(
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(XamlResourceIdAttribute).FullName}"),
new CodeAttributeArgument(new CodePrimitiveExpression(ResourceId)),
new CodeAttributeArgument(new CodePrimitiveExpression(TargetPath.Replace('\\', '/'))), //use forward slashes, paths are uris-like
new CodeAttributeArgument(RootType == null ? (CodeExpression)new CodePrimitiveExpression(null) : new CodeTypeOfExpression($"global::{RootClrNamespace}.{RootType}"))
));
if (XamlResourceIdOnly)
goto writeAndExit;
if (RootType == null)
throw new Exception("Something went wrong while executing XamlG");
var declNs = new CodeNamespace(RootClrNamespace);
ccu.Namespaces.Add(declNs);
var declType = new CodeTypeDeclaration(RootType) {
IsPartial = true,
CustomAttributes = {
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(XamlFilePathAttribute).FullName}"),
new CodeAttributeArgument(new CodePrimitiveExpression(XamlFile))),
}
};
if (AddXamlCompilationAttribute)
declType.CustomAttributes.Add(
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(XamlCompilationAttribute).FullName}"),
new CodeAttributeArgument(new CodeSnippetExpression($"global::{typeof(XamlCompilationOptions).FullName}.Compile"))));
if (HideFromIntellisense)
declType.CustomAttributes.Add(
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(System.ComponentModel.EditorBrowsableAttribute).FullName}"),
new CodeAttributeArgument(new CodeSnippetExpression($"global::{typeof(System.ComponentModel.EditorBrowsableState).FullName}.{nameof(System.ComponentModel.EditorBrowsableState.Never)}"))));
declType.BaseTypes.Add(BaseType);
declNs.Types.Add(declType);
//Create a default ctor calling InitializeComponent
if (GenerateDefaultCtor) {
var ctor = new CodeConstructor {
Attributes = MemberAttributes.Public,
CustomAttributes = { GeneratedCodeAttrDecl },
Statements = {
new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InitializeComponent")
}
};
declType.Members.Add(ctor);
}
//Create InitializeComponent()
var initcomp = new CodeMemberMethod {
Name = "InitializeComponent",
CustomAttributes = { GeneratedCodeAttrDecl }
};
declType.Members.Add(initcomp);
//Create and initialize fields
initcomp.Statements.Add(new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression(new CodeTypeReference($"global::{typeof(Extensions).FullName}")),
"LoadFromXaml", new CodeThisReferenceExpression(), new CodeTypeOfExpression(declType.Name)));
foreach (var namedField in NamedFields) {
declType.Members.Add(namedField);
var find_invoke = new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(new CodeTypeReference($"global::{typeof(NameScopeExtensions).FullName}")),
"FindByName", namedField.Type),
new CodeThisReferenceExpression(), new CodePrimitiveExpression(namedField.Name));
CodeAssignStatement assign = new CodeAssignStatement(
new CodeVariableReferenceExpression(namedField.Name), find_invoke);
initcomp.Statements.Add(assign);
}
writeAndExit:
//write the result
using (var writer = new StreamWriter(OutputFile))
Provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions());
}
IEnumerable<CodeMemberField> GetCodeMemberFields(XmlNode root, XmlNamespaceManager nsmgr)
{
var xPrefix = nsmgr.LookupPrefix(XamlParser.X2006Uri) ?? nsmgr.LookupPrefix(XamlParser.X2009Uri);
if (xPrefix == null)
yield break;
XmlNodeList names =
root.SelectNodes(
"//*[@" + xPrefix + ":Name" +
"][not(ancestor:: __f__:DataTemplate) and not(ancestor:: __f__:ControlTemplate) and not(ancestor:: __f__:Style) and not(ancestor:: __f__:VisualStateManager.VisualStateGroups)]", nsmgr);
foreach (XmlNode node in names) {
var name = GetAttributeValue(node, "Name", XamlParser.X2006Uri, XamlParser.X2009Uri);
var typeArguments = GetAttributeValue(node, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri);
var fieldModifier = GetAttributeValue(node, "FieldModifier", XamlParser.X2006Uri, XamlParser.X2009Uri);
var xmlType = new XmlType(node.NamespaceURI, node.LocalName,
typeArguments != null
? TypeArgumentsParser.ParseExpression(typeArguments, nsmgr, null)
: null);
var access = MemberAttributes.Private;
if (fieldModifier != null) {
switch (fieldModifier.ToLowerInvariant()) {
default:
case "private":
access = MemberAttributes.Private;
break;
case "public":
access = MemberAttributes.Public;
break;
case "protected":
access = MemberAttributes.Family;
break;
case "internal":
case "notpublic": //WPF syntax
access = MemberAttributes.Assembly;
break;
}
}
yield return new CodeMemberField {
Name = name,
Type = GetType(xmlType, node.GetNamespaceOfPrefix),
Attributes = access,
CustomAttributes = { GeneratedCodeAttrDecl }
};
}
}
static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
{
var instruction = xmlDoc.SelectSingleNode("processing-instruction('xaml-comp')") as XmlProcessingInstruction;
if (instruction == null)
return false;
var parts = instruction.Data.Split(' ', '=');
string compileValue = null;
var indexOfCompile = Array.IndexOf(parts, "compile");
if (indexOfCompile != -1)
compileValue = parts[indexOfCompile + 1].Trim('"', '\'');
return compileValue.Equals("true", StringComparison.InvariantCultureIgnoreCase);
}
CodeTypeReference GetType(XmlType xmlType,
Func<string, string> getNamespaceOfPrefix = null)
{
CodeTypeReference returnType = null;
var ns = GetClrNamespace(xmlType.NamespaceUri);
if (ns == null) {
// It's an external, non-built-in namespace URL.
returnType = GetCustomNamespaceUrlType(xmlType);
}
else {
var type = xmlType.Name;
type = $"{ns}.{type}";
if (xmlType.TypeArguments != null)
type = $"{type}`{xmlType.TypeArguments.Count}";
returnType = new CodeTypeReference(type);
}
returnType.Options |= CodeTypeReferenceOptions.GlobalReference;
if (xmlType.TypeArguments != null)
foreach (var typeArg in xmlType.TypeArguments)
returnType.TypeArguments.Add(GetType(typeArg, getNamespaceOfPrefix));
return returnType;
}
static string GetClrNamespace(string namespaceuri)
{
if (namespaceuri == XamlParser.X2009Uri)
return "System";
if (namespaceuri != XamlParser.X2006Uri &&
!namespaceuri.StartsWith("clr-namespace", StringComparison.InvariantCulture) &&
!namespaceuri.StartsWith("using:", StringComparison.InvariantCulture))
return null;
return XmlnsHelper.ParseNamespaceFromXmlns(namespaceuri);
}
static string GetAttributeValue(XmlNode node, string localName, params string[] namespaceURIs)
{
if (node == null)
throw new ArgumentNullException(nameof(node));
if (localName == null)
throw new ArgumentNullException(nameof(localName));
if (namespaceURIs == null)
throw new ArgumentNullException(nameof(namespaceURIs));
foreach (var namespaceURI in namespaceURIs) {
var attr = node.Attributes[localName, namespaceURI];
if (attr == null)
continue;
return attr.Value;
}
return null;
}
void GatherXmlnsDefinitionAttributes()
{
_xmlnsDefinitions = new List<XmlnsDefinitionAttribute>();
_xmlnsModules = new Dictionary<string, ModuleDefinition>();
var paths = References?.Split(';').Distinct().ToList() ?? new List<string>();
//Load xmlnsdef from Core and Xaml
paths.Add(typeof(Label).Assembly.Location);
paths.Add(typeof(Xamarin.Forms.Xaml.Extensions).Assembly.Location);
foreach (var path in paths) {
string asmName = IOPath.GetFileName(path);
if (AssemblyIsSystem(asmName))
// Skip the myriad "System." assemblies and others
continue;
using (var asmDef = AssemblyDefinition.ReadAssembly(path)) {
foreach (var ca in asmDef.CustomAttributes) {
if (ca.AttributeType.FullName == typeof(XmlnsDefinitionAttribute).FullName) {
_xmlnsDefinitions.Add(ca.GetXmlnsDefinition(asmDef));
_xmlnsModules[asmDef.FullName] = asmDef.MainModule;
}
}
}
}
}
bool AssemblyIsSystem(string name)
{
if (name.StartsWith("System.Maui", StringComparison.CurrentCultureIgnoreCase))
return false;
if (name.StartsWith("System.", StringComparison.CurrentCultureIgnoreCase))
return true;
else if (name.Equals("mscorlib.dll", StringComparison.CurrentCultureIgnoreCase))
return true;
else if (name.Equals("netstandard.dll", StringComparison.CurrentCultureIgnoreCase))
return true;
else
return false;
}
CodeTypeReference GetCustomNamespaceUrlType(XmlType xmlType)
{
if (_xmlnsDefinitions == null)
GatherXmlnsDefinitionAttributes();
IList<XamlLoader.FallbackTypeInfo> potentialTypes;
TypeReference typeReference = xmlType.GetTypeReference<TypeReference>(
_xmlnsDefinitions,
null,
(typeInfo) =>
{
if (typeInfo.AssemblyName == null || !_xmlnsModules.TryGetValue(typeInfo.AssemblyName, out ModuleDefinition module))
return null;
string typeName = typeInfo.TypeName.Replace('+', '/'); //Nested types
string fullName = $"{typeInfo.ClrNamespace}.{typeInfo.TypeName}";
return module.Types.Where(t => t.FullName == fullName).FirstOrDefault();
},
out potentialTypes);
if (typeReference == null)
throw new BuildException(BuildExceptionCode.TypeResolution, null, null, xmlType.Name);
return new CodeTypeReference(typeReference.FullName);
}
void CleanupXmlnsAssemblyData()
{
if ( _xmlnsModules != null ) {
foreach (var moduleDef in _xmlnsModules.Values) {
moduleDef.Dispose();
}
}
}
}
}