-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMmlcCli.php
More file actions
249 lines (214 loc) · 8.41 KB
/
MmlcCli.php
File metadata and controls
249 lines (214 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?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;
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;
use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandWatch;
class MmlcCli extends Cli
{
public function __construct()
{
$this->addCommand(new CommandDownload());
$this->addCommand(new CommandList());
$this->addCommand(new CommandInfo());
$this->addCommand(new CommandWatch());
}
public function run()
{
$command = $this->getCommand();
if (!$command && ($this->hasOption('--version') || $this->hasOption('-v'))) {
$this->showVersion();
} elseif ($command && ($this->hasOption('--help') || $this->hasOption('-h'))) {
$command->runHelp($this);
return;
} elseif ($command) {
$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();
}
}
private function runHelp()
{
TextRenderer::renderLogo();
echo "\n";
$this->showVersion();
echo "\n";
TextRenderer::renderHelpHeading('Usage:');
echo " command [options]\n";
echo "\n";
TextRenderer::renderHelpHeading('Options:');
TextRenderer::renderHelpOption('h', 'help', 'Display help for the given command.');
TextRenderer::renderHelpOption('v', 'version', 'Display this application version.');
echo "\n";
TextRenderer::renderHelpHeading('Commands:');
TextRenderer::renderHelpCommand('download', 'Download the latest version of module.');
TextRenderer::renderHelpCommand('install', 'Download and install a module in your shop. Use the -f or --force option to enforce.');
TextRenderer::renderHelpCommand('update', 'Update an already installed module to the latest version. Use the -f or --force option to enforce.');
TextRenderer::renderHelpCommand('uninstall', 'Uninstall a module from your shop. Use the -f or --force option to enforce.');
TextRenderer::renderHelpCommand('list', 'List all available modules that can be used with MMLC.');
TextRenderer::renderHelpCommand('search', 'Search for modules based on a specific search term.');
TextRenderer::renderHelpCommand('info', 'Display information and details for a specific module.');
TextRenderer::renderHelpCommand('status', 'Show the status of all installed modules in MMLC.');
TextRenderer::renderHelpCommand('create', 'Create a new module in MMLC. Use the -i option for the interactive mode.');
TextRenderer::renderHelpCommand('watch', 'Automatically detect and apply file changes for module development.');
TextRenderer::renderHelpCommand('discard', 'Discard changes to a module. Use the -f or --force option to enforce.');
TextRenderer::renderHelpCommand('self-update', 'Updates MMLC to the latest version.');
echo "\n";
echo "Read more at https://module-loader.de/documentation.php\n";
}
private function showVersion()
{
$mmlcVersion = App::getMmlcVersion();
$gitBranch = $this->getCurrentGitBranch(App::getRoot() . '/.git');
$extra = $gitBranch ? "git branch " . TextRenderer::color($gitBranch, TextRenderer::COLOR_YELLOW) : '';
echo TextRenderer::color('MMLC cli', TextRenderer::COLOR_GREEN)
. ' version ' . TextRenderer::color($mmlcVersion, TextRenderer::COLOR_YELLOW)
. ' ' . $extra . "\n";
}
private function getCurrentGitBranch(string $gitPath): ?string
{
if (!is_dir($gitPath)) {
return null;
}
$os = strtoupper(substr(PHP_OS, 0, 3));
$command = '';
switch ($os) {
case 'WIN':
$command = 'cd /d "' . $gitPath . '" & git symbolic-ref --short HEAD 2>NUL';
break;
case 'LIN':
case 'DAR':
$command = 'cd "' . $gitPath . '" && git symbolic-ref --short HEAD 2>/dev/null';
break;
default:
return 'unkown branch';
}
$output = trim('' . shell_exec($command));
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;
}
}