-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMmlcCli.php
More file actions
150 lines (127 loc) · 6.06 KB
/
MmlcCli.php
File metadata and controls
150 lines (127 loc) · 6.06 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
<?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\CommandUninstall;
use RobinTheHood\ModifiedModuleLoaderClient\Cli\Command\CommandWatch;
class MmlcCli extends Cli
{
public function __construct()
{
$this->addCommand(new CommandCreate());
$this->addCommand(new CommandDownload());
$this->addCommand(new CommandUninstall());
$this->addCommand(new CommandList());
$this->addCommand(new CommandInfo());
$this->addCommand(new CommandList());
$this->addCommand(new CommandWatch());
}
public function run()
{
$command = $this->getCommand();
if (!$command && ($this->hasOption('--version') || $this->hasOption('-v'))) {
$this->writeLine($this->renderVersion());
} elseif ($command && ($this->hasOption('--help') || $this->hasOption('-h'))) {
$this->writeLine($command->getHelp($this));
return;
} elseif ($command) {
$command->run($this);
return;
} else {
$this->getHelp();
}
}
private function getHelp()
{
$this->writeLine(
$this->renderLogo()
. "\n"
. $this->renderVersion()
. "\n"
. TextRenderer::renderHelpHeading('Usage:')
. " command [options]\n"
. "\n"
. TextRenderer::renderHelpHeading('Options:')
. TextRenderer::renderHelpOption('h', 'help', 'Display help for the given command.')
. TextRenderer::renderHelpOption('v', 'version', 'Display this application version.')
. "\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.')
. "\n"
. "Read more at https://module-loader.de/documentation.php"
);
}
public function renderLogo(): string
{
// return ''
// . " __ _____ _____ ______ ________ ____\n"
// . " / |/ / |/ / / / ____/ / ____/ / / _/\n"
// . " / /|_/ / /|_/ / / / / / / / / / / \n"
// . " / / / / / / / /___/ /___ / /___/ /____/ / \n"
// . "/_/ /_/_/ /_/_____/\____/ \____/_____/___/ \n";
// created with: https://patorjk.com/software/taag/#p=display&f=Slant&t=MMLC%20CLI
return ''
. " __ ___ __ ___ __ ______ ______ __ ____\n"
. " / |/ // |/ // / / ____/ / ____// / / _/\n"
. " / /|_/ // /|_/ // / / / / / / / / / \n"
. " / / / // / / // /___/ /___ / /___ / /___ _/ / \n"
. "/_/ /_//_/ /_//_____/\____/ \____//_____//___/ \n";
// cretated with: https://patorjk.com/software/taag/#p=display&h=1&f=Slant&t=MMLC%20CLI
}
private function renderVersion(): string
{
$mmlcVersion = App::getMmlcVersion();
$gitBranch = $this->getCurrentGitBranch(App::getRoot() . '/.git');
$extra = $gitBranch ? "git branch " . TextRenderer::color($gitBranch, TextRenderer::COLOR_YELLOW) : '';
return 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;
}
return $output;
}
}