-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathResourceResolver.cs
66 lines (58 loc) · 2.67 KB
/
ResourceResolver.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
using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Applebot
{
// Likely a temporary solution to be replace with Microsoft.Extensions.DependencyInjection configurations
static class ResourceResolver
{
private static DirectoryInfo _ConfigurationsDirectory;
public static DirectoryInfo ConfigurationsDirectory
{
get => _ConfigurationsDirectory ?? throw new InvalidOperationException("Cannot load configuration files until configurations directory has been set by the application");
set => _ConfigurationsDirectory = value;
}
private static DirectoryInfo _RuntimeDataDirectory;
public static DirectoryInfo RuntimeDataDirectory
{
get => _RuntimeDataDirectory ?? throw new InvalidOperationException("Cannot load runtime data until runtime data directory has been set by the application");
set => _RuntimeDataDirectory = value;
}
/// <summary>
/// Loads a configuration file based on <typeparamref name="TService"/> full name.
/// </summary>
public static async Task<TConfig> LoadConfigurationAsync<TService, TConfig>()
{
return await LoadConfigurationAsync<TConfig>(typeof(TService).FullName);
}
/// <summary>
/// Loads a configuration file based the configuration name.
/// <c>TestConfig -> Configurations/TestConfig.json</c>
/// </summary>
public static async Task<TConfig> LoadConfigurationAsync<TConfig>(string configurationName)
{
var path = Path.Combine(ConfigurationsDirectory.FullName, $"{configurationName}.json");
var json = await File.ReadAllTextAsync(path);
return JsonConvert.DeserializeObject<TConfig>(json);
}
/// <summary>
/// Creates if needed and returns the directory for runtime data based on <typeparamref name="TService"/> full name.
/// </summary>
public static async Task<DirectoryInfo> GetRuntimeDataDirectoryAsync<TService>()
{
return await GetRuntimeDataDirectoryAsync(typeof(TService).FullName);
}
/// <summary>
/// Creates if needed and returns the directory for runtime data based on the directory name.
/// <c>SomeDir -> RuntimeData/SomeDir</c>
/// </summary>
public static async Task<DirectoryInfo> GetRuntimeDataDirectoryAsync(string directoryName)
{
var path = Path.Combine(RuntimeDataDirectory.FullName, directoryName);
var info = new DirectoryInfo(path);
await Task.Run(() => info.Create());
return info;
}
}
}