Skip to content

Commit 2cc2dd8

Browse files
committed
ConfigItem.Format() marked as obsolete,
Use `ToString()` and `Formatted` instead. +IConfPlatform.Formatted
1 parent bc7c529 commit 2cc2dd8

File tree

5 files changed

+147
-102
lines changed

5 files changed

+147
-102
lines changed

MvsSln/Core/ConfigItem.cs

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,83 +24,40 @@
2424
*/
2525

2626
using System;
27-
using System.Diagnostics;
2827
using net.r_eg.MvsSln.Extensions;
2928

3029
namespace net.r_eg.MvsSln.Core
3130
{
3231
/// <summary>
33-
/// Basic item of configuration and platform.
32+
/// Basic item of the configuration and its platform.
3433
/// </summary>
35-
[DebuggerDisplay("{DbgDisplay}")]
3634
public class ConfigItem: IConfPlatform
3735
{
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 = '|';
4637

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;
5639

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; }
6245

6346
public string ConfigurationByRule
6447
{
65-
get => Rule?.Configuration(Configuration);
48+
get => Rule?.Configuration(Configuration) ?? Configuration;
6649
}
6750

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);
7652

77-
public string Platform
78-
{
79-
get;
80-
protected set;
81-
}
53+
public string Platform { get; protected set; }
8254

83-
public string PlatformByRule
84-
{
85-
get => Rule?.Platform(Platform);
86-
}
55+
public string PlatformByRule => Rule?.Platform(Platform) ?? Platform;
8756

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);
9660

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>
10461
public bool IsEqualByRule(string config, string platform, bool icase = false)
10562
{
10663
var cmp = icase ? StringComparison.InvariantCultureIgnoreCase
@@ -136,64 +93,103 @@ public override int GetHashCode()
13693
return 0.CalculateHashCode
13794
(
13895
Configuration,
139-
Platform
96+
Platform,
97+
Rule,
98+
SensitivityComparing
14099
);
141100
}
142101

143-
public override string ToString()
144-
{
145-
return Format();
146-
}
102+
public override string ToString() => Format(Configuration, Platform);
147103

148104
/// <summary>
149105
/// Compatible format: 'configname'|'platformname'
150106
/// http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivscfg.get_displayname.aspx
151107
/// </summary>
152108
public static string Format(string configuration, string platform)
153109
{
154-
return $"{configuration}|{platform}";
110+
return $"{configuration}{DELIM}{platform}";
155111
}
156112

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)
158123
{
159-
return Format(Configuration, Platform);
124+
Rule = rule;
125+
Configuration = configuration;
126+
Platform = platform;
160127
}
161128

129+
/// <summary>
130+
/// Initialize using rule <see cref="RuleOfConfig"/> by default.
131+
/// </summary>
132+
/// <inheritdoc cref="ConfigItem(IRuleOfConfig, string, string)"/>
162133
public ConfigItem(string configuration, string platform)
134+
: this(new RuleOfConfig(), configuration, platform)
163135
{
164-
Configuration = configuration;
165-
Platform = platform;
136+
166137
}
167138

139+
/// <summary>
140+
/// Initialize using rule <see cref="RuleOfConfig"/> by default.
141+
/// </summary>
142+
/// <inheritdoc cref="ConfigItem(IRuleOfConfig, string)"/>
168143
public ConfigItem(string formatted)
144+
: this(new RuleOfConfig(), formatted)
169145
{
170-
if(formatted == null) {
171-
return;
172-
}
173146

174-
string[] cfg = formatted.Split('|');
147+
}
175148

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+
{
177162

178-
// < 2 https://github.com/3F/MvsSln/issues/19
179-
Platform = cfg.Length < 2 ? string.Empty : cfg[1];
180163
}
181164

182165
protected virtual string Sensitivity(string name)
183166
{
184167
if(!SensitivityComparing) {
185168
return name;
186169
}
187-
return name.ToLowerInvariant();
170+
return name?.ToLowerInvariant();
188171
}
189172

190-
#region DebuggerDisplay
191-
192-
private string DbgDisplay
173+
private static string ExtractName(string raw, out int delimiter)
193174
{
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);
195185
}
196186

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+
}
198194
}
199195
}

MvsSln/Core/ConfigPrj.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ private void Set(string pGuid, bool build, bool deploy, ConfigSln sln)
105105

106106
#region DebuggerDisplay
107107

108+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
108109
private string DbgDisplay
109110
{
110-
get => $"{Format()} -> {Sln} : [{PGuid}]";
111+
get => $"{ToString()} -> {Sln} : [{PGuid}]";
111112
}
112113

113114
#endregion

MvsSln/Core/ConfigSln.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
using System.Diagnostics;
27-
2826
namespace net.r_eg.MvsSln.Core
2927
{
3028
/// <summary>
3129
/// Solution Configuration
3230
/// </summary>
33-
[DebuggerDisplay("{DbgDisplay}")]
3431
public class ConfigSln: ConfigItem, IConfPlatform
3532
{
3633
public ConfigSln(string configuration, string platform)
@@ -44,14 +41,5 @@ public ConfigSln(string formatted)
4441
{
4542

4643
}
47-
48-
#region DebuggerDisplay
49-
50-
private string DbgDisplay
51-
{
52-
get => Format();
53-
}
54-
55-
#endregion
5644
}
5745
}

MvsSln/Core/IConfPlatform.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,56 @@ namespace net.r_eg.MvsSln.Core
2828
public interface IConfPlatform
2929
{
3030
/// <summary>
31-
/// The custom rule of the Configuration and Platform names.
31+
/// Configured rule of the <see cref="Configuration"/> and <see cref="Platform"/> representations.
3232
/// </summary>
3333
IRuleOfConfig Rule { get; }
3434

3535
/// <summary>
36-
/// To use an `Sensitivity` logic when comparing {IConfPlatform}
36+
/// Use "sensitivity" logic when comparing <see cref="IConfPlatform"/>
3737
/// together with `==` , `!=`.
3838
/// </summary>
3939
bool SensitivityComparing { get; set; }
4040

41+
/// <summary>
42+
/// Configuration name.
43+
/// </summary>
4144
string Configuration { get; }
4245

46+
/// <summary>
47+
/// Configuration name using <see cref="Rule"/>.
48+
/// </summary>
4349
string ConfigurationByRule { get; }
4450

4551
/// <summary>
46-
/// {ConfigurationByRule} with optional case insensitive logic.
47-
/// Uses {SensitivityComparing} flag.
52+
/// <see cref="ConfigurationByRule"/> with optional case insensitive logic.
4853
/// </summary>
54+
/// <remarks>Uses <see cref="SensitivityComparing"/> flag.</remarks>
4955
string ConfigurationByRuleICase { get; }
5056

57+
/// <summary>
58+
/// Platform name.
59+
/// </summary>
5160
string Platform { get; }
5261

62+
/// <summary>
63+
/// Platform name using <see cref="Rule"/>.
64+
/// </summary>
5365
string PlatformByRule { get; }
5466

5567
/// <summary>
56-
/// {PlatformByRule} with optional case insensitive logic.
57-
/// Uses {SensitivityComparing} flag.
68+
/// <see cref="PlatformByRule"/> with optional case insensitive logic.
69+
/// Uses <see cref="SensitivityComparing"/> flag.
5870
/// </summary>
5971
string PlatformByRuleICase { get; }
6072

6173
/// <summary>
62-
/// Checking an config/platform by using {Rule} instance.
74+
/// Formatted final configuration.
75+
/// </summary>
76+
/// <remarks>Using <see cref="Rule"/>.</remarks>
77+
string Formatted { get; }
78+
79+
/// <summary>
80+
/// Checking an config/platform by using <see cref="Rule"/> instance.
6381
/// </summary>
6482
/// <param name="config">Configuration name.</param>
6583
/// <param name="platform">Platform name.</param>

MvsSlnTest/Core/ConfigItemTest.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public void EqTest2()
4040

4141
var target3 = new ConfigItem("Config1", "Platform1");
4242
Assert.Equal("Config1|Platform1", target3.ToString());
43+
44+
#pragma warning disable CS0618 // Type or member is obsolete
4345
Assert.Equal("Config1|Platform1", target3.Format());
46+
#pragma warning restore CS0618 // Type or member is obsolete
4447
}
4548

4649
[Fact]
@@ -104,7 +107,7 @@ public void NoPlatformTest1()
104107
[Fact]
105108
public void CtorTest1()
106109
{
107-
var target = new ConfigItem("");
110+
var target = new ConfigItem(string.Empty);
108111

109112
Assert.Equal(string.Empty, target.Configuration);
110113
Assert.Equal(string.Empty, target.Platform);
@@ -119,5 +122,44 @@ public void CtorTest1()
119122
Assert.Null(target.Configuration);
120123
Assert.Null(target.Platform);
121124
}
125+
126+
[Fact]
127+
public void FormatTest1()
128+
{
129+
string input = "Debug|Any CPU";
130+
string rulemod = "Debug|AnyCPU";
131+
132+
Assert.Equal(input, new ConfigItem(input).ToString());
133+
Assert.Equal(input, new ConfigItem((IRuleOfConfig)null, input).ToString());
134+
135+
#pragma warning disable CS0618 // Type or member is obsolete
136+
Assert.Equal(input, new ConfigItem(input).Format());
137+
Assert.Equal(input, new ConfigItem((IRuleOfConfig)null, input).Format());
138+
#pragma warning restore CS0618 // Type or member is obsolete
139+
140+
Assert.Equal(rulemod, new ConfigItem(input).Formatted);
141+
Assert.Equal(input, new ConfigItem((IRuleOfConfig)null, input).Formatted);
142+
}
143+
144+
[Fact]
145+
public void FormatTest2()
146+
{
147+
string name = "Debug";
148+
string platform = "Any CPU";
149+
150+
string res1 = "Debug|Any CPU";
151+
string res2 = "Debug|AnyCPU";
152+
153+
Assert.Equal(res1, new ConfigItem(name, platform).ToString());
154+
Assert.Equal(res1, new ConfigItem(null, name, platform).ToString());
155+
156+
#pragma warning disable CS0618 // Type or member is obsolete
157+
Assert.Equal(res1, new ConfigItem(name, platform).Format());
158+
Assert.Equal(res1, new ConfigItem(null, name, platform).Format());
159+
#pragma warning restore CS0618 // Type or member is obsolete
160+
161+
Assert.Equal(res2, new ConfigItem(name, platform).Formatted);
162+
Assert.Equal(res1, new ConfigItem(null, name, platform).Formatted);
163+
}
122164
}
123165
}

0 commit comments

Comments
 (0)