1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 //-----------------------------------------------------------------------
4 // </copyright>
5 // <summary>The task factory for intrinsic tasks.</summary>
6 //-----------------------------------------------------------------------
7 
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Reflection;
12 using System.Text;
13 using Microsoft.Build.Execution;
14 using Microsoft.Build.Framework;
15 using Microsoft.Build.Shared;
16 
17 namespace Microsoft.Build.BackEnd
18 {
19     /// <summary>
20     /// The factory
21     /// </summary>
22     internal class IntrinsicTaskFactory : ITaskFactory
23     {
24         /// <summary>
25         /// Constructor
26         /// </summary>
IntrinsicTaskFactory(Type intrinsicType)27         public IntrinsicTaskFactory(Type intrinsicType)
28         {
29             this.TaskType = intrinsicType;
30         }
31 
32         /// <summary>
33         /// Returns the factory name
34         /// </summary>
35         public string FactoryName
36         {
37             get { return "Intrinsic Task Factory"; }
38         }
39 
40         /// <summary>
41         /// Returns the task type.
42         /// </summary>
43         public Type TaskType
44         {
45             get;
46             private set;
47         }
48 
49         /// <summary>
50         /// Initialize the factory.
51         /// </summary>
Initialize(string taskName, IDictionary<string, TaskPropertyInfo> parameterGroup, string taskBody, IBuildEngine taskFactoryLoggingHost)52         public bool Initialize(string taskName, IDictionary<string, TaskPropertyInfo> parameterGroup, string taskBody, IBuildEngine taskFactoryLoggingHost)
53         {
54             if (!String.Equals(taskName, TaskType.Name, StringComparison.OrdinalIgnoreCase))
55             {
56                 ErrorUtilities.ThrowInternalError("Unexpected task name {0}.  Expected {1}", taskName, TaskType.Name);
57             }
58 
59             return true;
60         }
61 
62         /// <summary>
63         /// Gets all of the parameters on the task.
64         /// </summary>
GetTaskParameters()65         public TaskPropertyInfo[] GetTaskParameters()
66         {
67             PropertyInfo[] infos = TaskType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
68             var propertyInfos = new TaskPropertyInfo[infos.Length];
69             for (int i = 0; i < infos.Length; i++)
70             {
71                 propertyInfos[i] = new ReflectableTaskPropertyInfo(infos[i]);
72             }
73 
74             return propertyInfos;
75         }
76 
77         /// <summary>
78         /// Creates an instance of the task.
79         /// </summary>
CreateTask(IBuildEngine taskFactoryLoggingHost)80         public ITask CreateTask(IBuildEngine taskFactoryLoggingHost)
81         {
82             if (TaskType == typeof(MSBuild))
83             {
84                 return new MSBuild();
85             }
86             else if (TaskType == typeof(CallTarget))
87             {
88                 return new CallTarget();
89             }
90 
91             ErrorUtilities.ThrowInternalError("Unexpected intrinsic task type {0}", TaskType);
92             return null;
93         }
94 
95         /// <summary>
96         /// Cleanup for the task.
97         /// </summary>
CleanupTask(ITask task)98         public void CleanupTask(ITask task)
99         {
100         }
101     }
102 }
103