2 using System.Collections.Generic;
3 using System.Diagnostics;
7 using Microsoft.Build.Framework;
8 using Microsoft.Build.Utilities;
12 using Xamarin.Forms.Xaml;
16 using System.ComponentModel;
18 namespace Xamarin.Forms.Build.Tasks
20 [LoadInSeparateAppDomain]
21 public abstract class XamlTask : MarshalByRefObject, ITask
24 public string Assembly { get; set; }
25 public string DependencyPaths { get; set; }
26 public string ReferencePath { get; set; }
27 [Obsolete("this is no longer used")]
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public int Verbosity { get; set; }
30 public bool DebugSymbols { get; set; }
31 public string DebugType { get; set; }
33 protected TaskLoggingHelper LoggingHelper { get; }
37 LoggingHelper = new TaskLoggingHelper(this);
40 public IBuildEngine BuildEngine { get; set; }
41 public ITaskHost HostObject { get; set; }
46 return Execute(out _);
49 public abstract bool Execute(out IList<Exception> thrownExceptions);
51 internal static ILRootNode ParseXaml(Stream stream, TypeReference typeReference)
53 ILRootNode rootnode = null;
54 using (var reader = XmlReader.Create(stream)) {
55 while (reader.Read()) {
57 if (reader.NodeType == XmlNodeType.Whitespace)
59 if (reader.NodeType != XmlNodeType.Element) {
60 Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
65 rootnode = new ILRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), typeReference, reader as IXmlNamespaceResolver, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition), reader);
73 static class CecilExtensions
75 public static bool IsXaml(this EmbeddedResource resource, ModuleDefinition module, out string classname)
78 if (!resource.Name.EndsWith(".xaml", StringComparison.InvariantCulture))
81 using (var resourceStream = resource.GetResourceStream()) {
82 var xmlDoc = new XmlDocument();
83 xmlDoc.Load(resourceStream);
85 var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
87 var root = xmlDoc.SelectSingleNode("/*", nsmgr);
91 var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ??
92 root.Attributes["Class", XamlParser.X2009Uri];
93 if (rootClass != null) {
94 classname = rootClass.Value;
98 //no x:Class, but it might be a RD without x:Class and with <?xaml-comp compile="true" ?>
99 //in that case, it has a XamlResourceIdAttribute
100 var typeRef = GetTypeForResourceId(module, resource.Name);
101 if (typeRef != null) {
102 classname = typeRef.FullName;
110 static TypeReference GetTypeForResourceId(ModuleDefinition module, string resourceId)
112 foreach (var ca in module.GetCustomAttributes()) {
113 if (!TypeRefComparer.Default.Equals(ca.AttributeType, module.ImportReference(("XSF", "Xamarin.Forms.Xaml", "XamlResourceIdAttribute"))))
115 if (ca.ConstructorArguments[0].Value as string != resourceId)
117 return ca.ConstructorArguments[2].Value as TypeReference;