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.
|
|
|
|
|
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
using Microsoft.Build.Evaluation;
|
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;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
|
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
Name = "project",
|
|
|
|
FullName = LocalizableStrings.AppFullName,
|
|
|
|
Description = LocalizableStrings.AppDescription,
|
|
|
|
HandleRemainingArguments = true,
|
|
|
|
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
var relativeProjectPaths = RemainingArguments.Select((p) =>
|
2016-12-15 15:48:04 -08:00
|
|
|
PathUtility.GetRelativePath(
|
2016-12-16 01:04:09 -08:00
|
|
|
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
|
2016-12-15 15:48:04 -08:00
|
|
|
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;
|
2016-12-15 15:48:04 -08:00
|
|
|
foreach (var project in relativeProjectPaths)
|
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
AddProject(slnFile, project);
|
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
|
|
|
}
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
private void AddProject(SlnFile slnFile, string projectPath)
|
2016-12-14 13:53:11 -10:00
|
|
|
{
|
2016-12-20 13:04:01 -10:00
|
|
|
var projectPathNormalized = PathUtility.GetPathWithDirectorySeparator(projectPath);
|
2016-12-14 13:53:11 -10:00
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
if (slnFile.Projects.Any((p) =>
|
2016-12-14 13:53:11 -10:00
|
|
|
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
Reporter.Output.WriteLine(string.Format(
|
|
|
|
CommonLocalizableStrings.SolutionAlreadyContainsProject,
|
2016-12-16 01:04:09 -08:00
|
|
|
slnFile.FullPath,
|
2016-12-14 13:53:11 -10:00
|
|
|
projectPath));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string projectGuidString = null;
|
|
|
|
if (File.Exists(projectPath))
|
|
|
|
{
|
|
|
|
var projectElement = ProjectRootElement.Open(
|
|
|
|
projectPath,
|
|
|
|
new ProjectCollection(),
|
|
|
|
preserveFormatting: true);
|
|
|
|
|
|
|
|
var projectGuidProperty = projectElement.Properties.Where((p) =>
|
|
|
|
string.Equals(p.Name, "ProjectGuid", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
|
|
|
|
|
|
if (projectGuidProperty != null)
|
|
|
|
{
|
|
|
|
projectGuidString = projectGuidProperty.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var projectGuid = (projectGuidString == null)
|
|
|
|
? Guid.NewGuid()
|
|
|
|
: new Guid(projectGuidString);
|
|
|
|
|
|
|
|
var slnProject = new SlnProject
|
|
|
|
{
|
|
|
|
Id = projectGuid.ToString("B").ToUpper(),
|
|
|
|
TypeGuid = ProjectTypeGuids.CPSProjectTypeGuid,
|
|
|
|
Name = Path.GetFileNameWithoutExtension(projectPath),
|
|
|
|
FilePath = projectPathNormalized
|
|
|
|
};
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
slnFile.Projects.Add(slnProject);
|
2016-12-14 13:53:11 -10:00
|
|
|
Reporter.Output.WriteLine(
|
|
|
|
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, projectPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|