2016-11-10 20:05:19 -08: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-08 14:56:31 -08:00
|
|
|
using Microsoft.Build.Evaluation;
|
2016-11-10 20:05:19 -08:00
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-12-13 14:31:35 -10:00
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2016-12-08 12:59:14 -08:00
|
|
|
using NuGet.Frameworks;
|
2016-11-29 09:44:39 -08:00
|
|
|
using System.Collections.Generic;
|
2016-12-13 14:31:35 -10:00
|
|
|
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-10 20:05:19 -08:00
|
|
|
|
2016-11-15 16:41:29 -08:00
|
|
|
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
2016-11-10 20:05:19 -08:00
|
|
|
{
|
2016-11-15 16:41:29 -08:00
|
|
|
public class AddProjectToProjectReferenceCommand
|
2016-11-10 20:05:19 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
|
2016-11-10 20:05:19 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
CommandLineApplication app = parentApp.Command("p2p", throwOnUnexpectedArg: false);
|
|
|
|
app.FullName = LocalizableStrings.AppFullName;
|
|
|
|
app.Description = LocalizableStrings.AppDescription;
|
|
|
|
app.HandleRemainingArguments = true;
|
|
|
|
app.ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText;
|
2016-11-16 15:49:25 -08:00
|
|
|
|
2016-11-15 16:41:29 -08:00
|
|
|
app.HelpOption("-h|--help");
|
|
|
|
|
2016-11-22 10:45:45 -08:00
|
|
|
CommandOption frameworkOption = app.Option(
|
2016-12-13 14:31:35 -10:00
|
|
|
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
|
2016-12-04 23:24:07 -08:00
|
|
|
LocalizableStrings.CmdFrameworkDescription,
|
2016-11-22 10:45:45 -08:00
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
app.OnExecute(() =>
|
|
|
|
{
|
|
|
|
try
|
2016-11-16 15:49:25 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
if (!parentApp.Arguments.Any())
|
|
|
|
{
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
|
|
|
|
}
|
2016-11-16 15:49:25 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
var projectOrDirectory = parentApp.Arguments.First().Value;
|
|
|
|
if (string.IsNullOrEmpty(projectOrDirectory))
|
|
|
|
{
|
|
|
|
projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
|
|
|
|
}
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
var projects = new ProjectCollection();
|
|
|
|
var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, projectOrDirectory);
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
if (app.RemainingArguments.Count == 0)
|
|
|
|
{
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
string frameworkString = frameworkOption.Value();
|
|
|
|
List<string> references = app.RemainingArguments;
|
2016-11-30 13:43:43 -08:00
|
|
|
MsbuildProject.EnsureAllReferencesExist(references);
|
2016-12-09 12:11:11 -08:00
|
|
|
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(projects, r));
|
2016-12-08 12:59:14 -08:00
|
|
|
|
|
|
|
if (frameworkString == null)
|
|
|
|
{
|
|
|
|
foreach (var tfm in msbuildProj.GetTargetFrameworks())
|
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
foreach (var @ref in refs)
|
2016-12-08 12:59:14 -08:00
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
if (!@ref.CanWorkOnFramework(tfm))
|
2016-12-08 12:59:14 -08:00
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
|
|
|
|
@ref,
|
|
|
|
msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
|
|
|
|
return 1;
|
2016-12-08 12:59:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var framework = NuGetFramework.Parse(frameworkString);
|
2016-12-09 12:11:11 -08:00
|
|
|
if (!msbuildProj.IsTargettingFramework(framework))
|
2016-12-08 12:59:14 -08:00
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
Reporter.Error.WriteLine(string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, msbuildProj.ProjectRootElement.FullPath, frameworkString));
|
|
|
|
return 1;
|
2016-12-08 12:59:14 -08:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:11:11 -08:00
|
|
|
foreach (var @ref in refs)
|
2016-12-08 12:59:14 -08:00
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
if (!@ref.CanWorkOnFramework(framework))
|
2016-12-08 12:59:14 -08:00
|
|
|
{
|
2016-12-09 12:11:11 -08:00
|
|
|
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
|
|
|
|
@ref,
|
|
|
|
new string[] { frameworkString }));
|
|
|
|
return 1;
|
2016-12-08 12:59:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 13:43:43 -08:00
|
|
|
msbuildProj.ConvertPathsToRelative(ref references);
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
|
|
|
|
frameworkOption.Value(),
|
|
|
|
references);
|
|
|
|
|
|
|
|
if (numberOfAddedReferences != 0)
|
|
|
|
{
|
|
|
|
msbuildProj.ProjectRootElement.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (GracefulException e)
|
2016-11-16 15:49:25 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
|
|
|
app.ShowHelp();
|
|
|
|
return 1;
|
2016-11-16 15:49:25 -08:00
|
|
|
}
|
2016-11-15 16:41:29 -08:00
|
|
|
});
|
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
return app;
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
2016-12-08 12:59:14 -08:00
|
|
|
|
2016-12-09 12:11:11 -08:00
|
|
|
private static 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-11-10 20:05:19 -08:00
|
|
|
}
|
|
|
|
}
|