-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSidekickDockerUtility.cs
More file actions
181 lines (153 loc) · 4.98 KB
/
SidekickDockerUtility.cs
File metadata and controls
181 lines (153 loc) · 4.98 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using UnityEditor;
using UnityEngine;
using System.Diagnostics;
using System.IO;
using System.Linq;
public static class SidekickDockerUtility
{
private static SidekickConfig config;
private static string SidekickPath
{
get
{
if (config == null)
{
config = Resources.Load<SidekickConfig>("SidekickConfig");
if (config == null)
{
UnityEngine.Debug.LogError("Could not load SidekickConfig from Resources.");
return string.Empty;
}
}
return config.path;
}
}
[MenuItem("Sequence Dev/Start Sidekick", false, 0)]
private static void StartSidekick()
{
if (IsSidekickRunning()) return;
EnsureDockerDesktopRunning();
RunCommand("pnpm docker:restart", SidekickPath);
}
[MenuItem("Sequence Dev/Start Sidekick", true)]
private static bool ValidateStart() => !IsSidekickRunning();
[MenuItem("Sequence Dev/Stop Sidekick", false, 1)]
private static void StopSidekick()
{
if (!IsSidekickRunning()) return;
RunCommand("pnpm docker:stop", SidekickPath);
}
[MenuItem("Sequence Dev/Stop Sidekick", true)]
private static bool ValidateStop() => IsSidekickRunning();
private static void RunCommand(string command, string workingDirectory)
{
if (string.IsNullOrEmpty(workingDirectory) || !Directory.Exists(workingDirectory))
{
UnityEngine.Debug.LogError($"Sidekick path not set or invalid: {workingDirectory}");
return;
}
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c {command}",
WorkingDirectory = workingDirectory,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process process = new Process { StartInfo = psi };
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
UnityEngine.Debug.Log($"[Docker] {e.Data}");
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
UnityEngine.Debug.LogWarning($"[Docker] {e.Data}");
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
private static void EnsureDockerDesktopRunning()
{
const int maxWaitTimeSeconds = 120;
const int pollIntervalMs = 2000;
if (!Process.GetProcessesByName("Docker Desktop").Any())
{
string dockerDesktopPath = @"C:\Program Files\Docker\Docker\Docker Desktop.exe";
if (File.Exists(dockerDesktopPath))
{
Process.Start(dockerDesktopPath);
}
else
{
UnityEngine.Debug.LogWarning("[Docker] Docker Desktop not found at default path.");
return;
}
}
var stopwatch = new Stopwatch();
stopwatch.Start();
while (stopwatch.Elapsed.TotalSeconds < maxWaitTimeSeconds)
{
if (IsDockerDaemonReady())
{
return;
}
System.Threading.Thread.Sleep(pollIntervalMs);
}
UnityEngine.Debug.LogError("[Docker] Timed out waiting for Docker to become ready.");
}
private static bool IsDockerDaemonReady()
{
try
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c docker info",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
};
using (Process process = Process.Start(psi))
{
process.WaitForExit(3000);
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
return process.ExitCode == 0 && output.Contains("Server Version");
}
}
catch
{
return false;
}
}
private static bool IsSidekickRunning()
{
try
{
var psi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c docker ps --format \"{{.Names}}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(psi))
{
process.WaitForExit();
var output = process.StandardOutput.ReadToEnd();
return output.Contains("sidekick");
}
}
catch
{
return false;
}
}
}