11using System ;
2- using System . Collections . Concurrent ;
3- using System . Collections . Generic ;
42using System . IO ;
53using System . Linq ;
64using System . Threading . Tasks ;
@@ -15,6 +13,7 @@ namespace VsLinuxDebugger.Core
1513 public class RemoteDebugger
1614 {
1715 private bool _buildSuccessful ;
16+ private TaskCompletionSource < bool > _buildTask = null ;
1817 private DTE _dte ;
1918 private LaunchBuilder _launchBuilder ;
2019 private string _launchJsonPath = string . Empty ;
@@ -45,6 +44,20 @@ public async Task<bool> BeginAsync(BuildOptions buildOptions)
4544 return false ;
4645 }
4746
47+ if ( buildOptions . HasFlag ( BuildOptions . Build ) )
48+ {
49+ BuildBegin ( ) ;
50+
51+ await _buildTask . Task ;
52+
53+ // Work completed
54+ if ( ! _buildSuccessful )
55+ {
56+ LogOutput ( "Build was not successful." ) ;
57+ return false ;
58+ }
59+ }
60+
4861 using ( var ssh = new SshTool ( _options , _launchBuilder ) )
4962 {
5063 if ( ! ssh . Connect ( ) )
@@ -53,39 +66,33 @@ public async Task<bool> BeginAsync(BuildOptions buildOptions)
5366 return false ;
5467 }
5568
56- //// _ssh = ssh;
57-
5869 ssh . TryInstallVsDbg ( ) ;
5970 ssh . MakeDeploymentFolder ( ) ;
6071 ssh . CleanDeploymentFolder ( ) ;
6172
62- if ( buildOptions . HasFlag ( BuildOptions . Build ) )
63- {
64- BuildBegin ( ) ;
65- }
66-
6773 if ( buildOptions . HasFlag ( BuildOptions . Deploy ) )
6874 {
6975 if ( _options . RemoteDebugDisplayGui )
7076 ssh . Bash ( "export DISPLAY=:0" ) ;
7177
72- ssh . UploadFilesAsync ( ) ;
78+ await ssh . UploadFilesAsync ( ) ;
7379 }
7480 else if ( buildOptions . HasFlag ( BuildOptions . Publish ) )
7581 {
76- // NOT IMPL
82+ // This is PUBLISH not our 'deployer'
7783 }
7884
7985 if ( buildOptions . HasFlag ( BuildOptions . Debug ) )
8086 {
81- //// AttachToProcess ();
87+ BuildDebugAttacher ( ) ;
8288 }
83-
84- //// _ssh = null;
8589 }
90+
91+ BuildCleanup ( ) ;
8692 }
8793 catch ( Exception ex )
8894 {
95+ LogOutput ( $ "An error occurred during the build process. { ex . Message } ") ;
8996 return false ;
9097 }
9198
@@ -110,10 +117,34 @@ private void AttachToProcess()
110117
111118 private void BuildBegin ( )
112119 {
120+ // TODO: Disable the menu buttons.
113121 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
122+
114123 var dte = ( DTE ) Package . GetGlobalService ( typeof ( DTE ) ) ;
115124 BuildEvents = dte . Events . BuildEvents ;
116125
126+ _buildTask = new TaskCompletionSource < bool > ( ) ;
127+
128+ BuildEvents . OnBuildProjConfigDone += ( string project , string projectConfig , string platform , string solutionConfig , bool success ) =>
129+ {
130+ LogOutput ( $ "Project: { project } --- Success: { success } \n ") ;
131+
132+ if ( ! success )
133+ BuildCleanup ( ) ;
134+
135+ _buildSuccessful = Path . GetFileName ( project ) == $ "{ _launchBuilder . ProjectName } .csproj" && success ;
136+ } ;
137+
138+ BuildEvents . OnBuildDone += ( vsBuildScope scope , vsBuildAction action ) =>
139+ {
140+ // TODO: Re-endable the menu buttons.
141+ // Inform system that the task is complete
142+ _buildTask ? . TrySetResult ( true ) ;
143+
144+ var not = ! _buildSuccessful ? "not" : "" ;
145+ LogOutput ( $ "Build was { not } successful") ;
146+ } ;
147+
117148 // For some reason, cleanup isn't actually always ran when there has been an error.
118149 // This removes the fact that if you run a debug attempt, get a file error, that you don't get 2 message boxes, 3 message boxes, etc for each attempt.
119150 ////BuildEvents.OnBuildDone -= BuildEvents_OnBuildDoneAsync;
@@ -127,6 +158,7 @@ private void BuildBegin()
127158
128159 private void BuildCleanup ( )
129160 {
161+ // TODO: The file should be located in the project's output
130162 File . Delete ( _launchJsonPath ) ;
131163
132164 //// BuildEvents.OnBuildDone -= BuildEvents_OnBuildDoneAsync;
@@ -136,14 +168,12 @@ private void BuildCleanup()
136168 /// <summary>
137169 /// Start debugging using the remote visual studio server adapter
138170 /// </summary>
139- private void BuildDebug ( )
171+ private void BuildDebugAttacher ( )
140172 {
141- throw new NotImplementedException ( ) ;
173+ _launchJsonPath = _launchBuilder . GenerateLaunchJson ( ) ;
142174
143- //// _launchJsonPath = _localhost.ToJson();
144- ////
145- //// var dte = (DTE2)Package.GetGlobalService(typeof(SDTE));
146- //// dte.ExecuteCommand("DebugAdapterHost.Launch", $"/LaunchJson:\"{_launchJsonPath}\"");
175+ var dte = ( DTE2 ) Package . GetGlobalService ( typeof ( SDTE ) ) ;
176+ dte . ExecuteCommand ( "DebugAdapterHost.Launch" , $ "/LaunchJson:\" { _launchJsonPath } \" ") ;
147177 }
148178
149179 private bool Initialize ( )
@@ -155,25 +185,18 @@ private bool Initialize()
155185 if ( project == null )
156186 return false ;
157187
158- _launchBuilder = new LaunchBuilder ( _options )
159- {
160- AssemblyName = project . Properties . Item ( "AssemblyName" ) . Value . ToString ( ) ,
161- ProjectConfigName = project . ConfigurationManager . ActiveConfiguration . ConfigurationName ,
162- ProjectFullName = project . FullName ,
163- ProjectName = project . Name ,
164- SolutionFullName = dte . Solution . FullName ,
165- SolutionDirPath = Path . GetDirectoryName ( dte . Solution . FullName ) ,
166- OutputDirName = project . ConfigurationManager . ActiveConfiguration . Properties . Item ( "OutputPath" ) . Value . ToString ( ) ,
167- OutputDirFullName = Path . Combine ( Path . GetDirectoryName ( project . FullName ) , _launchBuilder . OutputDirName ) ,
168- } ;
188+ _launchBuilder = new LaunchBuilder ( dte , project , _options ) ;
169189
190+ // TODO: Commandline Args
170191 //// if (_options.UseCommandLineArgs)
171192 //// _launchBuilder.CommandLineArgs = ... extract from localSettings.json
172193
173194 return true ;
174195 }
175196
176197 /*
198+ * Borrowed from VSMonoDebugger
199+ *
177200 public async Task BuildStartupProjectAsync()
178201 {
179202 await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
@@ -228,38 +251,6 @@ private int BuildSolution()
228251 }
229252 */
230253
231- /////// <summary>Build is ready for the next steps.</summary>
232- ////private async void BuildEvents_OnBuildDoneAsync(vsBuildScope scope, vsBuildAction action)
233- ////{
234- //// if (_buildSuccessful)
235- //// {
236- //// string errormessage = await TransferFiles2Async().ConfigureAwait(true);
237- ////
238- //// if (errormessage == "")
239- //// {
240- //// StartDebug();
241- //// BuildCleanup();
242- //// }
243- //// else
244- //// {
245- //// Output($"Transferring files failed: {errormessage}");
246- //// }
247- //// }
248- ////}
249- ////
250- /////// <summary>The build finised sucessfully and no errors were found.</summary>
251- ////private void BuildEvents_OnBuildProjConfigDone(string project, string projectConfig, string platform, string solutionConfig, bool success)
252- ////{
253- //// string debugtext = $"Project: {project} --- Success: {success}\n";
254- ////
255- //// if (!success)
256- //// {
257- //// BuildCleanup();
258- //// }
259- ////
260- //// _buildSuccessful = Path.GetFileName(project) == _localhost.ProjectName + ".csproj" && success;
261- ////}
262-
263254 private bool IsCSharpProject ( Project vsProject )
264255 {
265256 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
0 commit comments