Skip to content

Add create command #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: feat/mmlc-cli
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bf31e7e
feat: add `create` command
grandeljay Nov 5, 2023
693b496
feat: add `archiveName` interaction
grandeljay Nov 6, 2023
e496d39
chore: merge `feat/mmlc-cli` into `create`
grandeljay Nov 6, 2023
69a029b
fix: refactor to match newest upstream changes
grandeljay Nov 6, 2023
a3b796d
fix: missing arguments
grandeljay Nov 6, 2023
badc16a
chore: merge `feat/mmlc-cli` into `create`
grandeljay Nov 7, 2023
d6d5d58
refactor: add `HelpRenderer`
grandeljay Nov 7, 2023
8a562e7
refactor: fix indentation
grandeljay Nov 7, 2023
8f87634
chore: merge `feat/mmlc-cli` into `create`
grandeljay Nov 14, 2023
80a5786
refactor: add specialised methods, rename `getPadding`
grandeljay Nov 14, 2023
3ae50ce
fix: remove duplicate command
grandeljay Nov 14, 2023
6f44386
refactor: remove unused method `renderLogo`
grandeljay Nov 14, 2023
208e135
fix: re-add required render methods
grandeljay Nov 14, 2023
118a28f
feat: automatically pad options and description
grandeljay Nov 14, 2023
d76011c
refactor: rename property `arguments` to `sections`
grandeljay Nov 14, 2023
42be461
refactor: simply code
grandeljay Nov 15, 2023
38a7d1e
feat: improve `HelpRenderer`
grandeljay Nov 15, 2023
d7c3ac7
refactor: remove unused method `leftPad`
grandeljay Nov 15, 2023
1d3e3c7
refactor: remove test option
grandeljay Nov 15, 2023
ff06fcd
refactor: let caller print new lines
grandeljay Nov 15, 2023
e4c0a99
Merge `feat/mmlc-cli` into `create`
grandeljay Nov 15, 2023
7b12d1e
chore: merge `feat/mmlc-cli` into `create`
grandeljay Nov 15, 2023
63f0497
refactor: move `rightPad` back where it was
grandeljay Nov 15, 2023
1efe045
feat: add create command
grandeljay Jan 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Classes/Cli/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
105 changes: 105 additions & 0 deletions src/Classes/Cli/Command/CommandCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <[email protected]>
*
* 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] <archiveName>');
$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();
}
}
2 changes: 2 additions & 0 deletions src/Classes/Cli/MmlcCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down