2 // MSBuild.cs: Task that can run .*proj files
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
8 // Copyright 2011 Xamarin Inc
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
34 using Microsoft.Build.BuildEngine;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.Tasks {
41 public class MSBuild : TaskExtension {
43 ITaskItem [] projects;
46 bool runEachTargetSeparately;
47 bool stopOnFirstFailure;
49 ITaskItem [] targetOutputs;
56 public override bool Execute ()
58 if (projects.Length == 0)
63 bool all_result = true;
64 stopOnFirstFailure = false;
65 List <ITaskItem > outputItems = new List <ITaskItem> ();
66 string currentDirectory = Environment.CurrentDirectory;
69 var global_properties = SplitPropertiesToDictionary ();
71 Log.LogMessage (MessageImportance.Low, "Global Properties:");
72 if (global_properties != null)
73 foreach (KeyValuePair<string, string> pair in global_properties)
74 Log.LogMessage (MessageImportance.Low, "\t{0} = {1}", pair.Key, pair.Value);
76 foreach (ITaskItem project in projects) {
77 filename = project.GetMetadata ("FullPath");
78 if (!File.Exists (filename)) {
79 Log.LogError ("Could not find the project file '{0}'", filename);
80 if (stopOnFirstFailure)
86 Directory.SetCurrentDirectory (Path.GetDirectoryName (filename));
87 outputs = new Hashtable ();
90 // Order of precedence:
91 // ToolsVersion property, %(Project.ToolsVersion)
92 string tv = ToolsVersion;
93 if (String.IsNullOrEmpty (tv))
94 // metadata on the Project item
95 tv = project.GetMetadata ("ToolsVersion");
97 if (!String.IsNullOrEmpty (tv) && Engine.GlobalEngine.Toolsets [tv] == null)
98 throw new UnknownToolsVersionException (tv);
100 result = BuildEngine2.BuildProjectFile (filename, targets, global_properties, outputs, tv);
101 } catch (InvalidProjectFileException e) {
102 Log.LogError ("Error building project {0}: {1}", filename, e.Message);
110 foreach (DictionaryEntry de in outputs) {
111 ITaskItem [] array = (ITaskItem []) de.Value;
112 foreach (ITaskItem item in array) {
114 ITaskItem new_item = new TaskItem (item);
116 // copy the metadata from original @project to here
117 // CopyMetadataTo does _not_ overwrite
118 project.CopyMetadataTo (new_item);
120 outputItems.Add (new_item);
122 //FIXME: Correctly rebase output paths to be relative to the
125 // File.Copy (item.ItemSpec, Path.Combine (currentDirectory, item.ItemSpec), true);
129 if (stopOnFirstFailure)
133 Directory.SetCurrentDirectory (currentDirectory);
137 targetOutputs = outputItems.ToArray ();
139 Directory.SetCurrentDirectory (currentDirectory);
143 void ThrowIfInvalidToolsVersion (string toolsVersion)
145 if (!String.IsNullOrEmpty (toolsVersion) && Engine.GlobalEngine.Toolsets [toolsVersion] == null)
146 throw new UnknownToolsVersionException (toolsVersion);
150 public ITaskItem [] Projects {
151 get { return projects; }
152 set { projects = value; }
156 public string [] Properties {
157 get { return properties; }
158 set { properties = value; }
161 public bool RebaseOutputs {
162 get { return rebaseOutputs; }
163 set { rebaseOutputs = value; }
167 public bool RunEachTargetSeparately {
168 get { return runEachTargetSeparately; }
169 set { runEachTargetSeparately = value; }
172 public bool StopOnFirstFailure {
173 get { return stopOnFirstFailure; }
174 set { stopOnFirstFailure = value; }
178 public ITaskItem [] TargetOutputs {
179 get { return targetOutputs; }
182 public string [] Targets {
183 get { return targets; }
184 set { targets = value; }
187 public bool BuildInParallel {
188 get { return buildInParallel; }
189 set { buildInParallel = value; }
192 public string ToolsVersion {
196 SortedDictionary<string, string> SplitPropertiesToDictionary ()
198 if (properties == null)
201 var global_properties = new SortedDictionary<string, string> ();
202 foreach (string kvpair in properties) {
203 if (String.IsNullOrEmpty (kvpair))
206 string [] parts = kvpair.Trim ().Split (new char [] {'='}, 2);
207 if (parts.Length != 2) {
208 Log.LogWarning ("Invalid key/value pairs ({0}) in Properties, ignoring.", kvpair);
212 global_properties.Add (parts [0], parts [1]);
215 return global_properties;