-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for dotnet sln add file
and dotnet sln add folder
#45072
Open
Bartleby2718
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
Bartleby2718:dev-9611-dotnet-sln-add-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 85 additions & 46 deletions
131
src/Cli/dotnet/commands/dotnet-sln/SlnArgumentValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,99 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using Microsoft.DotNet.Cli; | ||
using Microsoft.DotNet.Cli.Utils; | ||
|
||
namespace Microsoft.DotNet.Tools.Sln | ||
namespace Microsoft.DotNet.Tools.Sln; | ||
|
||
internal static class SlnArgumentValidator | ||
{ | ||
internal static class SlnArgumentValidator | ||
private static readonly SearchValues<char> s_invalidCharactersInSolutionFolderName = SearchValues.Create("/:?\\*\"<>|"); | ||
private static readonly string[] s_invalidSolutionFolderNames = | ||
[ | ||
// system reserved names per https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions | ||
"CON", "PRN", "AUX", "NUL", | ||
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", | ||
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", | ||
// relative path components | ||
".", "..", | ||
]; | ||
|
||
public enum CommandType | ||
{ | ||
Add, | ||
Remove | ||
} | ||
public static void ParseAndValidateArguments(IReadOnlyList<string> _arguments, CommandType commandType, bool _inRoot = false, string relativeRoot = null, string subcommand = null) | ||
{ | ||
public enum CommandType | ||
if (_arguments.Count == 0) | ||
{ | ||
Add, | ||
Remove | ||
string message = commandType == CommandType.Add | ||
? CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd | ||
: CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove; | ||
throw new GracefulException(message); | ||
} | ||
public static void ParseAndValidateArguments(string _fileOrDirectory, IReadOnlyCollection<string> _arguments, CommandType commandType, bool _inRoot = false, string relativeRoot = null) | ||
|
||
bool hasRelativeRoot = !string.IsNullOrEmpty(relativeRoot); | ||
|
||
if (_inRoot && hasRelativeRoot) | ||
{ | ||
if (_arguments.Count == 0) | ||
{ | ||
string message = commandType == CommandType.Add ? CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd : CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove; | ||
throw new GracefulException(message); | ||
} | ||
|
||
bool hasRelativeRoot = !string.IsNullOrEmpty(relativeRoot); | ||
|
||
if (_inRoot && hasRelativeRoot) | ||
{ | ||
// These two options are mutually exclusive | ||
throw new GracefulException(LocalizableStrings.SolutionFolderAndInRootMutuallyExclusive); | ||
} | ||
|
||
var slnFile = _arguments.FirstOrDefault(path => path.EndsWith(".sln")); | ||
if (slnFile != null) | ||
{ | ||
string args; | ||
if (_inRoot) | ||
{ | ||
args = $"--{SlnAddParser.InRootOption.Name} "; | ||
} | ||
else if (hasRelativeRoot) | ||
{ | ||
args = $"--{SlnAddParser.SolutionFolderOption.Name} {string.Join(" ", relativeRoot)} "; | ||
} | ||
else | ||
{ | ||
args = ""; | ||
} | ||
|
||
var projectArgs = string.Join(" ", _arguments.Where(path => !path.EndsWith(".sln"))); | ||
string command = commandType == CommandType.Add ? "add" : "remove"; | ||
throw new GracefulException(new string[] | ||
{ | ||
string.Format(CommonLocalizableStrings.SolutionArgumentMisplaced, slnFile), | ||
CommonLocalizableStrings.DidYouMean, | ||
$" dotnet solution {slnFile} {command} {args}{projectArgs}" | ||
}); | ||
} | ||
// These two options are mutually exclusive | ||
throw new GracefulException(LocalizableStrings.SolutionFolderAndInRootMutuallyExclusive); | ||
} | ||
|
||
// Something is wrong if there is a .sln file as an argument, so suggest that the arguments may have been misplaced. | ||
// However, it is possible to add .sln file as a solution item, so don't suggest in the case of dotnet sln add file. | ||
var slnFile = _arguments.FirstOrDefault(path => path.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)); | ||
if (slnFile == null || subcommand == "file") | ||
{ | ||
return; | ||
} | ||
|
||
string options = _inRoot | ||
? $"{SlnAddParser.InRootOption.Name} " | ||
: hasRelativeRoot | ||
? $"{SlnAddParser.SolutionFolderOption.Name} {string.Join(" ", relativeRoot)} " | ||
: ""; | ||
|
||
var nonSolutionArguments = string.Join( | ||
" ", | ||
_arguments.Where(a => !a.EndsWith(".sln", StringComparison.OrdinalIgnoreCase))); | ||
|
||
string command = commandType switch | ||
{ | ||
CommandType.Add => "add", | ||
CommandType.Remove => "remove", | ||
_ => throw new InvalidOperationException($"Unable to handle command type {commandType}"), | ||
}; | ||
throw new GracefulException( | ||
[ | ||
string.Format(CommonLocalizableStrings.SolutionArgumentMisplaced, slnFile), | ||
CommonLocalizableStrings.DidYouMean, | ||
subcommand == null | ||
? $" dotnet solution {slnFile} {command} {options}{nonSolutionArguments}" | ||
: $" dotnet solution {slnFile} {command} {subcommand} {options}{nonSolutionArguments}" | ||
]); | ||
} | ||
|
||
public static bool IsValidSolutionFolderName(string folderName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
if (string.IsNullOrWhiteSpace(folderName)) | ||
return false; | ||
|
||
if (folderName.AsSpan().IndexOfAny(s_invalidCharactersInSolutionFolderName) >= 0) | ||
return false; | ||
|
||
if (folderName.Any(char.IsControl)) | ||
return false; | ||
|
||
if (folderName.Any(char.IsSurrogate)) | ||
return false; | ||
|
||
if (s_invalidSolutionFolderNames.Contains(folderName, StringComparer.OrdinalIgnoreCase)) | ||
return false; | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bug where there were 4 dashes.