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

121 lines
4.7 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.
2017-03-10 16:43:44 -08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2016-12-08 14:56:31 -08:00
using Microsoft.Build.Evaluation;
2016-12-16 01:04:09 -08:00
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
2016-12-08 12:59:14 -08:00
using NuGet.Frameworks;
2016-11-15 16:41:29 -08:00
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
{
2017-03-10 16:43:44 -08:00
internal class AddProjectToProjectReferenceCommand : CommandBase
{
2017-03-10 16:43:44 -08:00
private readonly AppliedOption _appliedCommand;
private readonly string _fileOrDirectory;
2016-12-15 15:48:04 -08:00
public AddProjectToProjectReferenceCommand(
AppliedOption appliedCommand,
string fileOrDirectory,
ParseResult parseResult) : base(parseResult)
{
2017-03-10 16:43:44 -08:00
if (appliedCommand == null)
2016-12-16 01:04:09 -08:00
{
2017-03-10 16:43:44 -08:00
throw new ArgumentNullException(nameof(appliedCommand));
}
if (fileOrDirectory == null)
{
throw new ArgumentNullException(nameof(fileOrDirectory));
}
2017-03-10 16:43:44 -08:00
_appliedCommand = appliedCommand;
_fileOrDirectory = fileOrDirectory;
2016-12-15 15:48:04 -08:00
}
2017-03-10 16:43:44 -08:00
public override int Execute()
2016-12-15 15:48:04 -08:00
{
2016-12-16 13:27:41 -08:00
var projects = new ProjectCollection();
2017-03-10 16:43:44 -08:00
MsbuildProject msbuildProj = MsbuildProject.FromFileOrDirectory(projects, _fileOrDirectory);
2016-12-16 01:04:09 -08:00
2017-03-14 18:41:27 -07:00
var frameworkString = _appliedCommand.ValueOrDefault<string>("framework");
2016-11-15 16:41:29 -08:00
2017-03-10 16:43:44 -08:00
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ReferenceDoesNotExist);
List<MsbuildProject> refs = _appliedCommand.Arguments
.Select((r) => MsbuildProject.FromFile(projects, r))
.ToList();
2016-12-15 15:48:04 -08:00
if (frameworkString == null)
{
2016-12-16 01:04:09 -08:00
foreach (var tfm in msbuildProj.GetTargetFrameworks())
{
2016-12-15 15:48:04 -08:00
foreach (var @ref in refs)
{
2016-12-15 15:48:04 -08:00
if (!@ref.CanWorkOnFramework(tfm))
2016-12-08 12:59:14 -08:00
{
2016-12-15 15:48:04 -08:00
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
2017-03-10 16:43:44 -08:00
@ref,
msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
2016-12-15 15:48:04 -08:00
return 1;
2016-12-08 12:59:14 -08:00
}
}
}
2016-12-15 15:48:04 -08:00
}
else
{
var framework = NuGetFramework.Parse(frameworkString);
2017-02-16 17:14:54 -05:00
if (!msbuildProj.IsTargetingFramework(framework))
{
2016-12-15 15:48:04 -08:00
Reporter.Error.WriteLine(string.Format(
2017-03-10 16:43:44 -08:00
CommonLocalizableStrings.ProjectDoesNotTargetFramework,
msbuildProj.ProjectRootElement.FullPath,
frameworkString));
return 1;
}
2016-11-15 16:41:29 -08:00
2016-12-15 15:48:04 -08:00
foreach (var @ref in refs)
{
if (!@ref.CanWorkOnFramework(framework))
{
Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
2017-03-10 16:43:44 -08:00
@ref,
new string[] { frameworkString }));
2016-12-15 15:48:04 -08:00
return 1;
}
}
}
2017-03-10 16:43:44 -08:00
var relativePathReferences = _appliedCommand.Arguments.Select((r) =>
PathUtility.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r)))
.ToList();
2016-12-15 15:48:04 -08:00
2016-12-16 01:04:09 -08:00
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
2017-03-10 16:43:44 -08:00
frameworkString,
2016-12-15 15:48:04 -08:00
relativePathReferences);
if (numberOfAddedReferences != 0)
{
2016-12-16 01:04:09 -08:00
msbuildProj.ProjectRootElement.Save();
2016-12-15 15:48:04 -08:00
}
return 0;
2016-11-15 16:41:29 -08:00
}
2016-12-08 12:59:14 -08:00
2016-12-16 13:27:41 -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
}
}
2017-03-10 16:43:44 -08:00
}