Skip to content

Commit 824e7d3

Browse files
authored
Merge pull request #40 from SuessLabs/feature/AsyncConnections
Reduce VS Freezing
2 parents a5bb403 + 317da60 commit 824e7d3

File tree

5 files changed

+65
-45
lines changed

5 files changed

+65
-45
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
namespace GuiApp.PrismAvalonia.ViewModels
1+
using Microsoft.CodeAnalysis;
2+
using Prism.Commands;
3+
4+
namespace GuiApp.PrismAvalonia.ViewModels
25
{
36
public class DashboardViewModel : ViewModelBase
47
{
58
public DashboardViewModel()
69
{
710
Title = "Dashboard View!";
811
}
12+
13+
public DelegateCommand CmdBreakPoint => new(() =>
14+
{
15+
// Force a breakpoint
16+
System.Diagnostics.Debug.WriteLine("Breakpoint triggering");
17+
System.Diagnostics.Debugger.Break();
18+
});
919
}
1020
}

sandbox/GuiApp.PrismAvalonia/Views/DashboardView.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
1111
<TextBlock Text="Welcome to Prism.Avalonia!" />
1212
<TextBlock Text="{Binding Title}" />
13+
<Button Content="Trigger Breakpoint" Command="{Binding CmdBreakPoint}" />
1314
</StackPanel>
1415
</DockPanel>
1516
</UserControl>

src/VsLinuxDebugger/Core/RemoteDebugger.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ public async Task<bool> BeginAsync(BuildOptions buildOptions)
6363

6464
using (var ssh = new SshTool(_options, _launchBuilder))
6565
{
66-
if (!ssh.Connect())
66+
var success = await ssh.ConnectAsync();
67+
if (!success)
6768
{
6869
Logger.Output("Could not connect to remote device.");
6970
return false;
7071
}
7172

72-
ssh.TryInstallVsDbg();
73-
ssh.MakeDeploymentFolder();
74-
ssh.CleanDeploymentFolder();
73+
await ssh.TryInstallVsDbgAsync();
74+
await ssh.MakeDeploymentFolderAsync();
75+
await ssh.CleanDeploymentFolderAsync();
7576

7677
if (buildOptions.HasFlag(BuildOptions.Deploy))
7778
{

src/VsLinuxDebugger/Core/SshTool.cs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,35 @@ public SshTool(UserOptions opts, LaunchBuilder launch)
3131

3232
public SshClient Ssh => _ssh;
3333

34-
public string Bash(string command, bool createCommand = false)
34+
public async Task<string> BashAsync(string command, bool createCommand = false)
3535
{
3636
try
3737
{
3838
if (!_isConnected)
3939
{
4040
// attempt to reconnect
41-
if (!Connect())
41+
if (!await ConnectAsync())
4242
return "Cannot connect to remote host to execute Bash command.";
4343
}
4444

4545
Logger.Output($"BASH> {command}");
4646

4747
SshCommand cmd;
48-
if (!createCommand)
49-
{
50-
cmd = _ssh.RunCommand(command);
51-
}
52-
else
48+
49+
cmd = await Task.Run(() =>
5350
{
54-
cmd = _ssh.CreateCommand(command);
55-
cmd.Execute();
56-
}
51+
if (!createCommand)
52+
{
53+
cmd = _ssh.RunCommand(command);
54+
}
55+
else
56+
{
57+
cmd = _ssh.CreateCommand(command);
58+
cmd.Execute();
59+
}
60+
61+
return cmd;
62+
});
5763

5864
return cmd.Result;
5965
}
@@ -79,6 +85,7 @@ public string BashStream(string command, bool isSudo = false)
7985
var modes = new Dictionary<TerminalModes, uint>();
8086
// modes.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53);
8187

88+
// TODO: Make async
8289
using (var stream = _ssh.CreateShellStream("xterm", 255, 50, 800, 600, 1024, modes))
8390
{
8491
stream.Write(command + "\n");
@@ -89,13 +96,13 @@ public string BashStream(string command, bool isSudo = false)
8996
}
9097
else
9198
{
92-
Logger.Output($"BASH-SUDO: TRUE");
99+
Logger.Debug($"BASH-SUDO: TRUE");
93100
result = stream.Expect("password");
94-
Logger.Output($"BASH-SUDO: {result}");
101+
Logger.Debug($"BASH-SUDO: {result}");
95102

96103
stream.Write(_opts.UserPass + "\n");
97104
result = stream.Expect(prompt);
98-
Logger.Output($"BASH-SUDO: {result}");
105+
Logger.Debug($"BASH-SUDO: {result}");
99106
}
100107
}
101108

@@ -124,34 +131,30 @@ public string BashStream(string command, string searchText)
124131

125132
result = stream.ReadLine();
126133

127-
Logger.Output($"BashEx: Waiting for search text, '{searchText}'");
134+
Logger.Debug($"BashEx: Waiting for search text, '{searchText}'");
128135
result = stream.Expect(searchText);
129-
Logger.Output($"BashEx: {result}");
136+
Logger.Debug($"BashEx: {result}");
130137
}
131138

132139
return result;
133140
}
134141

135142
/// <summary>Cleans the contents of the deployment path.</summary>
136143
/// <param name="fullScrub">Clear entire base deployment folder (TRUE) or just our project.</param>
137-
public void CleanDeploymentFolder(bool fullScrub = false)
144+
public async Task CleanDeploymentFolderAsync(bool fullScrub = false)
138145
{
139146
// Whole deployment folder and hidden files
140147
// rm -rf xxx/* == Contents of the folder but not the folder itself
141148
// rm -rf xxx/{*,.*} == All hidden files and folders
142149
var filesAndFolders = "{*,.*}";
143150

144151
if (fullScrub)
145-
{
146-
Bash($"rm -rf \"{_opts.RemoteDeployBasePath}/{filesAndFolders}\"");
147-
}
152+
await BashAsync($"rm -rf \"{_opts.RemoteDeployBasePath}/{filesAndFolders}\"");
148153
else
149-
{
150-
Bash($"rm -rf {_launch.RemoteDeployProjectFolder}/{filesAndFolders}");
151-
}
154+
await BashAsync($"rm -rf {_launch.RemoteDeployProjectFolder}/{filesAndFolders}");
152155
}
153156

154-
public bool Connect()
157+
public async Task<bool> ConnectAsync()
155158
{
156159
PrivateKeyFile keyFile = null;
157160

@@ -178,7 +181,7 @@ public bool Connect()
178181
? new SshClient(_opts.HostIp, _opts.HostPort, _opts.UserName, keyFile)
179182
: new SshClient(_opts.HostIp, _opts.HostPort, _opts.UserName, _opts.UserPass);
180183

181-
_ssh.Connect();
184+
await Task.Run(() => _ssh.Connect());
182185
}
183186
catch (Exception ex)
184187
{
@@ -222,10 +225,10 @@ public void Dispose()
222225
_scp?.Dispose();
223226
}
224227

225-
public void MakeDeploymentFolder()
228+
public async Task MakeDeploymentFolderAsync()
226229
{
227230
// do we need SUDO?
228-
Bash($"mkdir -p {_opts.RemoteDeployBasePath}");
231+
await BashAsync($"mkdir -p {_opts.RemoteDeployBasePath}");
229232
//// Bash($"mkdir -p {_opts.RemoteDeployDebugPath}");
230233
//// Bash($"mkdir -p {_opts.RemoteDeployReleasePath}");
231234

@@ -234,17 +237,17 @@ public void MakeDeploymentFolder()
234237
: $":{_opts.UserGroupName}";
235238

236239
// Update ownership so it's not "root"
237-
Bash($"sudo chown -R {_opts.UserName}{group} {_opts.RemoteDeployBasePath}");
240+
await BashAsync($"sudo chown -R {_opts.UserName}{group} {_opts.RemoteDeployBasePath}");
238241
}
239242

240243
/// <summary>
241-
/// Instals VS Debugger if it doesn't exist already
244+
/// Install cURL and VS Debugger if it doesn't exist already.
242245
/// </summary>
243-
public void TryInstallVsDbg()
246+
public async Task TryInstallVsDbgAsync()
244247
{
245-
string arch = Bash("uname -m").Trim('\n');
248+
string arch = (await BashAsync("uname -m")).Trim('\n');
246249

247-
var curlExists = Bash("which curl ; echo $?");
250+
var curlExists = await BashAsync("which curl ; echo $?");
248251
if (curlExists.Equals("1\n"))
249252
{
250253
// TODO: Need to pass in password.
@@ -253,7 +256,7 @@ public void TryInstallVsDbg()
253256
}
254257

255258
//// OLD: var ret = Bash("[ -d ~/.vsdbg ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/.vsdbg");
256-
var ret = Bash($"[ -d {_opts.RemoteVsDbgBasePath} ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l {_opts.RemoteVsDbgBasePath}");
259+
var ret = await BashAsync($"[ -d {_opts.RemoteVsDbgBasePath} ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l {_opts.RemoteVsDbgBasePath}");
257260
Logger.Output($"Returned: {ret}");
258261
}
259262

@@ -282,7 +285,7 @@ public async Task<string> UploadFilesAsync()
282285
// TODO: Rev3 - Allow for both SFTP and SCP as a backup. This separating connection to a new disposable class.
283286
//// Logger.Output($"Connected to {_connectionInfo.Username}@{_connectionInfo.Host}:{_connectionInfo.Port} via SSH and {(_sftpClient != null ? "SFTP" : "SCP")}");
284287

285-
Bash($@"mkdir -p {_launch.RemoteDeployProjectFolder}");
288+
BashAsync($@"mkdir -p {_launch.RemoteDeployProjectFolder}");
286289

287290
var srcDirInfo = new DirectoryInfo(_launch.OutputDirFullPath);
288291
if (!srcDirInfo.Exists)
@@ -497,9 +500,10 @@ await Task.Run(() =>
497500
/// <returns>Returns true on success.</returns>
498501
private async Task<bool> PayloadDecompressAsync(string pathBuildTarGz, bool removeTarGz = true)
499502
{
503+
var decompressOutput = string.Empty;
504+
500505
try
501506
{
502-
var decompressOutput = string.Empty;
503507

504508
var cmd = "set -e";
505509
cmd += $";cd \"{_launch.RemoteDeployProjectFolder}\"";
@@ -509,19 +513,16 @@ private async Task<bool> PayloadDecompressAsync(string pathBuildTarGz, bool remo
509513
if (removeTarGz)
510514
cmd += $";rm \"{pathBuildTarGz}\"";
511515

512-
await Task.Run(() =>
513-
{
514-
decompressOutput = Bash(cmd);
515-
});
516+
decompressOutput = await BashAsync(cmd);
516517

517518
Logger.Output($"Payload Decompress results: '{decompressOutput}' (blank=OK)");
518-
519-
return true;
520519
}
521520
catch (Exception)
522521
{
523522
return false;
524523
}
524+
525+
return true;
525526
}
526527
}
527528
}

src/VsLinuxDebugger/Logger.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public static void Init(
4242
_name = name;
4343
}
4444

45+
public static void Debug(string message)
46+
{
47+
#if DEBUG
48+
Output($"DEBUG: {message}");
49+
#endif
50+
}
51+
4552
public static void Output(string message)
4653
{
4754
var msg = $"{FormattedTime}: {message}{Environment.NewLine}";

0 commit comments

Comments
 (0)