2 // Target.cs: Represents a target.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.BuildEngine {
36 public class Target : IEnumerable {
38 BatchingImpl batchingImpl;
39 BuildState buildState;
40 XmlAttribute condition;
41 XmlAttribute dependsOnTargets;
46 XmlElement targetElement;
47 ArrayList onErrorElements;
51 internal Target (XmlElement targetElement, Project project)
54 throw new ArgumentNullException ("project");
55 if (targetElement == null)
56 throw new ArgumentNullException ("targetElement");
58 this.targetElement = targetElement;
59 this.name = targetElement.GetAttribute ("Name");
60 this.condition = targetElement.GetAttributeNode ("Condition");
61 this.dependsOnTargets = targetElement.GetAttributeNode ("DependsOnTargets");
63 this.project = project;
64 this.engine = project.ParentEngine;
65 this.isImported = false;;
67 this.onErrorElements = new ArrayList ();
68 this.buildState = BuildState.NotStarted;
69 this.buildTasks = new ArrayList ();
70 this.batchingImpl = new BatchingImpl (project, this.targetElement);
72 foreach (XmlNode xn in targetElement.ChildNodes) {
73 if (xn is XmlElement) {
74 XmlElement xe = (XmlElement) xn;
75 if (xe.Name == "OnError") {
76 onErrorElements.Add (xe);
79 buildTasks.Add (new BuildTask (xe, this));
84 internal void Build ()
86 buildState = BuildState.Started;
87 if (dependsOnTargets == null) {
89 } else if (dependsOnTargets.Value == "") {
92 OldExpression dependencies = new OldExpression (Project);
93 dependencies.ParseSource (dependsOnTargets.Value);
95 string[] targetsToBuildFirst = (string[]) dependencies.ConvertTo (typeof (string[]));
96 foreach (string target in targetsToBuildFirst) {
97 string trimmed = target.Trim ();
98 Target t = (Target) project.Targets [trimmed];
100 throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
101 if (t.BuildState == BuildState.NotStarted) {
104 if (t.BuildState == BuildState.Started)
105 throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
109 buildState = BuildState.Finished;
112 private void RealBuild ()
114 bool executeOnErrors = false;
119 if (this.batchingImpl.BuildNeeded ()) {
120 foreach (BuildTask bt in buildTasks) {
121 if (this.batchingImpl.BatchBuildTask (bt) == false && bt.ContinueOnError == false) {
122 executeOnErrors = true;
131 LogTargetFinished (result);
133 if (executeOnErrors == true)
137 private void ExecuteOnErrors ()
139 foreach (XmlElement onError in onErrorElements) {
140 // FIXME: add condition
141 if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
142 throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
143 string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
144 foreach (string t in targetsToExecute)
145 this.project.Targets [t].Build ();
149 private void LogTargetSkipped ()
151 BuildMessageEventArgs bmea;
152 bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
153 name), null, "MSBuild", MessageImportance.Normal);
154 engine.EventSource.FireMessageRaised (this, bmea);
157 private void LogTargetStarted ()
159 TargetStartedEventArgs tsea;
160 string projectFile = project.FullFileName;
161 tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
162 engine.EventSource.FireTargetStarted (this, tsea);
165 private void LogTargetFinished (bool succeeded)
167 TargetFinishedEventArgs tfea;
168 string projectFile = project.FullFileName;
169 tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
170 engine.EventSource.FireTargetFinished (this, tfea);
174 public BuildTask AddNewTask (string taskName)
176 throw new NotImplementedException ();
179 public IEnumerator GetEnumerator ()
181 foreach (BuildTask bt in buildTasks) {
186 public void RemoveTask (BuildTask buildTask)
188 //taskElements.Remove (taskElement);
189 buildTasks.Remove (buildTask);
192 public string Condition {
193 get { return condition.Value; }
194 set { condition.Value = value; }
197 public string DependsOnTargets {
199 if (dependsOnTargets == null)
202 return dependsOnTargets.Value;
205 if (dependsOnTargets != null)
206 dependsOnTargets.Value = value;
210 public bool IsImported {
211 get { return isImported; }
212 internal set { isImported = value; }
219 internal Project Project {
220 get { return project; }
223 internal BuildState BuildState {
224 get { return buildState; }
228 internal enum BuildState {