6813ba4497429b8817667a3d248e25006797a032
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / XamlBuild / XamlGTask.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.IO;
19 using System.Xml;
20 using System.ComponentModel;
21 using Microsoft.Build.Framework;
22 using Microsoft.Build.Utilities;
23
24 namespace Tizen.NUI.Xaml.Build.Tasks
25 {
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class XamlGTask : Task
28     {
29         [Required]
30         public ITaskItem[] XamlFiles { get; set; }
31
32         [Required]
33         public ITaskItem[] OutputFiles { get; set; }
34
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; }
41
42         private void PrintParam(string logFileName, string log)
43         {
44             FileStream stream = null;
45             if (false == File.Exists(logFileName))
46             {
47                 stream = File.Create(logFileName);
48             }
49             else
50             {
51                 stream = File.Open(logFileName, FileMode.Append);
52             }
53
54             byte[] buffer = System.Text.Encoding.Default.GetBytes(log + "\n");
55             stream.Write(buffer, 0, buffer.Length);
56             stream.Close();
57         }
58
59         public override bool Execute()
60         {
61             if (true == PrintReferenceAssemblies)
62             {
63                 PrintParam(@"XamlG_Log.txt", "ReferencePath is " + ReferencePath);
64             }
65
66             //PrintParam(@"G:\XamlG_Log.txt", "ReferencePath is " + ReferencePath);
67             bool success = true;
68             //Log.LogMessage(MessageImportance.Normal, "Generating code behind for XAML files");
69
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");
73                 return true;
74             }
75
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");
78                 return false;
79             }
80
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('/', '\\');
88
89                 var generator = new XamlGenerator(xamlFile, Language, AssemblyName, outputFile, ReferencePath, Log);
90                 generator.AddXamlCompilationAttribute = AddXamlCompilationAttribute;
91                 generator.ReferencePath = ReferencePath;
92
93                 try {
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);
97                     }
98                 }
99                 catch (XmlException xe) {
100                     Log.LogError(null, null, null, xamlFile.ItemSpec, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source);
101                     success = false;
102                 }
103                 catch (Exception e) {
104                     Log.LogError(null, null, null, xamlFile.ItemSpec, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source);
105                     success = false;
106                 }
107             }
108
109             return success;
110         }
111     }
112 }
113