5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 using Microsoft.Build.Framework;
32 using System.Reflection;
33 using Microsoft.Build.Execution;
34 using Microsoft.Build.Evaluation;
35 using Microsoft.Build.Construction;
39 namespace Microsoft.Build.Internal
41 class BuildTaskDatabase
43 const string default_tasks_file = "Microsoft.Common.tasks";
44 static readonly Dictionary<string,BuildTaskDatabase> default_factory = new Dictionary<string, BuildTaskDatabase> ();
46 public static BuildTaskDatabase GetDefaultTaskDatabase (Toolset toolset)
48 BuildTaskDatabase defaults;
49 if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
50 defaults = new BuildTaskDatabase (toolset);
55 // for 'default' tasks.
56 BuildTaskDatabase (Toolset toolset)
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);
64 public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
66 LoadUsingTasks (projectInstance, projectRootElement);
69 internal class TaskDescription
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; }
78 public bool IsMatch (string name)
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);
87 internal class TaskAssembly
89 public string AssemblyName { get; set; }
90 public string AssemblyFile { get; set; }
91 public Assembly LoadedAssembly { get; set; }
94 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
95 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
97 public List<TaskDescription> Tasks {
98 get { return task_descs; }
101 void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
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));
107 ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
108 ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
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 () {
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,
121 task_descs.Add (task);
125 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
127 Type type = a.GetType (possiblyShortTypeName, false, true);
128 if (possiblyShortTypeName.IndexOf ('.') < 0)
129 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
131 throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));