Skip to content

Commit 7836c07

Browse files
committed
Refactor RemoveReferencesFromCsProj by simplifying package removal logic and removing support for global usings. Adjust execution strategies accordingly.
1 parent 87ae294 commit 7836c07

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

Services/Common/OJS.Workers/OJS.Workers.ExecutionStrategies/CSharp/DotNetCore/DotNetCoreProjectTestsExecutionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class DotNetCoreProjectTestsExecutionStrategy<TSettings> : CSharpProjectT
1919
protected new const string AdditionalExecutionArguments = "--noresult";
2020
protected const string CsProjFileExtension = ".csproj";
2121

22-
protected IEnumerable<string> PackageNamesToRemoveFromUserCsProjFile =>
22+
protected string[] PackageNamesToRemoveFromUserCsProjFile =>
2323
[
2424
"NUnit",
2525
"NUnitLite",

Services/Common/OJS.Workers/OJS.Workers.ExecutionStrategies/CSharp/DotNetCore/DotNetCoreUnitTestsExecutionStrategy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ protected override async Task<IExecutionResult<TestResult>> ExecuteAgainstTestsI
5757
DotNetCoreStrategiesHelper.RemoveReferencesFromCsProj(
5858
userCsProjPath,
5959
this.PackageNamesToRemoveFromUserCsProjFile,
60-
removeProjectReferences: true,
61-
removeGlobalUsings: true);
60+
removeProjectReferences: true);
6261

6362
var (csProjTemplate, csProjPath) = this.CreateNUnitLiteConsoleApp([userCsProjPath]);
6463

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace OJS.Workers.ExecutionStrategies.Helpers
22
{
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Xml.Linq;
65

@@ -13,59 +12,42 @@ internal static class DotNetCoreStrategiesHelper
1312
/// <param name="csProjPath">Path to .csproj file.</param>
1413
/// <param name="packageNames">The names of the packages that should be removed (case-insensitive).</param>
1514
/// <param name="removeProjectReferences">Whether to remove "<ProjectReference />" items as well.</param>
16-
/// <param name="removeGlobalUsings">Whether to remove "<Using />" items as well.</param>
17-
public static void RemoveReferencesFromCsProj(string csProjPath, IEnumerable<string> packageNames, bool removeProjectReferences = false, bool removeGlobalUsings = false)
15+
public static void RemoveReferencesFromCsProj(string csProjPath, string[] packageNames, bool removeProjectReferences = false)
1816
{
19-
var packageNamesToLower = new HashSet<string>(
20-
packageNames.Select(p => p.ToLowerInvariant()));
21-
2217
var csProjDoc = XDocument.Load(csProjPath);
2318

2419
var ns = csProjDoc.Root?.Name.Namespace ?? XNamespace.None;
2520

2621
var toRemove = csProjDoc
2722
.Descendants(ns + "PackageReference")
28-
.Where(pr =>
29-
{
30-
var include = pr.Attribute("Include")?.Value;
31-
return include != null && packageNamesToLower.Contains(include.ToLowerInvariant());
32-
})
23+
.Where(ShouldRemoveInclude)
3324
.ToList();
3425

3526
toRemove.AddRange(csProjDoc
3627
.Descendants(ns + "Using")
37-
.Where(u =>
38-
{
39-
var value = u.Attribute("Include")?.Value;
40-
return value != null &&
41-
packageNamesToLower.Any(pn =>
42-
value.Equals(pn, StringComparison.OrdinalIgnoreCase) ||
43-
value.StartsWith($"{pn}.", StringComparison.InvariantCultureIgnoreCase) ||
44-
value.StartsWith($"{pn}:", StringComparison.InvariantCultureIgnoreCase));
45-
}));
28+
.Where(ShouldRemoveInclude));
4629

4730
if (removeProjectReferences)
4831
{
4932
toRemove.AddRange(csProjDoc.Descendants(ns + "ProjectReference"));
5033
}
5134

52-
if (removeGlobalUsings)
53-
{
54-
toRemove.AddRange(csProjDoc.Descendants(ns + "Using")
55-
.Where(u =>
56-
{
57-
var value = u.Attribute("Include")?.Value;
58-
return value != null &&
59-
packageNamesToLower.Any(pn =>
60-
value == pn ||
61-
value.StartsWith($"{pn}.", StringComparison.InvariantCultureIgnoreCase) ||
62-
value.StartsWith($"{pn}:", StringComparison.InvariantCultureIgnoreCase));
63-
}));
64-
}
65-
6635
toRemove.ForEach(x => x.Remove());
6736

6837
csProjDoc.Save(csProjPath);
38+
39+
return;
40+
41+
bool ShouldRemoveInclude(XElement element)
42+
{
43+
var include = element.Attribute("Include")?.Value;
44+
return
45+
include != null &&
46+
packageNames.Any(pn =>
47+
include.Equals(pn, StringComparison.OrdinalIgnoreCase) ||
48+
include.StartsWith($"{pn}.", StringComparison.InvariantCultureIgnoreCase) ||
49+
include.StartsWith($"{pn}:", StringComparison.InvariantCultureIgnoreCase));
50+
}
6951
}
7052
}
7153
}

0 commit comments

Comments
 (0)