11using System ;
22using System . IO ;
33
4- using EntityFrameworkCore . Generator . Options ;
4+ using EntityFrameworkCore . Generator . Serialization ;
55
66using Microsoft . Extensions . Logging ;
77
1111namespace EntityFrameworkCore . Generator ;
1212
1313/// <summary>
14- /// Serialization and Deserialization for the <see cref="GeneratorOptions "/> class
14+ /// Serialization and Deserialization for the <see cref="Generator "/> class
1515/// </summary>
16- public class GeneratorOptionsSerializer : IGeneratorOptionsSerializer
16+ public class ConfigurationSerializer : IConfigurationSerializer
1717{
18- private readonly ILogger < GeneratorOptionsSerializer > _logger ;
18+ private readonly ILogger < ConfigurationSerializer > _logger ;
1919
2020 /// <summary>
21- /// Initializes a new instance of the <see cref="GeneratorOptionsSerializer "/> class.
21+ /// Initializes a new instance of the <see cref="ConfigurationSerializer "/> class.
2222 /// </summary>
2323 /// <param name="logger">The logger.</param>
24- public GeneratorOptionsSerializer ( ILogger < GeneratorOptionsSerializer > logger )
24+ public ConfigurationSerializer ( ILogger < ConfigurationSerializer > logger )
2525 {
2626 _logger = logger ;
2727 }
@@ -36,8 +36,8 @@ public GeneratorOptionsSerializer(ILogger<GeneratorOptionsSerializer> logger)
3636 /// </summary>
3737 /// <param name="directory">The directory where the file is located.</param>
3838 /// <param name="file">The name of the options file.</param>
39- /// <returns>An instance of <see cref="GeneratorOptions "/> if the file exists; otherwise <c>null</c>.</returns>
40- public GeneratorOptions Load ( string directory = null , string file = OptionsFileName )
39+ /// <returns>An instance of <see cref="Generator "/> if the file exists; otherwise <c>null</c>.</returns>
40+ public GeneratorModel Load ( string directory = null , string file = OptionsFileName )
4141 {
4242 var path = GetPath ( directory , file ) ;
4343 if ( ! File . Exists ( path ) )
@@ -46,20 +46,30 @@ public GeneratorOptions Load(string directory = null, string file = OptionsFileN
4646 return null ;
4747 }
4848
49- var factory = new GeneratorOptionsFactory ( ) ;
49+ _logger . LogInformation ( $ "Loading options file: { file } ") ;
50+ using var reader = File . OpenText ( path ) ;
51+
52+ return Load ( reader ) ;
53+ }
54+
55+ /// <summary>
56+ /// Loads the options using the specified <paramref name="reader" />
57+ /// </summary>
58+ /// <param name="reader">The reader.</param>
59+ /// <returns>
60+ /// An instance of <see cref="Generator" />.
61+ /// </returns>
62+ public GeneratorModel Load ( TextReader reader )
63+ {
64+ if ( reader == null )
65+ return null ;
5066
5167 var deserializer = new DeserializerBuilder ( )
5268 . WithNamingConvention ( CamelCaseNamingConvention . Instance )
53- . WithObjectFactory ( factory )
5469 . Build ( ) ;
5570
56- _logger . LogInformation ( $ "Loading options file: { file } ") ;
57- GeneratorOptions generatorOptions ;
58- using ( var streamReader = File . OpenText ( path ) )
59- generatorOptions = deserializer . Deserialize < GeneratorOptions > ( streamReader ) ;
60-
61- generatorOptions . Variables . ShouldEvaluate = true ;
62- return generatorOptions ;
71+ // use Serialization model for better yaml support
72+ return deserializer . Deserialize < GeneratorModel > ( reader ) ;
6373 }
6474
6575 /// <summary>
@@ -69,7 +79,7 @@ public GeneratorOptions Load(string directory = null, string file = OptionsFileN
6979 /// <param name="directory">The directory where the file is located.</param>
7080 /// <param name="file">The name of the options file.</param>
7181 /// <returns>The full path of the options file.</returns>
72- public string Save ( GeneratorOptions generatorOptions , string directory = null , string file = OptionsFileName )
82+ public string Save ( GeneratorModel generatorOptions , string directory = null , string file = OptionsFileName )
7383 {
7484 if ( string . IsNullOrWhiteSpace ( directory ) )
7585 directory = Environment . CurrentDirectory ;
@@ -88,17 +98,13 @@ public string Save(GeneratorOptions generatorOptions, string directory = null, s
8898 var path = Path . Combine ( directory , file ) ;
8999
90100 var serializer = new SerializerBuilder ( )
91- . ConfigureDefaultValuesHandling ( DefaultValuesHandling . OmitNull )
101+ . ConfigureDefaultValuesHandling ( DefaultValuesHandling . OmitDefaults )
92102 . WithNamingConvention ( CamelCaseNamingConvention . Instance )
93103 . Build ( ) ;
94104
95- generatorOptions . Variables . ShouldEvaluate = false ;
96-
97105 using ( var streamWriter = File . CreateText ( path ) )
98106 serializer . Serialize ( streamWriter , generatorOptions ) ;
99107
100- generatorOptions . Variables . ShouldEvaluate = true ;
101-
102108 return path ;
103109 }
104110
0 commit comments