| 1 | using CascadeIDE.Contracts; |
| 2 | namespace CascadeIDE.Features.Settings.DataAcquisition; |
| 3 | |
| 4 | /// <summary>DAL: сырой I/O для <c>settings.toml</c> в каталоге пользователя.</summary> |
| 5 | [IoBoundary] |
| 6 | public static class UserSettingsTomlFileAccess |
| 7 | { |
| 8 | public static string GetFilePath() => UserSettingsPaths.GetSettingsFilePath(); |
| 9 | |
| 10 | public static bool FileExists() => File.Exists(GetFilePath()); |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Читает файл; при отсутствии <paramref name="text"/> = null и <paramref name="mtimeUtc"/> = <see cref="DateTime.MinValue"/>. |
| 14 | /// При ошибке чтения — <paramref name="text"/> = null, mtime — с диска если файл ещё есть, иначе MinValue. |
| 15 | /// </summary> |
| 16 | public static void TryRead(out string? text, out DateTime mtimeUtc) |
| 17 | { |
| 18 | var path = GetFilePath(); |
| 19 | text = null; |
| 20 | mtimeUtc = DateTime.MinValue; |
| 21 | if (!File.Exists(path)) |
| 22 | return; |
| 23 | try |
| 24 | { |
| 25 | text = File.ReadAllText(path); |
| 26 | mtimeUtc = File.GetLastWriteTimeUtc(path); |
| 27 | } |
| 28 | catch |
| 29 | { |
| 30 | mtimeUtc = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static bool TryGetLastWriteTimeUtc(out DateTime mtimeUtc) |
| 35 | { |
| 36 | var path = GetFilePath(); |
| 37 | mtimeUtc = default; |
| 38 | if (!File.Exists(path)) |
| 39 | return false; |
| 40 | try |
| 41 | { |
| 42 | mtimeUtc = File.GetLastWriteTimeUtc(path); |
| 43 | return true; |
| 44 | } |
| 45 | catch |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public static void WriteAllText(string toml, out DateTime mtimeUtcAfterWrite) |
| 52 | { |
| 53 | var path = GetFilePath(); |
| 54 | File.WriteAllText(path, toml); |
| 55 | mtimeUtcAfterWrite = File.GetLastWriteTimeUtc(path); |
| 56 | } |
| 57 | } |
| 58 | |