Skip to content

Commit 746dd34

Browse files
AArnottdeepakaravindr
authored andcommitted
Use the $(UserProfile) MSBuild macro where possible
The MSBuild [projectname].nuget.targets files should use this macro instead of a hard-coded path that includes the username so that the generated file can be shared by multiple users.
1 parent e0e0e27 commit 746dd34

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/NuGet.Core/NuGet.Commands/MSBuildRestoreResult.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ namespace NuGet.Commands
1313
{
1414
public class MSBuildRestoreResult
1515
{
16+
/// <summary>
17+
/// The macros that we may use in MSBuild to replace path roots.
18+
/// </summary>
19+
private static readonly string[] MacroCandidates = new[]
20+
{
21+
"UserProfile", // e.g. C:\users\myusername
22+
};
23+
1624
/// <summary>
1725
/// Gets a boolean indicating if the necessary MSBuild file could be generated
1826
/// </summary>
@@ -101,6 +109,23 @@ public void Commit(ILogger log)
101109
}
102110
}
103111
}
112+
113+
private static string ReplacePathsWithMacros(string path)
114+
{
115+
foreach (var macroName in MacroCandidates)
116+
{
117+
string macroValue = Environment.GetEnvironmentVariable(macroName);
118+
if (path.StartsWith(macroValue, StringComparison.OrdinalIgnoreCase))
119+
{
120+
path = $"$({macroName})\\{path.Substring(macroValue.Length)}";
121+
}
122+
123+
break;
124+
}
125+
126+
return path;
127+
}
128+
104129
private void GenerateMSBuildErrorFile(string path)
105130
{
106131
var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
@@ -135,7 +160,7 @@ private void GenerateImportsFile(string path, IEnumerable<string> imports)
135160
new XElement(ns + "PropertyGroup",
136161
new XAttribute("Condition", "'$(NuGetPackageRoot)' == ''"),
137162

138-
new XElement(ns + "NuGetPackageRoot", RepositoryRoot)),
163+
new XElement(ns + "NuGetPackageRoot", ReplacePathsWithMacros(RepositoryRoot))),
139164
new XElement(ns + "ImportGroup", imports.Select(i =>
140165
new XElement(ns + "Import",
141166
new XAttribute("Project", Path.Combine("$(NuGetPackageRoot)", i)),

0 commit comments

Comments
 (0)