2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Diagnostics;
23 using Microsoft.Build.Framework;
24 using Microsoft.Build.Utilities;
28 namespace Tizen.NUI.Xaml.Build.Tasks
30 [LoadInSeparateAppDomain]
31 public abstract class XamlTask : MarshalByRefObject, ITask
33 internal const string nuiAssemblyName = "Tizen.NUI";
34 internal const string nuiNameSpace = "Tizen.NUI";
36 internal const string xamlAssemblyName = "Tizen.NUI";
37 internal const string xamlNameSpace = "Tizen.NUI.Xaml";
39 internal const string bindingAssemblyName = "Tizen.NUI";
40 internal const string bindingNameSpace = "Tizen.NUI.Binding";
41 internal const string bindingInternalNameSpace = "Tizen.NUI.Binding.Internals";
44 public string Assembly { get; set; }
45 public string DependencyPaths { get; set; }
46 public string ReferencePath { get; set; }
47 [Obsolete("this is no longer used")]
48 public int Verbosity { get; set; }
49 public bool DebugSymbols { get; set; }
50 public string DebugType { get; set; }
51 public string XamlFilePath { get; set; }
52 public bool NeedDebug { get; set; } = false;
54 protected TaskLoggingHelper LoggingHelper { get; }
58 LoggingHelper = new TaskLoggingHelper(this);
61 public IBuildEngine BuildEngine { get; set; }
62 public ITaskHost HostObject { get; set; }
67 return Execute(out _);
70 public abstract bool Execute(out IList<Exception> thrownExceptions);
72 internal static ILRootNode ParseXaml(Stream stream, TypeReference typeReference)
74 ILRootNode rootnode = null;
75 using (var reader = XmlReader.Create(stream)) {
76 while (reader.Read()) {
78 if (reader.NodeType == XmlNodeType.Whitespace)
80 if (reader.NodeType != XmlNodeType.Element) {
81 Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
86 rootnode = new ILRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), typeReference, reader as IXmlNamespaceResolver), reader);
93 internal static string GetResourceIdForPath(ModuleDefinition module, string path)
95 foreach (var ca in module.GetCustomAttributes())
97 if (!TypeRefComparer.Default.Equals(ca.AttributeType, module.ImportReference((xamlAssemblyName, xamlNameSpace, "XamlResourceIdAttribute"))))
99 if (ca.ConstructorArguments[1].Value as string != path)
101 return ca.ConstructorArguments[0].Value as string;
106 internal static string GetPathForType(ModuleDefinition module, TypeReference type)
108 foreach (var ca in type.Module.GetCustomAttributes())
110 if (!TypeRefComparer.Default.Equals(ca.AttributeType, module.ImportReference((xamlAssemblyName, xamlNameSpace, "XamlResourceIdAttribute"))))
112 if (!TypeRefComparer.Default.Equals(ca.ConstructorArguments[2].Value as TypeReference, type))
114 return ca.ConstructorArguments[1].Value as string;
119 internal static IList<XmlnsDefinitionAttribute> s_xmlnsDefinitions
122 } = new List<XmlnsDefinitionAttribute>();
125 public static class CecilExtensions
127 public static bool IsResourceDictionaryXaml(this EmbeddedResource resource, ModuleDefinition module, out string classname)
131 if (!resource.Name.EndsWith(".xaml", StringComparison.InvariantCulture))
134 using (var resourceStream = resource.GetResourceStream())
136 var xmlDoc = new XmlDocument();
137 xmlDoc.Load(resourceStream);
139 var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
141 var root = xmlDoc.SelectSingleNode("/*", nsmgr);
145 var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ??
146 root.Attributes["Class", XamlParser.X2009Uri];
147 if (rootClass != null)
149 classname = rootClass.Value;
153 if ("ResourceDictionary" == root.Name)
155 classname = "Tizen.NUI.Binding.ResourceDictionary";
163 public static bool IsXaml(this EmbeddedResource resource, ModuleDefinition module, out string classname)
166 if (!resource.Name.EndsWith(".xaml", StringComparison.InvariantCulture))
169 using (var resourceStream = resource.GetResourceStream()) {
170 var xmlDoc = new XmlDocument();
171 xmlDoc.Load(resourceStream);
173 var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
175 var root = xmlDoc.SelectSingleNode("/*", nsmgr);
179 var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ??
180 root.Attributes["Class", XamlParser.X2009Uri];
181 if (rootClass != null) {
182 classname = rootClass.Value;
186 //no x:Class, but it might be a RD without x:Class and with <?xaml-comp compile="true" ?>
187 //in that case, it has a XamlResourceIdAttribute
188 var typeRef = GetTypeForResourceId(module, resource.Name);
189 if (typeRef != null) {
190 classname = typeRef.FullName;
198 public static bool IsXaml(Stream resourceStream, ModuleDefinition module, out string classname)
202 var xmlDoc = new XmlDocument();
203 xmlDoc.Load(resourceStream);
205 var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
207 var root = xmlDoc.SelectSingleNode("/*", nsmgr);
211 var rootClass = root.Attributes["Class", XamlParser.X2006Uri] ??
212 root.Attributes["Class", XamlParser.X2009Uri];
213 if (rootClass != null)
215 classname = rootClass.Value;
222 static TypeReference GetTypeForResourceId(ModuleDefinition module, string resourceId)
224 foreach (var ca in module.GetCustomAttributes()) {
225 if (!TypeRefComparer.Default.Equals(ca.AttributeType, module.ImportReference((XamlTask.xamlAssemblyName, XamlTask.xamlNameSpace, "XamlResourceIdAttribute"))))
227 if (ca.ConstructorArguments[0].Value as string != resourceId)
229 return ca.ConstructorArguments[2].Value as TypeReference;