Skip to content

Commit

Permalink
sln-list: Support for slnx (#44537)
Browse files Browse the repository at this point in the history
  • Loading branch information
edvilme authored Nov 5, 2024
1 parent ffd7663 commit bd5b082
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,10 @@
<data name="SlnxGenerated" xml:space="preserve">
<value>.slnx file {0} generated.</value>
</data>
<data name="CannotMigrateSlnx" xml:space="preserve">
<value>Only .sln files can be migrated to .slnx format.</value>
</data>
<data name="SerializerNotFound" xml:space="preserve">
<value>Could not read solution file {0}. Supported files are .sln and .slnx valid solutions.</value>
</data>
</root>
20 changes: 17 additions & 3 deletions src/Cli/dotnet/commands/dotnet-sln/SlnCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.VisualStudio.SolutionPersistence;
using Microsoft.VisualStudio.SolutionPersistence.Serializer;
using NuGet.Packaging;
using LocalizableStrings = Microsoft.DotNet.Tools.Sln.LocalizableStrings;

Expand Down Expand Up @@ -50,11 +52,13 @@ internal static string GetSlnFileFullPath(string slnFileOrDirectory)
{
if (File.Exists(slnFileOrDirectory))
{
return slnFileOrDirectory;
return Path.GetFullPath(slnFileOrDirectory);
}
if (Directory.Exists(slnFileOrDirectory))
{
var files = Directory.GetFiles(slnFileOrDirectory, "*.sln", SearchOption.TopDirectoryOnly);
string[] files = [
..Directory.GetFiles(slnFileOrDirectory, "*.sln", SearchOption.TopDirectoryOnly),
..Directory.GetFiles(slnFileOrDirectory, "*.slnx", SearchOption.TopDirectoryOnly)];
if (files.Length == 0)
{
throw new GracefulException(CommonLocalizableStrings.CouldNotFindSolutionIn, slnFileOrDirectory);
Expand All @@ -63,9 +67,19 @@ internal static string GetSlnFileFullPath(string slnFileOrDirectory)
{
throw new GracefulException(CommonLocalizableStrings.MoreThanOneSolutionInDirectory, slnFileOrDirectory);
}
return files.Single().ToString();
return Path.GetFullPath(files.Single());
}
throw new GracefulException(CommonLocalizableStrings.CouldNotFindSolutionOrDirectory, slnFileOrDirectory);
}

internal static ISolutionSerializer GetSolutionSerializer(string solutionFilePath)
{
ISolutionSerializer? serializer = SolutionSerializers.GetSerializerByMoniker(solutionFilePath);
if (serializer is null)
{
throw new GracefulException(LocalizableStrings.SerializerNotFound, solutionFilePath);
}
return serializer;
}
}
}
35 changes: 24 additions & 11 deletions src/Cli/dotnet/commands/dotnet-sln/list/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.VisualStudio.SolutionPersistence;
using Microsoft.VisualStudio.SolutionPersistence.Model;
using CommandLocalizableStrings = Microsoft.DotNet.Tools.CommonLocalizableStrings;

namespace Microsoft.DotNet.Tools.Sln.List
{
Expand All @@ -23,25 +25,36 @@ public ListProjectsInSolutionCommand(

public override int Execute()
{
var slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
string solutionFileFullPath = SlnCommandParser.GetSlnFileFullPath(_fileOrDirectory);
try
{
ListAllProjectsAsync(solutionFileFullPath, CancellationToken.None).Wait();
return 0;
}
catch (Exception ex)
{
throw new GracefulException(CommandLocalizableStrings.InvalidSolutionFormatString, solutionFileFullPath, ex.Message);
}
}

private async Task ListAllProjectsAsync(string solutionFileFullPath, CancellationToken cancellationToken)
{
ISolutionSerializer serializer = SlnCommandParser.GetSolutionSerializer(solutionFileFullPath);
SolutionModel solution = await serializer.OpenAsync(solutionFileFullPath, cancellationToken);
string[] paths;

if (_displaySolutionFolders)
{
paths = slnFile.Projects
.GetProjectsByType(ProjectTypeGuids.SolutionFolderGuid)
.Select(folder => folder.GetFullSolutionFolderPath())
paths = solution.SolutionFolders
// VS-SolutionPersistence does not return a path object, so there might be issues with forward/backward slashes on different platforms
.Select(folder => Path.GetDirectoryName(folder.Path.TrimStart("/")))
.ToArray();
}
else
{
paths = slnFile.Projects
.GetProjectsNotOfType(ProjectTypeGuids.SolutionFolderGuid)
paths = solution.SolutionProjects
.Select(project => project.FilePath)
.ToArray();
}

if (paths.Length == 0)
{
Reporter.Output.WriteLine(CommonLocalizableStrings.NoProjectsFound);
Expand All @@ -51,14 +64,14 @@ public override int Execute()
Array.Sort(paths);

string header = _displaySolutionFolders ? LocalizableStrings.SolutionFolderHeader : LocalizableStrings.ProjectsHeader;
Reporter.Output.WriteLine($"{header}");
Reporter.Output.WriteLine(header);
Reporter.Output.WriteLine(new string('-', header.Length));
foreach (string slnProject in paths)
{
Reporter.Output.WriteLine(slnProject);
}
}
return 0;

}
}
}
14 changes: 7 additions & 7 deletions src/Cli/dotnet/commands/dotnet-sln/migrate/SlnMigrateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.VisualStudio.SolutionPersistence.Model;
using Microsoft.VisualStudio.SolutionPersistence.Serializer;
using LocalizableStrings = Microsoft.DotNet.Tools.Sln.LocalizableStrings;
using Microsoft.DotNet.Tools.Common;

namespace Microsoft.DotNet.Cli
{
Expand All @@ -26,13 +27,17 @@ public SlnMigrateCommand(
IReporter reporter = null)
: base(parseResult)
{
_slnFileOrDirectory = Path.GetFullPath(parseResult.GetValue(SlnCommandParser.SlnArgument));
_slnFileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument);
_reporter = reporter ?? Reporter.Output;
}

public override int Execute()
{
string slnFileFullPath = SlnCommandParser.GetSlnFileFullPath(_slnFileOrDirectory);
if (slnFileFullPath.HasExtension(".slnx"))
{
throw new GracefulException(LocalizableStrings.CannotMigrateSlnx);
}
string slnxFileFullPath = Path.ChangeExtension(slnFileFullPath, "slnx");
try
{
Expand All @@ -45,12 +50,7 @@ public override int Execute()

private async Task ConvertToSlnxAsync(string filePath, string slnxFilePath, CancellationToken cancellationToken)
{
// See if the file is a known solution file.
ISolutionSerializer? serializer = SolutionSerializers.GetSerializerByMoniker(filePath);
if (serializer is null)
{
throw new GracefulException("Could not find serializer for file {0}", filePath);
}
ISolutionSerializer serializer = SlnCommandParser.GetSolutionSerializer(filePath);
SolutionModel solution = await serializer.OpenAsync(filePath, cancellationToken);
await SolutionSerializers.SlnXml.SaveAsync(slnxFilePath, solution, cancellationToken);
_reporter.WriteLine(LocalizableStrings.SlnxGenerated, slnxFilePath);
Expand Down
10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Cli/dotnet/commands/dotnet-sln/xlf/LocalizableStrings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd5b082

Please sign in to comment.