[xbuild] Don't show the deprecation notice for minimal or quiet logging
[mono.git] / mcs / tools / xbuild / LoggerInfo.cs
1 //
2 // LoggerInfo.cs: Contains information about logger parameters.
3 //
4 // Authors:
5 //   Craig Sutherland (cj.sutherland(at)xtra.co.nz)
6 //   Daniel Nauck (dna(at)mono-project.de)
7 //
8 // (C) 2009 Craig Sutherland, Daniel Nauck
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29
30 using System;
31 using System.Globalization;
32 using System.IO;
33 using System.Linq;
34 using System.Reflection;
35 using System.Text.RegularExpressions;
36 using Mono.XBuild.Framework;
37
38 namespace Mono.XBuild.CommandLine
39 {
40         internal class LoggerInfo : AssemblyLoadInfo
41         {
42                 static readonly Regex assemblyInfoRegEx = new Regex(@"(?<assemblyName>[\w\.]+)(,\s?Version=(?<assemblyVersion>\d+\.\d+\.\d+\.\d+))?(,\s?Culture=(?<assemblyCulture>\w+))?(,\s?PublicKeyToken=(?<publicKeyToken>\w+))?",
43                         RegexOptions.Compiled | RegexOptions.CultureInvariant);
44
45                 string loggerAssemblyName;
46                 string loggerType;
47                 string loggerArgs;
48
49                 string assemblyInfoName;
50                 string assemblyInfoVersion;
51                 string assemblyInfoCulture;
52                 string assemblyInfoPublicKeyToken;
53
54                 internal LoggerInfo (string value)
55                 {
56                         if (!Parse (value))
57                                 return;
58
59                         if (string.IsNullOrEmpty (loggerType))
60                                 loggerType = GetLoggerTypeName (loggerAssemblyName);
61
62                         if (assemblyInfoName != null)
63                                 SetAssemblyName (LoadInfoType.AssemblyName, null, assemblyInfoName, assemblyInfoVersion, assemblyInfoCulture, assemblyInfoPublicKeyToken, loggerType);
64
65                         else
66                                 SetAssemblyName (LoadInfoType.AssemblyFilename, loggerAssemblyName, null, null, null, null, loggerType);
67                 }
68
69                 internal string Parameters {
70                         get { return loggerArgs; }
71                 }
72
73                 static string GetLoggerTypeName (string assemblyName)
74                 {
75                         Assembly loggerAssembly = null;
76
77                         // try to load assembly that contains the logger
78                         if (HasAssemblyInfo (assemblyName))
79                                 loggerAssembly = Assembly.Load (assemblyName);
80                         else if (File.Exists (assemblyName))
81                                 loggerAssembly = Assembly.LoadFile (assemblyName);
82
83                         if (loggerAssembly == null)
84                                 return null;
85
86                         // search for a class thats implement ILogger
87                         var loggerClass = (from t in loggerAssembly.GetTypes ()
88                                                 where t.IsClass &&
89                                                 t.GetInterface ("Microsoft.Build.Framework.ILogger") != null &&
90                                                 t.IsPublic
91                                                 select t).FirstOrDefault ();
92
93                         if (loggerClass != null)
94                                 return loggerClass.FullName;
95
96                         return null;
97                 }
98
99                 bool Parse (string arg)
100                 {
101                         // Wipe all the existing values, just in case
102                         loggerAssemblyName = null;
103                         loggerType = null;
104                         loggerArgs = null;
105                         assemblyInfoName = null;
106                         assemblyInfoVersion = null;
107                         assemblyInfoCulture = null;
108                         assemblyInfoPublicKeyToken = null;
109
110                         if (string.IsNullOrEmpty (arg))
111                                 return false;
112
113                         string [] parts = arg.Split (new char [] {':'}, 2);
114                         if (parts.Length != 2)
115                                 return false;
116
117                         if (string.Compare ("/l", parts [0], StringComparison.OrdinalIgnoreCase) != 0 &&
118                                 string.Compare ("/logger", parts [0], StringComparison.OrdinalIgnoreCase) != 0)
119                                 return false;
120
121                         arg = parts [1];
122
123                         // We have a logger arg, now get the various parts
124                         parts = arg.Split (new char [] {';'}, 2);
125                         string firstPart = parts [0];
126                         if (parts.Length > 1)
127                                 loggerArgs = parts [1];
128
129                         // Next see if there is a type name
130                         parts = firstPart.Split (new char [] {','}, 2);
131                         if (parts.Length == 1) {
132                                 loggerAssemblyName = firstPart;
133                         } else {
134                                 if (HasAssemblyInfo (parts [1])) {
135                                         loggerAssemblyName = firstPart;
136                                         GetAssemblyInfo (loggerAssemblyName);
137                                 } else {
138                                         loggerType = parts [0];
139                                         parts [0] = string.Empty;
140                                         loggerAssemblyName = string.Join (",", parts).Substring (1).Trim ();
141                                 }
142                         }
143                         
144                         return true;
145                 }
146
147                 static bool HasAssemblyInfo (string part)
148                 {
149                         var containsInfo = (part.IndexOf ("version=", StringComparison.OrdinalIgnoreCase) >= 0) ||
150                                 (part.IndexOf ("culture=", StringComparison.OrdinalIgnoreCase) >= 0) ||
151                                 (part.IndexOf ("publickeytoken=", StringComparison.OrdinalIgnoreCase) >= 0);
152
153                         return containsInfo;
154                 }
155
156                 void GetAssemblyInfo (string assemblyName)
157                 {
158                         var match = assemblyInfoRegEx.Match (assemblyName);
159
160                         if(match == null)
161                                 return;
162
163                         assemblyInfoName = match.Groups ["assemblyName"].Value;
164                         assemblyInfoVersion = match.Groups ["assemblyVersion"].Value;
165                         assemblyInfoCulture = match.Groups ["assemblyCulture"].Value;
166                         assemblyInfoPublicKeyToken = match.Groups ["publicKeyToken"].Value;
167                 }
168         }
169 }