dotnet-installer/src/dotnet/commands/dotnet-sln/remove/Program.cs

67 lines
2.2 KiB
C#
Raw Normal View History

2016-12-15 13:40:46 -08:00
// 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.
2017-03-10 16:43:44 -08:00
using System;
using System.IO;
using System.Linq;
2016-12-16 01:04:09 -08:00
using Microsoft.DotNet.Cli;
2017-03-10 16:43:44 -08:00
using Microsoft.DotNet.Cli.CommandLine;
2016-12-15 13:40:46 -08:00
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.Tools.Sln.Remove
2016-12-15 13:40:46 -08:00
{
2017-03-10 16:43:44 -08:00
internal class RemoveProjectFromSolutionCommand : CommandBase
2016-12-15 13:40:46 -08:00
{
2017-03-10 16:43:44 -08:00
private readonly AppliedOption _appliedCommand;
private readonly string _fileOrDirectory;
public RemoveProjectFromSolutionCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
2016-12-15 13:40:46 -08:00
{
2017-03-10 16:43:44 -08:00
if (appliedCommand == null)
2016-12-16 01:04:09 -08:00
{
2017-03-10 16:43:44 -08:00
throw new ArgumentNullException(nameof(appliedCommand));
}
2016-12-16 01:04:09 -08:00
2017-03-14 11:23:19 -07:00
if (appliedCommand.Arguments.Count == 0)
2017-03-10 16:43:44 -08:00
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove);
}
2016-12-16 01:04:09 -08:00
2017-03-10 16:43:44 -08:00
_appliedCommand = appliedCommand;
_fileOrDirectory = fileOrDirectory;
2016-12-15 13:40:46 -08:00
}
2017-03-10 16:43:44 -08:00
public override int Execute()
2016-12-15 13:40:46 -08:00
{
2017-03-10 16:43:44 -08:00
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
2016-12-15 13:40:46 -08:00
2017-03-10 16:43:44 -08:00
var relativeProjectPaths = _appliedCommand.Arguments.Select(p =>
PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
Path.GetFullPath(p)))
.ToList();
2016-12-15 13:40:46 -08:00
bool slnChanged = false;
foreach (var path in relativeProjectPaths)
{
2017-01-23 13:01:58 -08:00
slnChanged |= slnFile.RemoveProject(path);
2016-12-15 13:40:46 -08:00
}
2017-01-23 13:01:58 -08:00
slnFile.RemoveEmptyConfigurationSections();
2017-01-23 13:01:58 -08:00
slnFile.RemoveEmptySolutionFolders();
2016-12-15 13:40:46 -08:00
if (slnChanged)
{
2016-12-16 01:04:09 -08:00
slnFile.Write();
2016-12-15 13:40:46 -08:00
}
2016-12-16 01:04:09 -08:00
return 0;
2016-12-15 13:40:46 -08:00
}
}
2017-03-10 16:43:44 -08:00
}