dotnet-installer/src/dotnet/commands/dotnet-add/dotnet-add-proj/Program.cs

127 lines
4.6 KiB
C#
Raw Normal View History

// 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;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2016-12-15 15:48:04 -08:00
using Microsoft.DotNet.Tools.Add;
namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
{
2016-12-15 15:48:04 -08:00
public class AddProjectToSolution : IAddSubCommand
{
2016-12-15 15:48:04 -08:00
private SlnFile _slnFile;
2016-12-15 15:48:04 -08:00
public AddProjectToSolution(string fileOrDirectory)
{
_slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
}
2016-12-15 15:48:04 -08:00
public int Add(List<string> projectPaths)
{
if (projectPaths.Count == 0)
{
2016-12-15 15:48:04 -08:00
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
}
2016-12-15 15:48:04 -08:00
PathUtility.EnsureAllPathsExist(projectPaths, CommonLocalizableStrings.ProjectDoesNotExist);
var relativeProjectPaths = projectPaths.Select((p) =>
PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(_slnFile.BaseDirectory),
Path.GetFullPath(p))).ToList();
2016-12-15 15:48:04 -08:00
int preAddProjectCount = _slnFile.Projects.Count;
foreach (var project in relativeProjectPaths)
{
AddProject(project);
}
2016-12-15 15:48:04 -08:00
if (_slnFile.Projects.Count > preAddProjectCount)
{
_slnFile.Write();
}
2016-12-15 15:48:04 -08:00
return 0;
}
2016-12-15 15:48:04 -08:00
private void AddProject(string projectPath)
{
var projectPathNormalized = PathUtility.GetPathWithBackSlashes(projectPath);
2016-12-15 15:48:04 -08:00
if (_slnFile.Projects.Any((p) =>
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)))
{
Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.SolutionAlreadyContainsProject,
2016-12-15 15:48:04 -08:00
_slnFile.FullPath,
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-15 15:48:04 -08:00
_slnFile.Projects.Add(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, projectPath));
}
}
}
2016-12-15 15:48:04 -08:00
public class AddProjectToSolutionCommand : AddSubCommandBase
{
protected override string CommandName => "project";
protected override string LocalizedDisplayName => LocalizableStrings.AppFullName;
protected override string LocalizedDescription => LocalizableStrings.AppDescription;
protected override string LocalizedHelpText => LocalizableStrings.AppHelpText;
internal override void AddCustomOptions(CommandLineApplication app)
{
}
protected override IAddSubCommand CreateIAddSubCommand(string fileOrDirectory)
{
return new AddProjectToSolution(fileOrDirectory);
}
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var addSubCommand = new AddProjectToSolutionCommand();
return addSubCommand.Create(parentApp);
}
}
}