|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.CommandLine;
|
| 5 | +using Microsoft.Build.Construction; |
| 6 | +using Microsoft.Build.Execution; |
5 | 7 | using Microsoft.DotNet.Cli;
|
6 |
| -using Microsoft.DotNet.Cli.Sln.Internal; |
| 8 | +using Microsoft.DotNet.Cli.Utils; |
7 | 9 | using Microsoft.DotNet.Tools.Common;
|
| 10 | +using Microsoft.Extensions.EnvironmentAbstractions; |
| 11 | +using Microsoft.VisualStudio.SolutionPersistence; |
| 12 | +using Microsoft.VisualStudio.SolutionPersistence.Model; |
| 13 | +using Microsoft.VisualStudio.SolutionPersistence.Serializer.SlnV12; |
8 | 14 |
|
9 | 15 | namespace Microsoft.DotNet.Tools.Sln.Remove
|
10 | 16 | {
|
11 | 17 | internal class RemoveProjectFromSolutionCommand : CommandBase
|
12 | 18 | {
|
13 | 19 | private readonly string _fileOrDirectory;
|
14 |
| - private readonly IReadOnlyCollection<string> _arguments; |
| 20 | + private readonly IReadOnlyCollection<string> _projects; |
| 21 | + |
| 22 | + private int CountNonFolderDescendants( |
| 23 | + SolutionModel solution, |
| 24 | + SolutionFolderModel item, |
| 25 | + Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent, |
| 26 | + Dictionary<SolutionFolderModel, int> cached) |
| 27 | + { |
| 28 | + if (cached.ContainsKey(item)) |
| 29 | + { |
| 30 | + return cached[item]; |
| 31 | + } |
| 32 | + int count = item.Files?.Count ?? 0; |
| 33 | + var children = solutionItemsGroupedByParent.TryGetValue(item, out var items) ? items : Array.Empty<SolutionItemModel>(); |
| 34 | + foreach (var child in children) |
| 35 | + { |
| 36 | + count += child is SolutionFolderModel folderModel |
| 37 | + ? CountNonFolderDescendants(solution, folderModel, solutionItemsGroupedByParent, cached) |
| 38 | + : 1; |
| 39 | + } |
| 40 | + cached.Add(item, count); |
| 41 | + return count; |
| 42 | + } |
15 | 43 |
|
16 | 44 | public RemoveProjectFromSolutionCommand(ParseResult parseResult) : base(parseResult)
|
17 | 45 | {
|
18 | 46 | _fileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument);
|
19 | 47 |
|
20 |
| - _arguments = (parseResult.GetValue(SlnRemoveParser.ProjectPathArgument) ?? Array.Empty<string>()).ToList().AsReadOnly(); |
| 48 | + _projects = (parseResult.GetValue(SlnRemoveParser.ProjectPathArgument) ?? Array.Empty<string>()).ToList().AsReadOnly(); |
21 | 49 |
|
22 |
| - SlnArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _arguments, SlnArgumentValidator.CommandType.Remove); |
| 50 | + SlnArgumentValidator.ParseAndValidateArguments(_fileOrDirectory, _projects, SlnArgumentValidator.CommandType.Remove); |
23 | 51 | }
|
24 | 52 |
|
25 | 53 | public override int Execute()
|
26 | 54 | {
|
27 |
| - SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory); |
| 55 | + string solutionFileFullPath = SlnCommandParser.GetSlnFileFullPath(_fileOrDirectory); |
| 56 | + if (_projects.Count == 0) |
| 57 | + { |
| 58 | + throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove); |
| 59 | + } |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + var relativeProjectPaths = _projects.Select(p => |
| 64 | + { |
| 65 | + var fullPath = Path.GetFullPath(p); |
| 66 | + return Path.GetRelativePath( |
| 67 | + Path.GetDirectoryName(solutionFileFullPath), |
| 68 | + Directory.Exists(fullPath) |
| 69 | + ? MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName |
| 70 | + : fullPath); |
| 71 | + }); |
| 72 | + RemoveProjectsAsync(solutionFileFullPath, relativeProjectPaths, CancellationToken.None).Wait(); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + catch (Exception ex) when (ex is not GracefulException) |
| 76 | + { |
| 77 | + if (ex is SolutionException || ex.InnerException is SolutionException) |
| 78 | + { |
| 79 | + throw new GracefulException(CommonLocalizableStrings.InvalidSolutionFormatString, solutionFileFullPath, ex.Message); |
| 80 | + } |
| 81 | + if (ex.InnerException is GracefulException) |
| 82 | + { |
| 83 | + throw ex.InnerException; |
| 84 | + } |
| 85 | + throw new GracefulException(ex.Message, ex); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<string> projectPaths, CancellationToken cancellationToken) |
| 90 | + { |
| 91 | + ISolutionSerializer serializer = SlnCommandParser.GetSolutionSerializer(solutionFileFullPath); |
| 92 | + SolutionModel solution = await serializer.OpenAsync(solutionFileFullPath, cancellationToken); |
28 | 93 |
|
29 |
| - var baseDirectory = PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory); |
30 |
| - var relativeProjectPaths = _arguments.Select(p => |
| 94 | + // set UTF8 BOM encoding for .sln |
| 95 | + if (serializer is ISolutionSerializer<SlnV12SerializerSettings> v12Serializer) |
31 | 96 | {
|
32 |
| - var fullPath = Path.GetFullPath(p); |
33 |
| - return Path.GetRelativePath( |
34 |
| - baseDirectory, |
35 |
| - Directory.Exists(fullPath) ? |
36 |
| - MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName : |
37 |
| - fullPath |
38 |
| - ); |
39 |
| - }); |
| 97 | + solution.SerializerExtension = v12Serializer.CreateModelExtension(new() |
| 98 | + { |
| 99 | + Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true) |
| 100 | + }); |
| 101 | + } |
40 | 102 |
|
41 |
| - bool slnChanged = false; |
42 |
| - foreach (var path in relativeProjectPaths) |
| 103 | + foreach (var projectPath in projectPaths) |
43 | 104 | {
|
44 |
| - slnChanged |= slnFile.RemoveProject(path); |
| 105 | + var project = solution.FindProject(projectPath); |
| 106 | + if (project != null) |
| 107 | + { |
| 108 | + solution.RemoveProject(project); |
| 109 | + Reporter.Output.WriteLine(CommonLocalizableStrings.ProjectRemovedFromTheSolution, projectPath); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + Reporter.Output.WriteLine(CommonLocalizableStrings.ProjectNotFoundInTheSolution, projectPath); |
| 114 | + } |
45 | 115 | }
|
46 | 116 |
|
47 |
| - slnFile.RemoveEmptyConfigurationSections(); |
| 117 | + Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent = solution.SolutionItems |
| 118 | + .Where(i => i.Parent != null) |
| 119 | + .GroupBy(i => i.Parent) |
| 120 | + .ToDictionary(g => g.Key, g => g.ToArray()); |
48 | 121 |
|
49 |
| - slnFile.RemoveEmptySolutionFolders(); |
| 122 | + Dictionary<SolutionFolderModel, int> nonFolderDescendantsCount = new(); |
| 123 | + foreach (var item in solution.SolutionFolders) |
| 124 | + { |
| 125 | + CountNonFolderDescendants(solution, item, solutionItemsGroupedByParent, nonFolderDescendantsCount); |
| 126 | + } |
50 | 127 |
|
51 |
| - if (slnChanged) |
| 128 | + var emptyFolders = nonFolderDescendantsCount.Where(i => i.Value == 0).Select(i => i.Key); |
| 129 | + foreach (var folder in emptyFolders) |
52 | 130 | {
|
53 |
| - slnFile.Write(); |
| 131 | + solution.RemoveFolder(folder); |
54 | 132 | }
|
55 | 133 |
|
56 |
| - return 0; |
| 134 | + await serializer.SaveAsync(solutionFileFullPath, solution, cancellationToken); |
57 | 135 | }
|
58 | 136 | }
|
59 | 137 | }
|
0 commit comments