Several fixes to get ProjectUsingTaskElement could be loaded and task database gets...
[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                         BuildTaskDatabase defaults;
49                         if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
50                                 defaults = new BuildTaskDatabase (toolset);
51                         }
52                         return defaults;
53                 }
54                 
55                 // for 'default' tasks.
56                 BuildTaskDatabase (Toolset toolset)
57                 {
58                         ProjectRootElement root;
59                         using (var xml = XmlReader.Create (Path.Combine (toolset.ToolsPath, default_tasks_file)))
60                                 root = ProjectRootElement.Create (xml);
61                         LoadUsingTasks (null, root);
62                 }
63                 
64                 public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
65                 {
66                         LoadUsingTasks (projectInstance, projectRootElement);
67                 }
68                 
69                 internal class TaskDescription
70                 {
71                         public TaskAssembly TaskAssembly { get; set; }
72                         public string Name { get; set; }
73                         public Type TaskFactoryType { get; set; }
74                         public Type TaskType { get; set; }
75                         public IDictionary<string, TaskPropertyInfo> TaskFactoryParameters { get; set; }
76                         public string TaskBody { get; set; }
77                         
78                         public bool IsMatch (string name)
79                         {
80                                 int ridx = Name.LastIndexOf ('.');
81                                 int tidx = name.IndexOf ('.');
82                                 return string.Equals (Name, name, StringComparison.OrdinalIgnoreCase) ||
83                                         tidx < 0 && ridx > 0 && string.Equals (Name.Substring (ridx + 1), name, StringComparison.OrdinalIgnoreCase);
84                         }
85                 }
86                 
87                 internal class TaskAssembly
88                 {
89                         public string AssemblyName { get; set; }
90                         public string AssemblyFile { get; set; }
91                         public Assembly LoadedAssembly { get; set; }
92                 }
93                 
94                 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
95                 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
96
97                 public List<TaskDescription> Tasks {
98                         get { return task_descs; }
99                 }
100                 
101                 void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
102                 {
103                         Func<string,bool> cond = s => projectInstance != null ? projectInstance.EvaluateCondition (s) : Convert.ToBoolean (s);
104                         foreach (var ut in project.UsingTasks) {
105                                 var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (ut.AssemblyFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (ut.AssemblyName, StringComparison.OrdinalIgnoreCase));
106                                 if (ta == null) {
107                                         ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
108                                         ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
109                                         assemblies.Add (ta);
110                                 }
111                                 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)))
112                                         .ToDictionary (p => p.Name);
113                                 var task = new TaskDescription () {
114                                         TaskAssembly = ta,
115                                         Name = ut.TaskName,
116                                         TaskFactoryType = string.IsNullOrEmpty (ut.TaskFactory) ? null : LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory),
117                                         TaskType = string.IsNullOrEmpty (ut.TaskFactory) ? LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName) : null,
118                                         TaskFactoryParameters = pg,
119                                         TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.Evaluate : null,
120                                         };
121                                 task_descs.Add (task);
122                         }
123                 }
124                 
125                 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
126                 {
127                         Type type = a.GetType (possiblyShortTypeName, false, true);
128                         if (possiblyShortTypeName.IndexOf ('.') < 0)
129                                 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
130                         if (type == null)
131                                 throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));
132                         return type;
133                 }
134         }
135 }
136