Skip to content

Commit

Permalink
update some logic for new app and compoent
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 1, 2019
1 parent 1ae31c3 commit c49e1e6
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 24 deletions.
63 changes: 44 additions & 19 deletions src/Command/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,18 +53,23 @@ 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
*
* @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
*
* <info>Default template repos:</info>
*
* 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
Expand All @@ -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;
}
Expand All @@ -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");
}

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
});
}
}
35 changes: 30 additions & 5 deletions src/Creator/AbstractCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ abstract class AbstractCreator
*/
protected $onExecCmd;

/**
* @var callable
*/
protected $onMessage;

public static function new(array $config = [])
{
return new static($config);
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -142,7 +159,7 @@ public function getError(): string
}

/**
* Get new prject name
* Get new project name
*
* @return string
*/
Expand All @@ -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
*/
Expand All @@ -166,4 +183,12 @@ public function setName(string $name): self

return $this;
}

/**
* @param callable $onMessage
*/
public function setOnMessage(callable $onMessage): void
{
$this->onMessage = $onMessage;
}
}
6 changes: 6 additions & 0 deletions src/Creator/ComponentCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function create(): void
return;
}

$this->notifyMessage('Create component dir: ' . $path);
Dir::make($path);

$files = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions src/Creator/ProjectCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public function validate(): bool
return true;
}

/**
* @return array
*/
public function getInfo(): array
{
$info = [
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit c49e1e6

Please sign in to comment.