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.
20 using System.ComponentModel;
21 using Microsoft.Build.Framework;
22 using Microsoft.Build.Utilities;
24 namespace Tizen.NUI.Xaml.Build.Tasks
26 [EditorBrowsable(EditorBrowsableState.Never)]
27 public class XamlGTask : Task
30 public ITaskItem[] XamlFiles { get; set; }
33 public ITaskItem[] OutputFiles { get; set; }
35 public string Language { get; set; }
36 public string AssemblyName { get; set; }
37 public string DependencyPaths { get; set; }
38 public string ReferencePath { get; set; }
39 public bool AddXamlCompilationAttribute { get; set; }
40 public bool PrintReferenceAssemblies { get; set; }
42 private void PrintParam(string logFileName, string log)
44 FileStream stream = null;
45 if (false == File.Exists(logFileName))
47 stream = File.Create(logFileName);
51 stream = File.Open(logFileName, FileMode.Append);
54 byte[] buffer = System.Text.Encoding.Default.GetBytes(log + "\n");
55 stream.Write(buffer, 0, buffer.Length);
59 public override bool Execute()
61 if (true == PrintReferenceAssemblies)
63 PrintParam(@"XamlG_Log.txt", "ReferencePath is " + ReferencePath);
66 //PrintParam(@"G:\XamlG_Log.txt", "ReferencePath is " + ReferencePath);
68 //Log.LogMessage(MessageImportance.Normal, "Generating code behind for XAML files");
70 //NOTE: should not happen due to [Required], but there appears to be a place this is class is called directly
71 if (XamlFiles == null || OutputFiles == null) {
72 //Log.LogMessage("Skipping XamlG");
76 if (XamlFiles.Length != OutputFiles.Length) {
77 Log.LogError("\"{2}\" refers to {0} item(s), and \"{3}\" refers to {1} item(s). They must have the same number of items.", XamlFiles.Length, OutputFiles.Length, "XamlFiles", "OutputFiles");
81 for (int i = 0; i < XamlFiles.Length; i++) {
82 var xamlFile = XamlFiles[i];
83 var outputFile = OutputFiles[i].ItemSpec;
84 if (System.IO.Path.DirectorySeparatorChar == '/' && outputFile.Contains(@"\"))
85 outputFile = outputFile.Replace('\\','/');
86 else if (System.IO.Path.DirectorySeparatorChar == '\\' && outputFile.Contains(@"/"))
87 outputFile = outputFile.Replace('/', '\\');
89 var generator = new XamlGenerator(xamlFile, Language, AssemblyName, outputFile, ReferencePath, Log);
90 generator.AddXamlCompilationAttribute = AddXamlCompilationAttribute;
91 generator.ReferencePath = ReferencePath;
94 if (!generator.Execute()) {
95 //If Execute() fails, the file still needs to exist because it is added to the <Compile/> ItemGroup
96 File.WriteAllText (outputFile, string.Empty);
99 catch (XmlException xe) {
100 Log.LogError(null, null, null, xamlFile.ItemSpec, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source);
103 catch (Exception e) {
104 Log.LogError(null, null, null, xamlFile.ItemSpec, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source);