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