Skip to content

Commit 364fdbf

Browse files
committed
sln-remove: Support for slnx
1 parent 931d2df commit 364fdbf

File tree

1 file changed

+30
-22
lines changed
  • src/Cli/dotnet/commands/dotnet-sln/remove

1 file changed

+30
-22
lines changed

src/Cli/dotnet/commands/dotnet-sln/remove/Program.cs

+30-22
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,64 @@
44
using System.CommandLine;
55
using Microsoft.DotNet.Cli;
66
using Microsoft.DotNet.Cli.Sln.Internal;
7+
using Microsoft.DotNet.Cli.Utils;
78
using Microsoft.DotNet.Tools.Common;
9+
using Microsoft.VisualStudio.SolutionPersistence;
10+
using Microsoft.VisualStudio.SolutionPersistence.Model;
811

912
namespace Microsoft.DotNet.Tools.Sln.Remove
1013
{
1114
internal class RemoveProjectFromSolutionCommand : CommandBase
1215
{
1316
private readonly string _fileOrDirectory;
14-
private readonly IReadOnlyCollection<string> _arguments;
17+
private readonly IReadOnlyCollection<string> _projects;
1518

1619
public RemoveProjectFromSolutionCommand(ParseResult parseResult) : base(parseResult)
1720
{
1821
_fileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument);
1922

20-
_arguments = (parseResult.GetValue(SlnRemoveParser.ProjectPathArgument) ?? Array.Empty<string>()).ToList().AsReadOnly();
23+
_projects = (parseResult.GetValue(SlnRemoveParser.ProjectPathArgument) ?? Array.Empty<string>()).ToList().AsReadOnly();
2124

22-
SlnArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _arguments, SlnArgumentValidator.CommandType.Remove);
25+
SlnArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _projects, SlnArgumentValidator.CommandType.Remove);
2326
}
2427

2528
public override int Execute()
2629
{
27-
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
30+
string solutionFileFullPath = SlnCommandParser.GetSlnFileFullPath(_fileOrDirectory);
31+
if (_projects.Count == 0)
32+
{
33+
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove);
34+
}
2835

29-
var baseDirectory = PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory);
30-
var relativeProjectPaths = _arguments.Select(p =>
36+
IEnumerable<string> fullProjectPaths = _projects.Select(project =>
3137
{
32-
var fullPath = Path.GetFullPath(p);
33-
return Path.GetRelativePath(
34-
baseDirectory,
35-
Directory.Exists(fullPath) ?
36-
MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName :
37-
fullPath
38-
);
38+
var fullPath = Path.GetFullPath(project);
39+
return Directory.Exists(fullPath) ? MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName : fullPath;
3940
});
4041

41-
bool slnChanged = false;
42-
foreach (var path in relativeProjectPaths)
42+
try
4343
{
44-
slnChanged |= slnFile.RemoveProject(path);
44+
RemoveProjectsAsync(solutionFileFullPath, fullProjectPaths, CancellationToken.None).Wait();
45+
return 0;
4546
}
47+
catch (Exception ex)
48+
{
49+
throw new GracefulException(ex.Message, ex);
50+
}
51+
}
4652

47-
slnFile.RemoveEmptyConfigurationSections();
48-
49-
slnFile.RemoveEmptySolutionFolders();
53+
private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<string> projectPaths, CancellationToken cancellationToken)
54+
{
55+
ISolutionSerializer serializer = SlnCommandParser.GetSolutionSerializer(solutionFileFullPath);
56+
SolutionModel solution = await serializer.OpenAsync(solutionFileFullPath, cancellationToken);
5057

51-
if (slnChanged)
58+
foreach (var project in projectPaths)
5259
{
53-
slnFile.Write();
60+
SolutionProjectModel projectModel = solution.FindProject(project);
61+
solution.RemoveProject(projectModel);
5462
}
5563

56-
return 0;
64+
await serializer.SaveAsync(solutionFileFullPath, solution, cancellationToken);
5765
}
5866
}
5967
}

0 commit comments

Comments
 (0)