|
| 1 | +#nullable enable |
| 2 | +using System; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | + |
| 6 | +namespace DotNetCampus.CodeAnalysis.Utils.CodeAnalysis; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// 辅助源生成器获取 MSBuild 属性值。 |
| 10 | +/// </summary> |
| 11 | +internal static class AnalyzerConfigOptionsExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// 获取 build_property.{key} 的值。 |
| 15 | + /// </summary> |
| 16 | + /// <param name="options">源生成器配置选项。</param> |
| 17 | + /// <param name="key">要获取的 MSBuild 属性名。</param> |
| 18 | + /// <param name="value">属性值。</param> |
| 19 | + /// <typeparam name="T">属性类型。(只支持 <see langword="string"/> 和 <see langword="bool"/>。)</typeparam> |
| 20 | + /// <returns>获取到的值的结果。可放到 <see langword="if"/> 条件中。</returns> |
| 21 | + public static AnalyzerConfigOptionResult TryGetValue<T>( |
| 22 | + this AnalyzerConfigOptions options, |
| 23 | + string key, |
| 24 | + out T value) |
| 25 | + where T : notnull |
| 26 | + { |
| 27 | + if (options.TryGetValue($"build_property.{key}", out var stringValue) && !string.IsNullOrWhiteSpace(stringValue)) |
| 28 | + { |
| 29 | + value = ConvertFromString<T>(stringValue); |
| 30 | + return new AnalyzerConfigOptionResult(options, true) |
| 31 | + { |
| 32 | + UnsetPropertyNames = [], |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + value = default!; |
| 37 | + return new AnalyzerConfigOptionResult(options, false) |
| 38 | + { |
| 39 | + UnsetPropertyNames = [key], |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// 连续获取 build_property.{key} 的值。 |
| 45 | + /// </summary> |
| 46 | + /// <param name="builder">使用链式方式获取多个 MSBuild 属性的值。</param> |
| 47 | + /// <param name="key">要获取的 MSBuild 属性名。</param> |
| 48 | + /// <param name="value">属性值。</param> |
| 49 | + /// <typeparam name="T">属性类型。(只支持 <see langword="string"/> 和 <see langword="bool"/>。)</typeparam> |
| 50 | + /// <returns>获取到的值的结果。可放到 <see langword="if"/> 条件中,只有能连续获取到的所有属性值时,才满足条件。</returns> |
| 51 | + public static AnalyzerConfigOptionResult TryGetValue<T>( |
| 52 | + this AnalyzerConfigOptionResult builder, |
| 53 | + string key, |
| 54 | + out T value) |
| 55 | + where T : notnull |
| 56 | + { |
| 57 | + var options = builder.Options; |
| 58 | + |
| 59 | + if (options.TryGetValue($"build_property.{key}", out var stringValue) && !string.IsNullOrWhiteSpace(stringValue)) |
| 60 | + { |
| 61 | + value = ConvertFromString<T>(stringValue); |
| 62 | + return builder.Link(true, key); |
| 63 | + } |
| 64 | + |
| 65 | + value = default!; |
| 66 | + return builder.Link(false, key); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// 连续获取 build_property.{key} 的值,在未获取到的时候获取另外由 <paramref name="fallbackKeys"/> 指定的属性值。 |
| 71 | + /// </summary> |
| 72 | + /// <param name="options">源生成器配置选项。</param> |
| 73 | + /// <param name="key">要获取的 MSBuild 属性名。</param> |
| 74 | + /// <param name="fallbackKeys">当获取不到前一个属性名的值时,依次往后获取属性名的值。</param> |
| 75 | + /// <returns>第一个能获取到的属性的值。如果全获取不到,返回 <see langword="null"/>。</returns> |
| 76 | + public static string? TryGetValueWithFallback(this AnalyzerConfigOptions options, string key, params string[] fallbackKeys) |
| 77 | + { |
| 78 | + if (options.TryGetValue($"build_property.{key}", out var stringValue) && !string.IsNullOrWhiteSpace(stringValue)) |
| 79 | + { |
| 80 | + return stringValue; |
| 81 | + } |
| 82 | + |
| 83 | + foreach (var fallbackKey in fallbackKeys) |
| 84 | + { |
| 85 | + if (options.TryGetValue($"build_property.{fallbackKey}", out stringValue) && !string.IsNullOrWhiteSpace(stringValue)) |
| 86 | + { |
| 87 | + return stringValue; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private static T ConvertFromString<T>(string value) |
| 95 | + { |
| 96 | + if (typeof(T) == typeof(string)) |
| 97 | + { |
| 98 | + return (T)(object)value; |
| 99 | + } |
| 100 | + if (typeof(T) == typeof(bool)) |
| 101 | + { |
| 102 | + return (T)(object)value.Equals("true", StringComparison.OrdinalIgnoreCase); |
| 103 | + } |
| 104 | + return default!; |
| 105 | + } |
| 106 | + |
| 107 | + public static string EnsureGetValueWithFallback(this AnalyzerConfigOptions options, string key, params string[] fallbackKeys) |
| 108 | + { |
| 109 | + return TryGetValueWithFallback(options, key, fallbackKeys) |
| 110 | + ?? throw new InvalidOperationException($"No value found for key '{key}' or any of the fallback keys."); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// <summary> |
| 115 | +/// 用于连续获取 MSBuild 属性值的结果。 |
| 116 | +/// </summary> |
| 117 | +/// <param name="Options">源生成器配置选项。</param> |
| 118 | +/// <param name="GotValue">在此之前如果获取到了所有的属性值,则为 <see langword="true"/>。</param> |
| 119 | +public readonly record struct AnalyzerConfigOptionResult(AnalyzerConfigOptions Options, bool GotValue) |
| 120 | +{ |
| 121 | + /// <summary> |
| 122 | + /// 未能获取到值的属性名列表。 |
| 123 | + /// </summary> |
| 124 | + public required ImmutableList<string> UnsetPropertyNames { get; init; } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// 将当前结果与新的获取结果链接起来。只有所有的值都获取到了,才返回 <see langword="true"/>;否则返回 <see langword="false"/> 并记录未获取到值的属性名。 |
| 128 | + /// </summary> |
| 129 | + /// <param name="result">新的获取结果。</param> |
| 130 | + /// <param name="propertyName">新的属性名。</param> |
| 131 | + /// <returns>链接后的结果。</returns> |
| 132 | + public AnalyzerConfigOptionResult Link(bool result, string propertyName) |
| 133 | + { |
| 134 | + if (result) |
| 135 | + { |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + if (propertyName is null) |
| 140 | + { |
| 141 | + throw new ArgumentNullException(nameof(propertyName), @"The property name must be specified if the result is false."); |
| 142 | + } |
| 143 | + |
| 144 | + return this with |
| 145 | + { |
| 146 | + GotValue = false, |
| 147 | + UnsetPropertyNames = UnsetPropertyNames.Add(propertyName), |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// 允许将结果直接用在 <see langword="if"/> 条件中。<see langword="true"/> 表示所有属性都成功获取到了。 |
| 153 | + /// </summary> |
| 154 | + /// <param name="result">获取结果。</param> |
| 155 | + /// <returns>如果所有属性都成功获取到了,返回 <see langword="true"/>;否则返回 <see langword="false"/>。</returns> |
| 156 | + public static implicit operator bool(AnalyzerConfigOptionResult result) => result.GotValue; |
| 157 | +} |
0 commit comments