Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Internal / BuildTaskFactory.cs
1 // BuildTaskFactory.cs
2 //
3 // Author:
4 //   Atsushi Enomoto (atsushi@xamarin.com)
5 //
6 // Copyright (C) 2013 Xamarin Inc.
7 //
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:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
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.
26 //
27
28 using System;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Linq;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Internal;
34 using System.Collections.Generic;
35 using Microsoft.Build.Execution;
36
37 namespace Microsoft.Build.Internal
38 {
39         class BuildTaskFactory
40         {
41                 public BuildTaskFactory (BuildTaskDatabase builtInDatabase, BuildTaskDatabase perProjectDatabase)
42                 {
43                         this.built_in_database = builtInDatabase;
44                         this.per_project_database = perProjectDatabase;
45                 }
46                 
47                 readonly BuildTaskDatabase built_in_database, per_project_database;
48                 readonly List<ITaskFactory> task_factories = new List<ITaskFactory> ();
49                 
50                 public void ResetCaches ()
51                 {
52                         task_factories.Clear ();
53                 }
54                 
55                 public ITask CreateTask (string name, IDictionary<string,string> factoryIdentityParameters, IBuildEngine engine)
56                 {
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);
59                         if (td == null)
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);
63                                 if (tf == null) {
64                                         tf = (ITaskFactory) Activator.CreateInstance (td.TaskFactoryType);
65 #if NET_4_5
66                                         var tf2 = tf as ITaskFactory2;
67                                         if (tf2 != null)
68                                                 tf2.Initialize (name, factoryIdentityParameters, td.TaskFactoryParameters, td.TaskBody, engine);
69                                         else
70 #endif
71                                                 tf.Initialize (name, td.TaskFactoryParameters, td.TaskBody, engine);
72                                         task_factories.Add (tf);
73                                 }
74                                 return tf.CreateTask (engine);
75                         }
76                         else
77                                 return (ITask) Activator.CreateInstance (td.TaskType);
78                 }
79         }
80 }
81