2016-12-14 13:53:11 -10: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-14 13:53:11 -10:00
|
|
|
using Microsoft.DotNet.Cli.Sln.Internal;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
|
|
|
|
2017-01-06 10:58:23 -10:00
|
|
|
namespace Microsoft.DotNet.Tools.Sln.Add
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
internal class AddProjectToSolutionCommand : CommandBase
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
private readonly AppliedOption _appliedCommand;
|
|
|
|
private readonly string _fileOrDirectory;
|
|
|
|
|
2017-03-19 14:30:43 -07:00
|
|
|
public AddProjectToSolutionCommand(
|
|
|
|
AppliedOption appliedCommand,
|
|
|
|
string fileOrDirectory,
|
|
|
|
ParseResult parseResult) : base(parseResult)
|
2016-12-15 15:48:04 -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));
|
|
|
|
}
|
|
|
|
_appliedCommand = appliedCommand;
|
2016-12-16 01:04:09 -08:00
|
|
|
|
2017-03-11 11:47:04 -08:00
|
|
|
_fileOrDirectory = fileOrDirectory;
|
2016-12-15 15:48:04 -08:00
|
|
|
}
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
public override int Execute()
|
2016-12-15 15:48:04 -08:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
|
2016-12-16 01:04:09 -08:00
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
if (_appliedCommand.Arguments.Count == 0)
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2016-12-15 15:48:04 -08:00
|
|
|
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
|
|
|
|
}
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ProjectDoesNotExist);
|
|
|
|
|
|
|
|
var fullProjectPaths = _appliedCommand.Arguments
|
|
|
|
.Select(Path.GetFullPath)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var preAddProjectCount = slnFile.Projects.Count;
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2017-01-05 12:04:57 -10:00
|
|
|
foreach (var fullProjectPath in fullProjectPaths)
|
2016-12-15 15:48:04 -08:00
|
|
|
{
|
2017-01-23 13:01:58 -08:00
|
|
|
slnFile.AddProject(fullProjectPath);
|
2016-12-15 15:48:04 -08:00
|
|
|
}
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
if (slnFile.Projects.Count > preAddProjectCount)
|
2016-12-15 15:48:04 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
slnFile.Write();
|
2016-12-15 15:48:04 -08:00
|
|
|
}
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2016-12-15 15:48:04 -08:00
|
|
|
return 0;
|
2016-12-14 13:53:11 -10:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 16:43:44 -08:00
|
|
|
}
|