Skip to content

Commit

Permalink
update some for run script file
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 9, 2021
1 parent 0638978 commit 218dfa2
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 470 deletions.
20 changes: 13 additions & 7 deletions app/Component/ScriptRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Inhere\Kite\Component;

use Inhere\Console\Util\Show;
use Inhere\Kite\Helper\KiteUtil;
use Inhere\Kite\Helper\SysCmd;
use Inhere\Kite\Kite;
use InvalidArgumentException;
use RuntimeException;
use Toolkit\Cli\Cli;
use Toolkit\FsUtil\Dir;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj\AbstractObj;
use Toolkit\Stdlib\OS;
use function array_filter;
use function array_map;
use function array_merge;
Expand All @@ -29,6 +32,7 @@
use function strpos;
use function substr;
use function trim;
use const IN_PHAR;

/**
* class ScriptRunner
Expand Down Expand Up @@ -203,7 +207,7 @@ private function executeScripts(string $name, array $runArgs, $commands): void
*/
private function replaceScriptVars(string $name, string $cmdString, array $scriptArgs): string
{
if (strpos($cmdString, '$') === false) {
if (!str_contains($cmdString, '$')) {
return $cmdString;
}

Expand Down Expand Up @@ -248,6 +252,7 @@ public function runScriptFile(string $scriptFile, array $runArgs): void
$name = basename($scriptFile);

// must start withs '#!'
$binName = '';
if (!$line || !str_starts_with($line, '#!')) {
Cli::colored("will direct run the script file: $name", 'cyan');

Expand Down Expand Up @@ -282,6 +287,11 @@ public function runScriptFile(string $scriptFile, array $runArgs): void
$command .= ' ' . implode(' ', $runArgs);
}

// not in phar.
if ($binName === 'php' && !KiteUtil::isInPhar()) {
OS::setEnvVar('KITE_PATH', Kite::basePath());
}

$this->executeScript($command);
}

Expand Down Expand Up @@ -348,9 +358,7 @@ public function loadAllScriptFiles(): void
*/
public function getAllScriptFiles(string $keyword = ''): array
{
$extMatch = implode('|', array_map(static function ($ext) {
return trim($ext, '.');
}, $this->scriptExts));
$extMatch = implode('|', array_map(static fn($ext) => trim($ext, '.'), $this->scriptExts));

$files = [];
foreach ($this->scriptDirs as $dir) {
Expand All @@ -365,9 +373,7 @@ public function getAllScriptFiles(string $keyword = ''): array
}

if ($keyword) {
$files = array_filter($files, static function ($file) use($keyword) {
return stripos($file, $keyword) !== false;
});
$files = array_filter($files, static fn($file) => stripos($file, $keyword) !== false);
}

return $files;
Expand Down
7 changes: 3 additions & 4 deletions app/Console/Listener/NotFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,21 @@ public function __invoke(string $cmd, CliApplication $app): bool
return true;
}

$sr = Kite::scriptRunner();
$sr = Kite::scriptRunner();
$args = $app->getInput()->getFlags();

// - run custom scripts.
if ($sr->isScriptName($cmd)) {
$args = $app->getInput()->getFlags();
$app->note("input is an script name, redirect to run script: $cmd, args: " . DataHelper::toString($args));
$sr->runScriptByName($cmd, $args);

} elseif (Kite::plugManager()->isPlugin($cmd)) { // - is an plugin
$args = $app->getInput()->getFlags();
array_shift($args); // first is $cmd
$app->notice("input is an plugin name, will run plugin: $cmd, args: " . DataHelper::toString($args));

Kite::plugManager()->run($cmd, $app, $args);
} elseif ($sFile = $sr->findScriptFile($cmd)) { // - is script file
$args = $app->getInput()->getFlags();
array_shift($args); // first is script file
$app->notice("input is an script file, will call it: $cmd, args: " . DataHelper::toString($args));

$sr->runScriptFile($sFile, $args);
Expand Down
6 changes: 2 additions & 4 deletions app/Helper/AppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use function substr;
use function trim;
use function vdump;
use const IN_PHAR;
use const STDIN;

/**
Expand Down Expand Up @@ -57,10 +58,7 @@ public static function isVersion(string $version): bool
*/
public static function isInPhar(): bool
{
if (defined('IN_PHAR')) {
return IN_PHAR;
}
return false;
return defined('IN_PHAR') && IN_PHAR;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Toolkit\FsUtil\FS;
use Toolkit\Stdlib\OS;
use Toolkit\Stdlib\Str;
use function defined;
use function dirname;
use function in_array;
use function is_file;
use function str_replace;
use const IN_PHAR;

/**
* class KiteUtil
Expand Down Expand Up @@ -42,6 +44,14 @@ class KiteUtil
'clipboard',
];

/**
* @return bool
*/
public static function isInPhar(): bool
{
return defined('IN_PHAR') && IN_PHAR;
}

/**
* @param string $sep
*
Expand Down
Loading

0 comments on commit 218dfa2

Please sign in to comment.