Setting proper dotnet PATH during installation detection

This commit is contained in:
Pavel Shevaev 2024-11-20 09:25:46 +03:00
parent f9d5dd6b9e
commit e9d5e8468f
2 changed files with 29 additions and 7 deletions

View File

@ -22,11 +22,8 @@ function dotnet_shell_get($cmd)
function dotnet_set_env()
{
//TODO: do we need this one?
putenv("DOTNET_CLI_TELEMETRY_OPTOUT=1");
$dotnet_path = DotnetSupport::getPath();
if(is_win())
putenv("PATH=$dotnet_path;".getenv('PATH'));
else
putenv("PATH=$dotnet_path:".getenv('PATH'));
DotnetSupport::addEnvPath();
}

View File

@ -14,6 +14,7 @@ class DotnetSupport
$has_dotnet = true;
$dotnet_curr_ver = "";
$prev_path = self::addEnvPath();
try
{
$dotnet_curr_ver = self::shellGet("dotnet --version");
@ -23,6 +24,10 @@ class DotnetSupport
{
$has_dotnet = false;
}
finally
{
self::setEnvPath($prev_path);
}
if($has_dotnet && version_compare($dotnet_curr_ver, $version) >= 0)
return;
@ -34,9 +39,9 @@ class DotnetSupport
$version_parts = explode('.', $version);
$channel = $version_parts[0].'.'.$version_parts[1];
if(self::isWin())
self::Shell("powershell -NoProfile -ExecutionPolicy unrestricted Invoke-Expression '\"$install_file_path\" -Channel $channel'");
self::shell("powershell -NoProfile -ExecutionPolicy unrestricted Invoke-Expression '\"$install_file_path\" -Channel $channel'");
else
self::Shell("\"$install_file_path\" --channel $channel");
self::shell("\"$install_file_path\" --channel $channel");
}
static function getInstallerPath() : string
@ -60,6 +65,26 @@ class DotnetSupport
}
}
static function addEnvPath(string $path = '') : string
{
$prev_path = getenv('PATH');
if(!$path)
$path = DotnetSupport::getPath();
if(self::isWin())
self::setEnvPath("$path;$prev_path");
else
self::setEnvPath("$path:$prev_path");
return $prev_path;
}
static function setEnvPath(string $path)
{
putenv("PATH=$path");
}
static function isWin() : bool
{
return !(DIRECTORY_SEPARATOR == '/');