|
| 1 | +using System; |
| 2 | +using DbApiBuilderEntityGenerator.Core.Serialization; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using YamlDotNet.Serialization; |
| 5 | +using YamlDotNet.Serialization.NamingConventions; |
| 6 | + |
| 7 | +namespace DbApiBuilderEntityGenerator.Core; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Serialization and Deserialization for the <see cref="Generator"/> class |
| 11 | +/// </summary> |
| 12 | +public class ConfigurationSerializer : IConfigurationSerializer |
| 13 | +{ |
| 14 | + private readonly ILogger<ConfigurationSerializer> _logger; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initializes a new instance of the <see cref="ConfigurationSerializer"/> class. |
| 18 | + /// </summary> |
| 19 | + /// <param name="logger">The logger.</param> |
| 20 | + public ConfigurationSerializer(ILogger<ConfigurationSerializer> logger) |
| 21 | + { |
| 22 | + _logger = logger; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The options file name. Default 'generation.yml' |
| 27 | + /// </summary> |
| 28 | + public const string OptionsFileName = "generation.yml"; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Loads the options file using the specified <paramref name="directory"/> and <paramref name="file"/>. |
| 32 | + /// </summary> |
| 33 | + /// <param name="directory">The directory where the file is located.</param> |
| 34 | + /// <param name="file">The name of the options file.</param> |
| 35 | + /// <returns>An instance of <see cref="Generator"/> if the file exists; otherwise <c>null</c>.</returns> |
| 36 | + public GeneratorModel? Load(string? directory = null, string file = OptionsFileName) |
| 37 | + { |
| 38 | + var path = GetPath(directory, file); |
| 39 | + if (!File.Exists(path)) |
| 40 | + { |
| 41 | + _logger.LogWarning("Option file not found: {file}", file); |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + _logger.LogInformation("Loading options file: {file}", file); |
| 46 | + using var reader = File.OpenText(path); |
| 47 | + |
| 48 | + return Load(reader); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Loads the options using the specified <paramref name="reader" /> |
| 53 | + /// </summary> |
| 54 | + /// <param name="reader">The reader.</param> |
| 55 | + /// <returns> |
| 56 | + /// An instance of <see cref="Generator" />. |
| 57 | + /// </returns> |
| 58 | + public GeneratorModel? Load(TextReader reader) |
| 59 | + { |
| 60 | + if (reader == null) |
| 61 | + return null; |
| 62 | + |
| 63 | + var deserializer = new DeserializerBuilder() |
| 64 | + .WithNamingConvention(CamelCaseNamingConvention.Instance) |
| 65 | + .Build(); |
| 66 | + |
| 67 | + // use Serialization model for better yaml support |
| 68 | + return deserializer.Deserialize<GeneratorModel>(reader); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Saves the generator options to the specified <paramref name="directory"/> and <paramref name="file"/>. |
| 73 | + /// </summary> |
| 74 | + /// <param name="generatorOptions">The generator options to save.</param> |
| 75 | + /// <param name="directory">The directory where the file is located.</param> |
| 76 | + /// <param name="file">The name of the options file.</param> |
| 77 | + /// <returns>The full path of the options file.</returns> |
| 78 | + public string Save(GeneratorModel generatorOptions, string? directory = null, string file = OptionsFileName) |
| 79 | + { |
| 80 | + if (string.IsNullOrWhiteSpace(directory)) |
| 81 | + directory = Environment.CurrentDirectory; |
| 82 | + |
| 83 | + if (string.IsNullOrWhiteSpace(file)) |
| 84 | + file = OptionsFileName; |
| 85 | + |
| 86 | + if (!Directory.Exists(directory)) |
| 87 | + { |
| 88 | + _logger.LogTrace($"Creating Directory: {directory}"); |
| 89 | + Directory.CreateDirectory(directory); |
| 90 | + } |
| 91 | + |
| 92 | + _logger.LogInformation($"Saving options file: {file}"); |
| 93 | + |
| 94 | + var path = Path.Combine(directory, file); |
| 95 | + |
| 96 | + var serializer = new SerializerBuilder() |
| 97 | + .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults) |
| 98 | + .WithNamingConvention(CamelCaseNamingConvention.Instance) |
| 99 | + .Build(); |
| 100 | + |
| 101 | + using (var streamWriter = File.CreateText(path)) |
| 102 | + serializer.Serialize(streamWriter, generatorOptions); |
| 103 | + |
| 104 | + return path; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Determines if the specified options file exists. |
| 109 | + /// </summary> |
| 110 | + /// <param name="directory">The directory where the file is located.</param> |
| 111 | + /// <param name="file">The name of the options file.</param> |
| 112 | + /// <returns><c>true</c> if options file exits; otherwise <c>false</c>.</returns> |
| 113 | + public bool Exists(string? directory = null, string file = OptionsFileName) |
| 114 | + { |
| 115 | + var path = GetPath(directory, file); |
| 116 | + return File.Exists(path); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private static string GetPath(string? directory, string? file) |
| 121 | + { |
| 122 | + if (string.IsNullOrWhiteSpace(directory)) |
| 123 | + directory = Environment.CurrentDirectory; |
| 124 | + |
| 125 | + if (string.IsNullOrWhiteSpace(file)) |
| 126 | + file = OptionsFileName; |
| 127 | + |
| 128 | + var path = Path.Combine(directory, file); |
| 129 | + return path; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments