Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
115 changes: 115 additions & 0 deletions src/Classes/Cli/Command/CommandCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <mail@robinwieschendorf.de>
*
* 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\MmlcCli;
use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\Create\Argument;

class CommandCreate
Comment thread
grandeljay marked this conversation as resolved.
Outdated
{
private const ARGUMENT_ARCHIVE_NAME = 0;

public function __construct()
{
}

Comment thread
grandeljay marked this conversation as resolved.
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
{
echo "🏗️ \n";
}

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;
}

$this->create($cli);
}

public function help()
Comment thread
grandeljay marked this conversation as resolved.
Outdated
{
$this->renderHeading('Description:');
Comment thread
grandeljay marked this conversation as resolved.
Outdated
echo " Creates a new module. Can be done interactively.\n";
echo "\n";
$this->renderHeading('Usage:');
echo " create [options] <archiveName> \n";
echo "\n";
$this->renderHeading('Arguments:');
$this->renderOption('archiveName', 'The name of the archive (vendorName/moduleName).');
Comment thread
grandeljay marked this conversation as resolved.
Outdated
echo "\n";
$this->renderHeading('Options:');
$this->renderOption('--prefix=VENDOR_PREFIX', 'Usually an abbreviated vendorName. Can also be vendorName.');
Comment thread
grandeljay marked this conversation as resolved.
Outdated
$this->renderOption('-i, --interactive', 'Whether to create the module interactively (by answering questions).');
}


private function renderHeading(string $heading): void
Comment thread
grandeljay marked this conversation as resolved.
Outdated
{
echo "\e[33m$heading\e[0m\n";
}

private function renderOption($name, $description)
Comment thread
grandeljay marked this conversation as resolved.
Outdated
{
$name = $this->rightPad($name, 30);
echo " \e[32m$name\e[0m $description\n";
}

private function rightPad($text, $totalLength)
Comment thread
grandeljay marked this conversation as resolved.
Outdated
{
$textLength = strlen($text);

if ($textLength >= $totalLength) {
return $text; // Der Text ist bereits länger oder gleich der Ziel-Länge
}

$paddingLength = $totalLength - $textLength;
$padding = str_repeat(' ', $paddingLength);

return $text . $padding;
}
}
125 changes: 125 additions & 0 deletions src/Classes/Cli/MmlcCli.php
Comment thread
grandeljay marked this conversation as resolved.
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\CommandDownload;
use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandInfo;
use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandList;
Expand Down Expand Up @@ -42,6 +43,58 @@ public function run()
$command->run($this);
return;
} else {
switch ($command) {
case 'download':
$archiveName = $this->getArgument(2);
$this->downloadModule($archiveName);
break;
case 'install':
$archiveName = $this->getArgument(2);
$force = $this->hasOption('-f') || $this->hasOption('--force');
$this->installModule($archiveName, $force);
break;
case 'update':
$archiveName = $this->getArgument(2);
$force = $this->hasOption('-f') || $this->hasOption('--force');
$this->updateModule($archiveName, $force);
break;
case 'uninstall':
$archiveName = $this->getArgument(2);
$force = $this->hasOption('-f') || $this->hasOption('--force');
$this->uninstallModule($archiveName, $force);
break;
case 'list':
$this->listModules();
break;
case 'search':
$searchTerm = $this->getArgument(2);
$this->searchModules($searchTerm);
break;
case 'info':
$archiveName = $this->getArgument(2);
$this->moduleInfo($archiveName);
break;
case 'status':
$this->moduleStatus();
break;
case 'create':
$this->createModule();
break;
case 'watch':
$this->watchForFileChanges();
break;
case 'discard':
$archiveName = $this->getArgument(2);
$force = $this->hasOption('-f') || $this->hasOption('--force');
$this->discardChanges($archiveName, $force);
break;
case 'self-update':
$this->selfUpdate();
break;
default:
$this->showHelp();
break;
}
$this->runHelp();
}
}
Expand Down Expand Up @@ -118,7 +171,79 @@ private function getCurrentGitBranch(string $gitPath): ?string
if (empty($output)) {
return null;
}
}

private function moduleInfo($archiveName)
{
$command = new CommandInfo();
$command->run($archiveName);
}

private function moduleStatus()
{
// Implement displaying module status
}

private function createModule()
{
$command = new CommandCreate();
$command->run($this);
}

private function watchForFileChanges()
{
$command = new CommandWatch();
$command->run();
}

private function discardChanges($archiveName, $force = false)
{
// Implement discard changes logic
}

private function selfUpdate()
{
// Implement self-update logic
}

public function getCommand()
{
global $argv;
return isset($argv[1]) ? $argv[1] : 'help';
}

public function getArgument($index)
{
global $argv;
return isset($argv[$index]) ? $argv[$index] : null;
}

public function getFilteredArgument(int $argumentIndex): string
{
global $argv;

$arguments = $argv;
$filteredArguments = array();

foreach ($arguments as $index => $argument) {
if (0 === $index || 1 === $index) {
continue;
}

if ('-' === \substr($argument, 0, 1)) {
continue;
}

$filteredArguments[] = $argument;
}

return $filteredArguments[$argumentIndex] ?? '';
}

public function hasOption($option)
{
global $argv;
return in_array($option, $argv);
return $output;
}
}