Skip to content

Commit 97cd7b1

Browse files
Merge pull request #23822 from abpframework/cli-support-slnx-files
Cli: Support solution files in new SLNX format
2 parents d581cd6 + 2d2196c commit 97cd7b1

15 files changed

+152
-221
lines changed

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Build/FileSystemDotNetProjectBuildConfigReader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public FileSystemDotNetProjectBuildConfigReader(IJsonSerializer jsonSerializer)
2121
public DotNetProjectBuildConfig Read(string directoryPath)
2222
{
2323
var buildConfig = new DotNetProjectBuildConfig();
24-
var solutionFiles = Directory.GetFiles(directoryPath, "*.sln", SearchOption.TopDirectoryOnly);
25-
if (solutionFiles.Length == 1)
24+
var solutionFiles = Directory.GetFiles(directoryPath, "*.sln", SearchOption.TopDirectoryOnly)
25+
.Concat(Directory.GetFiles(directoryPath, "*.slnx", SearchOption.TopDirectoryOnly)).ToList();
26+
if (solutionFiles.Count == 1)
2627
{
2728
buildConfig.SlFilePath = solutionFiles.First();
2829
var configFile = GetClosestFile(directoryPath, _buildConfigName);
@@ -86,7 +87,7 @@ private GitRepository GetGitRepositoryUsingDirectory(string directoryPath)
8687
directoryInfo = directoryInfo.Parent;
8788
} while (directoryInfo?.Parent != null);
8889

89-
throw new Exception("There is no solution file (*.sln) and " + _buildConfigName +
90+
throw new Exception("There is no solution file (*.sln or *.slnx) and " + _buildConfigName +
9091
" in the working directory and working directory is not a GIT repository !");
9192
}
9293

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using Microsoft.Extensions.Logging;
56
using Microsoft.Extensions.Logging.Abstractions;
@@ -118,7 +119,7 @@ public string GetUsageInfo()
118119

119120
sb.AppendLine("");
120121
sb.AppendLine("'add-module' command is used to add a multi-package ABP module to a solution.");
121-
sb.AppendLine("It should be used in a folder containing a .sln file.");
122+
sb.AppendLine("It should be used in a folder containing a .sln or .slnx file.");
122123
sb.AppendLine("");
123124
sb.AppendLine("Usage:");
124125
sb.AppendLine(" abp add-module <module-name> [options]");
@@ -166,20 +167,21 @@ protected virtual string GetSolutionFile(CommandLineArgs commandLineArgs)
166167
return providedSolutionFile;
167168
}
168169

169-
var foundSolutionFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln");
170-
if (foundSolutionFiles.Length == 1)
170+
var foundSolutionFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln")
171+
.Concat(Directory.GetFiles(Directory.GetCurrentDirectory(), "*.slnx")).ToList();
172+
if (foundSolutionFiles.Count == 1)
171173
{
172174
return foundSolutionFiles[0];
173175
}
174176

175-
if (foundSolutionFiles.Length == 0)
177+
if (foundSolutionFiles.Count == 0)
176178
{
177-
throw new CliUsageException("'abp add-module' command should be used inside a folder containing a .sln file!");
179+
throw new CliUsageException("'abp add-module' command should be used inside a folder containing a .sln or .slnx file!");
178180
}
179181

180182
//foundSolutionFiles.Length > 1
181183

182-
var sb = new StringBuilder("There are multiple solution (.sln) files in the current directory. Please specify one of the files below:");
184+
var sb = new StringBuilder("There are multiple solution (.sln or .slnx) files in the current directory. Please specify one of the files below:");
183185

184186
foreach (var foundSolutionFile in foundSolutionFiles)
185187
{

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public string GetUsageInfo()
8383

8484
sb.AppendLine("");
8585
sb.AppendLine("'add-package' command is used to add an ABP package to a project.");
86-
sb.AppendLine("It should be used in a folder containing a .csproj file, .sln file or packages.json.");
86+
sb.AppendLine("It should be used in a folder containing a .csproj file, .sln file, .slnx file or packages.json.");
8787
sb.AppendLine("");
8888
sb.AppendLine("Usage:");
8989
sb.AppendLine("");
@@ -146,7 +146,8 @@ protected virtual string GetSolutionFile(CommandLineArgs commandLineArgs)
146146
return providedSolutionFile;
147147
}
148148

149-
return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln").FirstOrDefault();
149+
return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln")
150+
.Concat(Directory.GetFiles(Directory.GetCurrentDirectory(), "*.slnx")).FirstOrDefault();
150151
}
151152

152153
protected virtual string GetAngularDirectory(CommandLineArgs commandLineArgs)

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProjectCreationCommandBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ protected async Task<ProjectBuildArgs> GetProjectBuildArgsAsync(CommandLineArgs
175175

176176
if (slnPath == null)
177177
{
178-
slnPath = Directory.GetFiles(outputFolderRoot, "*.sln").FirstOrDefault();
178+
slnPath = Directory.GetFiles(outputFolderRoot, "*.sln")
179+
.Concat(Directory.GetFiles(outputFolderRoot, "*.slnx")).FirstOrDefault();
179180
}
180-
else if (slnPath.EndsWith(".sln"))
181+
else if (slnPath.EndsWith(".sln") || slnPath.EndsWith(".slnx"))
181182
{
182183
Directory.SetCurrentDirectory(Path.GetDirectoryName(slnPath));
183184
outputFolderRoot = Path.GetDirectoryName(slnPath);
@@ -190,15 +191,16 @@ protected async Task<ProjectBuildArgs> GetProjectBuildArgsAsync(CommandLineArgs
190191
{
191192
Directory.SetCurrentDirectory(slnPath);
192193
outputFolderRoot = slnPath;
193-
slnPath = Directory.GetFiles(outputFolderRoot, "*.sln").FirstOrDefault();
194+
slnPath = Directory.GetFiles(outputFolderRoot, "*.sln")
195+
.Concat(Directory.GetFiles(outputFolderRoot, "*.slnx")).FirstOrDefault();
194196
}
195197

196198
if (slnPath == null)
197199
{
198200
throw new CliUsageException($"This command should be run inside a folder that contains a microservice solution! Or use -{Options.MainSolution.Short} parameter.");
199201
}
200202

201-
var microserviceSolutionName = Path.GetFileName(slnPath).RemovePostFix(".sln");
203+
var microserviceSolutionName = Path.GetFileName(slnPath).RemovePostFix(".slnx", ".sln");
202204

203205
version ??= SolutionPackageVersionFinder.FindByCsprojVersion(slnPath);
204206
solutionName = SolutionName.Parse(microserviceSolutionName, projectName);

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private async Task GenerateCrudPageAsync(CommandLineArgs args)
119119
var solutionFile = args.Options.GetOrNull(Options.Crud.Solution.Short, Options.Crud.Solution.Long);
120120

121121
if (entityFile.IsNullOrEmpty() || !entityFile.EndsWith(".json") || !File.Exists(entityFile) ||
122-
solutionFile.IsNullOrEmpty() || !solutionFile.EndsWith(".sln"))
122+
solutionFile.IsNullOrEmpty() || !(solutionFile.EndsWith(".sln") || solutionFile.EndsWith(".slnx")))
123123
{
124124
throw new UserFriendlyException("Invalid Arguments!");
125125
}
@@ -476,7 +476,8 @@ void OpenSuiteInBrowserWithLaunchUrl()
476476
private object GetTargetSolutionOrNull(CommandLineArgs commandLineArgs)
477477
{
478478
return commandLineArgs.Options.GetOrNull(Options.Crud.Solution.Short, Options.Crud.Solution.Long)
479-
?? Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault();
479+
?? Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln", SearchOption.TopDirectoryOnly)
480+
.Concat(Directory.GetFiles(Directory.GetCurrentDirectory(), "*.slnx", SearchOption.TopDirectoryOnly)).FirstOrDefault();
480481
}
481482

482483
private Process StartSuite()

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private string GetWorkingDirectory(CommandLineArgs commandLineArgs)
7070
return null;
7171
}
7272

73-
if (path.EndsWith(".sln") || path.EndsWith(".csproj"))
73+
if (path.EndsWith(".sln") || path.EndsWith(".slnx") || path.EndsWith(".csproj"))
7474
{
7575
return Path.GetDirectoryName(path);
7676
}

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string d
6464
if (givenSolution.IsNullOrWhiteSpace())
6565
{
6666
solutions.AddRange(Directory.GetFiles(directory, "*.sln", SearchOption.AllDirectories));
67+
solutions.AddRange(Directory.GetFiles(directory, "*.slnx", SearchOption.AllDirectories));
6768
}
6869
else
6970
{
@@ -76,7 +77,7 @@ private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string d
7677
{
7778
foreach (var solution in solutions)
7879
{
79-
var solutionName = Path.GetFileName(solution).RemovePostFix(".sln");
80+
var solutionName = Path.GetFileName(solution).RemovePostFix(".slnx", ".sln");
8081

8182
await _nugetPackagesVersionUpdater.UpdateSolutionAsync(solution, checkAll: checkAll, version: version, leptonXVersion: leptonXVersion);
8283

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/AppNoLayersMoveProjectsStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void MoveFiles(ProjectBuildContext context, string projectFolder, string
4141

4242
public void ModifySolutionFile(ProjectBuildContext context, string pathInSlnFile, string newPathInSlnFile)
4343
{
44-
var slnFile = context.Files.First(file => file.Name.EndsWith(".sln"));
44+
var slnFile = context.Files.First(file => file.Name.EndsWith(".sln") ||file.Name.EndsWith(".slnx"));
4545
slnFile.SetContent(slnFile.Content.Replace($"\"{pathInSlnFile}\"", $"\"{newPathInSlnFile}\""));
4646
}
4747

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/ProjectRenameStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override void Execute(ProjectBuildContext context)
2424
}
2525
}
2626

27-
var files = context.Files.Where(f => f.Name.EndsWith(".sln") || f.Name.EndsWith(".cs"));
27+
var files = context.Files.Where(f => f.Name.EndsWith(".sln") || f.Name.EndsWith(".slnx") || f.Name.EndsWith(".cs"));
2828
foreach (var file in files)
2929
{
3030
file.NormalizeLineEndings();

framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/RemoveProjectFromSolutionStep.cs

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,120 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
4-
using Newtonsoft.Json;
5+
using System.Xml;
56
using Newtonsoft.Json.Linq;
7+
using Volo.Abp.Cli.ProjectBuilding.Files;
8+
using Formatting = Newtonsoft.Json.Formatting;
69

710
namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps;
811

912
public class RemoveProjectFromSolutionStep : ProjectBuildPipelineStep
1013
{
1114
private readonly string _projectName;
12-
private string _solutionFilePath;
15+
private string _solutionFilePathWithoutFileExtension;
1316
private string _projectFolderPath;
1417

1518
private string ProjectNameWithQuotes => $"\"{_projectName}\"";
1619

1720
public RemoveProjectFromSolutionStep(
1821
string projectName,
19-
string solutionFilePath = null,
22+
string solutionFilePathWithoutFileExtension = null,
2023
string projectFolderPath = null)
2124
{
2225
_projectName = projectName;
23-
_solutionFilePath = solutionFilePath;
2426
_projectFolderPath = projectFolderPath;
27+
28+
if (solutionFilePathWithoutFileExtension != null && solutionFilePathWithoutFileExtension.EndsWith(".sln"))
29+
{
30+
_solutionFilePathWithoutFileExtension = solutionFilePathWithoutFileExtension.RemovePostFix(".sln");
31+
}
32+
33+
if (solutionFilePathWithoutFileExtension != null && solutionFilePathWithoutFileExtension.EndsWith(".slnx"))
34+
{
35+
_solutionFilePathWithoutFileExtension = solutionFilePathWithoutFileExtension.RemovePostFix(".slnx");
36+
}
37+
else
38+
{
39+
_solutionFilePathWithoutFileExtension = solutionFilePathWithoutFileExtension;
40+
}
2541
}
2642

2743
public override void Execute(ProjectBuildContext context)
2844
{
2945
SetSolutionAndProjectPathsIfNull(context);
3046

31-
if (_solutionFilePath == null || _projectFolderPath == null)
47+
if (_solutionFilePathWithoutFileExtension == null || _projectFolderPath == null)
3248
{
3349
return;
3450
}
3551

3652
new RemoveFolderStep(_projectFolderPath).Execute(context);
37-
var solutionFile = context.GetFile(_solutionFilePath);
38-
solutionFile.NormalizeLineEndings();
39-
solutionFile.SetLines(RemoveProject(solutionFile.GetLines().ToList()));
53+
var solutionFile = context.FindFile(_solutionFilePathWithoutFileExtension + ".sln")
54+
?? context.GetFile(_solutionFilePathWithoutFileExtension + ".slnx");
55+
56+
if (solutionFile.Name.EndsWith(".sln"))
57+
{
58+
RemoveProjectFromSlnFile(solutionFile);
59+
}
60+
else
61+
{
62+
RemoveProjectFromSlnxFile(solutionFile);
63+
}
4064

4165
RemoveProjectFromAbpmdlFile(context);
4266
}
4367

68+
private void RemoveProjectFromSlnxFile(FileEntry solutionFile)
69+
{
70+
var document = new XmlDocument { PreserveWhitespace = true };
71+
document.LoadXml(solutionFile.Content);
72+
var projectNodes = document.SelectNodes("//Project");
73+
74+
if (projectNodes == null || projectNodes.Count < 1)
75+
{
76+
return;
77+
}
78+
79+
var nodesToBeRemoved = new List<XmlNode>();
80+
foreach (XmlNode projectNode in projectNodes)
81+
{
82+
var pathAttr = projectNode.Attributes?["Path"]?.Value;
83+
if (string.IsNullOrWhiteSpace(pathAttr))
84+
{
85+
continue;
86+
}
87+
88+
var normalized = pathAttr.Replace('\\', '/');
89+
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(normalized);
90+
91+
if (string.Equals(fileNameWithoutExtension, _projectName, StringComparison.OrdinalIgnoreCase))
92+
{
93+
nodesToBeRemoved.Add(projectNode);
94+
}
95+
}
96+
97+
foreach (var node in nodesToBeRemoved)
98+
{
99+
node.ParentNode!.RemoveChild(node);
100+
}
101+
102+
solutionFile.SetContent(
103+
document.OuterXml
104+
.SplitToLines()
105+
.Where(x=> !x.Trim().Equals(string.Empty))
106+
.JoinAsString(Environment.NewLine));
107+
}
108+
109+
private void RemoveProjectFromSlnFile(FileEntry solutionFile)
110+
{
111+
solutionFile.NormalizeLineEndings();
112+
solutionFile.SetLines(RemoveProject(solutionFile.GetLines().ToList()));
113+
}
114+
44115
private void RemoveProjectFromAbpmdlFile(ProjectBuildContext context)
45116
{
46-
var abpmdlFile = context.FindFile(_solutionFilePath.RemovePostFix(".sln") + ".abpmdl");
117+
var abpmdlFile = context.FindFile(_solutionFilePathWithoutFileExtension + ".abpmdl");
47118

48119
if (abpmdlFile == null)
49120
{
@@ -106,11 +177,14 @@ private string FindProjectKey(List<string> solutionFileLines)
106177

107178
private void SetSolutionAndProjectPathsIfNull(ProjectBuildContext context)
108179
{
109-
if (_solutionFilePath == null)
180+
if (_solutionFilePathWithoutFileExtension == null)
110181
{
111-
_solutionFilePath = context.FindFile("/aspnet-core/MyCompanyName.MyProjectName.sln")?.Name ??
112-
context.FindFile("/MyCompanyName.MyProjectName.sln")?.Name ??
113-
context.FindFile("/MyCompanyName.MyProjectName.MicroserviceName.sln")?.Name;
182+
_solutionFilePathWithoutFileExtension = context.FindFile("/aspnet-core/MyCompanyName.MyProjectName.sln")?.Name.RemovePostFix(".sln") ??
183+
context.FindFile("/aspnet-core/MyCompanyName.MyProjectName.slnx")?.Name.RemovePostFix(".slnx") ??
184+
context.FindFile("/MyCompanyName.MyProjectName.sln")?.Name.RemovePostFix(".sln") ??
185+
context.FindFile("/MyCompanyName.MyProjectName.slnx")?.Name.RemovePostFix(".slnx") ??
186+
context.FindFile("/MyCompanyName.MyProjectName.MicroserviceName.sln")?.Name.RemovePostFix(".sln") ??
187+
context.FindFile("/MyCompanyName.MyProjectName.MicroserviceName.slnx")?.Name.RemovePostFix(".slnx");
114188
}
115189
if (_projectFolderPath == null)
116190
{

0 commit comments

Comments
 (0)