Fix API: remove non-existent property in .NET (copy-paste bad).
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Execution / BuildSubmission.cs
1 // BuildSubmission.cs
2 //
3 // Author:
4 //   Rolf Bjarne Kvinge (rolf@xamarin.com)
5 //   Atsushi Enomoto (atsushi@xamarin.com)
6 //
7 // Copyright (C) 2011,2013 Xamarin Inc.
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using System.Linq;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Internal;
35 using System.Collections.Generic;
36
37 namespace Microsoft.Build.Execution
38 {
39         public class BuildSubmission
40         {
41                 static Random rnd = new Random ();
42
43                 internal BuildSubmission (BuildManager build, BuildRequestData requestData)
44                 {
45                         BuildManager = build;
46                         this.request = requestData;
47                         SubmissionId = rnd.Next ();
48                 }
49
50                 BuildRequestData request;
51                 BuildSubmissionCompleteCallback callback;
52                 bool is_started, is_completed, is_canceled;
53                 ManualResetEvent wait_handle = new ManualResetEvent (false);
54
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; }
60                 }
61                 public int SubmissionId { get; private set; }
62                 public WaitHandle WaitHandle {
63                         get {
64                                 if (!is_started)
65                                         throw new InvalidOperationException ("Build has not started yet");
66                                 return wait_handle;
67                         }
68                 }
69                 
70                 internal BuildRequestData BuildRequest {
71                         get { return this.request; }
72                 }
73
74                 internal void Cancel ()
75                 {
76                         if (is_canceled)
77                                 throw new InvalidOperationException ("Build has already canceled");
78                         is_canceled = true;
79                 }
80
81                 public BuildResult Execute ()
82                 {
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);
87                         wait_handle.Set ();
88                         if (callback != null)
89                                 callback (this);
90                         return BuildResult;
91                 }
92
93                 public void ExecuteAsync (BuildSubmissionCompleteCallback callback, object context)
94                 {
95                         if (is_started)
96                                 throw new InvalidOperationException ("Build has already started");
97                         if (is_canceled)
98                                 throw new InvalidOperationException ("Build has already canceled");
99                         is_started = true;
100                         this.AsyncContext = context;
101                         this.callback = callback;
102                         
103                         BuildManager.TaskFactory.StartNew (Execute);
104                 }
105         }
106 }
107