Skip to content

Commit 8f1096b

Browse files
authored
Clean-up code by introducing String compare helpers (#288)
1 parent cbfd66e commit 8f1096b

11 files changed

+56
-26
lines changed

src/Buildalyzer/Buildalyzer.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<PackageValidationBaselineVersion>7.0.1</PackageValidationBaselineVersion>
99
<OutputType>library</OutputType>
1010
<PackageReleaseNotes>
11+
<![CDATA[
12+
ToBeReleased
13+
- Drop Buildalyzer.EmptyDisposable. (BREAKING)
14+
]]>
1115
</PackageReleaseNotes>
1216
</PropertyGroup>
1317

src/Buildalyzer/Compiler/FSharpCommandLineParser.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#nullable enable
22

3+
using System;
4+
35
namespace Buildalyzer;
46

57
internal static class FSharpCommandLineParser
@@ -52,8 +54,8 @@ private static IEnumerable<string> Tokenize(string arg)
5254

5355
[Pure]
5456
public static bool NotCompilerLocation(string s)
55-
=> !s.EndsWith("fsc.dll", StringComparison.OrdinalIgnoreCase)
56-
&& !s.EndsWith("fsc.exe", StringComparison.OrdinalIgnoreCase);
57+
=> !s.IsMatchEnd("fsc.dll")
58+
&& !s.IsMatchEnd("fsc.exe");
5759

5860
private static readonly char[] Splitters = ['\r', '\n'];
5961
}

src/Buildalyzer/Compiler/RoslynCommandLineParser.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22

3+
using System;
34
using Microsoft.CodeAnalysis;
45

56
namespace Buildalyzer;
@@ -17,7 +18,7 @@ internal static class RoslynCommandLineParser
1718
{
1819
for (var i = 0; i < args.Length - 1; i++)
1920
{
20-
if (args[i].EndsWith(exec, StringComparison.OrdinalIgnoreCase))
21+
if (args[i].IsMatchEnd(exec))
2122
{
2223
return args[i..];
2324
}

src/Buildalyzer/Construction/ProjectFile.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Xml.Linq;
34

45
namespace Buildalyzer.Construction;
@@ -59,8 +60,8 @@ internal ProjectFile(string path)
5960

6061
/// <inheritdoc />
6162
public bool RequiresNetFramework =>
62-
_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Exists(i => x.GetAttributeValue(ProjectFileNames.Project)?.EndsWith(i, StringComparison.OrdinalIgnoreCase) ?? false))
63-
|| _projectElement.GetDescendants(ProjectFileNames.LanguageTargets).Any(x => ImportsThatRequireNetFramework.Exists(i => x.Value.EndsWith(i, StringComparison.OrdinalIgnoreCase)))
63+
_projectElement.GetDescendants(ProjectFileNames.Import).Any(x => ImportsThatRequireNetFramework.Exists(i => x.GetAttributeValue(ProjectFileNames.Project)?.IsMatchEnd(i) ?? false))
64+
|| _projectElement.GetDescendants(ProjectFileNames.LanguageTargets).Any(x => ImportsThatRequireNetFramework.Exists(i => x.Value.IsMatchEnd(i)))
6465
|| ToolsVersion != null;
6566

6667
/// <inheritdoc />

src/Buildalyzer/Environment/BuildEnvironment.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class BuildEnvironment
1313
!System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
1414
.Replace(" ", string.Empty)
1515
.Trim()
16-
.StartsWith(".NETFramework", StringComparison.OrdinalIgnoreCase);
16+
.IsMatchStart(".NETFramework");
1717

1818
private readonly Dictionary<string, string> _globalProperties;
1919
private readonly Dictionary<string, string> _environmentVariables;

src/Buildalyzer/Environment/DotNetInfoParser.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#nullable enable
22

3+
using System;
34
using System.IO;
45

56
namespace Buildalyzer.Environment;
67

78
internal static class DotNetInfoParser
89
{
9-
private const StringComparison IgnoreCase = StringComparison.OrdinalIgnoreCase;
10-
1110
[Pure]
1211
public static DotNetInfo Parse(IEnumerable<string> lines)
1312
{
@@ -94,20 +93,20 @@ void AddRunTime(string line)
9493

9594
[Pure]
9695
private static Version? Version(string prefix, string line)
97-
=> line.StartsWith(prefix, IgnoreCase) && System.Version.TryParse(line[prefix.Length..].Trim(), out var parsed)
96+
=> line.IsMatchStart(prefix) && System.Version.TryParse(line[prefix.Length..].Trim(), out var parsed)
9897
? parsed
9998
: null;
10099

101100
[Pure]
102101
private static string? Label(string prefix, string line)
103-
=> line.StartsWith(prefix, IgnoreCase) && line[prefix.Length..].Trim() is { Length: > 0 } label
102+
=> line.IsMatchStart(prefix) && line[prefix.Length..].Trim() is { Length: > 0 } label
104103
? label
105104
: null;
106105

107106
[Pure]
108107
private static string? BasePath(string line)
109108
{
110-
if (line.StartsWith("Base Path:", IgnoreCase))
109+
if (line.IsMatchStart("Base Path:"))
111110
{
112111
var path = line[10..].Trim();
113112

@@ -142,8 +141,7 @@ void AddRunTime(string line)
142141
private static string UnifyPath(string path) => path.Replace('\\', '/').TrimEnd('/');
143142

144143
[Pure]
145-
private static string? GlobalJson(string line)
146-
=> line.Equals("Not found", IgnoreCase) ? null : line;
144+
private static string? GlobalJson(string line) => line.IsMatch("Not found") ? null : line;
147145

148146
private static readonly HashSet<string> Headers = new(StringComparer.InvariantCultureIgnoreCase)
149147
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace System;
2+
3+
internal static class BuildalyzerStringExtensions
4+
{
5+
/// <summary>
6+
/// Returns true if the <paramref name="value"/> string has the same value, ignoring casing.
7+
/// </summary>
8+
[Pure]
9+
public static bool IsMatch(this string? self, string? value) => string.Equals(self, value, StringComparison.OrdinalIgnoreCase);
10+
11+
/// <summary>
12+
/// Returns true if the string starts with <paramref name="value"/>, ignoring casing.
13+
/// </summary>
14+
[Pure]
15+
public static bool IsMatchStart(this string self, string value)
16+
=> self.StartsWith(value, StringComparison.OrdinalIgnoreCase);
17+
18+
/// <summary>
19+
/// Returns true if the string ends with <paramref name="value"/>, ignoring casing.
20+
/// </summary>
21+
[Pure]
22+
public static bool IsMatchEnd(this string self, string value)
23+
=> self.EndsWith(value, StringComparison.OrdinalIgnoreCase);
24+
}

src/Buildalyzer/IO/IOPath.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public override bool Equals([NotNullWhen(true)] object? obj)
5050
/// <inheritdoc />
5151
[Pure]
5252
public bool Equals(IOPath other, bool caseSensitive)
53-
=> string.Equals(_path, other._path, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
53+
=> caseSensitive
54+
? _path == other._path
55+
: _path.IsMatch(other._path);
5456

5557
/// <inheritdoc />
5658
[Pure]

src/Buildalyzer/Logging/EventProcessor.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
152152
}
153153

154154
// Process the command line arguments for the Fsc task
155-
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
155+
if (e.SenderName.IsMatch("Fsc")
156156
&& !string.IsNullOrWhiteSpace(e.Message)
157157
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
158158
&& result.CompilerCommand is null)
@@ -161,14 +161,12 @@ private void MessageRaised(object sender, BuildMessageEventArgs e)
161161
}
162162

163163
// Process the command line arguments for the Csc task
164-
if (e is TaskCommandLineEventArgs cmd
165-
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
164+
if (e is TaskCommandLineEventArgs cmd && cmd.TaskName.IsMatch("Csc"))
166165
{
167166
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
168167
}
169168

170-
if (e is TaskCommandLineEventArgs cmdVbc &&
171-
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
169+
if (e is TaskCommandLineEventArgs cmdVbc && cmdVbc.TaskName.IsMatch("Vbc"))
172170
{
173171
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
174172
}

src/Buildalyzer/ProjectAnalyzer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private string GetCommand(
216216
string initialArguments = string.Empty;
217217
bool isDotNet = false; // false=MSBuild.exe, true=dotnet.exe
218218
if (string.IsNullOrWhiteSpace(buildEnvironment.MsBuildExePath)
219-
|| Path.GetExtension(buildEnvironment.MsBuildExePath).Equals(".dll", StringComparison.OrdinalIgnoreCase))
219+
|| Path.GetExtension(buildEnvironment.MsBuildExePath).IsMatch(".dll"))
220220
{
221221
// in case of no MSBuild path or a path to the MSBuild dll, run dotnet
222222
fileName = buildEnvironment.DotnetExePath;
@@ -256,7 +256,7 @@ private string GetCommand(
256256
// Setting the TargetFramework MSBuild property tells MSBuild which target framework to use for the outer build
257257
effectiveGlobalProperties[MsBuildProperties.TargetFramework] = targetFramework;
258258
}
259-
if (Path.GetExtension(ProjectFile.Path).Equals(".fsproj", StringComparison.OrdinalIgnoreCase)
259+
if (Path.GetExtension(ProjectFile.Path).IsMatch(".fsproj")
260260
&& effectiveGlobalProperties.ContainsKey(MsBuildProperties.SkipCompilerExecution))
261261
{
262262
// We can't skip the compiler for design-time builds in F# (it causes strange errors regarding file copying)

src/Buildalyzer/XDocumentExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace Buildalyzer;
55
internal static class XDocumentExtensions
66
{
77
public static IEnumerable<XElement> GetDescendants(this XDocument document, string name) =>
8-
document.Descendants().Where(x => string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase));
8+
document.Descendants().Where(x => x.Name.LocalName.IsMatch(name));
99

1010
public static IEnumerable<XElement> GetDescendants(this XElement element, string name) =>
11-
element.Descendants().Where(x => string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase));
11+
element.Descendants().Where(x => x.Name.LocalName.IsMatch(name));
1212

13-
public static string GetAttributeValue(this XElement element, string name) =>
14-
element.Attributes().FirstOrDefault(x => string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase))?.Value;
13+
public static string? GetAttributeValue(this XElement element, string name) =>
14+
element.Attributes().FirstOrDefault(x => x.Name.LocalName.IsMatch(name))?.Value;
1515

1616
// Adds a child element with the same namespace as the parent
1717
public static void AddChildElement(this XElement element, string name, string value) =>

0 commit comments

Comments
 (0)