This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttachToDockerContainerDialog.xaml.cs
162 lines (126 loc) · 5.45 KB
/
AttachToDockerContainerDialog.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace AttachToDockerContainer
{
public partial class AttachToDockerContainerDialog : DialogWindow
{
private const string VsDbgDefaultPath = "/vsdbg/vsdbg";
private readonly IServiceProvider _serviceProvider;
public AttachToDockerContainerDialog(IServiceProvider serviceProvider)
{
ThreadHelper.ThrowIfNotOnUIThread();
_serviceProvider = serviceProvider;
InitializeComponent();
AttachButton.IsEnabled = false;
PidComboBox.IsEnabled = false;
var containerNames = GetContainerNames();
var (previousContainer, previousVsDbgPath) = GetSettings();
ContainerComboBox.ItemsSource = containerNames;
ContainerComboBox.Text = containerNames.Contains(previousContainer)
? previousContainer
: containerNames.FirstOrDefault();
VsDbgPathTextBox.Text = previousVsDbgPath ?? VsDbgDefaultPath;
UpdateDotNetPIDs();
ContainerComboBox.SelectionChanged += ContainerComboBox_SelectionChanged;
}
private void AttachButton_Click(object sender, RoutedEventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
var containerName = ContainerComboBox.Text;
var vsDbgPath = VsDbgPathTextBox.Text;
var pid = (int)PidComboBox.SelectedItem;
SetSettings(containerName, vsDbgPath);
DebugAdapterHostLauncher.Instance.Launch(containerName, vsDbgPath, pid);
Close();
}
private void ContainerComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Action action = () => UpdateDotNetPIDs();
Dispatcher.BeginInvoke(action);
}
private void UpdateDotNetPIDs()
{
AttachButton.IsEnabled = false;
PidComboBox.IsEnabled = false;
var containerName = ContainerComboBox.SelectedItem as string;
if (string.IsNullOrWhiteSpace(containerName))
return;
var pidofResult = DockerCli.Execute($"exec -it {containerName} pidof dotnet");
var dotnetPidsParsed = pidofResult
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(pidStr => (valid: int.TryParse(pidStr.Trim(), out int pid), pid))
.ToArray();
if (dotnetPidsParsed.Length == 0 || dotnetPidsParsed.Any(vp => !vp.valid))
{
PidComboBox.ItemsSource = new[] { "Cannot find dotnet process!" };
PidComboBox.SelectedIndex = 0;
}
else
{
var dotnetPids = dotnetPidsParsed.Select(vp => vp.pid).ToArray();
PidComboBox.ItemsSource = dotnetPids;
PidComboBox.SelectedIndex = 0;
if (dotnetPids.Length > 1)
PidComboBox.IsEnabled = true;
if (dotnetPids.Length > 0)
AttachButton.IsEnabled = true;
}
}
private string[] GetContainerNames()
{
var output = DockerCli.Execute("ps --format \"{{.Names}}\"");
return output
.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
.OrderBy(n => n)
.ToArray();
}
private (string container, string vsDbg) GetSettings()
{
const string collectionPath = nameof(AttachToDockerContainerDialog);
ThreadHelper.ThrowIfNotOnUIThread();
SettingsStore.CollectionExists(collectionPath, out int exists);
if (exists != 1)
{
SettingsStore.CreateCollection(collectionPath);
}
SettingsStore.GetString(collectionPath, "container", out string container);
SettingsStore.GetString(collectionPath, "vsdbg", out string vsdbg);
return (container, vsdbg);
}
private void SetSettings(string container, string vsdbg)
{
const string collectionPath = nameof(AttachToDockerContainerDialog);
ThreadHelper.ThrowIfNotOnUIThread();
SettingsStore.CollectionExists(collectionPath, out int exists);
if (exists != 1)
{
SettingsStore.CreateCollection(collectionPath);
}
SettingsStore.SetString(collectionPath, "container", container);
SettingsStore.SetString(collectionPath, "vsdbg", vsdbg);
}
private IVsWritableSettingsStore _settingsStore = null;
private IVsWritableSettingsStore SettingsStore
{
get
{
ThreadHelper.ThrowIfNotOnUIThread();
if (_settingsStore == null)
{
var settingsManager = (IVsSettingsManager)_serviceProvider.GetService(typeof(SVsSettingsManager));
// Write the user settings to _settingsStore.
settingsManager.GetWritableSettingsStore(
(uint)__VsSettingsScope.SettingsScope_UserSettings,
out _settingsStore);
}
return _settingsStore;
}
}
}
}