4 // Atsushi Enomoto (atsushi@xamarin.com)
6 // Copyright (C) 2013 Xamarin Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Threading;
30 using System.Threading.Tasks;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Internal;
34 using System.Collections.Generic;
35 using Microsoft.Build.Execution;
37 namespace Microsoft.Build.Internal
39 class BuildTaskFactory
41 public BuildTaskFactory (BuildTaskDatabase builtInDatabase, BuildTaskDatabase perProjectDatabase)
43 this.built_in_database = builtInDatabase;
44 this.per_project_database = perProjectDatabase;
47 readonly BuildTaskDatabase built_in_database, per_project_database;
48 readonly List<ITaskFactory> task_factories = new List<ITaskFactory> ();
50 public void ResetCaches ()
52 task_factories.Clear ();
55 public ITask CreateTask (string name, IDictionary<string,string> factoryIdentityParameters, IBuildEngine engine)
57 Func<BuildTaskDatabase.TaskDescription,bool> fn = t => t.IsMatch (name);
58 var td = per_project_database.Tasks.FirstOrDefault (fn) ?? built_in_database.Tasks.FirstOrDefault (fn);
60 throw new InvalidOperationException (string.Format ("Task '{0}' could not be found", name));
61 if (td.TaskFactoryType != null) {
62 var tf = task_factories.FirstOrDefault (f => f.GetType () == td.TaskFactoryType);
64 tf = (ITaskFactory) Activator.CreateInstance (td.TaskFactoryType);
66 var tf2 = tf as ITaskFactory2;
68 tf2.Initialize (name, factoryIdentityParameters, td.TaskFactoryParameters, td.TaskBody, engine);
71 tf.Initialize (name, td.TaskFactoryParameters, td.TaskBody, engine);
72 task_factories.Add (tf);
74 return tf.CreateTask (engine);
77 return (ITask) Activator.CreateInstance (td.TaskType);