-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathConfigurationFileOptions.cs
106 lines (88 loc) · 3.36 KB
/
ConfigurationFileOptions.cs
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
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace CSharpier.Cli;
public class ConfigurationFileOptions
{
public int PrintWidth { get; init; } = 100;
public int TabWidth { get; init; } = 4;
public bool UseTabs { get; init; }
public List<string>? PreprocessorSymbolSets { get; init; }
private static readonly string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" };
internal static PrinterOptions CreatePrinterOptions(
string baseDirectoryPath,
IFileSystem fileSystem,
ILogger logger
)
{
DebugLogger.Log("Creating printer options for " + baseDirectoryPath);
var configurationFileOptions = Create(baseDirectoryPath, fileSystem, logger);
List<string[]> preprocessorSymbolSets;
if (configurationFileOptions.PreprocessorSymbolSets == null)
{
preprocessorSymbolSets = new();
}
else
{
preprocessorSymbolSets = configurationFileOptions.PreprocessorSymbolSets
.Select(
o =>
o.Split(
",",
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries
)
)
.ToList();
}
return new PrinterOptions
{
TabWidth = configurationFileOptions.TabWidth,
UseTabs = configurationFileOptions.UseTabs,
Width = configurationFileOptions.PrintWidth,
EndOfLine = EndOfLine.Auto,
PreprocessorSymbolSets = preprocessorSymbolSets,
};
}
public static ConfigurationFileOptions Create(
string baseDirectoryPath,
IFileSystem fileSystem,
ILogger? logger = null
)
{
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(baseDirectoryPath);
while (directoryInfo is not null)
{
var file = directoryInfo
.EnumerateFiles(".csharpierrc*", SearchOption.TopDirectoryOnly)
.Where(o => validExtensions.Contains(o.Extension, StringComparer.OrdinalIgnoreCase))
.OrderBy(o => o.Extension)
.FirstOrDefault();
if (file == null)
{
directoryInfo = directoryInfo.Parent;
continue;
}
var contents = fileSystem.File.ReadAllText(file.FullName);
if (string.IsNullOrWhiteSpace(contents))
{
logger?.LogWarning("The configuration file at " + file.FullName + " was empty.");
return new();
}
return contents.TrimStart().StartsWith("{") ? ReadJson(contents) : ReadYaml(contents);
}
return new ConfigurationFileOptions();
}
private static ConfigurationFileOptions ReadJson(string contents)
{
return JsonConvert.DeserializeObject<ConfigurationFileOptions>(contents);
}
private static ConfigurationFileOptions ReadYaml(string contents)
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
return deserializer.Deserialize<ConfigurationFileOptions>(contents);
}
}