first commit

This commit is contained in:
Pavel Shevaev 2022-05-17 19:01:32 +03:00
commit 7b5b958133
2 changed files with 93 additions and 0 deletions

11
composer.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "bit/taskman_env",
"description": "taskman gamectl env utils",
"homepage": "https://git.bit5.ru/composer/taskman_env",
"require": {
"php": ">=7.4"
},
"autoload": {
"classmap": ["env.inc.php"]
}
}

82
env.inc.php Normal file
View File

@ -0,0 +1,82 @@
<?php
namespace taskman;
use Exception;
task('envs', function()
{
global $GAME_ROOT;
$dir = $GAME_ROOT."/env/";
echo "=== Available envs ===\n";
$results = scan_files_rec(array($dir), array(".props.php"));
foreach($results as $file)
echo str_replace(".props.php", "", str_replace($dir, "", $file)) . "\n";
});
function put_env(&$argv)
{
global $GAME_ROOT;
$env = '';
$filtered = array();
for($i=0;$i<sizeof($argv);++$i)
{
$v = $argv[$i];
if(strpos($v, "--env") === 0)
{
list($tmp, $env) = explode("=", $v);
continue;
}
else
$filtered[] = $v;
}
$argv = $filtered;
//GAME_ENV env. variable has top priority
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
putenv("GAME_ENV=$env");
//default one
else
putenv("GAME_ENV=");
}
function include_env()
{
global $GAME_ROOT;
$env_filename = get_env_file();
if($env_filename && !file_exists($env_filename))
throw new Exception("Error setting environment. No such env file $env_filename");
else if($env_filename === null && file_exists("$GAME_ROOT/gamectl.props.php"))
$env_filename = "$GAME_ROOT/gamectl.props.php";
if($env_filename)
include($env_filename);
}
function get_env_file()
{
global $GAME_ROOT;
$env = '';
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
$env_filename = $GAME_ROOT . "/env/$env.props.php";
else
$env_filename = $GAME_ROOT . '/gamectl.props.php';
if(file_exists($env_filename))
return $env_filename;
return null;
}