-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
311 lines (273 loc) · 11.1 KB
/
Copy pathProgram.cs
File metadata and controls
311 lines (273 loc) · 11.1 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using DeepOcean.Deploy.Services;
using DeepOcean.Deploy.Tools;
using EmbedIO;
using EmbedIO.Files;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace DeepOcean.Deploy
{
class Program
{
static void Main(string[] args)
{
var url = "http://localhost:5000/";
if (args.Length > 0)
url = args[0];
var baseDir = AppContext.BaseDirectory;
var projectRoot = baseDir;
// If running from bin/Debug, step back to the project root
if (baseDir.Contains("bin") && baseDir.Contains("Debug"))
{
var dirInfo = new DirectoryInfo(baseDir);
while (dirInfo != null && !dirInfo.Name.Equals("DeepOcean.Deploy", StringComparison.OrdinalIgnoreCase))
{
dirInfo = dirInfo.Parent;
}
if (dirInfo != null)
{
projectRoot = dirInfo.FullName;
Directory.SetCurrentDirectory(projectRoot);
}
}
var wwwroot = Path.Combine(projectRoot, "wwwroot");
using (var server = CreateWebServer(url, wwwroot))
{
server.RunAsync();
Console.WriteLine($"Server is running on {url}");
Console.WriteLine("Server is running indefinitely. Close the console window to stop it.");
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
private static WebServer CreateWebServer(string url, string wwwroot)
{
var server = new WebServer(o => o
.WithUrlPrefix(url)
.WithMode(HttpListenerMode.EmbedIO))
.WithWebApi("/api", m => m.WithController<DeployController>())
.WithStaticFolder("/", wwwroot, true, m => m.WithContentCaching(false));
return server;
}
}
public class DeployRequest
{
public List<string> Projects { get; set; }
}
public class DeployController : WebApiController
{
private static readonly string ConfigFile = "projects_config.json";
public static ConcurrentQueue<string> LogsQueue = new ConcurrentQueue<string>();
public static List<string> LogsList = new List<string>();
public static void AddLog(string message)
{
Console.WriteLine(message);
LogsQueue.Enqueue(message);
LogsList.Add(message);
}
[Route(HttpVerbs.Get, "/projects")]
public async Task GetProjects()
{
if (!File.Exists(ConfigFile))
{
File.WriteAllText(ConfigFile, "[]");
}
string json = File.ReadAllText(ConfigFile);
HttpContext.Response.ContentType = "application/json";
using (var writer = HttpContext.OpenResponseText())
{
await writer.WriteAsync(json);
}
}
[Route(HttpVerbs.Post, "/projects")]
public async Task SaveProjects()
{
string requestBody;
using (var reader = HttpContext.OpenRequestText())
{
requestBody = await reader.ReadToEndAsync();
}
var json = JsonConvert.DeserializeObject<JArray>(requestBody);
if (json == null)
{
HttpContext.Response.StatusCode = 400;
return;
}
File.WriteAllText(ConfigFile, JsonConvert.SerializeObject(json, Formatting.Indented));
HttpContext.Response.StatusCode = 200;
}
[Route(HttpVerbs.Get, "/processes")]
public async Task GetProcesses()
{
var assembly = typeof(EventTools).Assembly;
var eventToolTypes = assembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(EventTools)));
var result = new List<object>();
foreach (var type in eventToolTypes)
{
result.Add(new
{
Name = type.Name,
Properties = GetTypeSchema(type)
});
}
// Also include custom scripted tools from CustomScripts/Tools/
var customTools = RoslynCompilerService.ListTools();
foreach (var toolName in customTools)
{
var props = await RoslynCompilerService.GetToolSchemaAsync(toolName);
result.Add(new { Name = toolName, Properties = props, IsCustom = true });
}
HttpContext.Response.ContentType = "application/json";
using (var writer = HttpContext.OpenResponseText())
{
await writer.WriteAsync(JsonConvert.SerializeObject(result));
}
}
[Route(HttpVerbs.Get, "/logs")]
public async Task GetLogs()
{
var result = new { logs = LogsList };
HttpContext.Response.ContentType = "application/json";
using (var writer = HttpContext.OpenResponseText())
{
await writer.WriteAsync(JsonConvert.SerializeObject(result));
}
}
[Route(HttpVerbs.Post, "/deploy")]
public async Task PostDeploy()
{
var requestData = await HttpContext.GetRequestDataAsync<DeployRequest>();
if (requestData == null || requestData.Projects == null || requestData.Projects.Count == 0)
{
HttpContext.Response.StatusCode = 400;
return;
}
LogsList.Clear();
AddLog($"Starting deployment for selected projects...");
try
{
if (!File.Exists(ConfigFile))
{
throw new Exception("Projects config file not found.");
}
var configJson = File.ReadAllText(ConfigFile);
var allProjects = JsonConvert.DeserializeObject<List<ProjectConfig>>(configJson);
var projectsToRun = allProjects.Where(p => requestData.Projects.Contains(p.ProjectName)).ToList();
var workFlowObj = new WorkFlowLogic();
var response = await workFlowObj.RunProjects(projectsToRun);
if (response.Success)
{
AddLog("✅ DEPLOYMENT COMPLETED SUCCESSFULLY!");
HttpContext.Response.StatusCode = 200;
}
else
{
AddLog($"❌ DEPLOYMENT FAILED: {response.Message}");
HttpContext.Response.StatusCode = 500;
}
}
catch (Exception ex)
{
AddLog($"❌ FATAL ERROR: {ex.Message}");
HttpContext.Response.StatusCode = 500;
}
}
// ─── Custom Tools API ────────────────────────────────────────────────
[Route(HttpVerbs.Get, "/custom-tools")]
public async Task GetCustomTools()
{
var tools = RoslynCompilerService.ListTools();
HttpContext.Response.ContentType = "application/json";
using var writer = HttpContext.OpenResponseText();
await writer.WriteAsync(JsonConvert.SerializeObject(tools));
}
[Route(HttpVerbs.Get, "/custom-tools/{name}")]
public async Task GetCustomTool(string name)
{
var tool = RoslynCompilerService.LoadTool(name);
HttpContext.Response.ContentType = "application/json";
using var writer = HttpContext.OpenResponseText();
await writer.WriteAsync(JsonConvert.SerializeObject(tool));
}
[Route(HttpVerbs.Post, "/custom-tools")]
public async Task SaveCustomTool()
{
string body;
using (var reader = HttpContext.OpenRequestText())
body = await reader.ReadToEndAsync();
var tool = JsonConvert.DeserializeObject<CustomToolFiles>(body);
if (tool == null || string.IsNullOrWhiteSpace(tool.Name))
{
HttpContext.Response.StatusCode = 400;
return;
}
RoslynCompilerService.SaveTool(tool);
HttpContext.Response.StatusCode = 200;
}
[Route(HttpVerbs.Delete, "/custom-tools/{name}")]
public async Task DeleteCustomTool(string name)
{
RoslynCompilerService.DeleteTool(name);
HttpContext.Response.StatusCode = 200;
await Task.CompletedTask;
}
[Route(HttpVerbs.Post, "/custom-tools/compile")]
public async Task CompileCustomTool()
{
string body;
using (var reader = HttpContext.OpenRequestText())
body = await reader.ReadToEndAsync();
var tool = JsonConvert.DeserializeObject<CustomToolFiles>(body);
if (tool == null)
{
HttpContext.Response.StatusCode = 400;
return;
}
// Save temp, compile, report result
var tempName = tool.Name ?? "_temp_compile_";
RoslynCompilerService.SaveTool(tool);
var logs = new List<string>();
var nugetDlls = await RoslynCompilerService.RestorePackagesAsync(tempName, m => logs.Add(m));
var (asm, error) = await RoslynCompilerService.CompileToolAsync(tempName, nugetDlls, m => logs.Add(m));
var response = new
{
Success = asm != null,
Error = error,
Logs = logs
};
HttpContext.Response.ContentType = "application/json";
using (var writer = HttpContext.OpenResponseText())
await writer.WriteAsync(JsonConvert.SerializeObject(response));
}
public static object GetTypeSchema(Type type, HashSet<Type> visited = null)
{
visited ??= new HashSet<Type>();
if (visited.Contains(type)) return null;
visited.Add(type);
var props = new List<object>();
foreach (var p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
bool isPrimitive = p.PropertyType.IsPrimitive || p.PropertyType == typeof(string) || p.PropertyType == typeof(decimal);
if (isPrimitive)
{
props.Add(new { Name = p.Name, Type = p.PropertyType.Name });
}
else
{
props.Add(new { Name = p.Name, Type = "Object", Fields = GetTypeSchema(p.PropertyType, visited) });
}
}
visited.Remove(type);
return props;
}
}
}