dotnet-installer/src/dotnet/commands/dotnet-sln/remove/Program.cs
Peter Huene 03f0c51292 Fix relative path handling on Windows.
On Windows, `PathUtility.GetRelativePath` was not properly handling
paths that differed by case in the drive reference (e.g. "C:\" vs.
"c:\").  The fix was to add the missing case-insensitive comparison
argument.

Replaced uses of `PathUtility.GetRelativePath` with
`Path.GetRelativePath` where possible (requires 2.0.0+).

Additionally, `PathUtility.RemoveExtraPathSeparators` was not handling
paths with drive references on Windows.  If the path contained a drive
reference, the separator between the drive reference and the first part
of the path was removed.  This is due to `Path.Combine` not handling
this case, so an explicit concatenation of the separator was added.

This commit resolves issue #7699.
2017-10-24 11:23:52 -07:00

67 lines
No EOL
2.2 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.Tools.Sln.Remove
{
internal class RemoveProjectFromSolutionCommand : CommandBase
{
private readonly AppliedOption _appliedCommand;
private readonly string _fileOrDirectory;
public RemoveProjectFromSolutionCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{
if (appliedCommand == null)
{
throw new ArgumentNullException(nameof(appliedCommand));
}
if (appliedCommand.Arguments.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove);
}
_appliedCommand = appliedCommand;
_fileOrDirectory = fileOrDirectory;
}
public override int Execute()
{
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
var relativeProjectPaths = _appliedCommand.Arguments.Select(p =>
Path.GetRelativePath(
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
Path.GetFullPath(p)))
.ToList();
bool slnChanged = false;
foreach (var path in relativeProjectPaths)
{
slnChanged |= slnFile.RemoveProject(path);
}
slnFile.RemoveEmptyConfigurationSections();
slnFile.RemoveEmptySolutionFolders();
if (slnChanged)
{
slnFile.Write();
}
return 0;
}
}
}