From c49e1e6a876353520f72da930295d7d46a0057d1 Mon Sep 17 00:00:00 2001 From: inhere Date: Sun, 1 Dec 2019 12:43:21 +0800 Subject: [PATCH] update some logic for new app and compoent --- src/Command/NewCommand.php | 63 ++++++++++++++++++++++---------- src/Creator/AbstractCreator.php | 35 +++++++++++++++--- src/Creator/ComponentCreator.php | 6 +++ src/Creator/ProjectCreator.php | 41 +++++++++++++++++++++ 4 files changed, 121 insertions(+), 24 deletions(-) diff --git a/src/Command/NewCommand.php b/src/Command/NewCommand.php index 01ecc34..cbb2bf5 100644 --- a/src/Command/NewCommand.php +++ b/src/Command/NewCommand.php @@ -9,6 +9,7 @@ use Swoft\Console\Helper\Show; use Swoft\Console\Input\Input; use Swoft\Console\Output\Output; +use SwoftLabs\Devtool\Creator\AbstractCreator; use SwoftLabs\Devtool\Creator\ComponentCreator; use SwoftLabs\Devtool\Creator\ProjectCreator; use function get_current_user; @@ -52,6 +53,10 @@ public function init(): void * "refresh", type="bool", * desc="whether remove old tmp caches before create new application" * ) + * @CommandOption( + * "no-install", type="bool", + * desc="dont run composer install after new application created" + * ) * @CommandArgument("name", type="string", desc="the new application project name", mode=Command::ARG_REQUIRED) * @param Input $input * @param Output $output @@ -59,11 +64,12 @@ public function init(): void * @example * {fullCommand} --type ws * {fullCommand} --type tcp - * {fullCommand} --repo https://github.com/UERANME/my-swoft-skeleton.git + * {fullCommand} --repo https://github.com/UESRNAME/my-swoft-skeleton.git * * Default template repos: * - * TYPE Github Repo URL + * TYPE Github Repository URL + * -----|------------------------------------------------ * http https://github.com/swoft-cloud/swoft-http-project.git * tcp https://github.com/swoft-cloud/swoft-tcp-project.git * rpc https://github.com/swoft-cloud/swoft-rpc-project.git @@ -81,23 +87,19 @@ public function application(Input $input, Output $output): void 'workDir' => $input->getWorkDir(), ]); - if ($input->boolOpt('debug')) { - $pcr->setOnExecCmd(function (string $cmd) { - Show::colored('> ' . $cmd, 'yellow'); - }); - } + $this->configCreator($pcr, $input->boolOpt('debug')); + $pcr->notifyMessage('Validate project information'); - $pcr->validate(); - if ($err = $pcr->getError()) { - $output->error($err); + if (!$pcr->validate()) { + $output->error($pcr->getError()); return; } $yes = $input->sameOpt(['y', 'yes'], false); - $output->aList($pcr->getInfo(), 'information'); + $output->aList($pcr->getInfo(), 'project information'); if (file_exists($path = $pcr->getProjectPath())) { - if (!$yes && !$output->confirm('project has been exist! delete it', false)) { + if (!$yes && !$output->confirm('project has been exist! Delete and recreate it', false)) { $output->colored('GoodBye!'); return; } @@ -119,8 +121,19 @@ public function application(Input $input, Output $output): void return; } + // composer install + if ($input->getBoolOpt('no-install')) { + $output->colored("\nCompleted!"); + return; + } + + $pcr->install(); + if ($err = $pcr->getError()) { + $output->error($err); + return; + } + $output->colored("\nCompleted!"); - $output->colored("- Project: $path created"); } /** @@ -163,11 +176,7 @@ public function component(Input $input, Output $output): void 'noLicense' => $input->getBoolOpt('no-license'), ]); - if ($input->boolOpt('debug')) { - $ccr->setOnExecCmd(function (string $cmd) { - Show::colored('> ' . $cmd, 'yellow'); - }); - } + $this->configCreator($ccr, $input->boolOpt('debug')); if (!$ccr->validate()) { $output->error($ccr->getError()); @@ -203,6 +212,22 @@ public function component(Input $input, Output $output): void } $output->colored("\nCompleted!"); - $output->colored("Component: $name created(path: $path)"); + } + + /** + * @param AbstractCreator $creator + * @param bool $debug + */ + protected function configCreator(AbstractCreator $creator, bool $debug): void + { + if ($debug) { + $creator->setOnExecCmd(function (string $cmd) { + Show::colored('> ' . $cmd, 'yellow'); + }); + } + + $creator->setOnMessage(function (string $msg) { + Show::colored('- ' . $msg); + }); } } diff --git a/src/Creator/AbstractCreator.php b/src/Creator/AbstractCreator.php index 5b1d71b..58358b4 100644 --- a/src/Creator/AbstractCreator.php +++ b/src/Creator/AbstractCreator.php @@ -38,6 +38,11 @@ abstract class AbstractCreator */ protected $onExecCmd; + /** + * @var callable + */ + protected $onMessage; + public static function new(array $config = []) { return new static($config); @@ -72,6 +77,17 @@ public function deleteDir(string $path): bool return $this->exec($cmd); } + /** + * @param string $msg + * @param array $args + */ + public function notifyMessage(string $msg, array $args = []): void + { + if ($cb = $this->onMessage) { + $cb($msg, $args); + } + } + /** * @param string $cmd * @@ -83,7 +99,8 @@ public function exec(string $cmd): bool $ret = Coroutine::exec($cmd); if ((int)$ret['code'] !== 0) { - $msg = $ret['output']; + $msg = $ret['output']; + $this->error = 'exec command fail' . ($msg ? ': ' . $msg : ''); return false; } @@ -96,7 +113,7 @@ public function exec(string $cmd): bool * * @return void */ - public function notifyCmdExec(string $cmd): void + protected function notifyCmdExec(string $cmd): void { if ($cb = $this->onExecCmd) { $cb($cmd); @@ -142,7 +159,7 @@ public function getError(): string } /** - * Get new prject name + * Get new project name * * @return string */ @@ -152,9 +169,9 @@ public function getName(): string } /** - * Set new prject name + * Set new project name * - * @param string $name new prject name + * @param string $name new project name * * @return self */ @@ -166,4 +183,12 @@ public function setName(string $name): self return $this; } + + /** + * @param callable $onMessage + */ + public function setOnMessage(callable $onMessage): void + { + $this->onMessage = $onMessage; + } } diff --git a/src/Creator/ComponentCreator.php b/src/Creator/ComponentCreator.php index 3ada0fa..feb5a62 100644 --- a/src/Creator/ComponentCreator.php +++ b/src/Creator/ComponentCreator.php @@ -103,6 +103,7 @@ public function create(): void return; } + $this->notifyMessage('Create component dir: ' . $path); Dir::make($path); $files = [ @@ -140,7 +141,11 @@ public function create(): void 'escapePkgNamespace' => str_replace('\\', '\\\\', $this->namespace), ]); + $this->notifyMessage('Create directory structure and base files'); + $this->createFiles($renderer, $files); + + $this->notifyMessage("Component: {$this->name} created(path: $path)"); } protected function createFiles(FileRenderer $renderer, array $files): void @@ -180,6 +185,7 @@ protected function createFiles(FileRenderer $renderer, array $files): void if ($info['render'] ?? false) { $renderer->setTplFile($tplFile); + /** @noinspection PhpUnhandledExceptionInspection */ $renderer->renderAs($dstFile); } else { $this->writeFile($dstFile, $content); diff --git a/src/Creator/ProjectCreator.php b/src/Creator/ProjectCreator.php index c884776..2a5e770 100644 --- a/src/Creator/ProjectCreator.php +++ b/src/Creator/ProjectCreator.php @@ -104,6 +104,9 @@ public function validate(): bool return true; } + /** + * @return array + */ public function getInfo(): array { $info = [ @@ -130,6 +133,7 @@ public function create(): void return; } + $this->notifyMessage('Begin create the new project: ' . $this->getName()); $tmpDir = Sys::getTempDir() . '/swoft-app-demos'; if (!file_exists($tmpDir)) { Dir::make($tmpDir, 0775); @@ -148,24 +152,61 @@ public function create(): void } if (!$hasExist) { + $this->notifyMessage('Clone Repo ' . $this->repoUrl); + $cmd = "cd $tmpDir && git clone --depth 1 {$this->repoUrl}"; if (!$this->exec($cmd)) { return; } } + $this->notifyMessage('Copy project files from local cache'); + + $this->notifyMessage('Remove .git directory on new project'); $cmd = "cp -R $dirPath $path && rm -rf {$path}/.git"; if (!$this->exec($cmd)) { return; } + + $this->notifyMessage("Project: $path created"); + } + + /** + * install by composer + * + * @param bool $noDev + */ + public function install(bool $noDev = false): void + { + $this->notifyMessage('Begin run composer install for init project'); + + $path = $this->projectPath; + $cmd = "cd $path && composer install"; + + if ($noDev) { + $cmd .= ' --no-dev'; + } + + if (!$this->exec($cmd)) { + return; + } + + $this->notifyMessage('Composer packages install complete'); } + /** + * @param string $path + * + * @return bool + */ public function deleteDir(string $path): bool { if (strlen($path) < 6) { throw new InvalidArgumentException('path is to short, cannot exec rm', 500); } + $this->notifyMessage('Delete Dir: ' . $path); + $cmd = "rm -rf $path"; return $this->exec($cmd); }