|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Builder; |
|
4
|
|
|
|
|
5
|
|
|
class _Lib |
|
6
|
|
|
{ |
|
7
|
|
|
protected $container; |
|
8
|
|
|
protected $image; |
|
9
|
|
|
protected $workdir; |
|
10
|
|
|
protected $os; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->workdir = realpath(__DIR__ . '/../..'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function getOs() |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$this->os) { |
|
20
|
|
|
$this->os = php_uname('s'); |
|
21
|
|
|
if (preg_match('/[Ww]in/', $this->os)) { |
|
22
|
|
|
$this->os = 'Windows'; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $this->os; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function fixDir($command) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->getOs() === "Windows") { |
|
32
|
|
|
return str_replace('/', '\\', $command); |
|
33
|
|
|
} |
|
34
|
|
|
return $command; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Execute the given command by displaying console output live to the user. |
|
39
|
|
|
* @param string|array $cmd : command to be executed |
|
40
|
|
|
* @return array exit_status : exit status of the executed command |
|
41
|
|
|
* output : console output of the executed command |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function liveExecuteCommand($cmd) |
|
44
|
|
|
{ |
|
45
|
|
|
// while (@ ob_end_flush()); // end all output buffers if any |
|
46
|
|
|
|
|
47
|
|
|
if (is_array($cmd)) { |
|
48
|
|
|
foreach ($cmd as $item) { |
|
49
|
|
|
$this->liveExecuteCommand($item); |
|
50
|
|
|
} |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$cmd = $this->replaceVariables($cmd); |
|
55
|
|
|
echo "\n>> $cmd\n"; |
|
56
|
|
|
|
|
57
|
|
|
$complement = " 2>&1 ; echo Exit status : $?"; |
|
58
|
|
|
if ($this->getOs() === "Windows") { |
|
59
|
|
|
$complement = ' & echo Exit status : %errorlevel%'; |
|
60
|
|
|
} |
|
61
|
|
|
$proc = popen("$cmd $complement", 'r'); |
|
62
|
|
|
|
|
63
|
|
|
$completeOutput = ""; |
|
64
|
|
|
|
|
65
|
|
|
while (!feof($proc)) { |
|
|
|
|
|
|
66
|
|
|
$liveOutput = fread($proc, 4096); |
|
|
|
|
|
|
67
|
|
|
$completeOutput = $completeOutput . $liveOutput; |
|
68
|
|
|
echo "$liveOutput"; |
|
69
|
|
|
@ flush(); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
pclose($proc); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
// get exit status |
|
75
|
|
|
preg_match('/[0-9]+$/', $completeOutput, $matches); |
|
76
|
|
|
|
|
77
|
|
|
$exitStatus = intval($matches[0]); |
|
78
|
|
|
// if ($exitStatus !== 0) { |
|
79
|
|
|
// exit($exitStatus); |
|
80
|
|
|
// } |
|
81
|
|
|
|
|
82
|
|
|
// return exit status and intended output |
|
83
|
|
|
return array ( |
|
84
|
|
|
'exit_status' => $exitStatus, |
|
85
|
|
|
'output' => str_replace("Exit status : " . $matches[0], '', $completeOutput) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected $dockerVariables = null; |
|
90
|
|
|
protected function getDockerVariables() |
|
91
|
|
|
{ |
|
92
|
|
|
// Builder Variables |
|
93
|
|
|
if ($this->dockerVariables === null) { |
|
94
|
|
|
$this->dockerVariables = [ |
|
95
|
|
|
'%env%' => Psr11::environment()->getCurrentEnv(), |
|
96
|
|
|
'%workdir%' => $this->workdir |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
// Get User Variables |
|
100
|
|
|
$variables = Psr11::container()->get('BUILDER_VARIABLES'); |
|
101
|
|
|
foreach ((array)$variables as $variable => $value) { |
|
102
|
|
|
$this->dockerVariables["%$variable%"] = $this->replaceVariables($value); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->dockerVariables; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string|\Closure $variableValue |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function replaceVariables($variableValue) |
|
114
|
|
|
{ |
|
115
|
|
|
// Builder Variables |
|
116
|
|
|
$args = $this->getDockerVariables(); |
|
117
|
|
|
|
|
118
|
|
|
if ($variableValue instanceof \Closure) { |
|
119
|
|
|
$string = $variableValue($args); |
|
120
|
|
|
} else { |
|
121
|
|
|
$string = $variableValue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Replace variables into string |
|
125
|
|
|
foreach ($args as $arg => $value) { |
|
126
|
|
|
$string = str_replace($arg, $value, $string); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $string; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|