1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 using System;
5 using System.IO;
6 using System.Diagnostics;
7 using Microsoft.Build.Framework;
8 using Microsoft.Build.Utilities;
9 using System.Globalization;
10 using System.Resources;
11 
12 namespace Microsoft.Build.Tasks
13 {
14     /// <summary>
15     /// Task that simply emits a message. Importance defaults to high if not specified.
16     /// </summary>
17     public sealed class Message : TaskExtension
18     {
19         private string _text;
20 
21         /// <summary>
22         /// Text to log.
23         /// </summary>
24         public string Text
25         {
26             get
27             {
28                 return _text;
29             }
30 
31             set
32             {
33                 _text = value;
34             }
35         }
36 
37         private string _importance;
38 
39         /// <summary>
40         /// Importance: high, normal, low (default normal)
41         /// </summary>
42         public string Importance
43         {
44             get
45             {
46                 return _importance;
47             }
48 
49             set
50             {
51                 _importance = value;
52             }
53         }
54 
55         private string _code;
56 
57         /// <summary>
58         /// Message code
59         /// </summary>
60         public string Code
61         {
62             get
63             {
64                 return _code;
65             }
66             set
67             {
68                 _code = value;
69             }
70         }
71 
72         private string _file;
73 
74         /// <summary>
75         /// Relevant file if any.
76         /// If none is provided and this is a critical message, the file containing the Message
77         /// task will be used.
78         /// </summary>
79         public string File
80         {
81             get
82             {
83                 return _file;
84             }
85             set
86             {
87                 _file = value;
88             }
89         }
90 
91         private string _helpKeyword;
92 
93         /// <summary>
94         /// Message help keyword
95         /// </summary>
96         public string HelpKeyword
97         {
98             get
99             {
100                 return _helpKeyword;
101             }
102             set
103             {
104                 _helpKeyword = value;
105             }
106         }
107 
108         private bool _isCritical;
109 
110         /// <summary>
111         /// Indicates if this is a critical message
112         /// </summary>
113         public bool IsCritical
114         {
115             get
116             {
117                 return _isCritical;
118             }
119             set
120             {
121                 _isCritical = value;
122             }
123         }
124 
Execute()125         public override bool Execute()
126         {
127             MessageImportance messageImportance;
128 
129             if ((Importance == null) || (Importance.Length == 0))
130             {
131                 messageImportance = MessageImportance.Normal;
132             }
133             else
134             {
135                 try
136                 {
137                     // Parse the raw importance string into a strongly typed enumeration.
138                     messageImportance = (MessageImportance)Enum.Parse(typeof(MessageImportance), Importance, true /* case-insensitive */);
139                 }
140                 catch (ArgumentException)
141                 {
142                     Log.LogErrorWithCodeFromResources("Message.InvalidImportance", Importance);
143                     return false;
144                 }
145             }
146 
147             if (Text != null)
148             {
149                 if (IsCritical)
150                 {
151                     Log.LogCriticalMessage(null, Code, HelpKeyword, File, 0, 0, 0, 0, "{0}", Text);
152                 }
153                 else
154                 {
155                     if (File != null)
156                     {
157                         Log.LogMessage(null, Code, HelpKeyword, File, 0, 0, 0, 0, messageImportance, "{0}", Text);
158                     }
159                     else
160                     {
161                         Log.LogMessage(messageImportance, "{0}", Text);
162                     }
163                 }
164             }
165 
166             return true;
167         }
168     }
169 }
170