2 // LoggerInfo.cs: Contains information about logger parameters.
5 // Craig Sutherland (cj.sutherland(at)xtra.co.nz)
6 // Daniel Nauck (dna(at)mono-project.de)
8 // (C) 2009 Craig Sutherland, Daniel Nauck
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System
.Globalization
;
35 using System
.Reflection
;
36 using System
.Text
.RegularExpressions
;
37 using Mono
.XBuild
.Framework
;
39 namespace Mono
.XBuild
.CommandLine
41 internal class LoggerInfo
: AssemblyLoadInfo
43 static readonly Regex assemblyInfoRegEx
= new Regex(@"(?<assemblyName>[\w\.]+)(,\s?Version=(?<assemblyVersion>\d+\.\d+\.\d+\.\d+))?(,\s?Culture=(?<assemblyCulture>\w+))?(,\s?PublicKeyToken=(?<publicKeyToken>\w+))?",
44 RegexOptions
.Compiled
| RegexOptions
.CultureInvariant
);
46 string loggerAssemblyName
;
50 string assemblyInfoName
;
51 string assemblyInfoVersion
;
52 string assemblyInfoCulture
;
53 string assemblyInfoPublicKeyToken
;
55 internal LoggerInfo (string value)
60 if (string.IsNullOrEmpty (loggerType
))
61 loggerType
= GetLoggerTypeName (loggerAssemblyName
);
63 if (assemblyInfoName
!= null)
64 SetAssemblyName (LoadInfoType
.AssemblyName
, null, assemblyInfoName
, assemblyInfoVersion
, assemblyInfoCulture
, assemblyInfoPublicKeyToken
, loggerType
);
67 SetAssemblyName (LoadInfoType
.AssemblyFilename
, loggerAssemblyName
, null, null, null, null, loggerType
);
70 internal string Parameters
{
71 get { return loggerArgs; }
74 static string GetLoggerTypeName (string assemblyName
)
76 Assembly loggerAssembly
= null;
78 // try to load assembly that contains the logger
79 if (HasAssemblyInfo (assemblyName
))
80 loggerAssembly
= Assembly
.Load (assemblyName
);
81 else if (File
.Exists (assemblyName
))
82 loggerAssembly
= Assembly
.LoadFile (assemblyName
);
84 if (loggerAssembly
== null)
87 // search for a class thats implement ILogger
88 var loggerClass
= (from t
in loggerAssembly
.GetTypes ()
90 t
.GetInterface ("Microsoft.Build.Framework.ILogger") != null &&
92 select t
).FirstOrDefault ();
94 if (loggerClass
!= null)
95 return loggerClass
.FullName
;
100 bool Parse (string arg
)
102 // Wipe all the existing values, just in case
103 loggerAssemblyName
= null;
106 assemblyInfoName
= null;
107 assemblyInfoVersion
= null;
108 assemblyInfoCulture
= null;
109 assemblyInfoPublicKeyToken
= null;
111 if (string.IsNullOrEmpty (arg
))
114 string [] parts
= arg
.Split (new char [] {':'}
, 2);
115 if (parts
.Length
!= 2)
118 if (string.Compare ("/l", parts
[0], StringComparison
.OrdinalIgnoreCase
) != 0 &&
119 string.Compare ("/logger", parts
[0], StringComparison
.OrdinalIgnoreCase
) != 0)
124 // We have a logger arg, now get the various parts
125 parts
= arg
.Split (new char [] {';'}
, 2);
126 string firstPart
= parts
[0];
127 if (parts
.Length
> 1)
128 loggerArgs
= parts
[1];
130 // Next see if there is a type name
131 parts
= firstPart
.Split (new char [] {','}
, 2);
132 if (parts
.Length
== 1) {
133 loggerAssemblyName
= firstPart
;
135 if (HasAssemblyInfo (parts
[1])) {
136 loggerAssemblyName
= firstPart
;
137 GetAssemblyInfo (loggerAssemblyName
);
139 loggerType
= parts
[0];
140 parts
[0] = string.Empty
;
141 loggerAssemblyName
= string.Join (",", parts
).Substring (1).Trim ();
148 static bool HasAssemblyInfo (string part
)
150 var containsInfo
= (part
.IndexOf ("version=", StringComparison
.OrdinalIgnoreCase
) >= 0) ||
151 (part
.IndexOf ("culture=", StringComparison
.OrdinalIgnoreCase
) >= 0) ||
152 (part
.IndexOf ("publickeytoken=", StringComparison
.OrdinalIgnoreCase
) >= 0);
157 void GetAssemblyInfo (string assemblyName
)
159 var match
= assemblyInfoRegEx
.Match (assemblyName
);
164 assemblyInfoName
= match
.Groups
["assemblyName"].Value
;
165 assemblyInfoVersion
= match
.Groups
["assemblyVersion"].Value
;
166 assemblyInfoCulture
= match
.Groups
["assemblyCulture"].Value
;
167 assemblyInfoPublicKeyToken
= match
.Groups
["publicKeyToken"].Value
;