Skip to content
Snippets Groups Projects
inc_functions.php 2.14 KiB
<?php
// ----------------------------------------------------------------------
// SHARED FUNCTIONS FOR INSTALLER AND BUILDER
// ----------------------------------------------------------------------

$aCol=[
    "reset" => "\e[0m",
    "red" => "\e[31m",
    "green" => "\e[32m",
    "yellow" => "\e[33m",
    "blue" => "\e[34m",
    "magenta" => "\e[35m",
    "cyan" => "\e[36m",
    "light_gray" => "\e[37m",
    "dark_gray" => "\e[90m",
    "light_red" => "\e[91m",
    "light_green" => "\e[92m",
    "light_yellow" => "\e[93m",
    "light_blue" => "\e[94m",
    "light_magenta" => "\e[95m",
];

function _h1($s): void
{
    global $aCol;
    echo "\n$aCol[magenta]>>>>>>>>>> $s$aCol[reset]\n";
    
    /*
    echo "\n   __".str_repeat("_", strlen($s))."\n";
    echo "__/ ".str_repeat(" ", strlen($s))." \___".str_repeat("_", 70-strlen($s))."\n";
    echo "    $s\n";
    echo str_repeat(".", 79)."\n\n";
    */
}
function _abort($sMessage, $iExitcode = 1): never
{
    global $aCol;
    echo "❌ $aCol[red]$sMessage.$aCol[reset]\n";
    exit($iExitcode);
}

function _ok(string $sMessage=""): void
{
    echo "✅ OK $sMessage\n";
}
function _skip(string $sMessage=""): void
{
    echo "🔹 SKIP: $sMessage\n";
}

function _chdir($sDir): void
{
    global $aCol;
    if (!is_dir($sDir)) {
        _abort("Directory '$sDir' not found.");
    }
    chdir($sDir);
    echo "$aCol[cyan]dir # " . getcwd() . "$aCol[reset]\n";
}

/**
 * Execute shell command and abort if it fails
 * @param mixed $cmd
 * @return void
 */
function _exec($cmd): void
{
    global $aCol;
    echo "$aCol[blue]cmd > $cmd$aCol[reset]\n";
    exec("$cmd 2>&1", $aOut, $rc);

    if (!count($aOut)) {
        $aOut = ["-- no output --"];
    }
    echo implode("\n", $aOut) . "\n";

    if ($rc != 0) {
        echo "rc=$rc ";
        _abort("Error: Command failed. Aborting.", $rc);
    }
    _ok();
}

function _mkdir(string $sMyDir): void
{
    if (!is_dir($sMyDir)) {
        echo "DIR > '$sMyDir' ";
        if (!mkdir($sMyDir, 0755, true)) {
            _abort("ERROR: mkdir failed.");
        }
        _ok();
        echo "\n";
    } else {
        _skip("mkdir: already exists: '$sMyDir");
    }

}