-
Notifications
You must be signed in to change notification settings - Fork 1
/
script-compile
executable file
·228 lines (183 loc) · 6.08 KB
/
script-compile
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
#!/usr/bin/php -q
<?php
ini_set("display_errors", true);
error_reporting(E_ALL);
define('PROJECT_DIR', realpath(dirname($_SERVER['argv'][0])));
define('PROJECT_COMMAND', "mvn clean compile");
define('PROJECT_CLEAN_REPOSITORY', false);
$PROJECT_DIR = PROJECT_DIR;
$PROJECT_COMMAND = PROJECT_COMMAND;
echo <<<EOF
===============================================================================
| Run '$PROJECT_COMMAND'
| in $PROJECT_DIR
|
| @author Moises P. Sena <[email protected]>
===============================================================================
EOF;
class Main {
public function __construct() {
$pomFile = PROJECT_DIR . '/pom.xml';
$xml = simplexml_load_file($pomFile);
if(!empty($xml->parent)) {
$xml->groupId = $xml->parent->groupId;
$xml->version = $xml->parent->version;
}
$artifactId = trim((string) $xml->artifactId);
$groupId = trim((string) $xml->groupId);
$version = trim((string) $xml->version);
if(PROJECT_CLEAN_REPOSITORY) {
$paths = array();
$this->getPaths($xml, PROJECT_DIR, $paths);
foreach ($paths as $path) {
$path = escapeshellarg($path);
Logger::info("Clean MVN cache repository ({$path})");
Shell::exec("rm -vrf $path", false);
}
}
$projectDir = escapeshellarg(PROJECT_DIR);
Shell::exec("cd $projectDir && " . PROJECT_COMMAND);
}
private function getRepoPath($xml) {
if(!empty($xml->parent)) {
$xml->groupId = $xml->parent->groupId;
$xml->version = $xml->parent->version;
}
$artifactId = trim((string) $xml->artifactId);
$groupId = trim((string) $xml->groupId);
$version = trim((string) $xml->version);
$path = $_SERVER['HOME'] . "/.m2/repository/"
. str_replace(".", "/", $groupId)
. "/{$artifactId}/{$version}";
return $path;
}
private function getPaths($xml, $base, &$paths) {
$paths[] = $this->getRepoPath($xml);
if(!empty($xml->modules)) {
foreach($xml->modules->module as $m) {
$path = "{$base}/{$m}";
if(is_dir($path)) {
$xmlModule = simplexml_load_file($path . "/pom.xml");
$this->getPaths($xmlModule, $path, $paths);
}
}
}
}
}
class Shell {
const BUF_SIZ = 1024; // max buffer size
const FD_WRITE = 0; // stdin
const FD_READ = 1; // stdout
const FD_ERR = 2; // stderr
private static $lastError;
public static function printOut($msg, $error = false) {
if($error) {
fwrite(STDERR, $msg);
}
else {
fwrite(STDOUT, $msg);
}
}
private static function procExec($cmd, $outcall = null) {
self::$lastError = "";
if($outcall == null) {
$outcall = function($msg) {
Shell::printOut($msg);
};
}
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$first_exitcode = 0;
$ptr = proc_open($cmd, $descriptorspec, $pipes, NULL, $_ENV);
if (!is_resource($ptr)) {
return false;
}
$errbuf = "";
while (($buffer = fgets($pipes[self::FD_READ], self::BUF_SIZ)) != NULL
|| ($errbuf = fgets($pipes[self::FD_ERR], self::BUF_SIZ)) != NULL) {
if (!isset($flag)) {
$pstatus = proc_get_status($ptr);
$first_exitcode = $pstatus["exitcode"];
$flag = true;
}
if (strlen($buffer)) {
$outcall($buffer);
}
if (strlen ( $errbuf )) {
self::$lastError .= $errbuf;
}
}
foreach ($pipes as $pipe) {
fclose($pipe);
}
/* Get the expected *exit* code to return the value */
$pstatus = proc_get_status($ptr);
if (!strlen($pstatus["exitcode"]) || $pstatus["running"]) {
/* we can trust the retval of proc_close() */
if ($pstatus["running"]) {
proc_terminate($ptr);
}
$ret = proc_close($ptr);
} else {
if ((($first_exitcode + 256) % 256) == 255
&& (($pstatus["exitcode"] + 256) % 256) != 255) {
$ret = $pstatus["exitcode"];
}
elseif (!strlen($first_exitcode)) {
$ret = $pstatus["exitcode"];
}
elseif ((($first_exitcode + 256) % 256) != 255) {
$ret = $first_exitcode;
}
else {
$ret = 0; /* we "deduce" an EXIT_SUCCESS ;) */
}
proc_close($ptr);
}
return ($ret + 256) % 256;
}
public static function getEnv() {
$env = `env`;
$env = preg_replace("/^([^=]+)=([^\n]+)\n/msi", "export $1='$2'\n", $env) . "\n";
return $env;
}
public static function exec($cmd, $exit = true, $outcall = null) {
$cmd = self::getEnv () . "\n" . $cmd;
$ret = self::procExec ( $cmd, $outcall );
if ($ret === false) {
Logger::error ( "not enough FD or out of memory.", $exit );
} elseif ($ret == 127) {
Logger::error ( "Command not found (returned by sh).", $exit, $ret );
} else if ($ret) {
Logger::error ( "Command failed: $cmd" );
if (! empty ( self::$lastError )) {
Logger::error ( str_repeat ( "-", 60 ) );
Logger::error ( "Error Message:" );
Logger::error ( trim ( self::$lastError ) );
Logger::error ( str_repeat ( "-", 60 ) );
}
if ($exit) {
Logger::error ( "Abort operation" );
exit ( $ret );
}
}
}
}
class Logger {
public static function help() {
$cmd = $_SERVER ['argv'] [0];
}
public static function error($msg, $exit = false, $code = 1) {
Shell::printOut("[ERROR]: $msg\n", true);
if($exit) {
exit ( $code );
}
}
public static function info($msg) {
Shell::printOut("[INFO]: $msg\n");
}
}
new Main();