Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Construction / ProjectTaskElement.cs
1 //
2 // ProjectTaskElement.cs
3 //
4 // Author:
5 //   Leszek Ciesielski (skolima@gmail.com)
6 //
7 // (C) 2011 Leszek Ciesielski
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32 using System.Xml;
33 using Microsoft.Build.Exceptions;
34 using Microsoft.Build.Internal;
35
36 namespace Microsoft.Build.Construction
37 {
38         [System.Diagnostics.DebuggerDisplayAttribute ("{Name} Condition={Condition} ContinueOnError={ContinueOnError} "
39                                                       + "#Outputs={Count}")]
40         public class ProjectTaskElement : ProjectElementContainer
41         {
42                 internal ProjectTaskElement (string taskName, ProjectRootElement containingProject)
43                         : this(containingProject)
44                 {
45                         Name = taskName;
46                 }
47                 internal ProjectTaskElement (ProjectRootElement containingProject)
48                 {
49                         ContainingProject = containingProject;
50                 }
51                 string continueOnError;
52                 public string ContinueOnError {
53                         get { return continueOnError ?? String.Empty; }
54                         set { continueOnError = value; }
55                 }
56                 string name;
57                 public string Name { get { return name ?? String.Empty; } private set { name = value; } }
58                 public ICollection<ProjectOutputElement> Outputs {
59                         get { return new CollectionFromEnumerable<ProjectOutputElement> (
60                                 new FilteredEnumerable<ProjectOutputElement> (AllChildren)); }
61                 }
62                 public IDictionary<string, string> Parameters {
63                         get { return parameters; }
64                 }
65                 public ProjectOutputElement AddOutputItem (string taskParameter, string itemType)
66                 {
67                         return AddOutputItem (taskParameter, itemType, null);
68                 }
69                 public ProjectOutputElement AddOutputItem (string taskParameter, string itemType, string condition)
70                 {
71                         var output = new ProjectOutputElement (taskParameter, itemType, null, ContainingProject);
72                         if( condition != null)
73                                 output.Condition = condition;
74                         AppendChild (output);
75                         return output;
76                 }
77                 public ProjectOutputElement AddOutputProperty (string taskParameter, string propertyName)
78                 {
79                         return AddOutputProperty (taskParameter, propertyName, null);
80                 }
81                 public ProjectOutputElement AddOutputProperty (string taskParameter, string propertyName,
82                                                                string condition)
83                 {
84                         var output = new ProjectOutputElement (taskParameter, null, propertyName, ContainingProject);
85                         if( condition != null)
86                                 output.Condition = condition;
87                         AppendChild (output);
88                         return output;
89                 }
90                 public string GetParameter (string name)
91                 {
92                         string value;
93                         if (parameters.TryGetValue (name, out value))
94                                 return value;
95                         return string.Empty;
96                 }
97                 public void RemoveAllParameters ()
98                 {
99                         parameters.Clear ();
100                 }
101                 public void RemoveParameter (string name)
102                 {
103                         parameters.Remove (name);
104                 }
105                 public void SetParameter (string name, string unevaluatedValue)
106                 {
107                         parameters[name] = unevaluatedValue;
108                 }
109                 internal override string XmlName {
110                         get { return Name; }
111                 }
112                 internal override ProjectElement LoadChildElement (XmlReader reader)
113                 {
114                         switch (reader.LocalName) {
115                         case "Output":
116                                 var output = ContainingProject.CreateOutputElement (null, null, null);
117                                 AppendChild (output);
118                                 return output;
119                         default:
120                                 throw new InvalidProjectFileException (string.Format (
121                                         "Child \"{0}\" is not a known node type.", reader.LocalName));
122                         }
123                 }
124                 internal override void LoadAttribute (string name, string value)
125                 {
126                         switch (name) {
127                         case "ContinueOnError":
128                                 ContinueOnError = value;
129                                 break;
130 #if NET_4_5
131                         case "ExecuteTargets":
132                                 ExecuteTargets = value;
133                                 break;
134                         case "MSBuildArchitecture":
135                                 MSBuildArchitecture = value;
136                                 break;
137                         case "MSBuildRuntime":
138                                 MSBuildRuntime = value;
139                                 break;
140 #endif
141                         case "xmlns":
142                                 break;
143                         case "Label":
144                                 Label = value;
145                                 break;
146                         case "Condition":
147                                 Condition = value;
148                                 break;
149                         default:
150                                 SetParameter (name, value);
151                                 break;
152                         }
153                 }
154                 internal override void SaveValue (XmlWriter writer)
155                 {
156                         SaveAttribute (writer, "ContinueOnError", ContinueOnError);
157                         foreach (var parameter in parameters) {
158                                 SaveAttribute (writer, parameter.Key, parameter.Value);
159                         }
160                         base.SaveValue (writer);
161                 }
162                 private Dictionary<string, string> parameters = new Dictionary<string, string> ();
163
164                 public string ExecuteTargets { get; set; }
165                 #if NET_4_5
166                 public ElementLocation ExecuteTargetsLocation { get; set; }
167                 public ElementLocation ContinueOnErrorLocation { get; set; }
168                 public string MSBuildArchitecture { get; set; }
169                 public ElementLocation MSBuildArchitectureLocation { get; set; }
170                 public string MSBuildRuntime { get; set; }
171                 public ElementLocation MSBuildRuntimeLocation { get; set; }
172                 #endif
173         }
174 }