forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DevController.php
370 lines (333 loc) · 12.4 KB
/
DevController.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
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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\build\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\FileHelper;
/**
* This command helps to set up a dev environment with all extensions and applications
*
* It will clone an extension or app repo and link the yii2 dev installation to the containted applications/extensions vendor dirs
* to help working on yii using the application to test it.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
class DevController extends Controller
{
public $defaultAction = 'all';
/**
* @var bool whether to use HTTP when cloning github repositories
*/
public $useHttp = false;
public $apps = [
'basic' => '[email protected]:yiisoft/yii2-app-basic.git',
'advanced' => '[email protected]:yiisoft/yii2-app-advanced.git',
'benchmark' => '[email protected]:yiisoft/yii2-app-benchmark.git',
];
public $extensions = [
'apidoc' => '[email protected]:yiisoft/yii2-apidoc.git',
'authclient' => '[email protected]:yiisoft/yii2-authclient.git',
'bootstrap' => '[email protected]:yiisoft/yii2-bootstrap.git',
'codeception' => '[email protected]:yiisoft/yii2-codeception.git',
'composer' => '[email protected]:yiisoft/yii2-composer.git',
'debug' => '[email protected]:yiisoft/yii2-debug.git',
'elasticsearch' => '[email protected]:yiisoft/yii2-elasticsearch.git',
'faker' => '[email protected]:yiisoft/yii2-faker.git',
'gii' => '[email protected]:yiisoft/yii2-gii.git',
'httpclient' => '[email protected]:yiisoft/yii2-httpclient.git',
'imagine' => '[email protected]:yiisoft/yii2-imagine.git',
'jui' => '[email protected]:yiisoft/yii2-jui.git',
'mongodb' => '[email protected]:yiisoft/yii2-mongodb.git',
'queue' => '[email protected]:yiisoft/yii2-queue.git',
'redis' => '[email protected]:yiisoft/yii2-redis.git',
'shell' => '[email protected]:yiisoft/yii2-shell.git',
'smarty' => '[email protected]:yiisoft/yii2-smarty.git',
'sphinx' => '[email protected]:yiisoft/yii2-sphinx.git',
'swiftmailer' => '[email protected]:yiisoft/yii2-swiftmailer.git',
'twig' => '[email protected]:yiisoft/yii2-twig.git',
];
/**
* Install all extensions and advanced + basic app
*/
public function actionAll()
{
if (!$this->confirm('Install all applications and all extensions now?')) {
return 1;
}
foreach($this->extensions as $ext => $repo) {
$ret = $this->actionExt($ext);
if ($ret !== 0) {
return $ret;
}
}
foreach($this->apps as $app => $repo) {
$ret = $this->actionApp($app);
if ($ret !== 0) {
return $ret;
}
}
return 0;
}
/**
* Runs a command in all extension and application directories
*
* Can be used to run e.g. `git pull`.
*
* ./build/build dev/run git pull
*
* @param string $command the command to run
*/
public function actionRun($command)
{
$command = implode(' ', func_get_args());
// root of the dev repo
$base = dirname(dirname(__DIR__));
$dirs = $this->listSubDirs("$base/extensions");
$dirs = array_merge($dirs, $this->listSubDirs("$base/apps"));
asort($dirs);
$oldcwd = getcwd();
foreach($dirs as $dir) {
$displayDir = substr($dir, strlen($base));
$this->stdout("Running '$command' in $displayDir...\n", Console::BOLD);
chdir($dir);
passthru($command);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
}
chdir($oldcwd);
}
/**
* This command installs a project template in the `apps` directory and links the framework and extensions
*
* It basically runs the following commands in the dev repo root:
*
* - Run `composer update`
* - `rm -rf apps/basic/vendor/yiisoft/yii2`
* - `rm -rf apps/basic/vendor/yiisoft/yii2-*`
*
* And replaces them with symbolic links to the extensions and framework path in the dev repo.
*
* Extensions required by the application are automatically installed using the `ext` action.
*
* @param string $app the application name e.g. `basic` or `advanced`.
* @param string $repo url of the git repo to clone if it does not already exist.
* @return int return code
*/
public function actionApp($app, $repo = null)
{
// root of the dev repo
$base = dirname(dirname(__DIR__));
$appDir = "$base/apps/$app";
if (!file_exists($appDir)) {
if (empty($repo)) {
if (isset($this->apps[$app])) {
$repo = $this->apps[$app];
if ($this->useHttp) {
$repo = str_replace('[email protected]:', 'https://github.com/', $repo);
}
} else {
$this->stderr("Repo argument is required for app '$app'.\n", Console::FG_RED);
return 1;
}
}
$this->stdout("cloning application repo '$app' from '$repo'...\n", Console::BOLD);
passthru('git clone ' . escapeshellarg($repo) . ' ' . $appDir);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
}
// cleanup
$this->stdout("cleaning up application '$app' vendor directory...\n", Console::BOLD);
$this->cleanupVendorDir($appDir);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
// composer update
$this->stdout("updating composer for app '$app'...\n", Console::BOLD);
chdir($appDir);
passthru('composer update --prefer-dist');
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
// link directories
$this->stdout("linking framework and extensions to '$app' app vendor dir...\n", Console::BOLD);
$this->linkFrameworkAndExtensions($appDir, $base);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
return 0;
}
/**
* This command installs an extension in the `extensions` directory and links the framework and other extensions
*
* @param string $extension the application name e.g. `basic` or `advanced`.
* @param string $repo url of the git repo to clone if it does not already exist.
*
* @return int
*/
public function actionExt($extension, $repo = null)
{
// root of the dev repo
$base = dirname(dirname(__DIR__));
$extensionDir = "$base/extensions/$extension";
if (!file_exists($extensionDir)) {
if (empty($repo)) {
if (isset($this->extensions[$extension])) {
$repo = $this->extensions[$extension];
if ($this->useHttp) {
$repo = str_replace('[email protected]:', 'https://github.com/', $repo);
}
} else {
$this->stderr("Repo argument is required for extension '$extension'.\n", Console::FG_RED);
return 1;
}
}
$this->stdout("cloning extension repo '$extension' from '$repo'...\n", Console::BOLD);
passthru('git clone ' . escapeshellarg($repo) . ' ' . $extensionDir);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
}
// cleanup
$this->stdout("cleaning up extension '$extension' vendor directory...\n", Console::BOLD);
$this->cleanupVendorDir($extensionDir);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
// composer update
$this->stdout("updating composer for extension '$extension'...\n", Console::BOLD);
chdir($extensionDir);
passthru('composer update --prefer-dist');
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
// link directories
$this->stdout("linking framework and extensions to '$extension' vendor dir...\n", Console::BOLD);
$this->linkFrameworkAndExtensions($extensionDir, $base);
$this->stdout("done.\n", Console::BOLD, Console::FG_GREEN);
return 0;
}
/**
* @inheritdoc
*/
public function options($actionID)
{
$options = parent::options($actionID);
if (in_array($actionID, ['ext', 'app', 'all'], true)) {
$options[] = 'useHttp';
}
return $options;
}
/**
* Remove all symlinks in the vendor subdirectory of the directory specified
* @param string $dir base directory
*/
protected function cleanupVendorDir($dir)
{
if (is_link($link = "$dir/vendor/yiisoft/yii2")) {
$this->stdout("Removing symlink $link.\n");
$this->unlink($link);
}
$extensions = $this->findDirs("$dir/vendor/yiisoft");
foreach($extensions as $ext) {
if (is_link($link = "$dir/vendor/yiisoft/yii2-$ext")) {
$this->stdout("Removing symlink $link.\n");
$this->unlink($link);
}
}
}
/**
* Creates symlinks to framework and extension sources for the application
* @param string $dir application directory
* @param string $base Yii sources base directory
*
* @return int
*/
protected function linkFrameworkAndExtensions($dir, $base)
{
if (is_dir($link = "$dir/vendor/yiisoft/yii2")) {
$this->stdout("Removing dir $link.\n");
FileHelper::removeDirectory($link);
$this->stdout("Creating symlink for $link.\n");
symlink("$base/framework", $link);
}
$extensions = $this->findDirs("$dir/vendor/yiisoft");
foreach($extensions as $ext) {
if (is_dir($link = "$dir/vendor/yiisoft/yii2-$ext")) {
$this->stdout("Removing dir $link.\n");
FileHelper::removeDirectory($link);
$this->stdout("Creating symlink for $link.\n");
if (!file_exists("$base/extensions/$ext")) {
$ret = $this->actionExt($ext);
if ($ret !== 0) {
return $ret;
}
}
symlink("$base/extensions/$ext", $link);
}
}
}
/**
* Properly removes symlinked directory under Windows, MacOS and Linux
*
* @param string $file path to symlink
*/
protected function unlink($file)
{
if (is_dir($file) && DIRECTORY_SEPARATOR === '\\') {
rmdir($file);
} else {
unlink($file);
}
}
/**
* Get a list of subdirectories for directory specified
* @param string $dir directory to read
*
* @return array list of subdirectories
*/
protected function listSubDirs($dir)
{
$list = [];
$handle = opendir($dir);
if ($handle === false) {
throw new InvalidParamException("Unable to open directory: $dir");
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
// ignore hidden directories
if ($file[0] === '.') {
continue;
}
if (is_dir("$dir/$file")) {
$list[] = "$dir/$file";
}
}
closedir($handle);
return $list;
}
/**
* Finds linkable applications
*
* @param string $dir directory to search in
* @return array list of applications command can link
*/
protected function findDirs($dir)
{
$list = [];
$handle = @opendir($dir);
if ($handle === false) {
return [];
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path) && preg_match('/^yii2-(.*)$/', $file, $matches)) {
$list[] = $matches[1];
}
}
closedir($handle);
foreach($list as $i => $e) {
if ($e === 'composer') { // skip composer to not break composer update
unset($list[$i]);
}
}
return $list;
}
}