| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Design; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Writes manifests and table files deterministically (§4.4): fixed property order, rows sorted by key with one row per |
| | | 5 | | /// indent, CRLF line endings and a final newline — the same input always gives the same bytes, so an unchanged export i |
| | | 6 | | /// </summary> |
| | | 7 | | internal static class SeedFileWriter |
| | | 8 | | { |
| | | 9 | | private const string NewLine = "\r\n"; |
| | | 10 | | |
| | | 11 | | |
| | | 12 | | public static string Manifest( SeedManifest manifest ) |
| | | 13 | | { |
| | 117 | 14 | | var lines = new List<string> |
| | 117 | 15 | | { |
| | 117 | 16 | | "{", |
| | 117 | 17 | | $" \"Id\": {SeedJsonValue.Text( manifest.Id )},", |
| | 117 | 18 | | $" \"ContextType\": {SeedJsonValue.Text( manifest.ContextType )}," |
| | 117 | 19 | | }; |
| | | 20 | | |
| | 117 | 21 | | if ( manifest.RequiresSchemaMigration is not null ) { lines.Add( $" \"RequiresSchemaMigration\": {SeedJsonValue |
| | | 22 | | |
| | 117 | 23 | | lines.Add( $" \"Tables\": [ {string.Join( ", ", manifest.Tables.Select( SeedJsonValue.Text ) )} ]" ); |
| | 117 | 24 | | lines.Add( "}" ); |
| | | 25 | | |
| | | 26 | | |
| | 117 | 27 | | return string.Join( NewLine, lines ) + NewLine; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public static string Table( DesignTable table ) |
| | | 31 | | { |
| | 933 | 32 | | var members = new List<string> { $" \"Entity\": {SeedJsonValue.Text( table.Entity )}" }; |
| | | 33 | | |
| | 933 | 34 | | if ( table.MatchBy.Count > 0 ) { members.Add( $" \"MatchBy\": [ {string.Join( ", ", table.MatchBy.Select( SeedJ |
| | | 35 | | |
| | 933 | 36 | | Section( members, nameof( DesignTable.Upsert ), table.Upsert ); |
| | 933 | 37 | | Section( members, nameof( DesignTable.Delete ), table.Delete ); |
| | 933 | 38 | | Section( members, nameof( DesignTable.SoftDelete ), table.SoftDelete ); |
| | 933 | 39 | | Section( members, nameof( DesignTable.Restore ), table.Restore ); |
| | 933 | 40 | | Section( members, nameof( DesignTable.Before ), table.Before ); |
| | | 41 | | |
| | | 42 | | |
| | 933 | 43 | | return "{" + NewLine + string.Join( "," + NewLine, members ) + NewLine + "}" + NewLine; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// Text with any line ending turned into CRLF and exactly one final newline (for other files the tools write). |
| | | 47 | | public static string Normalize( string text ) |
| | 84 | 48 | | => text.Replace( "\r\n", "\n", StringComparison.Ordinal ).Replace( '\r', '\n' ).TrimEnd( '\n' ).Replace( "\n", N |
| | | 49 | | |
| | | 50 | | |
| | | 51 | | private static void Section( List<string> members, string name, List<DesignRow> rows ) |
| | | 52 | | { |
| | 8166 | 53 | | if ( rows.Count == 0 ) { return; } |
| | | 54 | | |
| | | 55 | | |
| | 1164 | 56 | | var ordered = rows.OrderBy( r => r.Key, SeedKeyComparer.Instance ).Select( Row ); |
| | | 57 | | |
| | 1164 | 58 | | members.Add( $" {SeedJsonValue.Text( name )}: [{NewLine}{string.Join( "," + NewLine, ordered )}{NewLine} ]" ); |
| | 1164 | 59 | | } |
| | | 60 | | |
| | 2547 | 61 | | private static string Row( DesignRow row ) => $" {{ {string.Join( ", ", row.Values.Select( v => $"{SeedJsonValue. |
| | | 62 | | } |