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)
49 throw new ArgumentNullException ("toolset");
50 BuildTaskDatabase defaults;
51 if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
52 defaults = new BuildTaskDatabase (toolset);
57 // for 'default' tasks.
58 BuildTaskDatabase (Toolset toolset)
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);
66 public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
68 LoadUsingTasks (projectInstance, projectRootElement);
71 internal class TaskDescription
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; }
80 public bool IsMatch (string name)
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);
89 internal class TaskAssembly
91 public string AssemblyName { get; set; }
92 public string AssemblyFile { get; set; }
93 public Assembly LoadedAssembly { get; set; }
96 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
97 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
99 public List<TaskDescription> Tasks {
100 get { return task_descs; }
103 void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
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));
109 ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
110 ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
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 () {
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,
123 task_descs.Add (task);
127 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
129 Type type = a.GetType (possiblyShortTypeName, false, true);
130 if (possiblyShortTypeName.IndexOf ('.') < 0)
131 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
133 throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));