Skip to content

Commit

Permalink
feat: support dynmaic load project info.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 28, 2021
1 parent cc80470 commit d335d0b
Show file tree
Hide file tree
Showing 19 changed files with 700 additions and 2,475 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM php:7.4-alpine

FROM php:7.4-alpine

COPY --from=0 /home/worker/build ./
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ kite -h

## git 常用命令

```bash
kite git {command} [arguments ...] [--options ...]
```

## gitlab 常用命令

```bash
kite gitlab {command} [arguments ...] [--options ...]
```

## 其他工具命令

**env**
Expand Down
54 changes: 36 additions & 18 deletions app/Common/CmdRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Toolkit\Cli\Color;
use Toolkit\Sys\Exec;
use function is_array;
use function is_string;
use function sprintf;
use function trim;

Expand Down Expand Up @@ -76,25 +77,30 @@ class CmdRunner
private $printOutput = false;

/**
* @param string $cmd
* @param string|array $cmd
* @param string $workDir
*
* @return static
*/
public static function new(string $cmd = '', string $workDir = ''): self
public static function new($cmd = '', string $workDir = ''): self
{
return new self($cmd, $workDir);
}

/**
* Class constructor.
*
* @param string $command
* @param string|array $command One or multi commands
* @param string $workDir
*/
public function __construct(string $command = '', string $workDir = '')
public function __construct($command = '', string $workDir = '')
{
$this->command = $command;
if (is_string($command)) {
$this->command = $command;
} elseif (is_array($command)) {
$this->commands = $command;
}

$this->workDir = $workDir;
}

Expand Down Expand Up @@ -130,7 +136,7 @@ public function changeDir(string $workDir): self
*/
public function exec(bool $printOutput = false): array
{
$this->do($printOutput);
$this->run($printOutput);

return $this->getResult();
}
Expand All @@ -142,10 +148,7 @@ public function exec(bool $printOutput = false): array
*/
public function do(bool $printOutput = false): self
{
$this->printOutput = $printOutput;
$this->execute($this->command, $this->workDir);

return $this;
return $this->run($printOutput);
}

/**
Expand Down Expand Up @@ -190,9 +193,7 @@ public function afterOkDo(string $cmd, string $workDir = null): self
$this->workDir = $workDir;
}

$this->command = $cmd;

return $this->do($this->printOutput);
return $this->setCommand($cmd)->do($this->printOutput);
}

/**
Expand Down Expand Up @@ -324,9 +325,28 @@ public function run(bool $printOutput = false): self
{
$this->printOutput = $printOutput;

if ($command = $this->command) {
$this->innerExecute($command, $this->workDir);

// stop on error
if (0 !== $this->code && false === $this->ignoreError) {
Color::println("\nCommand exit code not equal to 0(code: {$this->code}), stop run.", 'red');
return $this;
}
}

if ($commands = $this->commands) {
$this->runCommands($commands);
}

return $this;
}

private function runCommands(array $commands): void
{
Color::println('Starting Handle', 'suc');
$step = 1;
foreach ($this->commands as $command) {
foreach ($commands as $command) {
$workDir = $this->workDir;

// see addWhere()
Expand All @@ -351,7 +371,7 @@ public function run(bool $printOutput = false): self
Color::println('- work dir is ' . $workDir, 'italic');
}

$this->execute($command, $workDir);
$this->innerExecute($command, $workDir);
$step++;

// stop on error
Expand All @@ -360,8 +380,6 @@ public function run(bool $printOutput = false): self
break;
}
}

return $this;
}

/**************************************************************************
Expand All @@ -372,7 +390,7 @@ public function run(bool $printOutput = false): self
* @param string $command
* @param string $workDir
*/
protected function execute(string $command, string $workDir): void
protected function innerExecute(string $command, string $workDir): void
{
if (!$command) {
throw new RuntimeException('The execute command cannot be empty');
Expand Down
160 changes: 82 additions & 78 deletions app/Common/GitLocal/AbstractGitLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Inhere\Kite\Common\GitLocal;

use Inhere\Console\IO\Output;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Helper\GitUtil;
use PhpGit\Info\RemoteInfo;
use PhpGit\Repo;
use RuntimeException;
use Toolkit\Cli\Color;
use Toolkit\Stdlib\Obj;
Expand All @@ -24,6 +25,11 @@ abstract class AbstractGitLocal
{
public const GITHUB_HOST = 'https://github.com';

/**
* @var Repo
*/
protected $repo;

/**
* @var string
*/
Expand Down Expand Up @@ -52,6 +58,8 @@ abstract class AbstractGitLocal
protected $output;

/**
* default remote name
*
* @var string
*/
protected $remote = 'origin';
Expand Down Expand Up @@ -134,6 +142,18 @@ public function __construct(Output $output = null, array $config = [])
$this->init($config);
}

/**
* @return Repo
*/
public function getRepo(): Repo
{
if (!$this->repo) {
$this->repo = Repo::new($this->workDir);
}

return $this->repo;
}

/**
* @param array $config
*/
Expand Down Expand Up @@ -161,6 +181,46 @@ public function clone(): array
return [];
}

/**
* @return string
*/
public function findProjectName(): string
{
$pjName = '';

$info = $this->getRemoteInfo();
$path = $info->path;

if ($path) {
// TODO check host is: github OR gitlab.
$pjName = $path;

// try padding some info
if (isset($this->projects[$path])) {
if (!isset($this->projects[$path]['forkGroup'])) {
$this->projects[$path]['forkGroup'] = $info->group;
}
if (!isset($this->projects[$path]['repo'])) {
$this->projects[$path]['repo'] = $info->repo;
}
}

$this->output->liteNote('auto parse project info from git remote url');
}

if (!$pjName) {
$dirName = $this->getDirName();

// try auto parse project name for dirname.
if (isset($this->projects[$dirName])) {
$pjName = $dirName;
$this->output->liteNote('auto parse project name from dirname.');
}
}

return $pjName;
}

/**
* @return string
*/
Expand Down Expand Up @@ -238,91 +298,18 @@ public function setRemote(string $remote): self
return $this;
}

/**
* @param string $remote
*
* @return $this
*/
public function parseRemote(string $remote = ''): self
{
if ($remote) {
$this->setRemote($remote);
}

Color::println('find and parse remote info ...', 'normal');
$str = 'git remote get-url --push ' . $this->remote;
$run = CmdRunner::new($str, $this->workDir);
$url = $run->do()->getOutput(true);
if ($run->isFail()) {
throw new RuntimeException($run->getError(), 500);
}

// git@gitlab.my.com:group/some-lib.git
if (strpos($url, 'git@') === 0) {
if (substr($url, -4) === '.git') {
$url = substr($url, 4, -4);
} else {
$url = substr($url, 4);
}

// $url = gitlab.my.com:group/some-lib
[$host, $path] = explode(':', $url, 2);
[$group, $repo] = explode('/', $path, 2);

$this->curRepo = $repo;
if ($this->remote === $this->getDefaultGroupName()) {
$this->curMainGroup = '';
}

$this->curForkGroup = $group;
$this->remoteInfo = [
'host' => $host,
'path' => $path,
'url' => $url,
'group' => $group,
'repo' => $repo,
];
} else { // eg: "https://github.com/ulue/swoft-component.git"
$info = parse_url($url);
// add
$info['url'] = $url;

$uriPath = $info['path'];
if (substr($uriPath, -4) === '.git') {
$uriPath = substr($uriPath, 0, -4);
}

$info['path'] = trim($uriPath, '/');

[$group, $repo] = explode('/', $info['path'], 2);
$info['group'] = $group;

// TODO
// $this->curGroup = $group;
$this->curRepo = $repo;

$this->curForkGroup = $group;
$this->remoteInfo = $info;
}

return $this;
}

/**
* @param string $pjName
*
* @return $this
*/
public function loadCurPjInfo(string $pjName = ''): self
public function loadProjectInfo(string $pjName = ''): self
{
if ($pjName) {
$this->setCurPjName($pjName);
}

$pjName = $this->curPjName;
if (!isset($this->projects[$pjName])) {
throw new RuntimeException("project '{$pjName}' is not found in the projects");
}

$defaultInfo = [
'name' => $pjName,
Expand All @@ -331,6 +318,21 @@ public function loadCurPjInfo(string $pjName = ''): self
'forkGroup' => $this->getValue('defaultForkGroup', ''),
];

// not exist. dynamic add
if (!isset($this->projects[$pjName])) {
// throw new RuntimeException("project '{$pjName}' is not found in the projects");
$info = $this->getRemoteInfo();
if ($info->isInvalid()) {
throw new RuntimeException("dynamic load project '{$pjName}' fail. not found git remote info");
}

$this->projects[$pjName] = [
'dynamic' => true,
'forkGroup' => $info->group,
'repo' => $info->repo,
];
}

$this->curPjInfo = array_merge($defaultInfo, $this->projects[$pjName]);
// set current repo
if (!$this->curRepo) {
Expand Down Expand Up @@ -475,11 +477,13 @@ public function getDirName(): string
}

/**
* @return array
* @param string $remote
*
* @return RemoteInfo
*/
public function getRemoteInfo(): array
public function getRemoteInfo(string $remote = ''): RemoteInfo
{
return $this->remoteInfo;
return $this->getRepo()->getRemoteInfo($remote);
}

/**
Expand Down
Loading

0 comments on commit d335d0b

Please sign in to comment.