108 lines
2.3 KiB
PHP
108 lines
2.3 KiB
PHP
<?php
|
|
namespace taskman;
|
|
use Exception;
|
|
|
|
//NOTE: this class must not have any dependencies on any packages
|
|
class DotnetSupport
|
|
{
|
|
const VERSION = "8.0.0";
|
|
|
|
static function install()
|
|
{
|
|
$version = self::VERSION;
|
|
|
|
$has_dotnet = true;
|
|
|
|
$dotnet_curr_ver = "";
|
|
$prev_path = self::addEnvPath();
|
|
try
|
|
{
|
|
$dotnet_curr_ver = self::shellGet("dotnet --version");
|
|
print("DOTNET CURRENT VERSION: $dotnet_curr_ver\n");
|
|
}
|
|
catch(\Throwable $th)
|
|
{
|
|
$has_dotnet = false;
|
|
}
|
|
finally
|
|
{
|
|
self::setEnvPath($prev_path);
|
|
}
|
|
|
|
if($has_dotnet && version_compare($dotnet_curr_ver, $version) >= 0)
|
|
return;
|
|
|
|
print("DOTNET VERSION MISMATCH: $dotnet_curr_ver < $version\n");
|
|
|
|
$install_file_path = self::getInstallerPath();
|
|
|
|
$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'");
|
|
else
|
|
self::shell("\"$install_file_path\" --channel $channel");
|
|
}
|
|
|
|
static function getInstallerPath() : string
|
|
{
|
|
if(self::isWin())
|
|
return __DIR__ . "/dotnet-install.ps1";
|
|
else
|
|
return __DIR__ . "/dotnet-install.sh";
|
|
}
|
|
|
|
static function getPath() : string
|
|
{
|
|
if(self::isWin())
|
|
{
|
|
$appdata_path = getenv('LOCALAPPDATA');
|
|
return "$appdata_path\\Microsoft\\dotnet\\";
|
|
}
|
|
else
|
|
{
|
|
return getenv("HOME")."/.dotnet/";
|
|
}
|
|
}
|
|
|
|
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 == '/');
|
|
}
|
|
|
|
static function shellGet(string $cmd, bool $as_string = true)
|
|
{
|
|
exec($cmd, $out, $code);
|
|
if($code !== 0)
|
|
throw new Exception("Error($code) executing shell cmd '$cmd'");
|
|
return $as_string ? implode("", $out) : $out;
|
|
}
|
|
|
|
static function shell(string $cmd)
|
|
{
|
|
system($cmd, $ret);
|
|
if($ret != 0)
|
|
throw new Exception("Shell execution error(exit code $ret)");
|
|
}
|
|
}
|