From 68245312b050c5b992120448353eba3e20cdc414 Mon Sep 17 00:00:00 2001 From: Justin Emgarten Date: Thu, 23 Mar 2017 00:14:34 -0700 Subject: [PATCH] Support for processing metaproj references from a solution file This change adds a task to modify project references coming from a solution file. When project build order is used the solution file read by restore contains links to .metaproj files instead of the actual projects. The actual project can be found by removing the metaproj extension. Cleaning up _SplitProjectReferencesByFileExistence, the outputs from this are not used since ProjectReference is used direcetly. There is no reason to depend on this external target. Fixes https://github.com/NuGet/Home/issues/4869 Fixes https://github.com/NuGet/Home/issues/4578 --- .../GetRestoreDotnetCliToolsTask.cs | 5 +- .../GetRestorePackageReferencesTask.cs | 5 +- .../GetRestoreProjectJsonPathTask.cs | 5 +- .../GetRestoreProjectReferencesTask.cs | 5 +- .../GetRestoreSolutionProjectsTask.cs | 76 +++++++++++++++++++ .../NuGet.Build.Tasks/NuGet.targets | 29 ++++--- .../WriteRestoreGraphTask.cs | 5 +- 7 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSolutionProjectsTask.cs diff --git a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreDotnetCliToolsTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreDotnetCliToolsTask.cs index 897d3b52067..5f985c9fa94 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreDotnetCliToolsTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreDotnetCliToolsTask.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/src/NuGet.Core/NuGet.Build.Tasks/GetRestorePackageReferencesTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/GetRestorePackageReferencesTask.cs index 113a2e05b50..ec6e7b7e276 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/GetRestorePackageReferencesTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/GetRestorePackageReferencesTask.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; using System.Collections.Generic; using System.Linq; using Microsoft.Build.Framework; diff --git a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectJsonPathTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectJsonPathTask.cs index 72320e47116..4910b102dd5 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectJsonPathTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectJsonPathTask.cs @@ -1,4 +1,7 @@ -using System.IO; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.IO; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Newtonsoft.Json; diff --git a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectReferencesTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectReferencesTask.cs index 0dc0b2d9e1f..2ecb83ae61c 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectReferencesTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreProjectReferencesTask.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSolutionProjectsTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSolutionProjectsTask.cs new file mode 100644 index 00000000000..5e859eff985 --- /dev/null +++ b/src/NuGet.Core/NuGet.Build.Tasks/GetRestoreSolutionProjectsTask.cs @@ -0,0 +1,76 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace NuGet.Build.Tasks +{ + /// + /// Convert .metaproj paths to project paths. + /// + public class GetRestoreSolutionProjectsTask : Task + { + private const string MetaProjExtension = ".metaproj"; + + /// + /// Solution project references. + /// + [Required] + public ITaskItem[] ProjectReferences { get; set; } + + /// + /// Root path used for resolving the absolute project paths. + /// + [Required] + public string SolutionFilePath { get; set; } + + /// + /// Output items + /// + [Output] + public ITaskItem[] OutputProjectReferences { get; set; } + + public override bool Execute() + { + // Log inputs + var log = new MSBuildLogger(Log); + log.LogDebug($"(in) ProjectReferences '{string.Join(";", ProjectReferences.Select(p => p.ItemSpec))}'"); + log.LogDebug($"(in) SolutionFilePath '{SolutionFilePath}'"); + + var entries = new List(); + var parentDirectory = Path.GetDirectoryName(SolutionFilePath); + + foreach (var project in ProjectReferences) + { + if (string.IsNullOrEmpty(project.ItemSpec)) + { + continue; + } + + var projectPath = Path.GetFullPath(Path.Combine(parentDirectory, project.ItemSpec)); + + // Check for the metaproj extension, this is auto generated by solutions instead of + // the actual project path when build order is set. For the purpose of restore + // the order is not important so we remove the extension to get the real project path. + if (projectPath.EndsWith(MetaProjExtension, StringComparison.OrdinalIgnoreCase)) + { + // Remove .metaproj from the path. + projectPath = projectPath.Substring(0, projectPath.Length - MetaProjExtension.Length); + } + + // Clone items using the modified path + var newEntry = new TaskItem(projectPath, project.CloneCustomMetadata()); + entries.Add(newEntry); + } + + OutputProjectReferences = entries.ToArray(); + + return true; + } + } +} diff --git a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets index 6cebcec8855..99c8c31cb77 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets +++ b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets @@ -76,6 +76,7 @@ Copyright (c) .NET Foundation. All rights reserved. + - - - - + + + + + <_FilteredRestoreGraphProjectInputItemsTmp @@ -210,7 +216,7 @@ Copyright (c) .NET Foundation. All rights reserved. Entry point for creating the project to project restore graph. ============================================================ --> - @@ -321,7 +327,7 @@ Copyright (c) .NET Foundation. All rights reserved. Returns="$(_CurrentProjectJsonPath)"> - @@ -452,12 +458,12 @@ Copyright (c) .NET Foundation. All rights reserved. <_RestoreCrossTargeting>true - + <_RestoreSkipContentFileWrite Condition=" '$(TargetFrameworks)' == '' AND '$(TargetFramework)' == '' ">true - + <_RestoreGraphEntry Include="$([System.Guid]::NewGuid())"> @@ -607,7 +613,6 @@ Copyright (c) .NET Foundation. All rights reserved. ============================================================ --> diff --git a/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs index 94406ef0df1..87d3948977e 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs @@ -1,4 +1,7 @@ -using System.IO; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.IO; using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities;