|
24 | 24 | */
|
25 | 25 |
|
26 | 26 | using System;
|
27 |
| -using System.Diagnostics; |
28 | 27 | using net.r_eg.MvsSln.Extensions;
|
29 | 28 |
|
30 | 29 | namespace net.r_eg.MvsSln.Core
|
31 | 30 | {
|
32 | 31 | /// <summary>
|
33 |
| - /// Basic item of configuration and platform. |
| 32 | + /// Basic item of the configuration and its platform. |
34 | 33 | /// </summary>
|
35 |
| - [DebuggerDisplay("{DbgDisplay}")] |
36 | 34 | public class ConfigItem: IConfPlatform
|
37 | 35 | {
|
38 |
| - /// <summary> |
39 |
| - /// The custom rule of the Configuration and Platform names. |
40 |
| - /// </summary> |
41 |
| - public IRuleOfConfig Rule |
42 |
| - { |
43 |
| - get; |
44 |
| - set; |
45 |
| - } = new RuleOfConfig(); |
| 36 | + protected const char DELIM = '|'; |
46 | 37 |
|
47 |
| - /// <summary> |
48 |
| - /// To use an `Sensitivity` logic when comparing {IConfPlatform} |
49 |
| - /// together with `==` , `!=`. |
50 |
| - /// </summary> |
51 |
| - public bool SensitivityComparing |
52 |
| - { |
53 |
| - get; |
54 |
| - set; |
55 |
| - } = true; |
| 38 | + private string _fmt; |
56 | 39 |
|
57 |
| - public string Configuration |
58 |
| - { |
59 |
| - get; |
60 |
| - protected set; |
61 |
| - } |
| 40 | + public IRuleOfConfig Rule { get; protected set; } |
| 41 | + |
| 42 | + public bool SensitivityComparing { get; set; } = true; |
| 43 | + |
| 44 | + public string Configuration { get; protected set; } |
62 | 45 |
|
63 | 46 | public string ConfigurationByRule
|
64 | 47 | {
|
65 |
| - get => Rule?.Configuration(Configuration); |
| 48 | + get => Rule?.Configuration(Configuration) ?? Configuration; |
66 | 49 | }
|
67 | 50 |
|
68 |
| - /// <summary> |
69 |
| - /// {ConfigurationByRule} with optional case insensitive logic. |
70 |
| - /// Uses {SensitivityComparing} flag. |
71 |
| - /// </summary> |
72 |
| - public string ConfigurationByRuleICase |
73 |
| - { |
74 |
| - get => Sensitivity(ConfigurationByRule); |
75 |
| - } |
| 51 | + public string ConfigurationByRuleICase => Sensitivity(ConfigurationByRule); |
76 | 52 |
|
77 |
| - public string Platform |
78 |
| - { |
79 |
| - get; |
80 |
| - protected set; |
81 |
| - } |
| 53 | + public string Platform { get; protected set; } |
82 | 54 |
|
83 |
| - public string PlatformByRule |
84 |
| - { |
85 |
| - get => Rule?.Platform(Platform); |
86 |
| - } |
| 55 | + public string PlatformByRule => Rule?.Platform(Platform) ?? Platform; |
87 | 56 |
|
88 |
| - /// <summary> |
89 |
| - /// {PlatformByRule} with optional case insensitive logic. |
90 |
| - /// Uses {SensitivityComparing} flag. |
91 |
| - /// </summary> |
92 |
| - public string PlatformByRuleICase |
93 |
| - { |
94 |
| - get => Sensitivity(PlatformByRule); |
95 |
| - } |
| 57 | + public string PlatformByRuleICase => Sensitivity(PlatformByRule); |
| 58 | + |
| 59 | + public string Formatted => _fmt ??= Format(ConfigurationByRule, PlatformByRule); |
96 | 60 |
|
97 |
| - /// <summary> |
98 |
| - /// Checking an config/platform by using {Rule} instance. |
99 |
| - /// </summary> |
100 |
| - /// <param name="config">Configuration name.</param> |
101 |
| - /// <param name="platform">Platform name.</param> |
102 |
| - /// <param name="icase">Case insensitive flag.</param> |
103 |
| - /// <returns></returns> |
104 | 61 | public bool IsEqualByRule(string config, string platform, bool icase = false)
|
105 | 62 | {
|
106 | 63 | var cmp = icase ? StringComparison.InvariantCultureIgnoreCase
|
@@ -136,64 +93,103 @@ public override int GetHashCode()
|
136 | 93 | return 0.CalculateHashCode
|
137 | 94 | (
|
138 | 95 | Configuration,
|
139 |
| - Platform |
| 96 | + Platform, |
| 97 | + Rule, |
| 98 | + SensitivityComparing |
140 | 99 | );
|
141 | 100 | }
|
142 | 101 |
|
143 |
| - public override string ToString() |
144 |
| - { |
145 |
| - return Format(); |
146 |
| - } |
| 102 | + public override string ToString() => Format(Configuration, Platform); |
147 | 103 |
|
148 | 104 | /// <summary>
|
149 | 105 | /// Compatible format: 'configname'|'platformname'
|
150 | 106 | /// http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivscfg.get_displayname.aspx
|
151 | 107 | /// </summary>
|
152 | 108 | public static string Format(string configuration, string platform)
|
153 | 109 | {
|
154 |
| - return $"{configuration}|{platform}"; |
| 110 | + return $"{configuration}{DELIM}{platform}"; |
155 | 111 | }
|
156 | 112 |
|
157 |
| - public string Format() |
| 113 | + [Obsolete("Use `ToString()` and `Formatted` instead.")] |
| 114 | + public string Format() => ToString(); |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Initialize using custom rule. |
| 118 | + /// </summary> |
| 119 | + /// <param name="rule">Custom rule. Use null to disable it.</param> |
| 120 | + /// <param name="configuration">Configuration name.</param> |
| 121 | + /// <param name="platform">Platform name.</param> |
| 122 | + public ConfigItem(IRuleOfConfig rule, string configuration, string platform) |
158 | 123 | {
|
159 |
| - return Format(Configuration, Platform); |
| 124 | + Rule = rule; |
| 125 | + Configuration = configuration; |
| 126 | + Platform = platform; |
160 | 127 | }
|
161 | 128 |
|
| 129 | + /// <summary> |
| 130 | + /// Initialize using rule <see cref="RuleOfConfig"/> by default. |
| 131 | + /// </summary> |
| 132 | + /// <inheritdoc cref="ConfigItem(IRuleOfConfig, string, string)"/> |
162 | 133 | public ConfigItem(string configuration, string platform)
|
| 134 | + : this(new RuleOfConfig(), configuration, platform) |
163 | 135 | {
|
164 |
| - Configuration = configuration; |
165 |
| - Platform = platform; |
| 136 | + |
166 | 137 | }
|
167 | 138 |
|
| 139 | + /// <summary> |
| 140 | + /// Initialize using rule <see cref="RuleOfConfig"/> by default. |
| 141 | + /// </summary> |
| 142 | + /// <inheritdoc cref="ConfigItem(IRuleOfConfig, string)"/> |
168 | 143 | public ConfigItem(string formatted)
|
| 144 | + : this(new RuleOfConfig(), formatted) |
169 | 145 | {
|
170 |
| - if(formatted == null) { |
171 |
| - return; |
172 |
| - } |
173 | 146 |
|
174 |
| - string[] cfg = formatted.Split('|'); |
| 147 | + } |
175 | 148 |
|
176 |
| - Configuration = cfg[0]; |
| 149 | + /// <summary> |
| 150 | + /// Initialize using custom rule. |
| 151 | + /// </summary> |
| 152 | + /// <param name="rule">Custom rule. Use null to disable it.</param> |
| 153 | + /// <param name="formatted">Raw formatted string.</param> |
| 154 | + public ConfigItem(IRuleOfConfig rule, string formatted) |
| 155 | + : this |
| 156 | + ( |
| 157 | + rule, |
| 158 | + ExtractName(formatted, out int delimiter), |
| 159 | + ExtractPlatform(formatted, delimiter) |
| 160 | + ) |
| 161 | + { |
177 | 162 |
|
178 |
| - // < 2 https://github.com/3F/MvsSln/issues/19 |
179 |
| - Platform = cfg.Length < 2 ? string.Empty : cfg[1]; |
180 | 163 | }
|
181 | 164 |
|
182 | 165 | protected virtual string Sensitivity(string name)
|
183 | 166 | {
|
184 | 167 | if(!SensitivityComparing) {
|
185 | 168 | return name;
|
186 | 169 | }
|
187 |
| - return name.ToLowerInvariant(); |
| 170 | + return name?.ToLowerInvariant(); |
188 | 171 | }
|
189 | 172 |
|
190 |
| - #region DebuggerDisplay |
191 |
| - |
192 |
| - private string DbgDisplay |
| 173 | + private static string ExtractName(string raw, out int delimiter) |
193 | 174 | {
|
194 |
| - get => Format(); |
| 175 | + if(raw == null) |
| 176 | + { |
| 177 | + delimiter = -1; |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
| 181 | + delimiter = raw.IndexOf(DELIM); |
| 182 | + if(delimiter == -1) return raw; |
| 183 | + |
| 184 | + return raw.Substring(0, delimiter); |
195 | 185 | }
|
196 | 186 |
|
197 |
| - #endregion |
| 187 | + private static string ExtractPlatform(string raw, int delimiter) |
| 188 | + { |
| 189 | + if(raw == null) return null; |
| 190 | + if(delimiter == -1) return string.Empty; |
| 191 | + |
| 192 | + return raw.Substring(delimiter + 1); |
| 193 | + } |
198 | 194 | }
|
199 | 195 | }
|
0 commit comments