diff --git a/src/Classes/Cli/Cli.php b/src/Classes/Cli/Cli.php index 2f6eb3d8..86c3b45a 100644 --- a/src/Classes/Cli/Cli.php +++ b/src/Classes/Cli/Cli.php @@ -74,6 +74,34 @@ public function getFilteredArgument(int $argumentIndex): string public function hasOption($option) { global $argv; - return in_array($option, $argv); + + foreach ($argv as $index => $value) { + $optionParts = explode('=', $value); + $optionName = $optionParts[0] ?? $value; + $optionValue = $optionParts[1] ?? ''; + + if ($option === $optionName) { + return true; + } + } + + return false; + } + + public function getOption($option) + { + global $argv; + + foreach ($argv as $index => $value) { + $optionParts = explode('=', $value); + $optionName = $optionParts[0] ?? $value; + $optionValue = $optionParts[1] ?? ''; + + if ($option === $optionName) { + return $optionValue; + } + } + + return false; } } diff --git a/src/Classes/Cli/Command/CommandCreate.php b/src/Classes/Cli/Command/CommandCreate.php new file mode 100644 index 00000000..b2c5b573 --- /dev/null +++ b/src/Classes/Cli/Command/CommandCreate.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient\Cli\Command; + +use RobinTheHood\ModifiedModuleLoaderClient\Cli\HelpRenderer; +use RobinTheHood\ModifiedModuleLoaderClient\Cli\MmlcCli; +use RobinTheHood\ModifiedModuleLoaderClient\ModuleCreator; + +class CommandCreate implements CommandInterface +{ + private const ARGUMENT_ARCHIVE_NAME = 0; + private const ARGUMENT_VENDOR_PREFIX = 1; + + public function __construct() + { + } + + public function getName(): string + { + return 'create'; + } + + public function run(MmlcCli $cli): void + { + if ($cli->hasOption('-i') || $cli->hasOption('--interactive')) { + $this->createInteractive($cli); + } else { + $this->create($cli); + } + } + + private function create(MmlcCli $cli): void + { + $archiveName = $cli->getFilteredArgument(self::ARGUMENT_ARCHIVE_NAME); + $archiveParts = explode('/', $archiveName); + $vendorName = $archiveParts[0] ?? 'MyCompany'; + $moduleName = $archiveParts[1] ?? 'My First Module'; + + if ($cli->hasOption('--prefix')) { + echo $cli->getOption('--prefix'); + } + $vendorPrefix = $cli->getFilteredArgument(self::ARGUMENT_VENDOR_PREFIX); + + $moduleCreator = new ModuleCreator(); + $moduleCreator->createModule($vendorPrefix, $vendorName, $moduleName); + } + + private function createInteractive(MmlcCli $cli): void + { + $archiveName = $cli->getFilteredArgument(self::ARGUMENT_ARCHIVE_NAME); + + if (!$archiveName) { + $vendorName = ''; + + while (!$vendorName) { + echo "1. What is the vendor name?\n"; + echo " Vendor name: "; + + $vendorName = readline(); + + echo "\n"; + } + + $moduleName = ''; + + while (!$moduleName) { + echo "2. What is the module name?\n"; + echo " Module name: "; + + $moduleName = readline(); + + echo "\n"; + } + + $archiveName = $vendorName . '/' . $moduleName; + + $arguments['archiveName'] = $archiveName; + } + + $this->create($cli); + } + + public function getHelp(MmlcCli $cli): string + { + $renderer = new HelpRenderer(); + $renderer->setDescription('Creates a new module. Can be done interactively. Read more at https://module-loader.de/documentation.php.'); + $renderer->setUsage('create', '[options] '); + $renderer->addArgument('archiveName', 'The name of the archive (vendorName/moduleName).'); + $renderer->addOption('', 'prefix=VENDOR_PREFIX', 'Usually an abbreviated vendorName. Can also be vendorName.'); + $renderer->addOption('i', 'interactive', 'Whether to create the module interactively (by answering questions).'); + + return $renderer->render(); + } +} diff --git a/src/Classes/Cli/MmlcCli.php b/src/Classes/Cli/MmlcCli.php index fbfde2ed..7c2f18c9 100644 --- a/src/Classes/Cli/MmlcCli.php +++ b/src/Classes/Cli/MmlcCli.php @@ -14,6 +14,7 @@ namespace RobinTheHood\ModifiedModuleLoaderClient\Cli; use RobinTheHood\ModifiedModuleLoaderClient\App; +use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandCreate; use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandDelete; use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandDiscard; use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandDownload; @@ -28,6 +29,7 @@ class MmlcCli extends Cli { public function __construct() { + $this->addCommand(new CommandCreate()); $this->addCommand(new CommandDownload()); $this->addCommand(new CommandInstall()); $this->addCommand(new CommandUpdate());