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.
|
|
|
|
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-11-29 09:44:39 -08:00
|
|
|
using System.Collections.Generic;
|
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
|
|
|
{
|
|
|
|
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",
|
|
|
|
FullName = ".NET Add Project to Project (p2p) reference Command",
|
2016-11-16 15:49:25 -08:00
|
|
|
Description = "Command to add project to project (p2p) reference",
|
|
|
|
AllowArgumentSeparator = true,
|
|
|
|
ArgumentSeparatorHelpText = "Project to project references to add"
|
2016-11-15 16:41:29 -08:00
|
|
|
};
|
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
|
|
|
CommandArgument projectArgument = app.Argument(
|
|
|
|
"<PROJECT>",
|
2016-11-16 15:49:25 -08:00
|
|
|
"The project file to modify. If a project file is not specified," +
|
2016-11-22 10:45:45 -08:00
|
|
|
" it searches the current working directory for an MSBuild file that has" +
|
|
|
|
" a file extension that ends in `proj` and uses that file.");
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-11-22 10:45:45 -08:00
|
|
|
CommandOption frameworkOption = app.Option(
|
|
|
|
"-f|--framework <FRAMEWORK>",
|
|
|
|
"Add reference only when targetting a specific framework",
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
|
|
CommandOption forceOption = app.Option(
|
|
|
|
"--force",
|
2016-11-22 13:58:16 -08:00
|
|
|
"Add reference even if it does not exist, do not convert paths to relative",
|
2016-11-22 10:45:45 -08:00
|
|
|
CommandOptionType.NoValue);
|
2016-11-15 16:41:29 -08:00
|
|
|
|
|
|
|
app.OnExecute(() => {
|
2016-11-22 13:58:16 -08:00
|
|
|
if (string.IsNullOrEmpty(projectArgument.Value))
|
2016-11-16 15:49:25 -08:00
|
|
|
{
|
2016-12-04 21:33:43 -08:00
|
|
|
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, "<Project>");
|
2016-11-16 15:49:25 -08:00
|
|
|
}
|
|
|
|
|
2016-11-29 10:23:04 -08:00
|
|
|
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
if (app.RemainingArguments.Count == 0)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-29 09:44:39 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-22 10:45:45 -08:00
|
|
|
List<string> references = app.RemainingArguments;
|
|
|
|
if (!forceOption.HasValue())
|
|
|
|
{
|
2016-11-30 13:43:43 -08:00
|
|
|
MsbuildProject.EnsureAllReferencesExist(references);
|
|
|
|
msbuildProj.ConvertPathsToRelative(ref references);
|
2016-11-22 10:45:45 -08:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:43:43 -08:00
|
|
|
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
|
2016-11-21 11:38:25 -08:00
|
|
|
frameworkOption.Value(),
|
2016-11-22 10:45:45 -08:00
|
|
|
references);
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
if (numberOfAddedReferences != 0)
|
|
|
|
{
|
2016-11-29 10:23:04 -08:00
|
|
|
msbuildProj.Project.Save();
|
2016-11-16 15:49:25 -08:00
|
|
|
}
|
2016-11-15 16:41:29 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return app.Execute(args);
|
|
|
|
}
|
|
|
|
catch (GracefulException e)
|
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
|
|
|
app.ShowHelp();
|
2016-11-15 16:41:29 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 20:05:19 -08:00
|
|
|
}
|
|
|
|
}
|