4 // Rolf Bjarne Kvinge (rolf@xamarin.com)
5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2011,2013 Xamarin Inc.
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.
30 using System.Threading;
31 using System.Threading.Tasks;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Internal;
35 using System.Collections.Generic;
37 namespace Microsoft.Build.Execution
39 public class BuildSubmission
41 static Random rnd = new Random ();
43 internal BuildSubmission (BuildManager build, BuildRequestData requestData)
46 this.request = requestData;
47 SubmissionId = rnd.Next ();
50 BuildRequestData request;
51 BuildSubmissionCompleteCallback callback;
52 bool is_started, is_completed, is_canceled;
53 ManualResetEvent wait_handle = new ManualResetEvent (false);
55 public object AsyncContext { get; private set; }
56 public BuildManager BuildManager { get; private set; }
57 public BuildResult BuildResult { get; set; }
58 public bool IsCompleted {
59 get { return is_completed; }
61 public int SubmissionId { get; private set; }
62 public WaitHandle WaitHandle {
65 throw new InvalidOperationException ("Build has not started yet");
70 internal BuildRequestData BuildRequest {
71 get { return this.request; }
74 internal void Cancel ()
77 throw new InvalidOperationException ("Build has already canceled");
81 public BuildResult Execute ()
83 var engine = new BuildEngine4 (this);
84 string toolsVersion = request.ExplicitlySpecifiedToolsVersion ?? request.ProjectInstance.ToolsVersion ?? BuildManager.OngoingBuildParameters.DefaultToolsVersion;
85 var outputs = new Dictionary<string,string> ();
86 BuildResult = engine.BuildProject (() => is_canceled, request.ProjectInstance, request.TargetNames, BuildManager.OngoingBuildParameters.GlobalProperties, outputs, toolsVersion);
93 public void ExecuteAsync (BuildSubmissionCompleteCallback callback, object context)
96 throw new InvalidOperationException ("Build has already started");
98 throw new InvalidOperationException ("Build has already canceled");
100 this.AsyncContext = context;
101 this.callback = callback;
103 BuildManager.TaskFactory.StartNew (Execute);