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.UsingTasks);
66 public BuildTaskDatabase (IBuildEngine engine, ProjectInstance projectInstance)
69 LoadUsingTasks (projectInstance, projectInstance.UsingTasks);
72 internal class TaskDescription
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; }
81 public bool IsMatch (string name)
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);
90 internal class TaskAssembly
92 public string AssemblyName { get; set; }
93 public string AssemblyFile { get; set; }
94 public Assembly LoadedAssembly { get; set; }
97 readonly IBuildEngine engine;
98 readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
99 readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
101 public List<TaskDescription> Tasks {
102 get { return task_descs; }
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)
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));
118 var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (aFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (aName, StringComparison.OrdinalIgnoreCase));
120 var path = Path.GetDirectoryName (string.IsNullOrEmpty (ut.Location.File) ? projectInstance.FullPath : ut.Location.File);
121 ta = new TaskAssembly () { AssemblyName = aName, AssemblyFile = aFile };
123 ta.LoadedAssembly = !string.IsNullOrEmpty (ta.AssemblyName) ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (Path.Combine (path, ta.AssemblyFile));
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));
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);
137 TaskDescription task = new TaskDescription () {
140 TaskFactoryParameters = pg,
141 TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.TaskBody : null,
143 if (string.IsNullOrEmpty (ut.TaskFactory)) {
144 type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName);
146 error = string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskName, ta.LoadedAssembly.FullName);
148 task.TaskType = type;
150 type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory);
152 error = string.Format ("For task '{0}' Specified factory type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskFactory, ta.LoadedAssembly.FullName);
154 task.TaskFactoryType = type;
157 engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, error, null, null));
159 task_descs.Add (task);
163 Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
165 Type type = a.GetType (possiblyShortTypeName, false, true);
166 if (possiblyShortTypeName.IndexOf ('.') < 0)
167 type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);