|
1 | | -using LogViewer.Config.Models; |
2 | | -using System.Reflection; |
| 1 | +using LogViewer.Config.Helpers; |
3 | 2 | using LogViewer.Config.Mergers; |
4 | | -using LogViewer.Config.Loaders; |
5 | | -using LogViewer.Config.Resources; |
6 | | -using LogViewer.Config.Schema; |
7 | | -using LogViewer.Config.Hashing; |
8 | | -using LogViewer.Config.Cache; |
9 | | -using LogViewer.Config.Helpers; |
| 3 | +using LogViewer.Config.Models; |
| 4 | +using LogViewer.Serializers.Yaml; |
| 5 | +using System.Reflection; |
10 | 6 |
|
11 | 7 | namespace LogViewer.Config |
12 | 8 | { |
13 | 9 | public class ConfigurationService |
14 | 10 | { |
15 | | - |
16 | | - // Embedded resources |
17 | | - |
18 | 11 | #if DEBUG |
19 | | - private static readonly string VsCodeSettingsResource = $"{Assembly.GetExecutingAssembly().GetName().Name}.Resources.vscode.settings.json"; |
20 | | - private static readonly string ConfigFileResource = $"{Assembly.GetExecutingAssembly().GetName().Name}.Resources.default_config.development.yaml"; |
21 | | - |
22 | | - private static readonly string VsCodeSettingsFile = PathHelper.NormalizePath("%SOLUTION%\\Config\\.vscode\\settings.json"); |
23 | | - private static readonly string SchemaFile = PathHelper.NormalizePath("%SOLUTION%\\Config\\schema.json"); |
24 | | - private static readonly string ConfigFile = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\debug\\config.yaml"); |
25 | | - private static readonly string CacheFolder = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\debug\\cache"); |
26 | | - |
| 12 | + private static readonly string ConfigFolder = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer_debug"); |
27 | 13 | #else |
28 | | - private static readonly string VsCodeSettingsResource = $"{Assembly.GetExecutingAssembly().GetName().Name}.Resources.vscode.settings.json"; |
29 | | - private static readonly string ConfigFileResource = $"{Assembly.GetExecutingAssembly().GetName().Name}.Resources.default_config.yaml"; |
30 | | - |
31 | | - private static readonly string VsCodeSettingsFile = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\release\\.vscode\\settings.json"); |
32 | | - private static readonly string SchemaFile = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\release\\schema.json"); |
33 | | - private static readonly string ConfigFile = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\release\\config.yaml"); |
34 | | - private static readonly string CacheFolder = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer\\release\\cache"); |
| 14 | + private static readonly string ConfigFolder = PathHelper.NormalizePath("%LOCALAPPDATA%\\LogViewer"); |
35 | 15 | #endif |
36 | 16 |
|
37 | | - private readonly IResourceProvider _resourceProvider; |
38 | | - private readonly ISchemaBuilder _schemaBuilder; |
39 | | - private readonly IConfigLoader _configLoader; |
40 | | - private readonly IConfigUpdateChecker _configUpdateChecker; |
| 17 | + private static readonly Assembly ThisAssembly = Assembly.GetExecutingAssembly(); |
| 18 | + private static readonly string Root = ThisAssembly.GetName().Name!; |
41 | 19 |
|
42 | | - public ConfigurationService() |
43 | | - { |
44 | | - // Shared infrastructure |
45 | | - var hashProvider = new Md5HashProvider(); |
46 | | - var cacheManager = new CacheManager(CacheFolder, hashProvider); |
47 | | - var merger = BuildMerger(); |
| 20 | + // Embedded resource names (verify via GetManifestResourceNames) |
| 21 | + private static readonly string VsCodeSettingsResource = $"{Root}.Resources..vscode.settings.json"; |
| 22 | + private static readonly string Organisations_SchemaResource = $"{Root}.Resources.definitions.organisations_schema.json"; |
| 23 | + private static readonly string System_OrganisationsResource = $"{Root}.Resources.system_organisations.yaml"; |
| 24 | + private static readonly string User_OrganisationsResource = $"{Root}.Resources.user_organisations.yaml"; |
48 | 25 |
|
49 | | - _resourceProvider = new ResourceProvider(); |
50 | | - _schemaBuilder = new SchemaBuilder(); |
51 | | - _configLoader = new RecursiveConfigLoader(cacheManager, merger); |
52 | | - _configUpdateChecker = new RecursiveConfigUpdateChecker(cacheManager); |
| 26 | + private static readonly string VsCodeSettingsFile = PathHelper.CombinePaths(ConfigFolder, ".vscode\\settings.json"); |
| 27 | + private static readonly string Organisations_SchemaFile = PathHelper.CombinePaths(ConfigFolder, "definitions\\organisations_schema.json"); |
| 28 | + private static readonly string User_OrganisationsFile = PathHelper.CombinePaths(ConfigFolder, "user_organisations.yaml"); |
53 | 29 |
|
54 | | - EnsureDefaultConfigExists(); |
55 | | - EnsureSchemaExists(); |
56 | | - EnsureVsCodeSettingsExists(); |
57 | | - } |
| 30 | + private readonly IConfigMerger configMerger; |
58 | 31 |
|
59 | | - private static IConfigMerger BuildMerger() |
| 32 | + public ConfigurationService() |
60 | 33 | { |
61 | | - var merger = new ConfigMerger(); |
62 | | - merger.RegisterMerger(new LogViewerConfigMerger()); |
63 | | - merger.RegisterMerger(new DictionaryMerger()); |
64 | | - merger.RegisterMerger(new ObjectMerger()); |
65 | | - return merger; |
| 34 | + ExportResourceIfNotExists(VsCodeSettingsResource, VsCodeSettingsFile); |
| 35 | + ExportResourceIfNotExists(Organisations_SchemaResource, Organisations_SchemaFile); |
| 36 | + ExportResourceIfNotExists(User_OrganisationsResource, User_OrganisationsFile); |
| 37 | + configMerger = BuildMerger(); |
66 | 38 | } |
67 | 39 |
|
68 | | - public async Task<LogViewerConfig> GetConfigAsync(CancellationToken token = default) |
| 40 | + public LogViewerConfig GetConfigAsync(CancellationToken token = default) |
69 | 41 | { |
70 | | - return await _configLoader.LoadAndMergeAsync(ConfigFile, token); |
| 42 | + LogViewerConfig config = new LogViewerConfig(); |
| 43 | + configMerger.TryMerge(config, GetConfigFromResource(System_OrganisationsResource)); |
| 44 | + configMerger.TryMerge(config, GetConfigFromFile(User_OrganisationsFile)); |
| 45 | + return config; |
71 | 46 | } |
72 | 47 |
|
73 | | - public async Task<bool> DownloadIfUpdatedAsync(CancellationToken token = default) |
| 48 | + private LogViewerConfig? GetConfigFromResource(string resource) |
74 | 49 | { |
75 | | - return await _configUpdateChecker.DownloadIfUpdatedRecursiveAsync(ConfigFile, token); |
| 50 | + string? yaml = ReadEmbeddedResource(resource); |
| 51 | + if (yaml == null) |
| 52 | + return null; |
| 53 | + if (!YamlSerializer.LoadYaml(yaml, out LogViewerConfig config)) |
| 54 | + return null; |
| 55 | + return config; |
76 | 56 | } |
77 | 57 |
|
78 | | - public void EnsureDefaultConfigExists() |
| 58 | + private LogViewerConfig? GetConfigFromFile(string filePath) |
79 | 59 | { |
80 | | - if (File.Exists(ConfigFile)) |
81 | | - return; |
82 | | - |
83 | | - Directory.CreateDirectory(Path.GetDirectoryName(ConfigFile)!); |
84 | | - var fileContents = _resourceProvider.ReadEmbeddedYaml(ConfigFileResource); |
85 | | - File.WriteAllText(ConfigFile, fileContents); |
86 | | - |
87 | | - Directory.CreateDirectory(Path.GetDirectoryName(SchemaFile)!); |
| 60 | + if (!YamlSerializer.LoadYaml(new FileInfo(filePath), out LogViewerConfig config)) |
| 61 | + return null; |
| 62 | + return config; |
88 | 63 | } |
89 | 64 |
|
90 | | - public void EnsureVsCodeSettingsExists() |
| 65 | + private static void ExportResourceIfNotExists(string resource, string destination) |
91 | 66 | { |
92 | | - if (File.Exists(VsCodeSettingsFile)) |
| 67 | + string path = Path.GetDirectoryName(destination)!; |
| 68 | + Directory.CreateDirectory(path); |
| 69 | + if (File.Exists(destination)) |
93 | 70 | return; |
94 | | - |
95 | | - Directory.CreateDirectory(Path.GetDirectoryName(VsCodeSettingsFile)!); |
96 | | - var fileContents = _resourceProvider.ReadEmbeddedYaml(VsCodeSettingsResource); |
97 | | - File.WriteAllText(VsCodeSettingsFile, fileContents); |
| 71 | + string? content = ReadEmbeddedResource(resource); |
| 72 | + if (content == null) |
| 73 | + return; |
| 74 | + File.WriteAllText(destination, content); |
98 | 75 | } |
99 | 76 |
|
100 | | - public void EnsureSchemaExists() |
| 77 | + private static IConfigMerger BuildMerger() |
101 | 78 | { |
102 | | - var newSchema = _schemaBuilder.GetSchema(); |
103 | | - |
104 | | - if (File.Exists(SchemaFile)) |
105 | | - { |
106 | | - var existingSchema = File.ReadAllText(SchemaFile); |
| 79 | + var merger = new ConfigMerger(); |
| 80 | + merger.RegisterMerger(new LogViewerConfigMerger()); |
| 81 | + merger.RegisterMerger(new DictionaryMerger()); |
| 82 | + merger.RegisterMerger(new ObjectMerger()); |
| 83 | + return merger; |
| 84 | + } |
107 | 85 |
|
108 | | - if (existingSchema == newSchema) |
109 | | - return; |
110 | | - } |
111 | 86 |
|
112 | | - Directory.CreateDirectory(Path.GetDirectoryName(SchemaFile)!); |
113 | | - File.WriteAllText(SchemaFile, newSchema); |
| 87 | + public static string? ReadEmbeddedResource(string resourceName) |
| 88 | + { |
| 89 | + using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); |
| 90 | + if (stream == null) |
| 91 | + return null; |
| 92 | + using var reader = new StreamReader(stream); |
| 93 | + return reader.ReadToEnd(); |
114 | 94 | } |
115 | 95 | } |
116 | | - |
117 | 96 | } |
0 commit comments