diff --git a/bin/InitAppCommand.php b/bin/InitAppCommand.php index 2a1dec4..5ed5b00 100644 --- a/bin/InitAppCommand.php +++ b/bin/InitAppCommand.php @@ -1,7 +1,7 @@ getArgValue('--dir'); $entry = $this->getArgValue('--entry'); + if ($entry === null) { $entry = $dirName; } - - + + if (defined('ROOT_DIR')) { $appPath = ROOT_DIR.DIRECTORY_SEPARATOR.$dirName; } else { $appPath = substr(__DIR__, 0, strlen(__DIR__) - strlen('vendor\webfiori\cli\bin')).$dirName; } - + try { $this->createAppClass($appPath, $dirName); $this->createEntryPoint($appPath, $dirName, $entry); $this->success('App created successfully.'); + return 0; } catch (Exception $ex) { $this->error('Unable to initialize due to an exception:'); $this->println($ex->getCode().' - '.$ex->getMessage()); + return -1; } } - private function createEntryPoint(string $appPath, string $dir, string $eName) { - $this->println('Creating "'.$dir.'/'.$eName.'"...'); - $file = new File($eName, $appPath); - if (!$file->isExist()) { - $data = "#!/usr/bin/env php\n" - ."create(true); - file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data); - return true; - } - $this->warning('File '.$eName.' already exist!'); - } private function createAppClass(string $appPath, string $dirName) { $this->println('Creating "'.$dirName.'/app.php"...'); $file = new File($appPath.DIRECTORY_SEPARATOR.'app.php'); + if (!$file->isExist()) { $file->append("append("namespace $dirName;\n\n"); @@ -74,8 +64,24 @@ private function createAppClass(string $appPath, string $dirName) { $file->append("exit(\$runner->start());\n\n"); $file->create(true); $file->write(false); + return true; } $this->warning('File app.php already exist!'); } + private function createEntryPoint(string $appPath, string $dir, string $eName) { + $this->println('Creating "'.$dir.'/'.$eName.'"...'); + $file = new File($eName, $appPath); + + if (!$file->isExist()) { + $data = "#!/usr/bin/env php\n" + ."create(true); + file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data); + + return true; + } + $this->warning('File '.$eName.' already exist!'); + } } diff --git a/bin/app.php b/bin/app.php index e32397c..814187e 100644 --- a/bin/app.php +++ b/bin/app.php @@ -1,13 +1,13 @@ register(new HelpCommand()); -$runner->register(new InitAppCommand()); -$runner->setDefaultCommand('help'); -exit($runner->start()); \ No newline at end of file +exit($runner->register(new HelpCommand()) + ->register(new InitAppCommand()) + ->setDefaultCommand('help') + ->start()); diff --git a/webfiori/cli/Argument.php b/webfiori/cli/Argument.php index 611700d..161140c 100644 --- a/webfiori/cli/Argument.php +++ b/webfiori/cli/Argument.php @@ -47,7 +47,7 @@ public function addAllowedValue(string $val) : Argument { if (!in_array($trim, $this->getAllowedValues())) { $this->allowedValues[] = $trim; } - + return $this; } /** diff --git a/webfiori/cli/CLICommand.php b/webfiori/cli/CLICommand.php index a1baf01..9dc6563 100644 --- a/webfiori/cli/CLICommand.php +++ b/webfiori/cli/CLICommand.php @@ -210,7 +210,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom $this->moveCursorLeft($numberOfCols + 1); } } - + return $this; } /** @@ -224,7 +224,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom */ public function clearConsole() : CLICommand { $this->prints("\ec"); - + return $this; } /** diff --git a/webfiori/cli/Option.php b/webfiori/cli/Option.php index 2bb5b04..bfb5e3c 100644 --- a/webfiori/cli/Option.php +++ b/webfiori/cli/Option.php @@ -1,5 +1,4 @@ commands[$cliCommand->getName()] = $cliCommand; - + return $this; } /** @@ -348,7 +348,7 @@ public function reset() : Runner { $this->inputStream = new StdIn(); $this->outputStream = new StdOut(); $this->commands = []; - + return $this; } /** @@ -444,7 +444,7 @@ public function setActiveCommand(CLICommand $c = null) : Runner { $this->getActiveCommand()->setInputStream($this->getInputStream()); $this->getActiveCommand()->setOwner($this); } - + return $this; } /** @@ -467,7 +467,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner { 'func' => $func, 'params' => $params ]; - + return $this; } /** @@ -488,7 +488,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner { */ public function setArgsVector(array $argsVector) : Runner { $this->argsV = $argsVector; - + return $this; } /** @@ -505,7 +505,7 @@ public function setArgsVector(array $argsVector) : Runner { */ public function setBeforeStart(callable $func) : Runner { $this->beforeStartPool[] = $func; - + return $this; } /** @@ -524,7 +524,7 @@ public function setDefaultCommand(string $commandName) : Runner { if ($c !== null) { $this->defaultCommand = $c; } - + return $this; } /** @@ -547,7 +547,7 @@ public function setDefaultCommand(string $commandName) : Runner { public function setInputs(array $inputs = []) : Runner { $this->setInputStream(new ArrayInputStream($inputs)); $this->setOutputStream(new ArrayOutputStream()); - + return $this; } @@ -561,7 +561,7 @@ public function setInputs(array $inputs = []) : Runner { */ public function setInputStream(InputStream $stream) : Runner { $this->inputStream = $stream; - + return $this; } /** @@ -574,7 +574,7 @@ public function setInputStream(InputStream $stream) : Runner { */ public function setOutputStream(OutputStream $stream) : Runner { $this->outputStream = $stream; - + return $this; } /** diff --git a/webfiori/cli/commands/HelpCommand.php b/webfiori/cli/commands/HelpCommand.php index a0e2c07..ecce842 100644 --- a/webfiori/cli/commands/HelpCommand.php +++ b/webfiori/cli/commands/HelpCommand.php @@ -1,8 +1,8 @@