Merge pull request #819 from brendanzagaeski/patch-1
[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);
64                 }
65                 
66                 public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
67                 {
68                         LoadUsingTasks (projectInstance, projectRootElement);
69                 }
70                 
71                 internal class TaskDescription
72                 {
73                         public TaskAssembly TaskAssembly { get; set; }
74                         public string Name { get; set; }
75                         public Type TaskFactoryType { get; set; }
76                         public Type TaskType { get; set; }
77                         public IDictionary<string, TaskPropertyInfo> TaskFactoryParameters { get; set; }
78                         public string TaskBody { get; set; }
79                         
80                         public bool IsMatch (string name)
81                         {
82                                 int ridx = Name.LastIndexOf ('.');
83                                 int tidx = name.IndexOf ('.');
84                                 return string.Equals (Name, name, StringComparison.OrdinalIgnoreCase) ||
85                                         tidx < 0 && ridx > 0 && string.Equals (Name.Substring (ridx + 1), name, StringComparison.OrdinalIgnoreCase);
86                         }
87                 }
88                 
89                 internal class TaskAssembly
90                 {
91                         public string AssemblyName { get; set; }
92                         public string AssemblyFile { get; set; }
93                         public Assembly LoadedAssembly { get; set; }
94                 }
95                 
96                 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
97                 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
98
99                 public List<TaskDescription> Tasks {
100                         get { return task_descs; }
101                 }
102                 
103                 void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
104                 {
105                         Func<string,bool> cond = s => projectInstance != null ? projectInstance.EvaluateCondition (s) : Convert.ToBoolean (s);
106                         foreach (var ut in project.UsingTasks) {
107                                 var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (ut.AssemblyFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (ut.AssemblyName, StringComparison.OrdinalIgnoreCase));
108                                 if (ta == null) {
109                                         ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
110                                         ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
111                                         assemblies.Add (ta);
112                                 }
113                                 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)))
114                                         .ToDictionary (p => p.Name);
115                                 var task = new TaskDescription () {
116                                         TaskAssembly = ta,
117                                         Name = ut.TaskName,
118                                         TaskFactoryType = string.IsNullOrEmpty (ut.TaskFactory) ? null : LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory),
119                                         TaskType = string.IsNullOrEmpty (ut.TaskFactory) ? LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName) : null,
120                                         TaskFactoryParameters = pg,
121                                         TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.Evaluate : null,
122                                         };
123                                 task_descs.Add (task);
124                         }
125                 }
126                 
127                 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
128                 {
129                         Type type = a.GetType (possiblyShortTypeName, false, true);
130                         if (possiblyShortTypeName.IndexOf ('.') < 0)
131                                 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
132                         if (type == null)
133                                 throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));
134                         return type;
135                 }
136         }
137 }
138