summaryrefslogtreecommitdiff
path: root/mcs/class/Microsoft.Build/Microsoft.Build.Internal/BuildTaskDatabase.cs
blob: 0b7660b2d4528951a09c9ee272d30ea0c58ce7d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// BuildTaskFactory.cs
//
// Author:
//   Atsushi Enomoto (atsushi@xamarin.com)
//
// Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Framework;
using System.Reflection;
using Microsoft.Build.Execution;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Construction;
using System.IO;
using System.Xml;

namespace Microsoft.Build.Internal
{
	class BuildTaskDatabase
	{
		const string default_tasks_file = "Microsoft.Common.tasks";
		static readonly Dictionary<string,BuildTaskDatabase> default_factory = new Dictionary<string, BuildTaskDatabase> ();

		public static BuildTaskDatabase GetDefaultTaskDatabase (Toolset toolset)
		{
			if (toolset == null)
				throw new ArgumentNullException ("toolset");
			BuildTaskDatabase defaults;
			if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
				defaults = new BuildTaskDatabase (toolset);
			}
			return defaults;
		}
		
		// for 'default' tasks.
		BuildTaskDatabase (Toolset toolset)
		{
			ProjectRootElement root;
			using (var xml = XmlReader.Create (Path.Combine (toolset.ToolsPath, default_tasks_file)))
				root = ProjectRootElement.Create (xml);
			LoadUsingTasks (null, root);
		}
		
		public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
		{
			LoadUsingTasks (projectInstance, projectRootElement);
		}
		
		internal class TaskDescription
		{
			public TaskAssembly TaskAssembly { get; set; }
			public string Name { get; set; }
			public Type TaskFactoryType { get; set; }
			public Type TaskType { get; set; }
			public IDictionary<string, TaskPropertyInfo> TaskFactoryParameters { get; set; }
			public string TaskBody { get; set; }
			
			public bool IsMatch (string name)
			{
				int ridx = Name.LastIndexOf ('.');
				int tidx = name.IndexOf ('.');
				return string.Equals (Name, name, StringComparison.OrdinalIgnoreCase) ||
					tidx < 0 && ridx > 0 && string.Equals (Name.Substring (ridx + 1), name, StringComparison.OrdinalIgnoreCase);
			}
		}
		
		internal class TaskAssembly
		{
			public string AssemblyName { get; set; }
			public string AssemblyFile { get; set; }
			public Assembly LoadedAssembly { get; set; }
		}
		
		readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
		readonly List<TaskDescription> task_descs = new List<TaskDescription> ();

		public List<TaskDescription> Tasks {
			get { return task_descs; }
		}
		
		void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
		{
			Func<string,bool> cond = s => projectInstance != null ? projectInstance.EvaluateCondition (s) : Convert.ToBoolean (s);
			foreach (var ut in project.UsingTasks) {
				var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (ut.AssemblyFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (ut.AssemblyName, StringComparison.OrdinalIgnoreCase));
				if (ta == null) {
					ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
					ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
					assemblies.Add (ta);
				}
				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)))
					.ToDictionary (p => p.Name);
				var task = new TaskDescription () {
					TaskAssembly = ta,
					Name = ut.TaskName,
					TaskFactoryType = string.IsNullOrEmpty (ut.TaskFactory) ? null : LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory),
					TaskType = string.IsNullOrEmpty (ut.TaskFactory) ? LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName) : null,
					TaskFactoryParameters = pg,
					TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.Evaluate : null,
					};
				task_descs.Add (task);
			}
		}
		
		Type LoadTypeFrom (Assembly a, string taskName, string possiblyShortTypeName)
		{
			Type type = a.GetType (possiblyShortTypeName, false, true);
			if (possiblyShortTypeName.IndexOf ('.') < 0)
				type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
			if (type == null)
				throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));
			return type;
		}
	}
}