-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboFile.php
480 lines (425 loc) · 9.75 KB
/
RoboFile.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
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
<?php
/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
//require '/Users/Shared/www/notifier/vendor/autoload.php';
//use Joli\JoliNotif\Notification;
//use Joli\JoliNotif\NotifierFactory;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class RoboFile extends \Robo\Tasks
{
const FORUM = "/var/www/html/devboards/";
const GIT = "/var/www/git/";
protected $dirsToScan = [];
/**
*
*/
public function symlinkCreate()
{
$this->scanForConfigs();
$this->createSymlinks();
// $this->notify('Symlink Updater', 'Symlinks created successfully.');
}
/**
* @param array $opts
*/
public function symlinkClean($opts = ['erase' => false])
{
$this->cleanSymlinks($opts);
// $this->notify('Symlink Updater', 'Symlinks ' . ($opts['erase'] ? 'erased' : 'cleaned') . ' successfully.');
}
/**
* @param string $onlyDir
*/
public function syntaxCheck($onlyDir = '')
{
$this->scanForConfigs($onlyDir, true);
$this->checkSyntax();
}
/**
* @param $product
* @param $version
* @param array $opts
*/
public function release($product, $version, $opts = ['repoDir' => NULL, 'changeLogDir' => NULL])
{
$version = ($version[0] != 'v' ? 'v' : '') . $version;
if ($opts['repoDir'])
{
$repoDir = $opts['repoDir'];
}
else
{
$repoDir = self::GIT . $product;
}
$this->output()->setVerbosity(OutputInterface::VERBOSITY_QUIET);
$this->taskGitStack()
->printOutput(false)
->dir($repoDir)
->exec(['tag', '--delete', $version])
->exec(['push', 'origin', ':refs/tags/' . $version])
->run()
;
$this->taskGitStack()
->printOutput(false)
->dir($repoDir)
->exec(['branch', '--delete', 'release/' . $version])
->exec(['push', 'origin', '--delete', 'release/' . $version])
->run()
;
$this->output()->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
/** @var \Robo\Collection\CollectionBuilder $collection */
$collection = $this->collectionBuilder();
// Update changelog
$this->_changeLog($product, $version, $collection, $opts['repoDir'], $opts['changeLogDir']);
$collection->taskGitStack()
->printOutput(false)
->dir($repoDir)
->exec(['flow', 'release', 'start', $version])
->exec(['flow', 'release', 'publish', $version])
->exec(['flow', 'release', 'finish', $version, '-m', $version, '--push', '--keepremote', '--nobackmerge'])
->checkout('develop')
->merge('master')
->push()
;
return $collection->run();
}
/**
* @param $product
* @param $version
* @param array $opts
*/
public function changeLog($product, $version, $opts = ['repoDir' => NULL, 'changeLogDir' => NULL])
{
/** @var \Robo\Collection\CollectionBuilder $collection */
$collection = $this->collectionBuilder();
$this->_changeLog($product, $version, $collection, $opts['repoDir'], $opts['changeLogDir']);
$collection->run();
}
/**
* @param $product
* @param $version
* @param \Robo\Collection\CollectionBuilder $collection
* @param null $repoDir
* @param null $changeLogDir
*/
protected function _changeLog($product, $version, \Robo\Collection\CollectionBuilder $collection, $repoDir = NULL, $changeLogDir = NULL)
{
$ds = DIRECTORY_SEPARATOR;
$version = ($version[0] != 'v' ? 'v' : '') . $version;
if (!$repoDir)
{
$repoDir = self::GIT . $product;
}
if (!$changeLogDir)
{
$changeLogDir = $repoDir . $ds . $product;
}
$changeLogTask = $this->taskChangelog($changeLogDir . $ds . 'CHANGELOG.md')
->version($version)
;
while ($resp = $this->ask("Changed in this release: "))
{
$changeLogTask->change($resp);
}
if ($changeLogTask->getChanges())
{
$changeLogTask->run();
// Commit change log
$collection->taskGitStack()
->stopOnFail()
->printOutput(false)
->dir($repoDir)
->add('**/CHANGELOG.md')
->commit('Updated changelog')
->push()
;
}
}
/**
* @param string $onlyDir
* @param bool $skipFlag
*/
protected function scanForConfigs($onlyDir = '', $skipFlag = false)
{
if ($onlyDir)
{
if (file_exists(self::GIT . $onlyDir . '/config.inc.php'))
{
// Grab the configuration file
require(self::GIT . $onlyDir . '/config.inc.php');
}
}
else
{
$d = dir(self::GIT);
while (false !== ($entry = $d->read()))
{
if (
$entry[0] == '.'
OR !is_dir(self::GIT . $entry)
OR !file_exists(self::GIT . $entry . '/config.inc.php')
)
{
// Skip this
continue;
}
// Grab the configuration file
require(self::GIT . $entry . '/config.inc.php');
}
$d->close();
}
}
/**
* @param $pathToApplication
* @param $version
* @param array $replace
*/
protected function updateFramework($pathToApplication, $version, $replace = [])
{
$this->_mirrorDir(self::GIT . 'framework/' . $version, $pathToApplication);
$d = new RecDir($pathToApplication . '/');
while (false !== ($file = $d->read()))
{
$this->taskReplaceInFile($file)
->from(array_keys($replace))
->to($replace)
->run();
}
$d->close();
}
/**
* @param $pathToApplication
* @param $version
* @param array $replace
*/
protected function updateFrameworkWithoutSymlink($pathToApplication, $version, $replace = [])
{
$this->_mirrorDir(self::FORUM . 'framework/' . $version, $pathToApplication);
$d = new RecDir($pathToApplication . '/');
while (false !== ($file = $d->read()))
{
$this->taskReplaceInFile($file)
->from(array_keys($replace))
->to($replace)
->run();
}
$d->close();
}
/**
*
*/
protected function createSymlinks()
{
foreach ($this->dirsToScan as $folder => $dirs)
{
foreach ($dirs as $dir)
{
$d = new RecDir($dir);
while (false !== ($entry = $d->read()))
{
// Grab our base entry
$baseEntry = str_replace($dir, '', $entry);
if (!is_dir(dirname(self::FORUM . $folder . '/' . $baseEntry)))
{
// Destination directory doesn't exist
mkdir(dirname(self::FORUM . $folder . '/' . $baseEntry), 0755, true);
}
if (!is_link(self::FORUM . $folder . '/' . $baseEntry))
{
if (file_exists(self::FORUM . $folder . '/' . $baseEntry))
{
// Get rid of the "normal" file
@unlink(self::FORUM . $folder . '/' . $baseEntry);
}
// Create the symlink
$this->taskFileSystemStack()
->symlink($entry, self::FORUM . $folder . '/' . $baseEntry)
->run();
}
}
$d->close();
}
}
}
/**
* @param $opts
*/
protected function cleanSymlinks($opts)
{
$d = new RecDir(self::FORUM);
while (false !== ($entry = $d->read()))
{
if (is_link($entry) AND (!@readlink($entry) OR $opts['erase']))
{
// Remove the symlink
@unlink($entry);
// Log this
$this->say('Deleted Symlink: ' . $entry);
}
}
$d->close();
}
/**
*
*/
protected function checkSyntax()
{
$taskStack = $this->taskExecStack()->printed(false)->stopOnFail();
foreach ($this->dirsToScan as $folder => $dirs)
{
foreach ($dirs as $dir)
{
$d = new RecDir($dir);
while (false !== ($entry = $d->read()))
{
if (pathinfo($entry, PATHINFO_EXTENSION) == 'php')
{
$taskStack = $taskStack->exec('php -l "' . $entry . '"');
}
}
$d->close();
}
}
$taskStack->run();
}
/**
* @param $dir
* @param $folder
*/
protected function addDirToScan($dir, $folder)
{
if (!isset($this->dirsToScan[$folder]))
{
// Make sure this is set
$this->dirsToScan[$folder] = array();
}
// Now store the dir
$this->dirsToScan[$folder][] = $dir;
}
/**
* @param $title
* @param $body
*/
protected function notify($title, $body)
{
/** @var \Joli\JoliNotif\Notifier $notifier */
$notifier = NotifierFactory::create();
if ($notifier)
{
$notification =
(new Notification())
->setTitle($title)
->setBody($body);
;
$notifier->send($notification);
}
}
}
class RecDir
{
protected $currentPath;
protected $slash;
protected $rootPath;
protected $recursiveTree;
protected $excludedRootDirs;
function __construct($rootPath, $win = false, $excludedRootDirs = array())
{
switch ($win)
{
case true:
$this->slash = '\\';
break;
default:
$this->slash = '/';
}
$this->rootPath = $rootPath;
$this->currentPath = $rootPath;
$this->excludedRootDirs = $excludedRootDirs;
$this->recursiveTree = array(dir($this->rootPath));
$this->rewind();
}
function __destruct()
{
$this->close();
}
public function close()
{
while (true === ($d = array_pop($this->recursiveTree)))
{
$d->close();
}
}
public function closeChildren()
{
while (count($this->recursiveTree) > 1 AND false !== ($d = array_pop($this->recursiveTree)))
{
$d->close();
return true;
}
return false;
}
public function getRootPath()
{
if (isset($this->rootPath))
{
return $this->rootPath;
}
return false;
}
public function getCurrentPath()
{
if (isset($this->currentPath))
{
return $this->currentPath;
}
return false;
}
public function read($debug = false)
{
if ($debug)
{
var_dump($this->recursiveTree);
}
while (count($this->recursiveTree) > 0)
{
$d = end($this->recursiveTree);
if ((false !== ($entry = $d->read())))
{
if ($entry != '.' AND $entry != '..' AND $entry != '.DS_Store')
{
$path = $d->path . $entry;
if (is_file($path))
{
return $path;
}
else if (is_dir($path . $this->slash))
{
$this->currentPath = $path . $this->slash;
if ($child = @dir($path . $this->slash))
{
$this->recursiveTree[] = $child;
}
}
}
}
else
{
array_pop($this->recursiveTree)->close();
}
}
return false;
}
public function rewind()
{
$this->closeChildren();
$this->rewindCurrent();
}
public function rewindCurrent()
{
return end($this->recursiveTree)->rewind();
}
}