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

236 lines
8.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.
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;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Tools.Common;
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",
FullName = ".NET Add Project to Project (p2p) reference Command",
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-15 16:41:29 -08:00
app.HelpOption("-h|--help");
CommandArgument projectArgument = app.Argument(
"<PROJECT>",
"The project file to modify. If a project file is not specified," +
" 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
CommandOption frameworkOption = app.Option(
"-f|--framework <FRAMEWORK>",
"Add reference only when targetting a specific framework",
CommandOptionType.SingleValue);
CommandOption forceOption = app.Option(
"--force",
"Add reference even if it does not exist, do not convert paths to relative",
CommandOptionType.NoValue);
2016-11-15 16:41:29 -08:00
app.OnExecute(() => {
if (string.IsNullOrEmpty(projectArgument.Value))
{
throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
}
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
if (app.RemainingArguments.Count == 0)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReference);
2016-11-15 16:41:29 -08:00
}
List<string> references = app.RemainingArguments;
if (!forceOption.HasValue())
{
EnsureAllReferencesExist(references);
ConvertPathsToRelative(projectDir, ref references);
}
2016-11-21 11:38:25 -08:00
int numberOfAddedReferences = AddProjectToProjectReference(
project,
frameworkOption.Value(),
references);
2016-11-15 16:41:29 -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)
{
Reporter.Error.WriteLine(e.Message.Red());
app.ShowHelp();
2016-11-15 16:41:29 -08:00
return 1;
}
}
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,
notExisting.Select((r) => string.Format(LocalizableStrings.ReferenceDoesNotExist, r))));
}
}
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
{
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))
{
throw new GracefulException(LocalizableStrings.ProjectDoesNotExist, filename);
2016-11-15 16:41:29 -08:00
}
var project = TryOpenProject(filename);
if (project == null)
{
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
{
DirectoryInfo dir;
try
2016-11-15 16:41:29 -08:00
{
dir = new DirectoryInfo(directory);
2016-11-15 16:41:29 -08:00
}
catch (ArgumentException)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
2016-11-15 16:41:29 -08:00
}
if (!dir.Exists)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
2016-11-15 16:41:29 -08:00
}
FileInfo[] files = dir.GetFiles("*proj");
if (files.Length == 0)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
2016-11-15 16:41:29 -08:00
}
if (files.Length > 1)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.MoreThanOneProjectInDirectory, directory);
2016-11-15 16:41:29 -08:00
}
FileInfo projectFile = files.First();
2016-11-15 16:41:29 -08:00
if (!projectFile.Exists)
2016-11-15 16:41:29 -08:00
{
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
2016-11-15 16:41:29 -08:00
}
var ret = TryOpenProject(projectFile.FullName);
if (ret == null)
{
throw new GracefulException(LocalizableStrings.FoundInvalidProject, projectFile.FullName);
}
2016-11-15 16:41:29 -08:00
return ret;
}
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
{
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);
foreach (var @ref in refs.Select((r) => NormalizeSlashesForMsbuild(r)))
2016-11-15 16:41:29 -08:00
{
if (root.HasExistingItemWithCondition(framework, @ref))
2016-11-15 16:41:29 -08:00
{
Reporter.Output.WriteLine(string.Format(LocalizableStrings.ProjectAlreadyHasAreference, @ref));
2016-11-15 16:41:29 -08:00
continue;
}
numberOfAddedReferences++;
2016-11-22 14:41:56 -08:00
itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));
2016-11-15 16:41:29 -08:00
Reporter.Output.WriteLine(string.Format(LocalizableStrings.ReferenceAddedToTheProject, @ref));
2016-11-15 16:41:29 -08:00
}
return numberOfAddedReferences;
2016-11-15 16:41:29 -08:00
}
}
}