|
1 | | -using Packer.Models; |
2 | | -using Serilog; |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | -using System.IO; |
6 | | -using System.Linq; |
7 | | -using System.Text.Encodings.Web; |
8 | | -using System.Text.Json; |
9 | | -using System.Text.Json.Serialization; |
10 | | -using System.Threading.Tasks; |
11 | | - |
12 | | - |
13 | | -namespace Packer |
14 | | -{ |
15 | | - /// <summary> |
16 | | - /// 杂项工具类 |
17 | | - /// </summary> |
18 | | - public static class Utils |
19 | | - { |
20 | | - /// <summary> |
21 | | - /// 从给定的命名空间获取局域配置 |
22 | | - /// </summary> |
23 | | - /// <param name="directory">命名空间目录</param> |
24 | | - /// <returns>若文件存在,返回<see cref="FloatingConfig"/>;否则,返回<see langword="null"/></returns> |
25 | | - public static FloatingConfig? RetrieveLocalConfig(DirectoryInfo directory) |
26 | | - { |
27 | | - var configFile = directory.GetFiles("local-config.json").FirstOrDefault(); |
28 | | - |
29 | | - if (configFile is null) return null; |
30 | | - |
31 | | - using var reader = configFile.OpenText(); |
32 | | - return JsonSerializer.Deserialize<FloatingConfig>( |
33 | | - reader.ReadToEnd(), |
34 | | - new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
35 | | - } |
36 | | - |
37 | | - /// <summary> |
38 | | - /// 从仓库根目录获取全局配置 |
39 | | - /// </summary> |
40 | | - /// <param name="configTemplate">配置路径模板</param> |
41 | | - /// <param name="version">打包版本,用于定位全局配置</param> |
42 | | - public static async Task<Config> RetrieveConfig(string configTemplate, string version) |
43 | | - { |
44 | | - Log.Information("正在获取配置。目标版本:{0}", version); |
45 | | - |
46 | | - var configPath = string.Format(configTemplate, version); |
47 | | - |
48 | | - Log.Information("配置位置:{0}", configPath); |
49 | | - |
50 | | - var content = await File.ReadAllBytesAsync(configPath); |
51 | | - return JsonSerializer.Deserialize<Config>( |
52 | | - content, |
53 | | - new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!; |
54 | | - } |
55 | | - |
56 | | - /// <summary> |
57 | | - /// 从给定的命名空间获取策略内容 |
58 | | - /// </summary> |
59 | | - /// <param name="directory">命名空间目录</param> |
60 | | - /// <returns>若文件存在,返回对应的内容;否则,返回<c>Direct</c></returns> |
61 | | - /// <exception cref="InvalidDataException">策略文件非法</exception> |
62 | | - public static List<PackerPolicy> RetrieveStrategy(DirectoryInfo directory) |
63 | | - { |
64 | | - var file = directory.GetFiles("packer-policy.json").FirstOrDefault(); |
65 | | - |
66 | | - if (file is null) |
67 | | - return new List<PackerPolicy> |
68 | | - { |
69 | | - new PackerPolicy { Type = PackerPolicyType.Direct } |
70 | | - }; |
71 | | - |
72 | | - using var reader = file.OpenText(); |
73 | | - var result = JsonSerializer.Deserialize<List<PackerPolicy>>( |
74 | | - reader.ReadToEnd(), |
75 | | - new JsonSerializerOptions |
76 | | - { |
77 | | - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
78 | | - Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } |
79 | | - }); |
80 | | - if (result is null) |
81 | | - throw new InvalidDataException($"The policy file {file.FullName} cannot have null values."); |
82 | | - return result; |
83 | | - } |
84 | | - } |
85 | | -} |
| 1 | +using Packer.Models; |
| 2 | +using Serilog; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | + |
| 11 | +namespace Packer.Helpers |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// 配置相关的工具类 |
| 15 | + /// </summary> |
| 16 | + public static class ConfigHelpers |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// 从给定的命名空间获取局域配置 |
| 20 | + /// </summary> |
| 21 | + /// <param name="directory">命名空间目录</param> |
| 22 | + /// <returns>若文件存在,返回<see cref="FloatingConfig"/>;否则,返回<see langword="null"/></returns> |
| 23 | + public static FloatingConfig? RetrieveLocalConfig(DirectoryInfo directory) |
| 24 | + { |
| 25 | + var configFile = directory.GetFiles("local-config.json").FirstOrDefault(); |
| 26 | + |
| 27 | + if (configFile is null) return null; |
| 28 | + |
| 29 | + using var reader = configFile.OpenText(); |
| 30 | + return JsonSerializer.Deserialize<FloatingConfig>( |
| 31 | + reader.ReadToEnd(), |
| 32 | + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// 从仓库根目录获取全局配置 |
| 37 | + /// </summary> |
| 38 | + /// <param name="configTemplate">配置路径模板</param> |
| 39 | + /// <param name="version">打包版本,用于定位全局配置</param> |
| 40 | + public static async Task<Config> RetrieveConfig(string configTemplate, string version) |
| 41 | + { |
| 42 | + Log.Information("正在获取配置。目标版本:{0}", version); |
| 43 | + |
| 44 | + var configPath = string.Format(configTemplate, version); |
| 45 | + |
| 46 | + Log.Information("配置位置:{0}", configPath); |
| 47 | + |
| 48 | + var content = await File.ReadAllBytesAsync(configPath); |
| 49 | + return JsonSerializer.Deserialize<Config>( |
| 50 | + content, |
| 51 | + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// 从给定的命名空间获取策略内容 |
| 56 | + /// </summary> |
| 57 | + /// <param name="directory">命名空间目录</param> |
| 58 | + /// <returns>若文件存在,返回对应的内容;否则,返回<c>Direct</c></returns> |
| 59 | + /// <exception cref="InvalidDataException">策略文件非法</exception> |
| 60 | + public static List<PackerPolicy> RetrieveStrategy(DirectoryInfo directory) |
| 61 | + { |
| 62 | + var file = directory.GetFiles("packer-policy.json").FirstOrDefault(); |
| 63 | + |
| 64 | + if (file is null) |
| 65 | + return new List<PackerPolicy> |
| 66 | + { |
| 67 | + new PackerPolicy { Type = PackerPolicyType.Direct } |
| 68 | + }; |
| 69 | + |
| 70 | + using var reader = file.OpenText(); |
| 71 | + var result = JsonSerializer.Deserialize<List<PackerPolicy>>( |
| 72 | + reader.ReadToEnd(), |
| 73 | + new JsonSerializerOptions |
| 74 | + { |
| 75 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 76 | + Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } |
| 77 | + }); |
| 78 | + if (result is null) |
| 79 | + throw new InvalidDataException($"The policy file {file.FullName} cannot have null values."); |
| 80 | + return result; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments