Skip to content

Commit

Permalink
fix: cli-utils App get opt error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 1, 2019
1 parent b739899 commit c706e9d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 29 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"Toolkit\\Traits\\": "libs/helper-utils/src/Traits"
}
},
"scripts": {
"test-cli": "phpunit -c libs/cli-utils/phpunit.xml.dist"
},
"suggest": {
"inhere/php-validate": "Very lightweight data validate tool",
"inhere/console": "a lightweight php console application library."
Expand Down
11 changes: 10 additions & 1 deletion libs/cli-utils/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(array $argv = null)
// get script file
$this->script = array_shift($argv);
// parse flags
[$this->args, $this->opts] = Flags::parseArgv($argv);
[$this->args, $this->opts] = Flags::parseArgv($argv, ['mergeOpts' => true]);
}

/**
Expand Down Expand Up @@ -207,6 +207,15 @@ protected function handleException(Throwable $e): int
return $code;
}

/**
* @param callable $handler
* @param array $config
*/
public function addByConfig(callable $handler, array $config): void
{
$this->addCommand($config['name'], $handler, $config);
}

/**
* @param string $command
* @param callable $handler
Expand Down
5 changes: 4 additions & 1 deletion libs/cli-utils/src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ public static function supportColor(): bool
public static function isSupportColor(): bool
{
if (DIRECTORY_SEPARATOR === '\\') {
return '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD || false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
return '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD ||
false !== getenv('ANSICON') ||
'ON' === getenv('ConEmuANSI') ||
'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
;
}

Expand Down
7 changes: 6 additions & 1 deletion libs/cli-utils/src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public static function simpleParseArgv(array $argv): array
* @param array $config
*
* @return array [args, short-opts, long-opts]
* If 'mergeOpts' is True, will return [args, opts]
*/
public static function parseArgv(array $params, array $config = []): array
{
Expand All @@ -113,7 +114,7 @@ public static function parseArgv(array $params, array $config = []): array
// List of parameters without values(bool option keys)
'boolOpts' => [], // ['debug', 'h']
// Whether merge short-opts and long-opts
// 'mergeOpts' => false,
'mergeOpts' => false,
// want parsed options. if not empty, will ignore no matched
'wantParsedOpts' => [],
// list of option allow array values.
Expand Down Expand Up @@ -198,6 +199,10 @@ public static function parseArgv(array $params, array $config = []): array
}
}

if ($config['mergeOpts']) {
return [$args, array_merge($sOpts, $lOpts)];
}

return [$args, $sOpts, $lOpts];
}

Expand Down
38 changes: 38 additions & 0 deletions libs/cli-utils/test/FlagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Toolkit\CliTest;

use PHPUnit\Framework\TestCase;
use function explode;
use Toolkit\Cli\Flags;

/**
* Class FlagsTest
*
* @package Toolkit\CliTest
*/
class FlagsTest extends TestCase
{
public function testParseArgv(): void
{
$rawArgv = explode(' ', 'git:tag --only-tag -d ../view arg0');

[$args, $sOpts, $lOpts] = Flags::parseArgv($rawArgv);

$this->assertNotEmpty($args);
$this->assertSame('git:tag', $args[0]);
$this->assertSame('arg0', $args[1]);

$this->assertSame('../view', $sOpts['d']);
$this->assertTrue($lOpts['only-tag']);

[$args, $opts] = Flags::parseArgv($rawArgv, ['mergeOpts' => true]);

$this->assertNotEmpty($args);
$this->assertSame('git:tag', $args[0]);
$this->assertSame('arg0', $args[1]);

$this->assertSame('../view', $opts['d']);
$this->assertTrue($opts['only-tag']);
}
}
63 changes: 37 additions & 26 deletions libs/dev-helper/Console/DevController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@
use Inhere\Console\Controller;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use RuntimeException;
use Toolkit\Sys\Sys;
use function array_filter;
use function array_intersect;
use function count;
use function defined;
use function implode;
use function is_dir;
use function is_file;
use function sprintf;
use function str_pad;
use const PHP_EOL;

/**
* Internal tool for toolkit development
Expand Down Expand Up @@ -54,7 +65,7 @@ public function listCommand(Input $input, Output $output): int
{
$this->checkEnv();

$output->colored('Components Total: ' . \count($this->components));
$output->colored('Components Total: ' . count($this->components));

$buffer = [];
$showRepo = (bool)$input->getOpt('show-repo');
Expand All @@ -65,9 +76,9 @@ public function listCommand(Input $input, Output $output): int
continue;
}

$remote = \sprintf($this->gitUrl, self::TYPE_HTTPS, $component);
$component = \str_pad($component, 20);
$buffer[] = \sprintf(' <comment>%s</comment> - %s', $component, $remote);
$remote = sprintf($this->gitUrl, self::TYPE_HTTPS, $component);
$component = str_pad($component, 20);
$buffer[] = sprintf(' <comment>%s</comment> - %s', $component, $remote);
}

$output->writeln($buffer);
Expand All @@ -93,7 +104,7 @@ public function listCommand(Input $input, Output $output): int
* @param Input $input
* @param Output $output
* @return int
* @throws \RuntimeException
* @throws RuntimeException
*/
public function addCommand(Input $input, Output $output): int
{
Expand All @@ -108,7 +119,7 @@ public function addCommand(Input $input, Output $output): int
$config['onExec'] = function (string $name) use ($output) {
$libPath = $this->componentDir . '/libs/' . $name;

if (\is_dir($libPath)) {
if (is_dir($libPath)) {
$output->liteWarning("Component cannot be repeat add: $name");

return false;
Expand Down Expand Up @@ -138,7 +149,7 @@ public function addCommand(Input $input, Output $output): int
* @param Input $input
* @param Output $output
* @return int
* @throws \RuntimeException
* @throws RuntimeException
*/
public function pullCommand(Input $input, Output $output): int
{
Expand Down Expand Up @@ -174,7 +185,7 @@ public function pullCommand(Input $input, Output $output): int
* @param Input $input
* @param Output $output
* @return int
* @throws \RuntimeException
* @throws RuntimeException
*/
public function pushCommand(Input $input, Output $output): int
{
Expand All @@ -194,32 +205,32 @@ public function pushCommand(Input $input, Output $output): int
* @param Output $output
* @param array $config
* @return int
* @throws \RuntimeException
* @throws RuntimeException
*/
protected function runGitSubtree(Input $input, Output $output, array $config): int
{
$this->checkEnv();
$output->writeln("<comment>Component Directory</comment>:\n $this->componentDir");

$operate = $config['operate'];
$names = \array_filter($input->getArgs(), 'is_int', ARRAY_FILTER_USE_KEY);
$names = array_filter($input->getArgs(), 'is_int', ARRAY_FILTER_USE_KEY);

if ($names) {
$back = $names;
$names = \array_intersect($names, $this->components);
$names = array_intersect($names, $this->components);

if (!$names) {
throw new \RuntimeException('Invalid component name entered: ' . \implode(', ', $back));
throw new RuntimeException('Invalid component name entered: ' . implode(', ', $back));
}
} elseif ($input->getSameOpt(['a', 'all'], false)) {
$names = $this->components;
} else {
throw new \RuntimeException('Please enter the name of the component that needs to be operated');
throw new RuntimeException('Please enter the name of the component that needs to be operated');
}

$output->writeln([
"<comment>{$config['operatedNames']}</comment>:",
' <info>' . \implode(', ', $names) . '</info>'
' <info>' . implode(', ', $names) . '</info>'
]);

$doneOne = ' OK';
Expand All @@ -240,8 +251,8 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
}

$ret = null;
$remote = \sprintf($this->gitUrl, $protoHost, $name);
$command = \sprintf('git subtree %s --prefix=libs/%s %s master%s', $operate, $name, $remote, $squash);
$remote = sprintf($this->gitUrl, $protoHost, $name);
$command = sprintf('git subtree %s --prefix=libs/%s %s master%s', $operate, $name, $remote, $squash);

$output->writeln("> <cyan>$command</cyan>");
$output->write("{$config['doing']} '$name' ...", false);
Expand All @@ -251,18 +262,18 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
[$code, $ret, $err] = Sys::run($command, $workDir);

if ($code !== 0) {
throw new \RuntimeException("Exec command failed. command: $command error: $err \nreturn: \n$ret");
throw new RuntimeException("Exec command failed. command: $command error: $err \nreturn: \n$ret");
}
}

$output->colored($doneOne, 'success');

if ($ret && $input->getOpt('show-result')) {
$output->writeln(\PHP_EOL . $ret);
$output->writeln(PHP_EOL . $ret);
}
}

$output->colored(\sprintf($config['done'], \count($names)), 'success');
$output->colored(sprintf($config['done'], count($names)), 'success');

return 0;
}
Expand All @@ -284,7 +295,7 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
* @param Input $input
* @param Output $output
* @return int
* @throws \RuntimeException
* @throws RuntimeException
*/
public function genApiCommand(Input $input, Output $output): int
{
Expand All @@ -298,7 +309,7 @@ public function genApiCommand(Input $input, Output $output): int
return -1;
}

if (!\is_file($samiPath)) {
if (!is_file($samiPath)) {
$output->colored('The sami.phar file is not exists! File: ' . $samiPath, 'error');

return -1;
Expand All @@ -313,7 +324,7 @@ public function genApiCommand(Input $input, Output $output): int
}

// php ~/Workspace/php/tools/sami.phar render --force
$command = \sprintf(
$command = sprintf(
'php ~/Workspace/php/tools/sami.phar %s %s%s',
'update',
$config,
Expand All @@ -327,11 +338,11 @@ public function genApiCommand(Input $input, Output $output): int
[$code, $ret,] = Sys::run($command, $workDir);

if ($code !== 0) {
throw new \RuntimeException("Exec command failed. command: $command return: \n$ret");
throw new RuntimeException("Exec command failed. command: $command return: \n$ret");
}

if ($input->getOpt('show-result')) {
$output->writeln(\PHP_EOL . $ret);
$output->writeln(PHP_EOL . $ret);
}
}

Expand All @@ -342,15 +353,15 @@ public function genApiCommand(Input $input, Output $output): int

private function checkEnv(): void
{
if (!\defined('TOOLKIT_DIR') || !TOOLKIT_DIR) {
if (!defined('TOOLKIT_DIR') || !TOOLKIT_DIR) {
$this->writeln('<error>Missing the TOOLKIT_DIR define</error>', true);
}

$this->componentDir = TOOLKIT_DIR;

$file = TOOLKIT_DIR . '/components.inc';

if (!\is_file($file)) {
if (!is_file($file)) {
$this->writeln('<error>Missing the components config.</error>', true);
}

Expand Down

0 comments on commit c706e9d

Please sign in to comment.