Skip to content

Commit c9b81ab

Browse files
authored
Enable central package management implicitly when Directory.Packages.props exists (#5572)
1 parent 51986ef commit c9b81ab

File tree

14 files changed

+191
-245
lines changed

14 files changed

+191
-245
lines changed

src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static class MsBuildUtility
2323
{
2424
internal const int MsBuildWaitTime = 2 * 60 * 1000; // 2 minutes in milliseconds
2525

26+
private const string NuGetProps = "NuGet.CommandLine.NuGet.props";
2627
private const string NuGetTargets = "NuGet.CommandLine.NuGet.targets";
2728
private static readonly XNamespace MSBuildNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
2829

@@ -103,10 +104,12 @@ public static async Task<DependencyGraphSpec> GetProjectReferencesAsync(
103104
}
104105

105106
using (var inputTargetPath = new TempFile(".nugetinputs.targets"))
107+
using (var nugetPropsPath = new TempFile(".nugetrestore.props"))
106108
using (var entryPointTargetPath = new TempFile(".nugetrestore.targets"))
107109
using (var resultsPath = new TempFile(".output.dg"))
108110
{
109-
// Read NuGet.targets from nuget.exe and write it to disk for msbuild.exe
111+
// Read NuGet.props and NuGet.targets from nuget.exe and write it to disk for msbuild.exe
112+
ExtractResource(NuGetProps, nugetPropsPath);
110113
ExtractResource(NuGetTargets, entryPointTargetPath);
111114

112115
// Build a .targets file of all restore inputs, this is needed to avoid going over the limit on command line arguments.
@@ -115,15 +118,15 @@ public static async Task<DependencyGraphSpec> GetProjectReferencesAsync(
115118
{ "RestoreUseCustomAfterTargets", "true" },
116119
{ "RestoreGraphOutputPath", resultsPath },
117120
{ "RestoreRecursive", recursive.ToString(CultureInfo.CurrentCulture).ToLowerInvariant() },
118-
{ "RestoreProjectFilterMode", "exclusionlist" }
121+
{ "RestoreProjectFilterMode", "exclusionlist" },
119122
};
120123

121124
var inputTargetXML = GetRestoreInputFile(entryPointTargetPath, properties, projectPaths);
122125

123126
inputTargetXML.Save(inputTargetPath);
124127

125128
// Create msbuild parameters and include global properties that cannot be set in the input targets path
126-
var arguments = GetMSBuildArguments(entryPointTargetPath, inputTargetPath, nugetExePath, solutionDirectory, solutionName, restoreConfigFile, sources, packagesDirectory, msbuildToolset, restoreLockProperties, EnvironmentVariableWrapper.Instance);
129+
var arguments = GetMSBuildArguments(entryPointTargetPath, nugetPropsPath, inputTargetPath, nugetExePath, solutionDirectory, solutionName, restoreConfigFile, sources, packagesDirectory, msbuildToolset, restoreLockProperties, EnvironmentVariableWrapper.Instance);
127130

128131
var processStartInfo = new ProcessStartInfo
129132
{
@@ -221,6 +224,7 @@ public static async Task<DependencyGraphSpec> GetProjectReferencesAsync(
221224

222225
public static string GetMSBuildArguments(
223226
string entryPointTargetPath,
227+
string nugetPropsPath,
224228
string inputTargetPath,
225229
string nugetExePath,
226230
string solutionDirectory,
@@ -253,6 +257,8 @@ public static string GetMSBuildArguments(
253257
args.Add($"/v:{msbuildVerbosity} ");
254258
}
255259

260+
AddProperty(args, "NuGetPropsFile", nugetPropsPath);
261+
256262
// Override the target under ImportsAfter with the current NuGet.targets version.
257263
AddProperty(args, "NuGetRestoreTargets", entryPointTargetPath);
258264
AddProperty(args, "RestoreUseCustomAfterTargets", bool.TrueString);

src/NuGet.Clients/NuGet.CommandLine/NuGet.CommandLine.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
</ItemGroup>
9292

9393
<ItemGroup>
94+
<EmbeddedResource Include="..\..\NuGet.Core\NuGet.Build.Tasks\NuGet.props">
95+
<Link>NuGet.props</Link>
96+
<SubType>Designer</SubType>
97+
</EmbeddedResource>
9498
<EmbeddedResource Include="..\..\NuGet.Core\NuGet.Build.Tasks\NuGet.targets">
9599
<Link>NuGet.targets</Link>
96100
<SubType>Designer</SubType>

src/NuGet.Core/NuGet.Build.Tasks/NuGet.props

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,32 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
99
Copyright (c) .NET Foundation. All rights reserved.
1010
***********************************************************************************************
1111
-->
12-
1312
<Project>
14-
1513
<!--
16-
Import 'Directory.Packages.props' which will contain centralized packages for all the projects and solutions under
17-
the directory in which the file is present. This is similar to 'Directory.Build.props/targets' logic which is present
18-
in the common props/targets which serve a similar purpose.
14+
Determine the path to the 'Directory.Packages.props' file, if the user did not:
15+
1. Set $(ManagePackageVersionsCentrally) to false
16+
2. Set $(ImportDirectoryPackagesProps) to false
17+
3. Already specify the path to a 'Directory.Packages.props' file via $(DirectoryPackagesPropsPath)
1918
-->
20-
21-
<PropertyGroup>
22-
<ImportDirectoryPackagesProps Condition="'$(ImportDirectoryPackagesProps)' == ''">true</ImportDirectoryPackagesProps>
19+
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And '$(DirectoryPackagesPropsPath)' == ''">
20+
<_DirectoryPackagesPropsFile Condition="'$(_DirectoryPackagesPropsFile)' == ''">Directory.Packages.props</_DirectoryPackagesPropsFile>
21+
<_DirectoryPackagesPropsBasePath Condition="'$(_DirectoryPackagesPropsBasePath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildProjectDirectory)', '$(_DirectoryPackagesPropsFile)'))</_DirectoryPackagesPropsBasePath>
22+
<DirectoryPackagesPropsPath Condition="'$(_DirectoryPackagesPropsBasePath)' != '' and '$(_DirectoryPackagesPropsFile)' != ''">$([MSBuild]::NormalizePath('$(_DirectoryPackagesPropsBasePath)', '$(_DirectoryPackagesPropsFile)'))</DirectoryPackagesPropsPath>
2323
</PropertyGroup>
2424

2525
<!--
26-
Determine the path to the 'Directory.Packages.props' file, if the user did not disable $(ImportDirectoryPackagesProps) and
27-
they did not already specify an absolute path to use via $(DirectoryPackagesPropsPath)
26+
Default $(ManagePackageVersionsCentrally) to true, import Directory.Packages.props, and set $(CentralPackageVersionsFileImported) to true if the user did not:
27+
1. Set $(ManagePackageVersionsCentrally) to false
28+
2. Set $(ImportDirectoryPackagesProps) to false
29+
3. The path specified in $(DirectoryPackagesPropsPath) exists
2830
-->
29-
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' == ''">
30-
<_DirectoryPackagesPropsFile Condition="'$(_DirectoryPackagesPropsFile)' == ''">Directory.Packages.props</_DirectoryPackagesPropsFile>
31-
<_DirectoryPackagesPropsBasePath Condition="'$(_DirectoryPackagesPropsBasePath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildProjectDirectory)', '$(_DirectoryPackagesPropsFile)'))</_DirectoryPackagesPropsBasePath>
32-
<DirectoryPackagesPropsPath Condition="'$(_DirectoryPackagesPropsBasePath)' != '' and '$(_DirectoryPackagesPropsFile)' != ''">$([MSBuild]::NormalizePath('$(_DirectoryPackagesPropsBasePath)', '$(_DirectoryPackagesPropsFile)'))</DirectoryPackagesPropsPath>
31+
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
32+
<ManagePackageVersionsCentrally Condition="'$(ManagePackageVersionsCentrally)' == ''">true</ManagePackageVersionsCentrally>
3333
</PropertyGroup>
3434

35-
<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' != '' and Exists('$(DirectoryPackagesPropsPath)')"/>
35+
<Import Project="$(DirectoryPackagesPropsPath)" Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')" />
3636

37-
<PropertyGroup Condition="'$(ImportDirectoryPackagesProps)' == 'true' and '$(DirectoryPackagesPropsPath)' != '' and Exists('$(DirectoryPackagesPropsPath)')">
37+
<PropertyGroup Condition="'$(ManagePackageVersionsCentrally)' != 'false' And '$(ImportDirectoryPackagesProps)' != 'false' And Exists('$(DirectoryPackagesPropsPath)')">
3838
<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>
3939
</PropertyGroup>
40-
4140
</Project>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<Project />
1+
<Project>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
4+
</PropertyGroup>
5+
</Project>

0 commit comments

Comments
 (0)