-
Notifications
You must be signed in to change notification settings - Fork 8
/
SilverStripeBuildTask.php
110 lines (96 loc) · 2.43 KB
/
SilverStripeBuildTask.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
<?php
/*
*
All code covered by the BSD license located at http://silverstripe.org/bsd-license/
*/
/**
* Build task that provides some commonly used functionality
*
* @author marcus
*/
abstract class SilverStripeBuildTask extends Task {
/**
* @see formatWithColor()
* @var array
*/
private static $ansiCodes = array(
'bold' => 1,
'fg-black' => 30,
'fg-red' => 31,
'fg-green' => 32,
'fg-yellow' => 33,
'fg-blue' => 34,
'fg-magenta' => 35,
'fg-cyan' => 36,
'fg-white' => 37,
'bg-black' => 40,
'bg-red' => 41,
'bg-green' => 42,
'bg-yellow' => 43,
'bg-blue' => 44,
'bg-magenta' => 45,
'bg-cyan' => 46,
'bg-white' => 47
);
protected function devBuild() {
if (file_exists('vendor/silverstripe/framework/cli-script.php')) {
$this->log("Running dev/build");
$this->exec('php vendor/silverstripe/framework/cli-script.php dev/build disable_perms=1');
}
}
/**
* Get some input from the user
*
* @param string $prompt
* @return string
*/
protected function getInput($prompt) {
require_once 'phing/input/InputRequest.php';
$request = new InputRequest($prompt);
$request->setPromptChar(':');
$this->project->getInputHandler()->handleInput($request);
$value = $request->getInput();
return $value;
}
protected function exec($cmd, $returnContent = false, $ignoreError = false) {
$ret = null;
$return = null;
if ($returnContent) {
$ret = shell_exec($cmd);
} else {
passthru($cmd, $return);
}
if ($return != 0 && !$ignoreError) {
throw new BuildException("Command '$cmd' failed");
}
return $ret;
}
/**
* Formats a buffer with a specified ANSI color sequence if colors are
* enabled. (Taken from PHPUnit)
*
* eg. $this->formatWithColor("fg-white, bg-red", "ERROR")
* $this->formatWithColor("fg-black, bg-green", "SUCCESS");
*
* @param string $color
* @param string $buffer
*
* @return string
*/
protected function formatWithColor($color, $buffer)
{
$codes = array_map('trim', explode(',', $color));
$lines = explode("\n", $buffer);
$padding = max(array_map('strlen', $lines));
$styles = array();
foreach ($codes as $code) {
$styles[] = self::$ansiCodes[$code];
}
$style = sprintf("\x1b[%sm", implode(';', $styles));
$styledLines = array();
foreach ($lines as $line) {
$styledLines[] = $style . str_pad($line, $padding) . "\x1b[0m";
}
return implode("\n", $styledLines);
}
}