| | | 1 | | #region Usings |
| | | 2 | | using System.Text.Encodings.Web; |
| | | 3 | | using System.Text.Unicode; |
| | | 4 | | #endregion |
| | | 5 | | |
| | | 6 | | namespace ZhEaIsNsAaBn.DbSeeder.Design; |
| | | 7 | | |
| | | 8 | | /// <c>.dbseeder/profiles.json</c> under the project folder (deterministic, CRLF). |
| | | 9 | | internal sealed class SeedProfileStore |
| | | 10 | | { |
| | 3 | 11 | | private static readonly JsonSerializerOptions Options = new() { WriteIndented = true, Encoder = JavaScriptEncoder.Cr |
| | | 12 | | |
| | | 13 | | private readonly string _path; |
| | | 14 | | |
| | | 15 | | |
| | 822 | 16 | | public SeedProfileStore( string root ) => _path = Path.Combine( root, ".dbseeder", "profiles.json" ); |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | public List<SeedProfile> Load() |
| | 411 | 20 | | => File.Exists( _path ) |
| | 411 | 21 | | ? JsonSerializer.Deserialize<ProfilesFile>( File.ReadAllText( _path ), Options )?.Profiles.ToList() ?? [] |
| | 411 | 22 | | : []; |
| | | 23 | | |
| | | 24 | | public void Save( IEnumerable<SeedProfile> profiles ) |
| | | 25 | | { |
| | 84 | 26 | | Directory.CreateDirectory( Path.GetDirectoryName( _path )! ); |
| | | 27 | | |
| | 84 | 28 | | var file = new ProfilesFile { Profiles = profiles.OrderBy( p => p.Name, StringComparer.Ordinal ).ToList() }; |
| | | 29 | | |
| | 84 | 30 | | File.WriteAllText( _path, SeedFileWriter.Normalize( JsonSerializer.Serialize( file, Options ) ), new UTF8Encodin |
| | 84 | 31 | | } |
| | | 32 | | |
| | | 33 | | |
| | | 34 | | private sealed class ProfilesFile |
| | | 35 | | { |
| | | 36 | | public IReadOnlyList<SeedProfile> Profiles { get; init; } = []; |
| | | 37 | | } |
| | | 38 | | } |