Implementation of the 2.0 session state model
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Target.cs
1 //
2 // Target.cs: Represents a target.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
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 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Xml;
34 using Microsoft.Build.Framework;
35
36 namespace Microsoft.Build.BuildEngine {
37         public class Target : IEnumerable {
38         
39                 BatchingImpl    batchingImpl;
40                 BuildState      buildState;
41                 Engine          engine;
42                 ImportedProject importedProject;
43                 string          name;
44                 Project         project;
45                 XmlElement      targetElement;
46                 List <XmlElement>       onErrorElements;
47                 List <BuildTask>        buildTasks;
48                 
49                 internal Target (XmlElement targetElement, Project project, ImportedProject importedProject)
50                 {
51                         if (project == null)
52                                 throw new ArgumentNullException ("project");
53                         if (targetElement == null)
54                                 throw new ArgumentNullException ("targetElement");
55
56                         this.targetElement = targetElement;
57                         this.name = targetElement.GetAttribute ("Name");
58
59                         this.project = project;
60                         this.engine = project.ParentEngine;
61                         this.importedProject = importedProject;
62
63                         this.onErrorElements  = new List <XmlElement> ();
64                         this.buildState = BuildState.NotStarted;
65                         this.buildTasks = new List <BuildTask> ();
66                         this.batchingImpl = new BatchingImpl (project, this.targetElement);
67
68                         foreach (XmlNode xn in targetElement.ChildNodes) {
69                                 if (xn is XmlElement) {
70                                         XmlElement xe = (XmlElement) xn;
71                                         if (xe.Name == "OnError") {
72                                                 onErrorElements.Add (xe);
73                                                 continue;
74                                         }
75                                         buildTasks.Add (new BuildTask (xe, this));
76                                 }
77                         }
78                 }
79                 
80                 [MonoTODO]
81                 public BuildTask AddNewTask (string taskName)
82                 {
83                         if (taskName == null)
84                                 throw new ArgumentNullException ("taskName");
85                 
86                         XmlElement task = project.XmlDocument.CreateElement (taskName, Project.XmlNamespace);
87                         targetElement.AppendChild (task);
88                         BuildTask bt = new BuildTask (task, this);
89                         buildTasks.Add (bt);
90                         
91                         return bt;
92                 }
93
94                 public IEnumerator GetEnumerator ()
95                 {
96                         foreach (BuildTask bt in buildTasks)
97                                 yield return bt;
98                 }
99
100                 // FIXME: shouldn't we remove it from XML?
101                 public void RemoveTask (BuildTask buildTask)
102                 {
103                         if (buildTask == null)
104                                 throw new ArgumentNullException ("buildTask");
105                         buildTasks.Remove (buildTask);
106                 }
107                 
108                 // FIXME: log errors instead of throwing exceptions
109                 internal bool Build ()
110                 {
111                         bool result = true;
112                 
113                         try {
114                                 buildState = BuildState.Started;
115
116                                 if (DependsOnTargets != String.Empty) {
117                                         Expression dependencies = new Expression ();
118                                         dependencies.Parse (DependsOnTargets);
119                                         
120                                         string[] targetsToBuildFirst = (string[]) dependencies.ConvertTo (Project, typeof (string[]));
121                                         foreach (string target in targetsToBuildFirst) {
122                                                 string trimmed = target.Trim ();
123                                                 Target t = (Target) project.Targets [trimmed];
124                                                 if (t == null)
125                                                         throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
126                                                 if (t.BuildState == BuildState.NotStarted) {
127                                                         if (!t.Build ()) {
128                                                                 result = false;
129                                                                 break;
130                                                         }
131
132                                                 }
133                                                 if (t.BuildState == BuildState.Started)
134                                                         throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
135                                         }
136                                 }
137                         
138                                 if (result)
139                                         result = RealBuild ();
140                         } catch (Exception) {
141                                 return false;
142                         } finally {
143                                 buildState = BuildState.Finished;
144                         }
145                         
146                         return result;
147                 }
148                 
149                 private bool RealBuild ()
150                 {
151                         bool executeOnErrors = false;
152                         bool result = true;
153                 
154                         LogTargetStarted ();
155                         
156                         if (batchingImpl.BuildNeeded ()) {
157                                 foreach (BuildTask bt in buildTasks) {
158                                         result = batchingImpl.BatchBuildTask (bt);
159                                 
160                                         if (!result && !bt.ContinueOnError) {
161                                                 executeOnErrors = true;
162                                                 break;
163                                         }
164                                 }
165                         } else {
166                                 LogTargetSkipped ();
167                         }
168
169                         LogTargetFinished (result);
170                         
171                         if (executeOnErrors == true)
172                                 ExecuteOnErrors ();
173                                 
174                         return result;
175                 }
176                 
177                 private void ExecuteOnErrors ()
178                 {
179                         foreach (XmlElement onError in onErrorElements) {
180                                 // FIXME: add condition
181                                 if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
182                                         throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
183                                 string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
184                                 foreach (string t in targetsToExecute)
185                                         this.project.Targets [t].Build ();
186                         }
187                 }
188
189                 private void LogTargetSkipped ()
190                 {
191                         BuildMessageEventArgs bmea;
192                         bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
193                                 name), null, "MSBuild", MessageImportance.Normal);
194                         engine.EventSource.FireMessageRaised (this, bmea);
195                 }
196                 
197                 private void LogTargetStarted ()
198                 {
199                         TargetStartedEventArgs tsea;
200                         string projectFile = project.FullFileName;
201                         tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
202                         engine.EventSource.FireTargetStarted (this, tsea);
203                 }
204                 
205                 private void LogTargetFinished (bool succeeded)
206                 {
207                         TargetFinishedEventArgs tfea;
208                         string projectFile = project.FullFileName;
209                         tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
210                         engine.EventSource.FireTargetFinished (this, tfea);
211                 }
212         
213                 public string Condition {
214                         get { return targetElement.GetAttribute ("Condition"); }
215                         set { targetElement.SetAttribute ("Condition", value); }
216                 }
217
218                 public string DependsOnTargets {
219                         get { return targetElement.GetAttribute ("DependsOnTargets"); }
220                         set { targetElement.SetAttribute ("DependsOnTargets", value); }
221                 }
222
223                 public bool IsImported {
224                         get { return importedProject != null; }
225                 }
226
227                 public string Name {
228                         get { return name; }
229                 }
230                 
231                 internal Project Project {
232                         get { return project; }
233                 }
234                 
235                 internal BuildState BuildState {
236                         get { return buildState; }
237                 }
238         }
239         
240         internal enum BuildState {
241                 NotStarted,
242                 Started,
243                 Finished,
244                 Skipped
245         }
246 }
247
248 #endif