|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands\Babel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PhpZip\ZipFile; |
|
8
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
9
|
|
|
|
|
10
|
|
|
class BabelRequire extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature='babel:require {extension : The package name of the extension} {--ignore-platform-reqs : Ignore the Platform Requirements when install} {--exception}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description='Download a given Babel Extension to NOJ'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new command instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Execute the console command. |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
public function handle() |
|
42
|
|
|
{ |
|
43
|
|
|
$extension=$this->argument('extension'); |
|
44
|
|
|
$exception=$this->option('exception'); |
|
45
|
|
|
$ignoreReqs=$this->option('ignore-platform-reqs'); |
|
46
|
|
|
$output=new BufferedOutput(); |
|
47
|
|
|
if (is_dir(babel_path("Extension/$extension/"))) { |
|
48
|
|
|
if (!$exception) { |
|
49
|
|
|
$this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An extension named <fg=green>$extension</> already took place, did you mean <fg=green>php artisan bable:update $extension</>?</>\n"); |
|
50
|
|
|
} else { |
|
51
|
|
|
throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An extension named <fg=green>$extension</> already took place, did you mean <fg=green>php artisan bable:update $extension</>?</>\n"); |
|
52
|
|
|
} |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
$marketspaceRaw=json_decode(file_get_contents(config('babel.mirror')."/babel.json"), true); |
|
56
|
|
|
$marketspacePackages=$marketspaceRaw["packages"]; |
|
57
|
|
|
$marketspaceHash=$marketspaceRaw["content-hash"]; |
|
|
|
|
|
|
58
|
|
|
$packageCodeColumn=array_column($marketspacePackages, 'code'); |
|
59
|
|
|
$targetPackage=$marketspacePackages[array_search($extension, $packageCodeColumn)]; |
|
60
|
|
|
// if(!isset($targetPackage["downloadURL"]) || trim($targetPackage["downloadURL"])=="" || is_null($targetPackage["downloadURL"])){ |
|
61
|
|
|
if (!isset($targetPackage["downloadURL"]) || !is_array($targetPackage["downloadURL"]) || is_null($targetPackage["downloadURL"]) || !isset($targetPackage["downloadURL"][0]["url"]) || trim($targetPackage["downloadURL"][0]["url"])=="" || is_null($targetPackage["downloadURL"][0]["url"])) { |
|
62
|
|
|
if (!$exception) { |
|
63
|
|
|
$this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
|
64
|
|
|
} else { |
|
65
|
|
|
throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
|
66
|
|
|
} |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
//todo: check requirements |
|
70
|
|
|
$this->line("Downloading <fg=green>$extension</>(<fg=yellow>{$targetPackage['version']}</>)"); |
|
71
|
|
|
$filename="$extension-".basename($targetPackage["downloadURL"][0]["url"]); |
|
72
|
|
|
file_put_contents(babel_path("Tmp/$filename"), file_get_contents($targetPackage["downloadURL"][0]["url"])); |
|
73
|
|
|
// unzip |
|
74
|
|
|
if (!is_dir(babel_path("Tmp/$extension/"))) { |
|
75
|
|
|
mkdir(babel_path("Tmp/$extension/"), 0777, true); |
|
76
|
|
|
} |
|
77
|
|
|
try { |
|
78
|
|
|
$zipFile=new ZipFile(); |
|
79
|
|
|
$zipFile->openFile(babel_path("Tmp/$filename"))->extractTo(babel_path("Tmp/$extension/"))->close(); |
|
80
|
|
|
$babelPath=glob_recursive(babel_path("Tmp/$extension/babel.json")); |
|
81
|
|
|
if (empty($babelPath)) { |
|
82
|
|
|
if (!$exception) { |
|
83
|
|
|
$this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>There exists no <fg=green>babel.json</> files.</>\n"); |
|
84
|
|
|
} else { |
|
85
|
|
|
throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
|
86
|
|
|
} |
|
87
|
|
|
return; |
|
88
|
|
|
} else { |
|
89
|
|
|
$babelPath=dirname($babelPath[0]); |
|
90
|
|
|
// if(is_dir(babel_path("Extension/$extension/"))) mkdir(babel_path("Extension/$extension/")); |
|
91
|
|
|
rename($babelPath, babel_path("Extension/$extension/")); |
|
92
|
|
|
} |
|
93
|
|
|
} catch (\PhpZip\Exception\ZipException $e) { |
|
94
|
|
|
$this->postProc($filename, $extension); |
|
95
|
|
|
if (!$exception) { |
|
96
|
|
|
$this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
|
97
|
|
|
} else { |
|
98
|
|
|
throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
|
99
|
|
|
} |
|
100
|
|
|
return; |
|
101
|
|
|
} catch (Exception $e) { |
|
102
|
|
|
$this->postProc($filename, $extension); |
|
103
|
|
|
if (!$exception) { |
|
104
|
|
|
$this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
|
105
|
|
|
} else { |
|
106
|
|
|
throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
|
107
|
|
|
} |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
$this->postProc($filename, $extension); |
|
111
|
|
|
$this->line("Downloaded <fg=green>$extension</>(<fg=yellow>{$targetPackage['version']}</>)"); |
|
112
|
|
|
$this->call("babel:install", [ |
|
113
|
|
|
'extension' => $extension, |
|
114
|
|
|
'--ignore-platform-reqs' => $ignoreReqs, |
|
115
|
|
|
]); |
|
116
|
|
|
$output->fetch(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function postProc($filename, $extension) |
|
120
|
|
|
{ |
|
121
|
|
|
unlink(babel_path("Tmp/$filename")); |
|
122
|
|
|
$this->delDir(babel_path("Tmp/$extension/")); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function delDir($dir) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!is_dir($dir)) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
$it=new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
|
131
|
|
|
$files=new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); |
|
132
|
|
|
foreach ($files as $file) { |
|
133
|
|
|
if ($file->isDir()) { |
|
134
|
|
|
rmdir($file->getRealPath()); |
|
135
|
|
|
} else { |
|
136
|
|
|
unlink($file->getRealPath()); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
rmdir($dir); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|