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 taskElements;
48 ArrayList onErrorElements;
50 internal Target (Project project, string name)
53 throw new ArgumentNullException ("project");
55 throw new ArgumentNullException ("name");
56 this.buildState = BuildState.NotStarted;
57 this.project = project;
58 this.engine = project.ParentEngine;
60 this.isImported = false;;
61 taskElements = new ArrayList ();
62 onErrorElements = new ArrayList ();
65 internal void BindToXml (XmlElement targetElement)
67 if (targetElement == null)
68 throw new ArgumentNullException ("targetElement");
69 this.targetElement = targetElement;
70 // FIXME: check if Target element is valid
71 this.condition = targetElement.GetAttributeNode ("Condition");
72 this.dependsOnTargets = targetElement.GetAttributeNode ("DependsOnTargets");
73 this.batchingImpl = new BatchingImpl (project, this.targetElement);
74 foreach (XmlNode xn in targetElement.ChildNodes) {
75 if (xn is XmlElement) {
76 XmlElement xe = (XmlElement) xn;
77 if (xe.Name == "OnError") {
78 onErrorElements.Add (xe);
81 TaskElement te = new TaskElement ();
82 te.BindToXml (xe, this);
83 taskElements.Add (te);
88 internal void Build ()
90 buildState = BuildState.Started;
91 if (dependsOnTargets == null) {
93 } else if (dependsOnTargets.Value == "") {
96 Expression dependencies = new Expression (Project, dependsOnTargets.Value);
97 string[] targetsToBuildFirst = (string[]) dependencies.ToArray (typeof (string[]));
98 foreach (string target in targetsToBuildFirst) {
99 string trimmed = target.Trim ();
100 Target t = (Target) project.Targets [trimmed];
102 throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
103 if (t.BuildState == BuildState.NotStarted) {
106 if (t.BuildState == BuildState.Started)
107 throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
111 buildState = BuildState.Finished;
114 private void RealBuild ()
116 bool executeOnErrors = false;
121 if (this.batchingImpl.BuildNeeded ()) {
122 foreach (TaskElement te in taskElements) {
123 if (this.batchingImpl.BatchTaskElement (te) == false && te.ContinueOnError == false) {
124 executeOnErrors = true;
133 LogTargetFinished (result);
135 if (executeOnErrors == true)
139 private void ExecuteOnErrors ()
141 foreach (XmlElement onError in onErrorElements) {
142 // FIXME: add condition
143 if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
144 throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
145 string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
146 foreach (string t in targetsToExecute)
147 this.project.Targets [t].Build ();
151 private void LogTargetSkipped ()
153 BuildMessageEventArgs bmea;
154 bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
155 name), null, "MSBuild", MessageImportance.Normal);
156 engine.EventSource.FireMessageRaised (this, bmea);
159 private void LogTargetStarted ()
161 TargetStartedEventArgs tsea;
162 string projectFile = project.FullFileName;
163 tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
164 engine.EventSource.FireTargetStarted (this, tsea);
167 private void LogTargetFinished (bool succeeded)
169 TargetFinishedEventArgs tfea;
170 string projectFile = project.FullFileName;
171 tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
172 engine.EventSource.FireTargetFinished (this, tfea);
175 public TaskElement AddNewTaskElement (string taskName)
177 TaskElement te = new TaskElement ();
178 taskElements.Add (te);
182 public IEnumerator GetEnumerator ()
184 foreach (TaskElement te in taskElements) {
189 public void RemoveTaskElement (TaskElement taskElement)
191 taskElements.Remove (taskElement);
194 public string Condition {
195 get { return condition.Value; }
196 set { condition.Value = value; }
199 public string DependsOnTargets {
201 if (dependsOnTargets == null)
204 return dependsOnTargets.Value;
207 if (dependsOnTargets != null)
208 dependsOnTargets.Value = value;
212 public bool IsImported {
213 get { return isImported; }
214 internal set { isImported = value; }
221 internal Project Project {
222 get { return project; }
225 internal BuildState BuildState {
226 get { return buildState; }
230 internal enum BuildState {