Merge pull request #2768 from lambdageek/dev/monoerror-cominterop
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Internal / BuildTaskDatabase.cs
1 //
2 // BuildTaskFactory.cs
3 //
4 // Author:
5 //   Atsushi Enomoto (atsushi@xamarin.com)
6 //
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Linq;
31 using Microsoft.Build.Framework;
32 using System.Reflection;
33 using Microsoft.Build.Execution;
34 using Microsoft.Build.Evaluation;
35 using Microsoft.Build.Construction;
36 using System.IO;
37 using System.Xml;
38
39 namespace Microsoft.Build.Internal
40 {
41         class BuildTaskDatabase
42         {
43                 const string default_tasks_file = "Microsoft.Common.tasks";
44                 static readonly Dictionary<string,BuildTaskDatabase> default_factory = new Dictionary<string, BuildTaskDatabase> ();
45
46                 public static BuildTaskDatabase GetDefaultTaskDatabase (Toolset toolset)
47                 {
48                         if (toolset == null)
49                                 throw new ArgumentNullException ("toolset");
50                         BuildTaskDatabase defaults;
51                         if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
52                                 defaults = new BuildTaskDatabase (toolset);
53                         }
54                         return defaults;
55                 }
56                 
57                 // for 'default' tasks.
58                 BuildTaskDatabase (Toolset toolset)
59                 {
60                         ProjectRootElement root;
61                         using (var xml = XmlReader.Create (Path.Combine (toolset.ToolsPath, default_tasks_file)))
62                                 root = ProjectRootElement.Create (xml);
63                         LoadUsingTasks (null, root.UsingTasks);
64                 }
65                 
66                 public BuildTaskDatabase (IBuildEngine engine, ProjectInstance projectInstance)
67                 {
68                         this.engine = engine;
69                         LoadUsingTasks (projectInstance, projectInstance.UsingTasks);
70                 }
71                 
72                 internal class TaskDescription
73                 {
74                         public TaskAssembly TaskAssembly { get; set; }
75                         public string Name { get; set; }
76                         public Type TaskFactoryType { get; set; }
77                         public Type TaskType { get; set; }
78                         public IDictionary<string, TaskPropertyInfo> TaskFactoryParameters { get; set; }
79                         public string TaskBody { get; set; }
80                         
81                         public bool IsMatch (string name)
82                         {
83                                 int ridx = Name.LastIndexOf ('.');
84                                 int tidx = name.IndexOf ('.');
85                                 return string.Equals (Name, name, StringComparison.OrdinalIgnoreCase) ||
86                                         tidx < 0 && ridx > 0 && string.Equals (Name.Substring (ridx + 1), name, StringComparison.OrdinalIgnoreCase);
87                         }
88                 }
89                 
90                 internal class TaskAssembly
91                 {
92                         public string AssemblyName { get; set; }
93                         public string AssemblyFile { get; set; }
94                         public Assembly LoadedAssembly { get; set; }
95                 }
96
97                 readonly IBuildEngine engine;
98                 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
99                 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
100
101                 public List<TaskDescription> Tasks {
102                         get { return task_descs; }
103                 }
104
105                 // FIXME: my guess is the tasks does not have to be loaded entirely but only requested tasks must be loaded at invocation time.
106                 void LoadUsingTasks (ProjectInstance projectInstance, IEnumerable<ProjectUsingTaskElement> usingTasks)
107                 {
108                         Func<string,bool> cond = s => projectInstance != null ? projectInstance.EvaluateCondition (s) : Convert.ToBoolean (s);
109                         Func<string,string> expand = s => projectInstance != null ? projectInstance.ExpandString (s) : s;
110                         foreach (var ut in usingTasks) {
111                                 var aName = expand (ut.AssemblyName);
112                                 var aFile = expand (ut.AssemblyFile);
113                                 if (string.IsNullOrEmpty (aName) && string.IsNullOrEmpty (aFile)) {
114                                         var errorNoAssembly = string.Format ("Task '{0}' does not specify either of AssemblyName or AssemblyFile.", ut.TaskName);
115                                         engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, errorNoAssembly, null, null));
116                                         continue;
117                                 }
118                                 var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (aFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (aName, StringComparison.OrdinalIgnoreCase));
119                                 if (ta == null) {
120                                         var path = Path.GetDirectoryName (string.IsNullOrEmpty (ut.Location.File) ? projectInstance.FullPath : ut.Location.File);
121                                         ta = new TaskAssembly () { AssemblyName = aName, AssemblyFile = aFile };
122                                         try {
123                                                 ta.LoadedAssembly = !string.IsNullOrEmpty (ta.AssemblyName) ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (Path.Combine (path, ta.AssemblyFile));
124                                         } catch {
125                                                 var errorNotLoaded = string.Format ("For task '{0}' Specified assembly '{1}' was not found", ut.TaskName, string.IsNullOrEmpty (ta.AssemblyName) ? Path.Combine (path, ta.AssemblyFile) : ta.AssemblyName);
126                                                 engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, errorNotLoaded, null, null));
127                                                 continue;
128                                         }
129                                         assemblies.Add (ta);
130                                 }
131                                 var pg = ut.ParameterGroup == null ? null : ut.ParameterGroup.Parameters.Select (p => new TaskPropertyInfo (p.Name, Type.GetType (p.ParameterType), cond (p.Output), cond (p.Required)))
132                                         .ToDictionary (p => p.Name);
133                                 
134
135                                 Type type = null;
136                                 string error = null;
137                                 TaskDescription task = new TaskDescription () {
138                                         TaskAssembly = ta,
139                                         Name = ut.TaskName,
140                                         TaskFactoryParameters = pg,
141                                         TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.TaskBody : null,
142                                         };
143                                 if (string.IsNullOrEmpty (ut.TaskFactory)) {
144                                         type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName);
145                                         if (type == null)
146                                                 error = string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskName, ta.LoadedAssembly.FullName);
147                                         else
148                                                 task.TaskType = type;
149                                 } else {
150                                         type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory);
151                                         if (type == null)
152                                                 error = string.Format ("For task '{0}' Specified factory type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskFactory, ta.LoadedAssembly.FullName);
153                                         else
154                                                 task.TaskFactoryType = type;
155                                 }
156                                 if (error != null)
157                                         engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, error, null, null));
158                                 else
159                                         task_descs.Add (task);
160                         }
161                 }
162                 
163                 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
164                 {
165                         Type type = a.GetType (possiblyShortTypeName, false, true);
166                         if (possiblyShortTypeName.IndexOf ('.') < 0)
167                                 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
168                         return type;
169                 }
170         }
171 }
172