-
Notifications
You must be signed in to change notification settings - Fork 48
v4 create group
Inhere edited this page Oct 18, 2021
·
6 revisions
先看一个代码示例,来自我的项目 inhere/kite
<?php declare(strict_types=1);
namespace Inhere\Kite\Console\Controller;
use Inhere\Console\Controller;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Common\Cmd;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use PhpGit\Changelog\Filter\KeywordsFilter;
use PhpGit\Changelog\Formatter\GithubReleaseFormatter;
use PhpGit\Changelog\Formatter\SimpleFormatter;
use PhpGit\Changelog\GitChangeLog;
use PhpGit\Git;
use PhpGit\Info\TagsInfo;
use PhpGit\Repo;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Obj\ConfigObject;
use Toolkit\Stdlib\Str;
/**
* Class GitController
*/
class GitController extends Controller
{
protected static $name = 'git';
protected static $description = 'Provide useful tool commands for quick use git';
/**
* @var ConfigObject
*/
private $settings;
public static function aliases(): array
{
return ['g'];
}
protected static function commandAliases(): array
{
return [
'changelog' => ['chlog', 'clog', 'cl'],
'log' => ['l', 'lg'],
];
}
/**
* @return string[]
*/
protected function options(): array
{
return [
'--dry-run' => 'bool;Dry-run the workflow, dont real execute',
'-y, --yes' => 'Direct execution without confirmation',
// '-i, --interactive' => 'Run in an interactive environment[TODO]',
];
}
protected function beforeRun(): void
{
if ($this->app && !$this->settings) {
$this->settings = ConfigObject::new($this->app->getArrayParam('git'));
}
}
/**
* display recently git commits information by `git log`
*
* @arguments
* maxCommit int;Max display how many commits;;15
*
* @options
* --abbrev-commit Only display the abbrev commit ID
* --exclude Exclude contains given sub-string. multi by comma split.
* --file Export changelog message to file
* --format The git log option `--pretty` value.
* can be one of oneline, short, medium, full, fuller, reference, email, raw, format:<string> and tformat:<string>.
* --max-commit int;Max display how many commits
* --no-color bool;Dont use color render git output
* --no-merges bool;No contains merge request logs
*
* @param FlagsParser $fs
* @param Output $output
*/
public function logCommand(FlagsParser $fs, Output $output): void
{
$b = Git::new()->newCmd('log');
$noColor = $fs->getOpt('no-color');
$exclude = $fs->getOpt('exclude');
$noMerges = $fs->getOpt('no-merges');
$abbrevID = $fs->getOpt('abbrev-commit');
$maxCommit = $fs->getOpt('max-commit', $fs->getArg('maxCommit'));
// git log --color --graph --pretty=format:'%Cred%h%Creset:%C(ul yellow)%d%Creset %s (%Cgreen%cr%Creset, %C(bold blue)%an%Creset)' --abbrev-commit -10
$b->add('--graph');
$b->addIf('--color', !$noColor);
$b->add('--pretty=format:"%Cred%h%Creset:%C(ul yellow)%d%Creset %s (%Cgreen%cr%Creset, %C(bold blue)%an%Creset)"');
$b->addIf("--exclude=$exclude", $exclude);
$b->addIf('--abbrev-commit', $abbrevID);
$b->addIf('--no-merges', $noMerges);
$b->add('-' . abs($maxCommit));
$b->runAndPrint();
$output->success('Complete');
}
}
我的其他PHP项目
- inhere/kite 方便本地开发和使用的个人CLI工具应用
- php-toolkit/pflag PHP编写的,通用的命令行标志(选项和参数)解析库
- phppkg/easytpl 使用简单且快速的 PHP 模板引擎
- inhere/php-validate 一个简洁小巧且功能完善的php验证库
- inhere/sroute 轻量且快速的HTTP请求路由库