-
Notifications
You must be signed in to change notification settings - Fork 1
/
control
493 lines (434 loc) · 14.6 KB
/
control
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/* * Copyright (c) 2022 ณวสันต์ วิศิษฏ์ศิงขร
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* * Control version 0.1.3
* Github : https://github.com/Arikato111/control
*/
/* ! What's new !
* v1.0.3
- run scripts command in package.json
* v1.0.2
- Change report status when do something
- ltp support update
- add -h to show manual
*/
define('CONTROL_VERSION', '0.1.3');
define('baseUrl', 'https://package.anytion.com/install/');
ini_set('user_agent', '3lcieh2dfbon3032a');
if (isset($argv[1])) {
switch ($argv[1]) {
case 'list':
ShowListPackage();
break;
case 'serve':
runServer($argv);
break;
case 'install':
case 'i':
installPackage($argv);
break;
case 'url':
urlInstall($argv);
break;
case 'create':
createTemplate($argv);
break;
case 'rm':
case 'remove':
removeModule($argv);
break;
case 'init':
setInit($argv);
break;
case 'spa':
setSPA();
break;
case '-v':
case '--version':
echo CONTROL_VERSION . PHP_EOL;
break;
case 'update':
update();
break;
case 'help':
case '-h':
case '--help':
Help();
break;
default:
$package = getPackage();
if (isset($package->scripts) && isset($package->scripts->{$argv[1]})) {
system($package->scripts->{$argv[1]}, $output);
} else {
echo 'command not found';
}
}
} else {
Help();
}
function ShowListPackage()
{
$package = getPackage();
$data = $package->package;
foreach ($data as $key => $pk) {
echo "~@ {$key} :^{$pk->version}\n";
}
}
function urlInstall($argv)
{
if (!isset($argv[2]) || !isset($argv[3])) {
echo "# control url install <github-branch-url> | for install library\n";
echo "# control url create <github-branch-url> | for install template\n";
return;
}
$url = $argv[3];
if (
str_contains($url, 'https://github.com/') &&
str_contains($url, '/tree/')
) {
$name = explode('/', $url)[4];
echo "installing $url\n";
ini_set('user_agent', '3lcieh2dfbon3032a');
switch ($argv[2]) {
case 'i':
case 'install':
addModule($url, $name);
break;
case 'create':
addTemplate($url);
break;
default:
echo "# control url install <github-branch-url> | for install library\n";
echo "# control url create <github-branch-url> | for install template\n";
}
} else {
echo "ERROR url\n";
echo "# control url install <github-branch-url> | for install library\n";
echo "# control url create <github-branch-url> | for install template\n";
}
}
function installPackage($argv)
{
if (!isset($argv[2])) {
// install all package from package.json
$package = getPackage();
if (!(key($package->package) ?? false)) {
echo "no any package to install";
return;
}
foreach ($package->package as $key => $pk) {
addModule($pk->url, $key, $pk->version, true, false);
}
exit;
}
$package_list = [];
for ($i = 2; isset($argv[$i]); $i++) {
$errorReport = " - !! Error not found: ";
if (strpos($argv[$i], '@') !== false) {
[$module_name, $version] = explode('@', $argv[$i], 2);
$url = file_get_contents(baseUrl . 'module/' . $module_name . '/' . $version);
# check found package
if (!($url ?? false)) {
echo $errorReport . $argv[$i] . PHP_EOL;
die;
}
array_push($package_list, [
'name' => $module_name,
'version' => $version,
'url' => $url
]);
} else {
$url = file_get_contents(baseUrl . 'module/' . $argv[$i]);
if (!($url ?? false)) {
echo $errorReport . $argv[$i] . PHP_EOL;
die;
}
array_push($package_list, [
'name' => $argv[$i],
'version' => 'main',
'url' => $url
]);
}
}
echo "this is library will installing \n\n";
foreach ($package_list as $pkl) {
echo ' - ' . $pkl['name'] . '@' . $pkl['version'] . " url - " . $pkl['url'] . PHP_EOL;
}
echo "\nAre you sure to install all? ( [Enter] to install, [ctrl + c] to cancel )\n";
readline();
foreach ($package_list as $pkl) {
addModule($pkl['url'], $pkl['name'], $pkl['version'], false, false);
}
}
function createTemplate($argv)
{
if (!isset($argv[2])) {
echo ' # control create <template>' . PHP_EOL;
echo ' or' . PHP_EOL;
echo ' # control create <template@version>' . PHP_EOL;
return;
}
if (strpos($argv[2], '@') !== false) {
[$template_name, $template_version] = explode('@', $argv[2], 2);
$url = @file_get_contents(baseUrl . 'template/' . $template_name . '/' . $template_version);
} else {
$url = @file_get_contents(baseUrl . 'template/' . $argv[2]);
}
if (empty($url)) {
echo "- !! Error not found template: " . $argv[2] . " \n";
exit;
}
addTemplate($url);
}
function removeModule($argv)
{
$module_name = $argv[2];
$package = getPackage();
if (!isset($package->package->{$module_name})) {
echo '- !! can not remove: package not installed' . PHP_EOL;
return;
}
unset($package->package->{$module_name});
savePackage($package);
$dir = './modules/' . $argv[2];
function allInDir(string $dir): void
{
$getDir = glob($dir);
if (!empty($getDir)) {
foreach ($getDir as $file) {
if (!is_dir($file)) {
unlink($file);
} else {
allInDir("$file/*");
rmdir($file);
}
}
}
}
allInDir($dir);
@rmdir($dir);
echo "successfuly\n";
}
function getPackage()
{
if (!file_exists('./package.json')) {
echo "Not found package.json\n";
echo '# control init | to create package.json';
exit;
}
$package = json_decode(file_get_contents('./package.json'));
if (!isset($package->package)) $package->package = (object)[];
return $package;
}
function savePackage($package)
{
$package_json = json_encode($package, JSON_PRETTY_PRINT);
$package_json = str_replace('\/', '/', $package_json);
file_put_contents('./package.json', $package_json);
}
/************** Manage *********************** */
function addTemplate($url)
{
echo "installing template from github url : '$url'\n";
echo "Do you want to install? ( [Enter] to install, [ctrl + c] to cancel )\n";
readline(": ");
$url = str_replace('https://github.com/', 'https://api.github.com/repos/', $url);
$url = str_replace('tree', 'git/trees', $url);
$json = @file_get_contents("$url?recursive=1");
if ($json == false) {
echo "Error installing failed\n";
return;
}
$jo = json_decode($json);
$tree = $jo->tree;
$url = str_replace('git/trees/', '/', $url);
$url = str_replace('https://api.github.com/repos', 'https://raw.githubusercontent.com', $url);
foreach ($tree as $value) {
if ($value->mode == "100644") {
echo "Downloading file from $url/{$value->path}\n";
$file = file_get_contents("$url/" . $value->path);
echo "installing file {$value->path}\n";
@file_put_contents($value->path, $file);
} else if ($value->mode == "040000") {
echo "create directory {$value->path}\n";
mkdir($value->path);
} else {
echo "ERROR!! Not know mode";
}
}
$ex = strpos(__DIR__, '/') === false ? '\\' : '/';
$project_name = explode($ex, __DIR__);
$project_name = $project_name[sizeof($project_name) - 1];
$package = getPackage();
$package->name = $project_name;
savePackage($package);
echo "install successfuly\n";
}
function addModule($url, $moduleName, $version = 'main', $has_modules = false, $asking = true)
{
if (empty($url)) {
echo "ERROR ! URL EMTY\n";
exit;
}
$package = getPackage();
$module = [];
$module['version'] = $version;
$module['url'] = $url;
if ($asking) {
echo "installing $moduleName from github url : '$url'\n";
echo "Do you want to install? ( [Enter] to install, [ctrl + c] to cancel )\n";
readline(": ");
}
$url = str_replace('https://github.com/', 'https://api.github.com/repos/', $url);
$url = str_replace('tree', 'git/trees', $url);
if (!file_exists('./modules')) mkdir('./modules');
$json = @file_get_contents("$url?recursive=1");
if ($json == false) {
echo "Error installing failed\n";
return;
}
$jo = json_decode($json);
mkdir('./modules/' . $moduleName);
$tree = $jo->tree;
$url = str_replace('git/trees/', '/', $url);
$url = str_replace('https://api.github.com/repos', 'https://raw.githubusercontent.com', $url);
foreach ($tree as $value) {
if ($value->mode == "100644") {
echo "- Downloading file: {$value->path}\n";
$file = file_get_contents("$url/" . $value->path);
echo "installing file: -> $moduleName/{$value->path}\n";
@file_put_contents("modules/$moduleName/" . $value->path, $file);
} else if ($value->mode == "040000") {
echo "create directory ./modules/$moduleName/{$value->path}\n";
mkdir("modules/$moduleName/" . $value->path);
} else {
echo "Not know mode";
exit;
}
}
if (!$has_modules) {
$pj_name = $moduleName;
$package->package->$pj_name = $module;
savePackage($package);
}
echo "\ninstalled {$moduleName} successfully\n\n";
}
/************** Manage *********************** */
function update()
{
ini_set('user_agent', '3lcieh2dfbon3032a');
file_put_contents('control', @file_get_contents('https://raw.githubusercontent.com/Arikato111/control/master/control'));
}
// *********** init ************************** //
function setInit($argv)
{
$ex = strpos(__DIR__, '/') === false ? '\\' : '/';
$project_name = explode($ex, __DIR__);
$project_name = $project_name[sizeof($project_name) - 1];
if (isset($argv[2]) && $argv[2] == '-y') {
$json_format =
'{
"name": "' . $project_name . '",
"version": "1.0.0",
"description": "",
"author": "",
"license": "ISC",
"package": {}
}';
file_put_contents('./package.json', $json_format);
} else {
$project = [];
echo "project name : ($project_name) ";
$project['name'] = readline();
if (empty($project['name'])) $project['name'] = $project_name;
echo "version : (1.0.0) ";
$project['version'] = readline();
if (empty($project['version'])) $project['version'] = '1.0.0';
echo 'description : ';
$project['description'] = readline();
echo 'author : ';
$project['author'] = readline();
echo 'License : (ISC) ';
$project['license'] = readline();
if (empty($project['license'])) $project['license'] = 'ISC';
$project['package'] = (object)[];
$project_json = json_encode($project, JSON_PRETTY_PRINT);
echo $project_json;
echo PHP_EOL;
echo 'Is it Okay ? ( [Enter] to Yes, [ctrl + c] to cancel )';
readline();
file_put_contents('./package.json', $project_json);
}
echo 'Create package.json is successfuly';
echo PHP_EOL;
}
/********************************************* */
function runServer($argv)
{
if (isset($argv[2])) {
switch ($argv[2]) {
case '-p':
case '--port':
if (!isset($argv[3])) {
echo 'Please insert port number' . PHP_EOL;
echo " # control serve --port <port>\n";
exit;
}
$port = $argv[3];
break;
default:
echo 'Not found commant ' . $argv[2];
exit;
}
} else {
$port = 3000;
}
while (true) {
@exec("php -S localhost:{$port}");
$port++;
}
}
function setSPA()
{
file_put_contents(
'.htaccess',
'<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/public [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</IfModule>'
);
mkdir('./public');
}
function Help()
{
echo "\n";
echo "--- CONTROL " . CONTROL_VERSION . " ---\n";
echo ' # control -v | to check vertion of `control`' . PHP_EOL;
echo "\n--- Project ---\n";
echo ' # control init | to set project and create package.json ' . PHP_EOL;
echo ' # control spa | to set project as spa' . PHP_EOL;
echo ' # control update | for update control ' . PHP_EOL;
echo ' # control serve | to run server at localhost [not recommended]' . PHP_EOL . PHP_EOL;
echo "\n--- package manage ---\n";
echo ' # control list | to show library list' . PHP_EOL;
echo ' # control install | to install all library from package.json ' . PHP_EOL;
echo ' # control install <library> | to install library' . PHP_EOL;
echo ' # control install <library>@<version> | to install library with version' . PHP_EOL;
echo ' # control create <template> | to install template' . PHP_EOL;
echo ' # control create <template@version> | to install template with version' . PHP_EOL;
echo ' # control remove <library> | to remove library' . PHP_EOL . PHP_EOL;
echo "\n--- package from url ---\n";
echo " # control url install <github-branch-url> | for install library with github branch url\n";
echo " # control url create <github-branch-url> | for install template with github branch url \n";
echo "\n--- run scripts ---\n";
echo " # control <script> | for run scripts command in package.json\n";
echo "\n--- Github ---\n";
echo " Github url | https://github.com/Arikato111/control\n";
}