Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 CliFolderPathCalculatorCore.GetDotnetHomePathFromSystemEnvironment()
?? throw new ConfigurationException(
string.Format(
LocalizableStrings.FailedToDetermineUserHomeDirectory,
Expand Down
37 changes: 33 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,15 @@ static class CliFolderPathCalculatorCore

return home;
}

// Static convenience methods for callers that use process environment variables.

private static readonly CliFolderPathCalculatorCore s_default = new();

public static string? GetDotnetUserProfileFolderPathFromSystemEnvironment()
=> s_default.GetDotnetUserProfileFolderPath();

public static string? GetDotnetHomePathFromSystemEnvironment()
=> s_default.GetDotnetHomePath();
Comment thread
baronfel marked this conversation as resolved.
Outdated
}
}
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 = CliFolderPathCalculatorCore.GetDotnetHomePathFromSystemEnvironment();
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 = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPathFromSystemEnvironment();
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 = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPathFromSystemEnvironment();
ResolutionResult? result = null;
if (cachedState.DotnetRootPath is not null && cachedState.SdkVersion is not null)
{
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)
CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPathFromSystemEnvironment() 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 = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPathFromSystemEnvironment();

// 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 = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPathFromSystemEnvironment();

// 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