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

138 lines
5.3 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.
2016-12-08 14:56:31 -08:00
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
2016-12-15 15:48:04 -08:00
using Microsoft.DotNet.Tools.Add;
using Microsoft.DotNet.Tools.Common;
2016-12-08 12:59:14 -08:00
using NuGet.Frameworks;
using System.Collections.Generic;
using System.IO;
2016-12-08 12:59:14 -08:00
using System.Linq;
2016-12-09 12:11:11 -08:00
using System.Text;
2016-11-15 16:41:29 -08:00
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
2016-12-15 15:48:04 -08:00
public class AddProjectToProjectReference : IAddSubCommand
{
2016-12-15 15:48:04 -08:00
private CommandOption _frameworkOption;
private MsbuildProject _msbuildProj;
private ProjectCollection _projects;
internal AddProjectToProjectReference(string fileOrDirectory, CommandOption frameworkOption)
{
2016-12-15 15:48:04 -08:00
_projects = new ProjectCollection();
_msbuildProj = MsbuildProject.FromFileOrDirectory(_projects, fileOrDirectory);
_frameworkOption = frameworkOption;
}
2016-12-15 15:48:04 -08:00
public int Add(List<string> references)
{
if (references.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
}
2016-11-15 16:41:29 -08:00
2016-12-15 15:48:04 -08:00
string frameworkString = _frameworkOption.Value();
PathUtility.EnsureAllPathsExist(references, CommonLocalizableStrings.ReferenceDoesNotExist);
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(_projects, r));
2016-12-15 15:48:04 -08:00
if (frameworkString == null)
{
2016-12-15 15:48:04 -08:00
foreach (var tfm in _msbuildProj.GetTargetFrameworks())
{
2016-12-15 15:48:04 -08:00
foreach (var @ref in refs)
{
2016-12-15 15:48:04 -08:00
if (!@ref.CanWorkOnFramework(tfm))
2016-12-08 12:59:14 -08:00
{
2016-12-15 15:48:04 -08:00
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
2016-12-09 12:11:11 -08:00
@ref,
2016-12-15 15:48:04 -08:00
_msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
return 1;
2016-12-08 12:59:14 -08:00
}
}
}
2016-12-15 15:48:04 -08:00
}
else
{
var framework = NuGetFramework.Parse(frameworkString);
if (!_msbuildProj.IsTargettingFramework(framework))
{
2016-12-15 15:48:04 -08:00
Reporter.Error.WriteLine(string.Format(
CommonLocalizableStrings.ProjectDoesNotTargetFramework,
_msbuildProj.ProjectRootElement.FullPath,
frameworkString));
return 1;
}
2016-11-15 16:41:29 -08:00
2016-12-15 15:48:04 -08:00
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(framework))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
@ref,
new string[] { frameworkString }));
return 1;
}
}
}
var relativePathReferences = references.Select((r) =>
PathUtility.GetRelativePath(_msbuildProj.ProjectDirectory, Path.GetFullPath(r))).ToList();
int numberOfAddedReferences = _msbuildProj.AddProjectToProjectReferences(
_frameworkOption.Value(),
relativePathReferences);
if (numberOfAddedReferences != 0)
{
_msbuildProj.ProjectRootElement.Save();
}
return 0;
2016-11-15 16:41:29 -08:00
}
2016-12-08 12:59:14 -08:00
2016-12-15 15:48:04 -08:00
private string GetProjectNotCompatibleWithFrameworksDisplayString(MsbuildProject project, IEnumerable<string> frameworksDisplayStrings)
2016-12-08 12:59:14 -08:00
{
2016-12-09 12:11:11 -08:00
var sb = new StringBuilder();
sb.AppendLine(string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFrameworks, project.ProjectRootElement.FullPath));
foreach (var tfm in frameworksDisplayStrings)
{
sb.AppendLine($" - {tfm}");
}
return sb.ToString();
2016-12-08 12:59:14 -08:00
}
}
2016-12-15 15:48:04 -08:00
public class AddProjectToProjectReferenceCommand : AddSubCommandBase
{
private CommandOption _frameworkOption;
protected override string CommandName => "p2p";
protected override string LocalizedDisplayName => LocalizableStrings.AppFullName;
protected override string LocalizedDescription => LocalizableStrings.AppDescription;
protected override string LocalizedHelpText => LocalizableStrings.AppHelpText;
internal override void AddCustomOptions(CommandLineApplication app)
{
_frameworkOption = app.Option(
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
LocalizableStrings.CmdFrameworkDescription,
CommandOptionType.SingleValue);
}
protected override IAddSubCommand CreateIAddSubCommand(string fileOrDirectory)
{
return new AddProjectToProjectReference(fileOrDirectory, _frameworkOption);
}
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var addSubCommand = new AddProjectToProjectReferenceCommand();
return addSubCommand.Create(parentApp);
}
}
}