Skip to content

Commit

Permalink
up: update some for the tool manage and install logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 24, 2022
1 parent 8b4cb26 commit f810026
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
6 changes: 5 additions & 1 deletion app/Console/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
class ToolCommand extends Command
{
public const OPT_DRY_RUN = 'dry-run';
public const OPT_PROXY_ENV = 'proxy-env';

protected static string $name = 'tool';
protected static string $desc = 'some little tool commands';

Expand All @@ -41,7 +44,8 @@ protected function subCommands(): array

protected function configure(): void
{
$this->flags->addOptByRule('dry-run,try', 'bool;Dry-run the workflow, dont real execute');
$this->flags->addOptByRule(self::OPT_DRY_RUN . ',try', 'bool;Dry-run the workflow, dont real execute');
$this->flags->addOptByRule(self::OPT_PROXY_ENV, 'bool;open proxy env settings on run command');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions app/Console/Component/AutoSetProxyEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public function applyProxyEnv(string $realCName, string $realGName = ''): bool
return false;
}

/**
* @param string $cmdId
*
* @return bool
*/
public function directApply(string $cmdId): bool
{
if (!$this->envSettings) {
return false;
}

$this->setProxyEnv($this->envSettings, $cmdId);
return true;
}

/**
* @param array $settings
* @param string $command
Expand Down
74 changes: 52 additions & 22 deletions app/Console/Model/BinTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public function buildCmdRunner(string $command): CmdRunner
public function run(string $command, CmdRunner $runner): void
{
if ($tips = $this->getBeforeTip($command)) {
Cli::cyan('Beginning.tips:');
Cli::info('Beginning.tips:');
Cli::writeln($tips);
}

$runner->runAndPrint();

if ($tips = $this->getAfterTip($command)) {
Cli::cyan('Complete.tips:');
Cli::magenta("\n[Complete.tips]:");
Cli::writeln($tips);
}
}
Expand All @@ -109,17 +109,33 @@ public function run(string $command, CmdRunner $runner): void
public function getCmdScripts(string $command): string|array
{
$this->mustCommand($command);
$name = $this->name;
$cmd = $this->commands[$command];

switch ($command) {
case 'install':
$info = $this->install;
$cmd = $info['run'];
break;
case 'update':
$info = $this->update;
$cmd = $info['run'];
break;
case 'remove':
$info = $this->remove;
$cmd = $info['run'];
break;
default:
$cmd = $this->commands[$command];
}

$name = $this->name;
if (is_string($cmd) && str_starts_with($cmd, '@')) {
$refCmd = substr($cmd, 1);
if (!$this->hasCommand($refCmd)) {
throw new InvalidArgumentException("not found refer command '$refCmd' in the tool '$name'");
$refName = substr($cmd, 1);
if (!$this->isExtraCmd($refName)) {
throw new InvalidArgumentException("not found refer command '$refName' in the tool '$name'");
}

// use refer command
$cmd = $this->commands[$refCmd];
// use refer command info
$cmd = $this->getCmdScripts($refName);
}

return $cmd;
Expand All @@ -137,13 +153,37 @@ public function getCommand(string $command): string|array
return $this->commands[$command];
}

/**
* @param string $cmd
*
* @return bool
*/
public function isBuiltIn(string $cmd): bool
{
return in_array($cmd, self::BUILT_IN, true);
}

/**
* @param string $command
*
* @return bool
*/
public function isExtraCmd(string $command): bool
{
return isset($this->commands[$command]);
}

/**
* @param string $command
*
* @return bool
*/
public function hasCommand(string $command): bool
{
if ($this->isBuiltIn($command)) {
return true;
}

return isset($this->commands[$command]);
}

Expand All @@ -163,16 +203,6 @@ private function mustCommand(string $command): void
}
}

/**
* @param string $cmd
*
* @return bool
*/
public function isBuiltIn(string $cmd): bool
{
return in_array($cmd, self::BUILT_IN, true);
}

/**
* @param array $commands
*
Expand Down Expand Up @@ -225,7 +255,7 @@ public function getInstall(): array
*/
public function setInstall(array|string $install): void
{
if (is_string($install)) {
if (is_string($install) || !isset($install['run'])) {
$install = [
'run' => $install,
];
Expand All @@ -247,7 +277,7 @@ public function getUpdate(): array
*/
public function setUpdate(array|string $update): void
{
if (is_string($update)) {
if (is_string($update) || !isset($update['run'])) {
$update = [
'run' => $update,
];
Expand All @@ -269,7 +299,7 @@ public function getRemove(): array
*/
public function setRemove(array|string $remove): void
{
if (is_string($remove)) {
if (is_string($remove) || !isset($remove['run'])) {
$remove = [
'run' => $remove,
];
Expand Down
7 changes: 6 additions & 1 deletion app/Console/SubCmd/ToolCmd/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Command\ToolCommand;
use Inhere\Kite\Console\Manager\ToolManager;
use Inhere\Kite\Kite;
use Toolkit\Stdlib\Helper\Assert;
Expand Down Expand Up @@ -62,7 +63,11 @@ protected function execute(Input $input, Output $output)
$cr = $tool->buildCmdRunner($command);

if ($p = $this->getParent()) {
$cr->setDryRun($p->getFlags()->getOpt('dry-run'));
$cr->setDryRun($p->getFlags()->getOpt(ToolCommand::OPT_DRY_RUN));

if ($p->getFlags()->getOpt(ToolCommand::OPT_PROXY_ENV)) {
Kite::autoProxy()->directApply($this->getCommandId());
}
}

// $cr->setEnvVars();
Expand Down

0 comments on commit f810026

Please sign in to comment.