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 System.Collections.Generic;
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.Build.Construction;
|
2016-11-16 15:49:25 -08:00
|
|
|
using Microsoft.Build.Evaluation;
|
2016-11-22 13:58:16 -08:00
|
|
|
using Microsoft.DotNet.Tools.Common;
|
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-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
|
2016-11-16 15:49:25 -08:00
|
|
|
}
|
|
|
|
|
2016-11-22 13:58:16 -08:00
|
|
|
ProjectRootElement project;
|
|
|
|
string projectDir;
|
|
|
|
if (File.Exists(projectArgument.Value))
|
|
|
|
{
|
|
|
|
project = GetProjectFromFileOrThrow(projectArgument.Value);
|
|
|
|
projectDir = new FileInfo(projectArgument.Value).DirectoryName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
project = GetProjectFromDirectoryOrThrow(projectArgument.Value);
|
|
|
|
projectDir = projectArgument.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
projectDir = PathUtility.EnsureTrailingSlash(projectDir);
|
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-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReference);
|
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-22 13:58:16 -08:00
|
|
|
EnsureAllReferencesExist(references);
|
|
|
|
ConvertPathsToRelative(projectDir, ref references);
|
2016-11-22 10:45:45 -08:00
|
|
|
}
|
|
|
|
|
2016-11-21 11:38:25 -08:00
|
|
|
int numberOfAddedReferences = AddProjectToProjectReference(
|
|
|
|
project,
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
project.Save();
|
|
|
|
}
|
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-22 13:58:16 -08:00
|
|
|
internal static void EnsureAllReferencesExist(List<string> references)
|
|
|
|
{
|
|
|
|
var notExisting = new List<string>();
|
|
|
|
foreach (var r in references)
|
|
|
|
{
|
|
|
|
if (!File.Exists(r))
|
|
|
|
{
|
|
|
|
notExisting.Add(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notExisting.Count > 0)
|
|
|
|
{
|
|
|
|
throw new GracefulException(
|
|
|
|
string.Join(
|
|
|
|
Environment.NewLine,
|
2016-11-23 15:35:01 -08:00
|
|
|
notExisting.Select((r) => string.Format(LocalizableStrings.ReferenceDoesNotExist, r))));
|
2016-11-22 13:58:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void ConvertPathsToRelative(string root, ref List<string> references)
|
|
|
|
{
|
|
|
|
root = PathUtility.EnsureTrailingSlash(Path.GetFullPath(root));
|
|
|
|
references = references.Select((r) => PathUtility.GetRelativePath(root, Path.GetFullPath(r))).ToList();
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:41:29 -08:00
|
|
|
// There is ProjectRootElement.TryOpen but it does not work as expected
|
|
|
|
// I.e. it returns null for some valid projects
|
2016-11-21 11:38:25 -08:00
|
|
|
internal static ProjectRootElement TryOpenProject(string filename)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
return ProjectRootElement.Open(filename, new ProjectCollection(), preserveFormatting: true);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 11:38:25 -08:00
|
|
|
internal static ProjectRootElement GetProjectFromFileOrThrow(string filename)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.ProjectDoesNotExist, filename);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var project = TryOpenProject(filename);
|
|
|
|
if (project == null)
|
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.ProjectIsInvalid, filename);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
2016-11-21 11:38:25 -08:00
|
|
|
internal static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
DirectoryInfo dir;
|
|
|
|
try
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
dir = new DirectoryInfo(directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
2016-11-16 15:49:25 -08:00
|
|
|
catch (ArgumentException)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
if (!dir.Exists)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
FileInfo[] files = dir.GetFiles("*proj");
|
|
|
|
if (files.Length == 0)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
if (files.Length > 1)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.MoreThanOneProjectInDirectory, directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
FileInfo projectFile = files.First();
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
if (!projectFile.Exists)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
var ret = TryOpenProject(projectFile.FullName);
|
|
|
|
if (ret == null)
|
2016-11-16 14:35:48 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
throw new GracefulException(LocalizableStrings.FoundInvalidProject, projectFile.FullName);
|
2016-11-16 14:35:48 -08:00
|
|
|
}
|
|
|
|
|
2016-11-15 16:41:29 -08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:58:16 -08:00
|
|
|
private static string NormalizeSlashesForMsbuild(string path)
|
|
|
|
{
|
|
|
|
return path.Replace('/', '\\');
|
|
|
|
}
|
|
|
|
|
2016-11-21 11:38:25 -08:00
|
|
|
internal static int AddProjectToProjectReference(ProjectRootElement root, string framework, IEnumerable<string> refs)
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
int numberOfAddedReferences = 0;
|
2016-11-15 16:41:29 -08:00
|
|
|
const string ProjectItemElementType = "ProjectReference";
|
|
|
|
|
2016-11-22 14:41:56 -08:00
|
|
|
ProjectItemGroupElement itemGroup = root.FindUniformOrCreateItemGroupWithCondition(ProjectItemElementType, framework);
|
2016-11-22 13:58:16 -08:00
|
|
|
foreach (var @ref in refs.Select((r) => NormalizeSlashesForMsbuild(r)))
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-16 15:49:25 -08:00
|
|
|
if (root.HasExistingItemWithCondition(framework, @ref))
|
2016-11-15 16:41:29 -08:00
|
|
|
{
|
2016-11-23 15:35:01 -08:00
|
|
|
Reporter.Output.WriteLine(string.Format(LocalizableStrings.ProjectAlreadyHasAreference, @ref));
|
2016-11-15 16:41:29 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-16 15:49:25 -08:00
|
|
|
numberOfAddedReferences++;
|
2016-11-22 14:41:56 -08:00
|
|
|
itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));
|
2016-11-15 16:41:29 -08:00
|
|
|
|
2016-11-23 15:35:01 -08:00
|
|
|
Reporter.Output.WriteLine(string.Format(LocalizableStrings.ReferenceAddedToTheProject, @ref));
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
2016-11-16 15:49:25 -08:00
|
|
|
|
|
|
|
return numberOfAddedReferences;
|
2016-11-15 16:41:29 -08:00
|
|
|
}
|
2016-11-10 20:05:19 -08:00
|
|
|
}
|
|
|
|
}
|