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

126 lines
4.9 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.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
2016-12-08 12:59:14 -08:00
using NuGet.Frameworks;
using System.Collections.Generic;
2016-12-08 12:59:14 -08:00
using System.Linq;
2016-11-15 16:41:29 -08:00
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
2016-11-15 16:41:29 -08:00
public class AddProjectToProjectReferenceCommand
{
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
2016-11-15 16:41:29 -08:00
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
{
Name = "dotnet add p2p",
2016-12-04 23:24:07 -08:00
FullName = LocalizableStrings.AppFullName,
Description = LocalizableStrings.AppDescription,
AllowArgumentSeparator = true,
2016-12-04 23:24:07 -08:00
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText
2016-11-15 16:41:29 -08:00
};
2016-11-15 16:41:29 -08:00
app.HelpOption("-h|--help");
CommandArgument projectArgument = app.Argument(
2016-12-04 23:24:07 -08:00
$"<{LocalizableStrings.CmdProject}>",
LocalizableStrings.CmdProjectDescription);
2016-11-15 16:41:29 -08:00
CommandOption frameworkOption = app.Option(
2016-12-04 23:24:07 -08:00
$"-f|--framework <{LocalizableStrings.CmdFramework}>",
LocalizableStrings.CmdFrameworkDescription,
CommandOptionType.SingleValue);
CommandOption forceOption = app.Option(
"--force",
2016-12-04 23:24:07 -08:00
LocalizableStrings.CmdForceDescription,
CommandOptionType.NoValue);
2016-11-15 16:41:29 -08:00
app.OnExecute(() => {
if (string.IsNullOrEmpty(projectArgument.Value))
{
2016-12-04 23:24:07 -08:00
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>");
}
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
2016-11-15 16:41:29 -08:00
if (app.RemainingArguments.Count == 0)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
2016-11-15 16:41:29 -08:00
}
2016-12-08 12:59:14 -08:00
string frameworkString = frameworkOption.Value();
List<string> references = app.RemainingArguments;
if (!forceOption.HasValue())
{
MsbuildProject.EnsureAllReferencesExist(references);
2016-12-08 12:59:14 -08:00
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(r));
if (frameworkString == null)
{
foreach (var tfm in msbuildProj.GetTargetFrameworks())
{
foreach (var r in refs)
{
if (!r.CanWorkOnFramework(tfm))
{
throw new GracefulException(string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFramework, r.ProjectRoot.FullPath, GetFrameworkDisplayString(tfm)));
}
}
}
}
else
{
var framework = NuGetFramework.Parse(frameworkString);
if (!msbuildProj.TargetsFramework(framework))
{
throw new GracefulException(string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, msbuildProj.ProjectRoot.FullPath, GetFrameworkDisplayString(framework)));
}
foreach (var r in refs)
{
if (!r.CanWorkOnFramework(framework))
{
throw new GracefulException(string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFramework, r.ProjectRoot.FullPath, GetFrameworkDisplayString(framework)));
}
}
}
msbuildProj.ConvertPathsToRelative(ref references);
}
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
2016-11-21 11:38:25 -08:00
frameworkOption.Value(),
references);
2016-11-15 16:41:29 -08:00
if (numberOfAddedReferences != 0)
{
2016-12-08 12:59:14 -08:00
msbuildProj.ProjectRoot.Save();
}
2016-11-15 16:41:29 -08:00
return 0;
});
try
{
return app.Execute(args);
}
catch (GracefulException e)
{
Reporter.Error.WriteLine(e.Message.Red());
app.ShowHelp();
2016-11-15 16:41:29 -08:00
return 1;
}
}
2016-12-08 12:59:14 -08:00
private static string GetFrameworkDisplayString(NuGetFramework framework)
{
return framework.GetShortFolderName();
}
}
}