-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFramyInstaller.php
executable file
·287 lines (235 loc) · 6.23 KB
/
FramyInstaller.php
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/php
<?php
/**
* FramyInstaller
*
* CLI tool made to make Framy application handling
* easier.
*
* @copyright Copyright FramyInstaller
* @Author Marco Bier <mrfibunacci@gmail.com>
*/
const VERSION = 'v0.1-alpha.1';
const COMMANDS = [
'HelpCommand',
'InstallCommand'
];
const INSTALL_DIR = '/usr/bin/FramyInstaller';
const REPO = 'https://github.com/FramyFramework/Framy.git';
error_reporting(E_WARNING);
class Argument
{
public $name;
public $description;
private $value;
function __construct(string $name, string $description)
{
$this->name = $name;
$this->description= $description;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value): void
{
$this->value = $value;
}
}
abstract class Command
{
protected $name;
protected $help;
protected $arguments;
protected $options;
protected static $availableArguments = [];
protected static $availableOptions = [];
function __construct(array $options, array $arguments)
{
try {
$this->configure();
$this->setArguments($arguments);
$this->setOptions($options);
} catch (\Exception $e) {
print($e->getMessage()."\n");exit();
}
}
/**
* Configures the current command.
*/
protected function configure(){}
public function execute(){}
private function compareArrays(&$arr1, &$arr2): bool
{
return (count($arr1) == count($arr2));
}
/**
* @param mixed $help
*/
public function setHelp($help): void
{
$this->help = $help;
}
public function setArguments(array $arguments)
{
if(!self::compareArrays(self::$availableArguments, $arguments))
throw new \Exception("Command argument not valid\n");
else
foreach ($arguments as $key => $argument) {
self::$availableArguments[$key]->setValue($argument);
}
$this->arguments = $arguments;
}
/**
* @param string $name
* @return Argument|false
*/
public function getArgument(string $name)
{
foreach(self::$availableArguments as $argument) {
if($argument->name == $name)
return $argument;
}
return false;
}
public function setOptions(array $options)
{
if(!self::compareArrays(self::$availableOptions, $options))
throw new \Exception("Command options not valid\n");
else
$this->options = $options;
}
/**
* @param string $name
* @throws \Exception
* @return $this
*/
public function setName($name)
{
$this->validateName($name);
$this->name = $name;
return $this;
}
/**
* Validates a command name
*
* It must be non-empty and parts can optionally be separated by ":".
*
* @param string $name
* @throws \Exception When the name is invalid
*/
private function validateName($name)
{
if(!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name))
throw new \Exception(sprintf('Command name "%s" is invalid.', $name));
}
}
class HelpCommand extends Command
{
protected function configure()
{
$this->setName("HelpCommand")
->setHelp("The Help command displays help for a given command.");
self::$availableArguments = [
new Argument("Name", "The name of the command you want to get help of")
];
}
public function execute()
{
print("Help Command\n");
print("TODO: implement help command\n");
}
}
class InstallCommand extends Command
{
protected function configure()
{
$this->setName("InstallCommand");
}
public function execute()
{
print("Installing FramyInstaller...\n");
exec("cp FramyInstaller.php ".INSTALL_DIR);
print("Copied file to '".INSTALL_DIR."'\n");
print("Successfully installed! Use FramyInstaller\n");
}
}
class CreateCommand extends Command
{
protected function configure()
{
$this->setName("CreateCommand")
->setHelp("Usage: Navigate to the directory in which
your project shall be located and execute: 'FramyInstall create NewProject'\n");
self::$availableArguments = [
new Argument("ProjectName", "Name of the newly created project\n")
];
}
public function execute()
{
$prjctName = $this->getArgument("ProjectName")->getValue();
print("Creating new Project: $prjctName\n");
exec("git clone ".REPO." $prjctName");
print("Done navigate there using 'cd $prjctName'\n");
}
}
class Console
{
/**
* @var Command
*/
private $command;
function __construct(array $arguments)
{
$this->welcomeMessage();
try {
$class = ucfirst($arguments[1])."Command";
if(!class_exists($class) || $class == "Command")
throw new \Exception();
} catch (\Exception $e) {
$class = "HelpCommand";
} finally {
$this->command = new $class($this->getOptions($arguments), $this->getArguments($arguments));
}
}
private function welcomeMessage(): void
{
print("FramyInstaller ".VERSION."\n");
print("The Framy Framework manager!\n\n");
}
private function getArguments(array $arguments):array
{
$arg = [];
array_shift($arguments);
array_shift($arguments);
foreach ($arguments as $argument) {
if(substr($argument, 0, 1) !== "-")
$arg[] = $argument;
}
return $arg;
}
private function getOptions(array $arguments): array
{
$opt = [];
array_shift($arguments);
array_shift($arguments);
foreach ($arguments as $option) {
if(substr($option, 0, 1) === "-")
$opt[] = $option;
}
return $opt;
}
public function run()
{
// Execute command
$this->command->execute();
}
}
$App = new Console($argv);
$App->run();