| 1 | using CascadeIDE.Contracts; |
| 2 | namespace CascadeIDE.Features.CursorAcp.DataAcquisition; |
| 3 | |
| 4 | /// <summary>Сопоставление пути из настроек с <c>cursor-agent.cmd</c> из пакета Cursor ACP.</summary> |
| 5 | [IoBoundary] |
| 6 | public static class CursorAcpAgentPath |
| 7 | { |
| 8 | /// <summary>Возвращает полный путь к cmd и рабочий каталог для процесса.</summary> |
| 9 | public static bool TryResolve(string? configured, out string cmdPath, out string workingDirectory) |
| 10 | { |
| 11 | cmdPath = ""; |
| 12 | workingDirectory = ""; |
| 13 | if (string.IsNullOrWhiteSpace(configured)) |
| 14 | return TryResolveFromPath(out cmdPath, out workingDirectory); |
| 15 | |
| 16 | var trimmed = configured.Trim(); |
| 17 | if (File.Exists(trimmed)) |
| 18 | { |
| 19 | var ext = Path.GetExtension(trimmed); |
| 20 | if (string.Equals(ext, ".cmd", StringComparison.OrdinalIgnoreCase) |
| 21 | || string.Equals(ext, ".bat", StringComparison.OrdinalIgnoreCase)) |
| 22 | { |
| 23 | cmdPath = CanonicalFilePath.Normalize(trimmed); |
| 24 | workingDirectory = Path.GetDirectoryName(cmdPath) ?? ""; |
| 25 | return true; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if (!Directory.Exists(trimmed)) |
| 30 | return false; |
| 31 | |
| 32 | var dir = CanonicalFilePath.Normalize(trimmed); |
| 33 | foreach (var rel in new[] { Path.Combine("dist-package", "cursor-agent.cmd"), "cursor-agent.cmd" }) |
| 34 | { |
| 35 | var p = Path.Combine(dir, rel); |
| 36 | if (File.Exists(p)) |
| 37 | { |
| 38 | cmdPath = CanonicalFilePath.Normalize(p); |
| 39 | workingDirectory = Path.GetDirectoryName(cmdPath) ?? dir; |
| 40 | return true; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | private static bool TryResolveFromPath(out string cmdPath, out string workingDirectory) |
| 48 | { |
| 49 | cmdPath = ""; |
| 50 | workingDirectory = ""; |
| 51 | |
| 52 | foreach (var candidate in new[] { "cursor-agent", "cursor-agent.cmd", "cursor-agent.bat" }) |
| 53 | { |
| 54 | var resolved = EnvironmentReadinessExecutablePathProbe.TryResolveExecutablePath(candidate); |
| 55 | if (string.IsNullOrWhiteSpace(resolved)) |
| 56 | continue; |
| 57 | |
| 58 | cmdPath = resolved; |
| 59 | workingDirectory = Path.GetDirectoryName(resolved) ?? ""; |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |