Skip to content

Commit f6b89cd

Browse files
committed
temp
1 parent efb73ba commit f6b89cd

File tree

10 files changed

+121
-160
lines changed

10 files changed

+121
-160
lines changed

cli/nugetswitcher.cli/Program.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
using NuGetSwitcher.Core.Commands;
88
using NuGetSwitcher.Core.Commands.Enums;
99

10+
using NuGetSwitcher.Core.References;
11+
1012
using NuGetSwitcher.Interface.Logger.Enums;
1113

1214
using System.Reflection;
15+
using System.Threading.Tasks;
1316

1417
namespace NuGetSwitcher.CLI
1518
{
1619
internal static class Program
1720
{
18-
public static void Main(string[] args)
21+
public static async Task Main(string[] args)
1922
{
2023
CliArguments cliArgs = new
2124
CliArguments
@@ -29,23 +32,23 @@ public static void Main(string[] args)
2932
CliOptions
3033
(cliArgs.IncludeProjectFile, cliArgs.ExcludeProjectFile, logger);
3134

32-
CliSolution solution = new
33-
CliSolution
35+
CliSolution<ProjectReference> solution = new
36+
CliSolution<ProjectReference>
3437
(cliArgs.AbsolutePath);
3538

36-
ProjectCommand projectCommand = new ProjectCommand(options, solution, logger);
37-
PackageCommand packageCommand = new PackageCommand(options, solution, logger);
39+
ProjectCommand<ProjectReference> projectCommand = new ProjectCommand<ProjectReference>(options, solution, logger);
40+
PackageCommand<ProjectReference> packageCommand = new PackageCommand<ProjectReference>(options, solution, logger);
3841

3942
switch (cliArgs.Mode)
4043
{
4144
case Mode.VR:
4245
logger.LogMessage(Assembly.GetExecutingAssembly().GetName().Version.ToString(), Category.I);
4346
break;
4447
case Mode.PK:
45-
packageCommand.Switch();
48+
await packageCommand.SwitchAsync();
4649
break;
4750
case Mode.PR:
48-
projectCommand.Switch();
51+
await projectCommand.SwitchAsync();
4952
break;
5053
default:
5154
logger.LogMessage($"Unable to continue program execution, input arguments: { string.Join(' ', args) }", Category.E);

cli/nugetswitcher.cliservice/Solution/CliSolution.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
using NuGetSwitcher.Core.Solution.Abstract;
44

5+
using NuGetSwitcher.Interface.References;
6+
57
namespace NuGetSwitcher.CLIService.Solution
68
{
7-
public sealed class CliSolution(string absolutePath) : AbstractSolution(absolutePath)
9+
public sealed class CliSolution<TReference>(string absolutePath) : AbstractSolution<TReference> (absolutePath) where TReference : class, IProjectReference
810
{
911
/// <inheritdoc cref="ProjectCollection"/>
1012
private readonly

lib/nugetswitcher.core/Commands/AbstractCommand.cs

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,37 @@
22
using CliWrap.Buffered;
33
using CliWrap.Builders;
44

5-
using NuGet.ProjectModel;
6-
75
using NuGetSwitcher.Interface.Logger;
86
using NuGetSwitcher.Interface.Logger.Enums;
97

10-
using NuGetSwitcher.Interface.Options;
11-
using NuGetSwitcher.Interface.References;
12-
13-
using System;
14-
using System.Collections.Generic;
15-
168
using System.IO;
17-
using System.Linq;
9+
using System.Threading.Tasks;
1810

1911
namespace NuGetSwitcher.Core.Commands
2012
{
21-
public abstract class AbstractCommand(string referenceType, IOptions option, ILogger logger)
13+
public abstract class AbstractCommand(ILogger logger)
2214
{
23-
public abstract void Switch();
24-
25-
/// <summary>
26-
/// Iterates through the dependencies provided in the lock file and matches
27-
/// against items found in the directories listed in the configuration file.
28-
/// For each matched item, the passed delegate is executed.
29-
/// </summary>
30-
protected virtual void IterateAndExecute<TReference>(IEnumerable<TReference> references, Action<IProjectReference, LockFileTargetLibrary, string> func) where TReference : IProjectReference
15+
protected ILogger Logger
3116
{
32-
logger.Clear();
33-
34-
IReadOnlyDictionary<string, string> items = option.GetIncludeItems(referenceType);
35-
36-
foreach (IProjectReference reference in references)
37-
{
38-
foreach (LockFileTargetLibrary library in reference.GetLockFileLibraries())
39-
{
40-
if (items.TryGetValue(library.Name, out string absolutePath))
41-
{
42-
func(reference, library, absolutePath);
43-
}
44-
}
45-
46-
reference.Save();
47-
}
17+
get => logger;
4818
}
49-
50-
/// <summary>
51-
/// Adds reference to the project. It is assumed
52-
/// that the original reference has been removed
53-
/// earlier.
54-
/// </summary>
55-
///
56-
/// <param name="unevaluatedInclude">
57-
/// Must contain the assembly
58-
/// name or the absolute path
59-
/// to the project or Package
60-
/// Id.
61-
/// </param>
62-
protected virtual void AddReference(IProjectReference reference, string referenceType, string unevaluatedInclude, Dictionary<string, string> metadata)
63-
{
64-
if (reference.TryAddReference(unevaluatedInclude, referenceType, metadata))
65-
{
66-
logger.LogMessage($"{referenceType}: {Path.GetFileName(unevaluatedInclude)} has been added", Category.I, project: reference.Project.FullPath);
67-
}
68-
}
69-
19+
20+
public abstract ValueTask SwitchAsync();
21+
7022
/// <summary>
7123
/// Performs an action on the solution file, taking
7224
/// into account the passed <paramref name="builder"/>.
7325
/// </summary>
74-
protected virtual void SlnAction(string solution, IEnumerable<string> projects, ArgumentsBuilder builder)
26+
///
27+
/// <param name="absolutePath">
28+
/// Absolute path to the solution file.
29+
/// </param>
30+
protected async Task SlnActionAsync(string absolutePath, ArgumentsBuilder builder)
7531
{
76-
void Log(string source, Category category)
77-
{
78-
if (!string.IsNullOrWhiteSpace(source))
79-
{
80-
logger.Clear();
81-
logger.LogMessage(source, category);
82-
}
83-
}
84-
85-
if (projects.Any())
86-
{
87-
// TODO: Switch to async.
88-
BufferedCommandResult result = Cli.Wrap("dotnet").WithWorkingDirectory(Path.GetDirectoryName(solution)).WithArguments(args => args.Add("sln").Add(solution).Add(builder.Build(), false)).ExecuteBufferedAsync().GetAwaiter().GetResult();
89-
90-
Log(result.StandardOutput, Category.I);
91-
Log(result.StandardError, Category.E);
92-
}
32+
// TODO: Switch to async.
33+
BufferedCommandResult result = await Cli.Wrap("dotnet").WithWorkingDirectory(Path.GetDirectoryName(absolutePath)).WithArguments(args => args.Add("sln").Add(absolutePath).Add(builder.Build(), false)).ExecuteBufferedAsync();
34+
35+
logger.LogMessage(result.StandardError ?? result.StandardOutput, result.IsSuccess ? Category.I : Category.E);
9336
}
9437
}
9538
}

lib/nugetswitcher.core/Commands/PackageCommand.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
using Microsoft.Build.Evaluation;
44

5-
using NuGetSwitcher.Core.Constants;
6-
using NuGetSwitcher.Core.References;
5+
using NuGetSwitcher.Core.Constants;
6+
77
using NuGetSwitcher.Interface.Logger;
88
using NuGetSwitcher.Interface.Logger.Enums;
99

@@ -13,26 +13,18 @@
1313

1414
using System.Collections.Generic;
1515
using System.IO;
16-
using System.Linq;
16+
using System.Threading.Tasks;
1717

1818
namespace NuGetSwitcher.Core.Commands
1919
{
20-
public sealed class PackageCommand(IOptions option, ISolution solution, ILogger logger) : AbstractCommand(ReferenceType.PackageReference, option, logger)
20+
public sealed class PackageCommand<TReference>(IOptions option, ISolution<TReference> solution, ILogger logger) : AbstractCommand(logger)where TReference: class, IProjectReference
2121
{
22-
/// <summary>
23-
/// Returns references that match the passed type and are marked with the Temp attribute.
24-
/// </summary>
25-
private static IEnumerable<ProjectItem> GetTempItems(IProjectReference reference, string referenceType)
26-
{
27-
return reference.Project.GetItems(referenceType).Where(i => i.HasMetadata("Temp")).ToList();
28-
}
29-
3022
/// <summary>
3123
/// Removes projects from the solution file.
3224
/// </summary>
33-
private void RemoveFromSolution(string absolutePath, IEnumerable<string> projects)
25+
private async ValueTask RemoveFromSolutionAsync(string absolutePath, IEnumerable<string> projects)
3426
{
35-
SlnAction(absolutePath, projects, new ArgumentsBuilder().Add("remove").Add(projects));
27+
await SlnActionAsync(absolutePath, new ArgumentsBuilder().Add("remove").Add(projects));
3628
}
3729

3830
/// <summary>
@@ -41,13 +33,13 @@ private void RemoveFromSolution(string absolutePath, IEnumerable<string> project
4133
/// the Dependencies or FrameworkAssemblies sections of the
4234
/// lock file.
4335
/// </summary>
44-
public IEnumerable<string> SwitchDependencies(IProjectReference reference, string referenceType)
36+
private IEnumerable<string> SwitchDependencies(IProjectReference reference, string referenceType)
4537
{
4638
List<string> projects = new
4739
List<string>
4840
();
4941

50-
foreach (ProjectItem item in GetTempItems(reference, referenceType))
42+
foreach (ProjectItem item in reference.GetItems(referenceType, "Temp"))
5143
{
5244
if (referenceType == ReferenceType.ProjectReference)
5345
{
@@ -70,28 +62,31 @@ public IEnumerable<string> SwitchDependencies(IProjectReference reference, strin
7062
item.ItemType = referenceType;
7163
}
7264

73-
logger.LogMessage($"{referenceType}: {Path.GetFileName(item.EvaluatedInclude)} has been switched back", Category.I, project: reference.Project.FullPath);
65+
Logger.LogMessage($"{referenceType}: {Path.GetFileName(item.EvaluatedInclude)} has been switched back", Category.I, project: reference.Project.FullPath);
7466
}
7567

7668
return projects;
7769
}
7870

7971
/// <inheritdoc/>
80-
public override void Switch()
72+
public override async ValueTask SwitchAsync()
8173
{
8274
HashSet<string> projects = new
8375
HashSet<string>
8476
();
8577

86-
foreach (IProjectReference reference in solution.GetLoadedProjects<ProjectReference>())
78+
foreach (TReference reference in await solution.GetLoadedProjectsAsync())
8779
{
88-
projects.UnionWith(SwitchDependencies(reference, ReferenceType.Reference));
80+
SwitchDependencies(reference, ReferenceType.Reference);
8981
projects.UnionWith(SwitchDependencies(reference, ReferenceType.ProjectReference));
9082

9183
reference.Save();
9284
}
9385

94-
RemoveFromSolution(solution.AbsolutePath, projects);
86+
if (projects.Count != default)
87+
{
88+
await RemoveFromSolutionAsync(solution.AbsolutePath, projects);
89+
}
9590
}
9691
}
9792
}

0 commit comments

Comments
 (0)