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.
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
using Microsoft.DotNet.Cli;
|
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
|
|
|
using Microsoft.DotNet.Tools.Sln;
|
2016-12-14 13:53:11 -10:00
|
|
|
using System;
|
2017-01-03 07:18:45 -10:00
|
|
|
using System.Collections.Generic;
|
2016-12-14 13:53:11 -10:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
2017-01-06 10:58:23 -10:00
|
|
|
namespace Microsoft.DotNet.Tools.Sln.Add
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
internal class AddProjectToSolutionCommand : DotNetSubCommandBase
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
public static DotNetSubCommandBase Create()
|
2016-12-15 15:48:04 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
var command = new AddProjectToSolutionCommand()
|
|
|
|
{
|
2017-01-06 10:58:23 -10:00
|
|
|
Name = "add",
|
|
|
|
FullName = LocalizableStrings.AddAppFullName,
|
|
|
|
Description = LocalizableStrings.AddSubcommandHelpText,
|
2016-12-16 01:04:09 -08:00
|
|
|
HandleRemainingArguments = true,
|
2017-01-06 10:58:23 -10:00
|
|
|
ArgumentSeparatorHelpText = LocalizableStrings.AddSubcommandHelpText,
|
2016-12-16 01:04:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
command.HelpOption("-h|--help");
|
|
|
|
|
|
|
|
return command;
|
2016-12-15 15:48:04 -08:00
|
|
|
}
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
public override int Run(string fileOrDirectory)
|
2016-12-15 15:48:04 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
|
|
|
|
|
|
|
|
if (RemainingArguments.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
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
PathUtility.EnsureAllPathsExist(RemainingArguments, CommonLocalizableStrings.ProjectDoesNotExist);
|
2017-01-05 12:04:57 -10:00
|
|
|
var fullProjectPaths = RemainingArguments.Select((p) => Path.GetFullPath(p)).ToList();
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
int preAddProjectCount = slnFile.Projects.Count;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|