Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static string DotnetHomePath
{
get
{
return CliFolderPathCalculatorCore.GetDotnetHomePath()
return new CliFolderPathCalculatorCore().GetDotnetHomePath()
?? throw new ConfigurationException(
string.Format(
LocalizableStrings.FailedToDetermineUserHomeDirectory,
Expand Down
28 changes: 24 additions & 4 deletions src/Common/CliFolderPathCalculatorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@

namespace Microsoft.DotNet.Configurer
{
static class CliFolderPathCalculatorCore
class CliFolderPathCalculatorCore
{
public const string DotnetHomeVariableName = "DOTNET_CLI_HOME";
public const string DotnetProfileDirectoryName = ".dotnet";

public static string? GetDotnetUserProfileFolderPath()
private readonly Func<string, string?> _getEnvironmentVariable;

/// <summary>
/// Creates an instance that reads environment variables from the process environment.
/// </summary>
public CliFolderPathCalculatorCore()
: this(Environment.GetEnvironmentVariable)
{
}

/// <summary>
/// Creates an instance that reads environment variables via the supplied delegate.
/// Use this from MSBuild tasks to route reads through TaskEnvironment.
/// </summary>
public CliFolderPathCalculatorCore(Func<string, string?> getEnvironmentVariable)
{
_getEnvironmentVariable = getEnvironmentVariable ?? throw new ArgumentNullException(nameof(getEnvironmentVariable));
}

public string? GetDotnetUserProfileFolderPath()
{
string? homePath = GetDotnetHomePath();
if (homePath is null)
Expand All @@ -19,9 +38,9 @@ static class CliFolderPathCalculatorCore
return Path.Combine(homePath, DotnetProfileDirectoryName);
}

public static string? GetDotnetHomePath()
public string? GetDotnetHomePath()
{
var home = Environment.GetEnvironmentVariable(DotnetHomeVariableName);
var home = _getEnvironmentVariable(DotnetHomeVariableName);
if (string.IsNullOrEmpty(home))
{
home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Expand All @@ -33,5 +52,6 @@ static class CliFolderPathCalculatorCore

return home;
}

}
}
2 changes: 1 addition & 1 deletion src/RazorSdk/Tool/ServerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal static string GetPidFilePath()
var path = Environment.GetEnvironmentVariable("DOTNET_BUILD_PIDFILE_DIRECTORY");
if (string.IsNullOrEmpty(path))
{
var homePath = CliFolderPathCalculatorCore.GetDotnetHomePath();
var homePath = new CliFolderPathCalculatorCore().GetDotnetHomePath();
if (homePath is null)
{
// Couldn't locate the user profile directory. Bail.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private sealed class CachedState
};

// First check if requested SDK resolves to a workload SDK pack
string? userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
string? userProfileDir = new CliFolderPathCalculatorCore().GetDotnetUserProfileFolderPath();
ResolutionResult? workloadResult = null;
if (dotnetRoot is not null && netcoreSdkVersion is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private class CachedState
resolverContext.State = cachedState;
}

string? userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
string? userProfileDir = new CliFolderPathCalculatorCore().GetDotnetUserProfileFolderPath();
ResolutionResult? result = null;
if (cachedState.DotnetRootPath is not null && cachedState.SdkVersion is not null)
{
Expand Down
33 changes: 27 additions & 6 deletions src/Tasks/Microsoft.NET.Build.Tasks/FrameworkReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@

namespace Microsoft.NET.Build.Tasks
{
internal static class FrameworkReferenceResolver
internal class FrameworkReferenceResolver
{
public static string GetDefaultReferenceAssembliesPath()
private readonly Func<string, string> _getEnvironmentVariable;

/// <summary>
/// Creates an instance that reads environment variables from the process environment.
/// </summary>
public FrameworkReferenceResolver()
: this(Environment.GetEnvironmentVariable)
{
}

/// <summary>
/// Creates an instance that reads environment variables via the supplied delegate.
/// Use this from MSBuild tasks to route reads through TaskEnvironment.
/// </summary>
public FrameworkReferenceResolver(Func<string, string> getEnvironmentVariable)
{
_getEnvironmentVariable = getEnvironmentVariable ?? throw new ArgumentNullException(nameof(getEnvironmentVariable));
}

public string GetDefaultReferenceAssembliesPath()
{
// Allow setting the reference assemblies path via an environment variable
var referenceAssembliesPath = DotNetReferenceAssembliesPathResolver.Resolve();
// Allow setting the reference assemblies path via an environment variable.
// We read this directly instead of calling DotNetReferenceAssembliesPathResolver.Resolve()
// because that runtime method uses process-global Environment.GetEnvironmentVariable.
var referenceAssembliesPath = _getEnvironmentVariable(DotNetReferenceAssembliesPathResolver.DotNetReferenceAssembliesPathEnv);

if (!string.IsNullOrEmpty(referenceAssembliesPath))
{
Expand All @@ -28,12 +49,12 @@ public static string GetDefaultReferenceAssembliesPath()

// References assemblies are in %ProgramFiles(x86)% on
// 64 bit machines
var programFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
var programFiles = _getEnvironmentVariable("ProgramFiles(x86)");

if (string.IsNullOrEmpty(programFiles))
{
// On 32 bit machines they are in %ProgramFiles%
programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
programFiles = _getEnvironmentVariable("ProgramFiles");
}

if (string.IsNullOrEmpty(programFiles))
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
.WithReferenceProjectInfos(referenceProjects)
.WithRuntimePackAssets(runtimePackAssets)
.WithCompilationOptions(compilationOptions)
.WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
.WithReferenceAssembliesPath(new FrameworkReferenceResolver().GetDefaultReferenceAssembliesPath())
.WithPackagesThatWereFiltered(GetFilteredPackages());

if (CompileReferences.Length > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ IEnumerable<string> GetPackFolders()
if (!string.IsNullOrEmpty(NetCoreRoot) && !string.IsNullOrEmpty(NETCoreSdkVersion))
{
if (WorkloadFileBasedInstall.IsUserLocal(NetCoreRoot, NETCoreSdkVersion) &&
CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath() is { } userProfileDir)
new CliFolderPathCalculatorCore().GetDotnetUserProfileFolderPath() is { } userProfileDir)
{
yield return Path.Combine(userProfileDir, "packs");
}
Expand Down Expand Up @@ -1177,7 +1177,7 @@ private Lazy<WorkloadResolver> LazyCreateWorkloadResolver()
{
return new(() =>
{
string? userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
string? userProfileDir = new CliFolderPathCalculatorCore().GetDotnetUserProfileFolderPath();

// When running MSBuild tasks, the current directory is always the project directory, so we can use that as the
// starting point to search for global.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override void ExecuteCore()
{
if (MissingWorkloadPacks.Any())
{
string userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
string userProfileDir = new CliFolderPathCalculatorCore().GetDotnetUserProfileFolderPath();

// When running MSBuild tasks, the current directory is always the project directory, so we can use that as the
// starting point to search for global.json
Expand Down
Loading