-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectLauncherClient.cs
More file actions
85 lines (72 loc) · 2.85 KB
/
Copy pathProjectLauncherClient.cs
File metadata and controls
85 lines (72 loc) · 2.85 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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Text.Json;
using System.Threading.Tasks;
using ProjectLauncher.Core.IPC;
namespace Community.PowerToys.Run.Plugin.ProjectLauncher
{
public class ProjectLauncherClient
{
private const string PipeName = "AtC.ProjectLauncher.IPC";
public async Task<List<IpcProjectResult>> QueryAsync(string query)
{
var req = new IpcSearchRequest
{
Type = IpcRequestType.Search,
Query = query,
Limit = 20
};
var response = await SendRequestAsync(req);
return response.Results ?? new List<IpcProjectResult>();
}
public async Task<Dictionary<string, string>?> GetSettingsAsync()
{
var req = new IpcSearchRequest
{
Type = IpcRequestType.GetSettings
};
var response = await SendRequestAsync(req);
return response.IDEKeywords;
}
public async Task<IpcSearchResponse> LaunchAsync(string projectPath, string ideId)
{
var req = new IpcSearchRequest
{
Type = IpcRequestType.Launch,
TargetProject = projectPath,
TargetIde = ideId
};
return await SendRequestAsync(req);
}
public async Task<IpcSearchResponse> SendRequestAsync(IpcSearchRequest request)
{
try
{
using var client = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut);
var cts = new System.Threading.CancellationTokenSource(500); // 500ms timeout
try
{
await client.ConnectAsync(cts.Token);
}
catch
{
return new IpcSearchResponse { Success = false, Message = "Connection failed" };
}
using var reader = new StreamReader(client);
using var writer = new StreamWriter(client) { AutoFlush = true };
var jsonRequest = JsonSerializer.Serialize(request);
await writer.WriteLineAsync(jsonRequest);
var jsonResponse = await reader.ReadLineAsync();
if (string.IsNullOrEmpty(jsonResponse)) return new IpcSearchResponse { Success = false, Message = "Empty response" };
var response = JsonSerializer.Deserialize<IpcSearchResponse>(jsonResponse);
return response ?? new IpcSearchResponse { Success = false, Message = "Invalid response" };
}
catch (Exception ex)
{
return new IpcSearchResponse { Success = false, Message = ex.Message };
}
}
}
}