| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | using CascadeIDE.Models; |
| 4 | |
| 5 | namespace CascadeIDE.Services; |
| 6 | |
| 7 | /// <summary>Один JSON на workspace: <c>.cascade-ide/debug-hypotheses.json</c> (ADR 0001).</summary> |
| 8 | public static class DebugHypothesesStorage |
| 9 | { |
| 10 | private static readonly JsonSerializerOptions ReadOptions = new() |
| 11 | { |
| 12 | PropertyNameCaseInsensitive = true, |
| 13 | Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, |
| 14 | }; |
| 15 | |
| 16 | private static readonly JsonSerializerOptions WriteOptions = new() |
| 17 | { |
| 18 | WriteIndented = true, |
| 19 | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 20 | Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, |
| 21 | }; |
| 22 | |
| 23 | public static string GetFilePath(string workspaceRootPath) => |
| 24 | Path.Combine(workspaceRootPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), ".cascade-ide", "debug-hypotheses.json"); |
| 25 | |
| 26 | public static DebugHypothesesFileRoot Load(string workspaceRootPath) |
| 27 | { |
| 28 | if (string.IsNullOrWhiteSpace(workspaceRootPath)) |
| 29 | return new DebugHypothesesFileRoot(); |
| 30 | try |
| 31 | { |
| 32 | var path = GetFilePath(workspaceRootPath); |
| 33 | if (!File.Exists(path)) |
| 34 | return new DebugHypothesesFileRoot(); |
| 35 | var json = File.ReadAllText(path); |
| 36 | return JsonSerializer.Deserialize<DebugHypothesesFileRoot>(json, ReadOptions) ?? new DebugHypothesesFileRoot(); |
| 37 | } |
| 38 | catch |
| 39 | { |
| 40 | return new DebugHypothesesFileRoot(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public static void Save(string workspaceRootPath, DebugHypothesesFileRoot root) |
| 45 | { |
| 46 | if (string.IsNullOrWhiteSpace(workspaceRootPath)) |
| 47 | return; |
| 48 | try |
| 49 | { |
| 50 | var dir = Path.Combine(workspaceRootPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), ".cascade-ide"); |
| 51 | Directory.CreateDirectory(dir); |
| 52 | var path = GetFilePath(workspaceRootPath); |
| 53 | File.WriteAllText(path, JsonSerializer.Serialize(root, WriteOptions)); |
| 54 | } |
| 55 | catch |
| 56 | { |
| 57 | // как у других локальных JSON-артефактов IDE |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |