// 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.Add { internal class AddProjectToSolutionCommand : CommandBase { private readonly AppliedOption _appliedCommand; private readonly string _fileOrDirectory; public AddProjectToSolutionCommand( AppliedOption appliedCommand, string fileOrDirectory, ParseResult parseResult) : base(parseResult) { if (appliedCommand == null) { throw new ArgumentNullException(nameof(appliedCommand)); } _appliedCommand = appliedCommand; _fileOrDirectory = fileOrDirectory; } public override int Execute() { SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory); if (_appliedCommand.Arguments.Count == 0) { throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd); } PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ProjectDoesNotExist); var fullProjectPaths = _appliedCommand.Arguments .Select(Path.GetFullPath) .ToList(); var preAddProjectCount = slnFile.Projects.Count; foreach (var fullProjectPath in fullProjectPaths) { slnFile.AddProject(fullProjectPath); } if (slnFile.Projects.Count > preAddProjectCount) { slnFile.Write(); } return 0; } } }