Skip to content

Commit

Permalink
update some for git command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 28, 2020
1 parent f6f693b commit 6223cc1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 47 deletions.
31 changes: 10 additions & 21 deletions app/Common/CmdRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class CmdRunner
*/
private $output = '';

/**
* @var bool
*/
private $printOutput = false;

/**
* @param string $cmd
* @param string $workDir
Expand All @@ -61,26 +56,31 @@ public function __construct(string $cmd, string $workDir = '')
}

/**
* @param bool $printOutput
*
* @return array
*/
public function exec(): array
public function exec(bool $printOutput = false): array
{
$this->do();
$this->do($printOutput);

return $this->getResult();
}

/**
* @param bool $printOutput
*
* @return $this
*/
public function do(): self
public function do(bool $printOutput = false): self
{
$ret = SysCmd::exec($this->cmd, $this->workDir);
// $ret = SysCmd::exec($this->cmd, $this->workDir);
$ret = SysCmd::exec2($this->cmd, $this->workDir);

$this->code = $ret['code'];
$this->output = $ret['output'];

if ($this->printOutput) {
if ($printOutput) {
echo $this->output;
}

Expand Down Expand Up @@ -153,15 +153,4 @@ public function getResult(): array
'output' => $this->output,
];
}

/**
* @param bool $printOutput
*
* @return CmdRunner
*/
public function setPrintOutput(bool $printOutput): CmdRunner
{
$this->printOutput = $printOutput;
return $this;
}
}
4 changes: 3 additions & 1 deletion app/Console/Command/UpdateSelfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class UpdateSelfCommand extends Command
*/
protected $repoDir;

/**
* @return string[]
*/
public static function aliases(): array
{
return ['upself', 'update-self'];
Expand All @@ -47,7 +50,6 @@ protected function init(): void
}

/**
* do execute
* @param Input $input
* @param Output $output
*/
Expand Down
33 changes: 25 additions & 8 deletions app/Console/Group/GitFlowGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class GitFlowGroup extends Controller
*/
private $config = [];

/**
* @var string
*/
private $curBranchName = '';

public static function aliases(): array
{
return ['git-flow', 'gf'];
Expand All @@ -39,7 +44,9 @@ protected function configure(): void
$action = $this->getAction();

if ($action === 'sync') {
$this->addCommentsVar('curBranchName', GitUtil::getCurrentBranchName());
$this->curBranchName = GitUtil::getCurrentBranchName();

$this->addCommentsVar('curBranchName', $this->curBranchName);
}
}

Expand All @@ -59,25 +66,35 @@ protected function beforeExecute(): bool
* @options
* -b, --branch The sync code branch name, default is current branch
*
* @param Input $input
* @param Input $input
* @param Output $output
*
* @example
* {binWithCmd} Sync code from the main repo remote {curBranchName} branch
* {binWithCmd} master Sync code from the main repo remote master branch
* {binWithCmd} Sync code from the main repo remote {curBranchName} branch
* {binWithCmd} -b master Sync code from the main repo remote master branch
*
*/
public function syncCommand(Input $input, Output $output): void
{
$output->aList([
'Work Dir' => $input->getPwd(),
], 'Work Information');
$pwd = $input->getPwd();
$info = [
'Work Dir' => $pwd,
];
$output->aList($info, 'Work Information');

$curBranch = GitUtil::getCurrentBranchName();
if (!$curBranch = $this->curBranchName) {
$curBranch = GitUtil::getCurrentBranchName();
}

$forkRemote = $this->config['fork']['remote'];
$mainRemote = $this->config['main']['remote'];

$remotes = GitUtil::getRemotes($pwd);
if (!isset($remotes[$mainRemote])) {
$output->liteError("The remote '{$mainRemote}' is not exists");
return;
}

// git pull main BRANCH
$str = "git pull {$mainRemote} $curBranch";
$cmd = CmdRunner::new($str);
Expand Down
31 changes: 31 additions & 0 deletions app/Helper/GitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Inhere\Kite\Common\CmdRunner;
use Toolkit\Cli\Color;
use Toolkit\Sys\Sys;
use function array_filter;
use function explode;
use function sprintf;
use function trim;

Expand Down Expand Up @@ -52,6 +54,35 @@ public static function getLatestCommitId(string $workDir = ''): string
return $cmd->do()->getOutput(true);
}

/**
* @param string $workDir
*
* @return array
*/
public static function getRemotes(string $workDir = ''): array
{
// git remote -v
$cmd = 'git remote -v';
$run = CmdRunner::new($cmd, $workDir);
$out = $run->do(false)->getOutput(true);

if (!$out) {
return [];
}

// parse
$lines = explode("\n", $out);

$remotes = [];
foreach ($lines as $line) {
[$name, $url] = array_filter(explode(' ', trim($line)));
// add
$remotes[$name] = $url;
}

return $remotes;
}

/**
* @param string $message
*/
Expand Down
31 changes: 14 additions & 17 deletions app/Helper/SysCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,56 +65,53 @@ public static function exec(string $cmd, string $workDir = '', bool $coRun = fal
}

/**
* Method to execute a command in the sys
* Method to execute a command in the system
* Uses :
* 1. system
* 2. passthru
* 3. exec
* 4. shell_exec
*
* @param string $command
* @param bool $returnStatus
* @param string|null $cwd
* @param string $command
* @param string $workDir
* @param bool $returnStatus
*
* @return array|string
*/
public static function exec2(string $command, bool $returnStatus = true, string $cwd = null)
public static function exec2(string $command, string $workDir = '', bool $returnStatus = true)
{
$output = '';
$exitCode = 1;

if ($cwd) {
chdir($cwd);
if ($workDir) {
chdir($workDir);
}

// system
$status = 1;
if (function_exists('system')) {
ob_start();
system($command, $exitCode);
system($command, $status);
$output = ob_get_clean();

// passthru
} elseif (function_exists('passthru')) {
ob_start();
passthru($command, $exitCode);
passthru($command, $status);
$output = ob_get_clean();
// exec
} elseif (function_exists('exec')) {
exec($command, $output, $exitCode);
$output = implode("\n", $output);
exec($command, $outputs, $status);
$output = implode("\n", $outputs);

// shell_exec
} elseif (function_exists('shell_exec')) {
$output = shell_exec($command);
} else {
$output = 'Command execution not possible on this system';
$exitCode = 0;
$output = 'Command execution not possible on this system';
}

if ($returnStatus) {
return [
'code' => $status,
'output' => trim($output),
'status' => $exitCode
];
}

Expand Down

0 comments on commit 6223cc1

Please sign in to comment.