Skip to content

Commit

Permalink
up: update the tool manage and config logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 22, 2022
1 parent e272c65 commit 0c7a044
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 11 deletions.
15 changes: 14 additions & 1 deletion app/Component/ScriptRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class ScriptRunner extends AbstractObj
*/
private bool $enable = true;

/**
* @var int exit code of exec script
*/
private int $errCode = 0;

/**
* @var array
*/
Expand Down Expand Up @@ -353,7 +358,7 @@ private function executeScript(string $command, bool $onlyOne = false, string $w
if ($this->dryRun) {
Cli::colored('DRY-RUN: ' . $command, 'cyan');
} else {
SysCmd::quickExec($command, $workdir);
$this->errCode = SysCmd::quickExec($command, $workdir);
}

if ($onlyOne) {
Expand Down Expand Up @@ -607,4 +612,12 @@ public function setEnable(bool|int $enable): void
$this->enable = (bool)$enable;
}

/**
* @return int
*/
public function getErrCode(): int
{
return $this->errCode;
}

}
14 changes: 8 additions & 6 deletions app/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected function execute(Input $input, Output $output): int

// default list script commands
if ($listType) {
$name = $this->flags->getOpt('info', $name);
$this->listScripts($output, $name);
return 0;
}
Expand Down Expand Up @@ -175,10 +176,13 @@ private function listScriptFiles(Output $output, string $name): void
private function listScripts(Output $output, string $name): void
{
$listOpt = [
'ucFirst' => false,
'ucFirst' => false,
'ucTitleWords' => false,
'filterEmpty' => true,
];

if ($name && $this->sr->isScriptName($name)) {
$desc = '';
$item = $this->sr->getScript($name);

// [_meta => [desc, ]]
Expand All @@ -187,15 +191,14 @@ private function listScripts(Output $output, string $name): void
unset($item['_meta']);

$desc = $meta['desc'] ?? '';
if ($desc) {
$output->colored($desc . "\n", 'cyan');
}
// $output->colored(ucfirst($desc), 'cyan');
}

$output->aList([
'name' => $name,
'desc' => $desc,
'command' => $item,
], 'script information', $listOpt);
], "Script: $name", $listOpt);
} else {
$count = $this->sr->getScriptCount();
$output->aList($this->sr->getScripts(), "registered scripts(total: $count)", $listOpt);
Expand All @@ -208,7 +211,6 @@ private function listScripts(Output $output, string $name): void
*/
private function searchScripts(Output $output, string $kw): void
{
// search
$matched = $this->sr->searchScripts($kw);

$count = count($matched);
Expand Down
116 changes: 116 additions & 0 deletions app/Console/Model/BinTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Toolkit\Cli\Cli;
use Toolkit\Stdlib\Obj;
use Toolkit\Stdlib\Obj\AbstractObj;
use function in_array;
use function is_string;
use function substr;

Expand All @@ -18,6 +19,8 @@
*/
class BinTool extends AbstractObj
{
public const BUILT_IN = ['install', 'update', 'remove'];

public string $name = '';
public string $desc = '';

Expand All @@ -26,6 +29,29 @@ class BinTool extends AbstractObj

public array $envVars = [];

/**
* @var string[]
*/
protected array $deps = [];

/**
* @var array{run: array, deps: array}
* @see ToolCmdMeta
*/
protected array $install = [];

/**
* @var array{run: array, deps: array}
* @see ToolCmdMeta
*/
protected array $update = [];

/**
* @var array{run: array, deps: array}
* @see ToolCmdMeta
*/
protected array $remove = [];

public array $commands = [];

public array $beforeTips = [];
Expand Down Expand Up @@ -107,6 +133,7 @@ public function getCmdScripts(string $command): string|array
public function getCommand(string $command): string|array
{
$this->mustCommand($command);

return $this->commands[$command];
}

Expand All @@ -127,11 +154,25 @@ public function hasCommand(string $command): bool
*/
private function mustCommand(string $command): void
{
if ($this->isBuiltIn($command)) {
return;
}

if (!isset($this->commands[$command])) {
throw new InvalidArgumentException("command '$command' is not found in tool: " . $this->name);
}
}

/**
* @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 @@ -162,4 +203,79 @@ public function getAfterTip(string $command): array|string
{
return $this->afterTips[$command] ?? '';
}

/**
* @param array|string $deps
*/
public function setDeps(array|string $deps): void
{
$this->deps = (array)$deps;
}

/**
* @return array
*/
public function getInstall(): array
{
return $this->install;
}

/**
* @param array|string $install
*/
public function setInstall(array|string $install): void
{
if (is_string($install)) {
$install = [
'run' => $install,
];
}

$this->install = $install;
}

/**
* @return array
*/
public function getUpdate(): array
{
return $this->update;
}

/**
* @param array|string $update
*/
public function setUpdate(array|string $update): void
{
if (is_string($update)) {
$update = [
'run' => $update,
];
}

$this->update = $update;
}

/**
* @return array
*/
public function getRemove(): array
{
return $this->remove;
}

/**
* @param array|string $remove
*/
public function setRemove(array|string $remove): void
{
if (is_string($remove)) {
$remove = [
'run' => $remove,
];
}

$this->remove = $remove;
}

}
35 changes: 35 additions & 0 deletions app/Console/Model/ToolCmdMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Model;

use Toolkit\Stdlib\Obj\AbstractObj;

/**
* class ToolCmdMeta
*
* @author inhere
* @date 2022/5/16
*/
class ToolCmdMeta extends AbstractObj
{
public string $name = '';

protected array $run = [];
protected array $deps = [];

/**
* @param array|string $deps
*/
public function setDeps(array|string $deps): void
{
$this->deps = (array)$deps;
}

/**
* @param array|string $run
*/
public function setRun(array|string $run): void
{
$this->run = (array)$run;
}
}
4 changes: 2 additions & 2 deletions app/Console/SubCmd/ToolCmd/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InstallCommand extends Command

public static function aliases(): array
{
return ['ins', 'i'];
return ['ins', 'in', 'i'];
}

protected function configure(): void
Expand Down Expand Up @@ -57,7 +57,7 @@ protected function execute(Input $input, Output $output)
}

$command = 'install';
$output->info("Will run '$command' for the tool: $name");
$output->info("Will $command the tool: $name");

$cr = $tool->buildCmdRunner($command);

Expand Down
2 changes: 1 addition & 1 deletion app/Console/SubCmd/ToolCmd/ListToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ protected function execute(Input $input, Output $output)
return;
}

$output->aList($tm->getToolsInfo(), 'Tools');
$output->aList($tm->getToolsInfo(), 'Tools management');
}
}
2 changes: 1 addition & 1 deletion app/Console/SubCmd/ToolCmd/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function execute(Input $input, Output $output)
$tool = $tm->getToolModel($name);

$command = 'update';
$output->info("Will run '$command' for the tool: $name");
$output->info("Will $command the tool: $name");

$cr = $tool->buildCmdRunner($command);

Expand Down
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@
'scripts' => require 'scripts.php',
// command aliases. element is: alias command => real command
'aliases' => require 'aliases.php',
// custom tools management
'toolManager' => [
'workdir' => '',
],
'tools' => require 'tools.php',
];
21 changes: 21 additions & 0 deletions config/tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [
'act' => [
'desc' => 'Run your GitHub Actions locally 🚀',
'deps' => 'brew', // all(install, update) required brew
// 'workdir' => '/var/tmp',
'homepage' => 'https://github.com/nektos/act',
'afterTips' => [
'install' => 'tool has been installed to the OS'
],
'install' => [
// run, command, script
'run' => 'brew install act',
// 'deps' => 'brew', // install required brew
],
'update' => [
'run' => 'brew upgrade act',
],
],
];

0 comments on commit 0c7a044

Please sign in to comment.