| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 
 | //
// TargetBatchingImpl.cs: Class that implements Target Batching Algorithm from the wiki.
//
// Author:
//   Marek Sieradzki (marek.sieradzki@gmail.com)
//   Ankit Jain (jankit@novell.com)
//
// (C) 2005 Marek Sieradzki
// Copyright 2008 Novell, Inc (http://www.novell.com)
// Copyright 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if NET_2_0
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Build.Framework;
namespace Microsoft.Build.BuildEngine {
	internal class TargetBatchingImpl : BatchingImplBase
	{
		string		inputs;
		string		outputs;
		public TargetBatchingImpl (Project project, XmlElement targetElement)
			: base (project)
		{
			if (targetElement == null)
				throw new ArgumentNullException ("targetElement");
			inputs = targetElement.GetAttribute ("Inputs");
			outputs = targetElement.GetAttribute ("Outputs");
		}
		public bool Build (Target target, out bool executeOnErrors)
		{
			executeOnErrors = false;
			try {
				if (!BuildTargetNeeded ()) {
					LogTargetStarted (target);
					LogTargetSkipped (target);
					LogTargetFinished (target, true);
					return true;
				}
				Init ();
				ParseTargetAttributes (target);
				BatchAndPrepareBuckets ();
				return Run (target, out executeOnErrors);
			} finally {
				consumedItemsByName = null;
				consumedMetadataReferences = null;
				consumedQMetadataReferences = null;
				consumedUQMetadataReferences = null;
				batchedItemsByName = null;
				commonItemsByName = null;
			}
		}
		bool Run (Target target, out bool executeOnErrors)
		{
			executeOnErrors = false;
			if (buckets.Count > 0) {
				foreach (Dictionary<string, BuildItemGroup> bucket in buckets)
					if (!RunTargetWithBucket (bucket, target, out executeOnErrors))
						return false;
				return true;
			} else {
				return RunTargetWithBucket (null, target, out executeOnErrors);
			}
		}
		bool RunTargetWithBucket (Dictionary<string, BuildItemGroup> bucket, Target target, out bool executeOnErrors)
		{
			bool target_result = true;
			executeOnErrors = false;
			LogTargetStarted (target);
			if (bucket != null)
				project.PushBatch (bucket, commonItemsByName);
			try {
				if (!BuildTargetNeeded ()) {
					LogTargetSkipped (target);
					return true;
				}
				for (int i = 0; i < target.BuildTasks.Count; i ++) {
					//FIXME: parsing attributes repeatedly
					BuildTask bt = target.BuildTasks [i];
					TaskBatchingImpl batchingImpl = new TaskBatchingImpl (project);
					bool task_result = batchingImpl.Build (bt, out executeOnErrors);
					if (task_result)
						continue;
					// task failed, if ContinueOnError,
					// ignore failed state for target
					target_result = bt.ContinueOnError;
					if (!bt.ContinueOnError) {
						executeOnErrors = true;
						return false;
					}
				}
			} finally {
				if (bucket != null)
					project.PopBatch ();
				LogTargetFinished (target, target_result);
			}
			return target_result;
		}
		// Parse target's Input and Output attributes to get list of referenced
		// metadata and items to determine batching
		void ParseTargetAttributes (Target target)
		{
			if (!String.IsNullOrEmpty (inputs))
				ParseAttribute (inputs);
			if (!String.IsNullOrEmpty (outputs))
				ParseAttribute (outputs);
		}
		bool BuildTargetNeeded ()
		{
			ITaskItem [] inputFiles;
			ITaskItem [] outputFiles;
			DateTime oldestInput, youngestOutput;
			if (String.IsNullOrEmpty (inputs.Trim ()))
				return true;
			if (String.IsNullOrEmpty (outputs.Trim ()))
				return true;
			Expression e = new Expression ();
			e.Parse (inputs, ParseOptions.AllowItemsMetadataAndSplit);
			inputFiles = (ITaskItem[]) e.ConvertTo (project, typeof (ITaskItem[]), ExpressionOptions.ExpandItemRefs);
			e = new Expression ();
			e.Parse (outputs, ParseOptions.AllowItemsMetadataAndSplit);
			outputFiles = (ITaskItem[]) e.ConvertTo (project, typeof (ITaskItem[]), ExpressionOptions.ExpandItemRefs);
			if (inputFiles == null || inputFiles.Length == 0)
				return false;
			//FIXME: if input specified, then output must also
			//	 be there, add tests and confirm
			if (outputFiles == null || outputFiles.Length == 0)
				return false;
			if (File.Exists (inputFiles [0].ItemSpec))
				oldestInput = File.GetLastWriteTime (inputFiles [0].ItemSpec);
			else
				return true;
			if (File.Exists (outputFiles [0].ItemSpec))
				youngestOutput = File.GetLastWriteTime (outputFiles [0].ItemSpec);
			else
				return true;
			foreach (ITaskItem item in inputFiles) {
				string file = item.ItemSpec;
				if (file.Trim () == String.Empty)
					continue;
				if (File.Exists (file.Trim ())) {
					if (File.GetLastWriteTime (file.Trim ()) > oldestInput)
						oldestInput = File.GetLastWriteTime (file.Trim ());
				} else {
					return true;
				}
			}
			foreach (ITaskItem item in outputFiles) {
				string file = item.ItemSpec;
				if (file.Trim () == String.Empty)
					continue;
				if (File.Exists (file.Trim ())) {
					if (File.GetLastWriteTime (file.Trim ()) < youngestOutput)
						youngestOutput = File.GetLastWriteTime (file.Trim ());
				} else
					return true;
			}
			if (oldestInput > youngestOutput)
				return true;
			else
				return false;
		}
 		void LogTargetSkipped (Target target)
		{
			BuildMessageEventArgs bmea;
			bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
				target.Name), null, "MSBuild", MessageImportance.Normal);
			target.Engine.EventSource.FireMessageRaised (this, bmea);
		}
		void LogTargetStarted (Target target)
		{
			TargetStartedEventArgs tsea;
			string projectFile = project.FullFileName;
			tsea = new TargetStartedEventArgs (String.Format ("Target {0} started.", target.Name), null,
					target.Name, projectFile, target.TargetFile);
			target.Engine.EventSource.FireTargetStarted (this, tsea);
		}
		void LogTargetFinished (Target target, bool succeeded)
		{
			TargetFinishedEventArgs tfea;
			string projectFile = project.FullFileName;
			tfea = new TargetFinishedEventArgs (String.Format ("Target {0} finished.", target.Name), null,
					target.Name, projectFile, target.TargetFile, succeeded);
			target.Engine.EventSource.FireTargetFinished (this, tfea);
		}
	}
}
#endif
 |