Tweaking install routines

This commit is contained in:
Pavel Shevaev 2024-09-23 19:34:41 +03:00
parent 0137fd04eb
commit f052effcd8
3 changed files with 83 additions and 62 deletions

View File

@ -6,6 +6,6 @@
"php": ">=8.3" "php": ">=8.3"
}, },
"autoload": { "autoload": {
"classmap": ["dotnet.inc.php"] "classmap": ["support.inc.php", "dotnet.inc.php"]
} }
} }

View File

@ -1,14 +1,6 @@
<?php <?php
namespace taskman; namespace taskman;
class DotnetSupport
{
static function install()
{
dotnet_install("8.0.0");
}
}
task('dotnet_set_env', ['always' => true], function() { task('dotnet_set_env', ['always' => true], function() {
dotnet_set_env(); dotnet_set_env();
}); });
@ -27,65 +19,13 @@ function dotnet_shell_get($cmd)
return shell_get('dotnet ' . $cmd); return shell_get('dotnet ' . $cmd);
} }
function dotnet_install(string $version)
{
$has_dotnet = true;
$dotnet_curr_ver = "";
try
{
$dotnet_curr_ver = shell_get("dotnet --version");
print("DOTNET CURRENT VERSION: $dotnet_curr_ver\n");
}
catch(\Throwable $th)
{
$has_dotnet = false;
}
if($has_dotnet && version_compare($dotnet_curr_ver, $version) >= 0)
return;
print("DOTNET VERSION MISMATCH: $dotnet_curr_ver < $version\n");
$install_file_path = dotnet_get_installer_path();
$version_parts = explode('.', $version);
$channel = $version_parts[0].'.'.$version_parts[1];
if(is_win())
shell("powershell -NoProfile -ExecutionPolicy unrestricted Invoke-Expression '\"$install_file_path\" -Channel $channel'");
else
shell("\"$install_file_path\" --channel $channel");
}
function dotnet_set_env() function dotnet_set_env()
{ {
putenv("DOTNET_CLI_TELEMETRY_OPTOUT=1"); putenv("DOTNET_CLI_TELEMETRY_OPTOUT=1");
$dotnet_path = dotnet_get_path(); $dotnet_path = DotnetSupport::getPath();
if(is_win()) if(is_win())
putenv("PATH=$dotnet_path;".getenv('PATH')); putenv("PATH=$dotnet_path;".getenv('PATH'));
else else
putenv("PATH=$dotnet_path:".getenv('PATH')); putenv("PATH=$dotnet_path:".getenv('PATH'));
} }
function dotnet_get_path()
{
if(is_win())
{
$appdata_path = getenv('LOCALAPPDATA');
return "$appdata_path\\Microsoft\\dotnet\\";
}
else
{
return getenv("HOME")."/.dotnet/";
}
}
function dotnet_get_installer_path() : string
{
if(is_win())
return __DIR__ . "/dotnet-install.ps1";
else
return __DIR__ . "/dotnet-install.sh";
}

81
support.inc.php Normal file
View File

@ -0,0 +1,81 @@
<?php
namespace taskman;
//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 = "";
try
{
$dotnet_curr_ver = self::shellGet("dotnet --version");
print("DOTNET CURRENT VERSION: $dotnet_curr_ver\n");
}
catch(\Throwable $th)
{
$has_dotnet = false;
}
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 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)");
}
}