forked from hakre/XMLReaderIterator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
517 lines (426 loc) · 12.8 KB
/
build.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env php
<?php
/**
* This file is part of the XMLReaderIterator package.
*
* Copyright (C) 2012, 2013, 2014 hakre <http://hakre.wordpress.com>
*
* build script
*/
$errors = 0;
$warnings = 0;
$buildDir = __DIR__ . '/build';
$concatenateDir = $buildDir . '/include';
$concatenateFile = $concatenateDir . '/xmlreader-iterators.php';
$autoLoadFile = __DIR__ . '/autoload.php';
### test if composer.json validates ###
built_test_composer_validate_json($errors);
### test if a valid version can be obtained from composer.json ###
$composerVersion = built_test_composer_json_get_version($errors);
### test if autoload.php contains all classes ###
build_test_autoload_file($errors, $autoLoadFile);
### test if tests run clean ###
build_test_tests($errors);
if ($errors) {
printf("ERROR: Build (Tests only) had %d errors, quitting.\n", $errors);
return;
}
### clean ###
build_make_clean($errors, $buildDir, $concatenateDir);
### create concatenateFile ###
build_create_concatenate_file($errors, $concatenateFile, $autoLoadFile, $composerVersion);
copy_file_to_dir('README.md', $concatenateDir);
### conditional build target into gist ###
$gistDir = __DIR__ . '/../' . basename(__dir__) . '-Gist-5147685';
if (is_dir($gistDir)) {
copy_dir_to_dir($concatenateDir, $gistDir);
} else {
printf("INFO: Gist build target directory not found.\n");
}
if ($errors) {
printf("ERROR: Build had %d errors.\n");
}
/**
* @param $errors
*
* @return string|null
*/
function built_test_composer_json_get_version(&$errors)
{
$file = 'composer.json';
$data = json_decode_file($file, true);
$version = isset($data['version']) ? $data['version'] : null;
if (!strlen($version)) {
echo "ERROR: Unable to obtain version from composer.json.\n";
$errors++;
return null;
}
echo "INFO: composer.json version is $version.\n";
if (!preg_match('~^\d.\d.\d$~', $version)) {
echo "ERROR: Unable to validate version '$version'.\n";
$errors++;
return null;
}
return $version;
}
/**
* @see http://php.net/json_decode
*
* @param $path
* @param bool $assoc
* @param int $depth
* @param int $options
*
* @return mixed
*/
function json_decode_file($path, $assoc = false, $depth = 512, $options = 0)
{
return json_decode(file_get_contents($path), $assoc, $depth, $options);
}
/**
* @param $errors
*/
function built_test_composer_validate_json(&$errors)
{
echo "INFO: Validating composer.json before building:\n";
$composer = 'composer';
$command = "$composer --no-ansi --version";
$lastline = exec($command, $output, $exitCode);
list($versionLine) = $output;
if (!preg_match('~^Composer version [0-9a-f]{40} 2\d{3}-(?:0\d|1[0-2])-(?:[0-2]\d|3[0-1]) (?:[0-1]\d|2[0-3]):[0-5]\d:(?:[0-5]\d|60)$~', $versionLine)) {
echo "ERROR: Unable to invoke Composer.\n";
$errors++;
return;
}
$command = "$composer --no-ansi validate";
system($command, $exitCode);
if ($exitCode !== 0) {
echo "ERROR: Composer json validation did return exit code $exitCode which is not 0.\n";
$errors++;
return;
}
echo "INFO: composer.json validation did pass. You might need to review warnings your own.\n";
return;
}
/**
* @param $errors
*/
function build_test_tests(&$errors)
{
echo "INFO: Running phpunit testuite before building:\n";
$phpunit = 'phpunit';
$command = "$phpunit --version";
$lastline = exec($command, $output, $exitCode);
list($versionLine) = $output;
if (!preg_match('~^PHPUnit \d\.\d\.\d+ by Sebastian Bergmann\.$~', $versionLine)) {
echo "ERROR: Unable to invoke PHPUnit.\n";
$errors++;
return;
}
$command = "$phpunit --stop-on-failure tests";
$result = system($command, $exitCode);
if ($result === false) {
echo "ERROR: Unable to invoke PHPUnit tests.\n";
$errors++;
return;
}
if ($exitCode !== 0) {
echo "ERROR: PHPUnit did return exit code $exitCode which is not 0.\n";
$errors++;
return;
}
echo "INFO: phpunit testuite did pass.\n";
return;
}
/**
* @param $errors
* @param $autoLoadFile
*/
function build_test_autoload_file(&$errors, $autoLoadFile)
{
require_once($autoLoadFile);
foreach (glob('src/*.php') as $file) {
$class = basename($file, '.php');
if (!class_exists($class) && !interface_exists($class)) {
echo "ERROR: ", $class, " does not exists.\n";
$errors++;
}
}
}
/**
* @param $errors
* @param $concatenateFile
* @param $autoLoadFile
*
* @internal param $buildDir
* @internal param $concatenateFileHandle
*/
function build_create_concatenate_file(&$errors, $concatenateFile, $autoLoadFile, $version)
{
if (!is_dir(dirname($concatenateFile))) {
echo "ERROR: target dir '", dirname($concatenateFile), "' missing.\n";
$errors++;
return;
} else {
$concatenateFileHandle = fopen($concatenateFile, 'c+');
if (!$concatenateFileHandle) {
echo "ERROR: concatenateFile '$concatenateFile' can not be created.\n";
$errors++;
return;
}
}
### write concatenateFile based on autoload.php ###
$pattern = '~^require .*\'/([^.]*\.php)\';$~';
$lines = preg_grep($pattern, file($autoLoadFile));
if (!$lines) {
echo "ERROR: Problem parsing file.\n";
}
$count = 0;
foreach ($lines as $line) {
$result = preg_match($pattern, $line, $matches);
if (!$result) {
echo "ERROR: Problem parsing file.\n";
continue;
}
$file = sprintf('src/%s', $matches[1]);
$handle = fopen($file, 'r');
if (!$handle) {
echo "ERROR: Can not open file '$file' for reading.\n";
continue;
}
if (!isset($concatenateFileHandle)) {
fclose($handle);
continue;
}
if ($count !== 0 && false === fseek_first_empty_line($handle)) { // first file is complete copy
echo "ERROR: Problem reading file until first empty line.\n";
continue;
}
stream_copy_to_stream($handle, $concatenateFileHandle);
fclose($handle);
$count++;
}
printf("INFO: concatenated %d files into %s.\n", $count, cwdname($concatenateFile));
### change the part of line ###
do {
$buffer = stream_get_contents($concatenateFileHandle, 512, 0);
$search = ' * This file is part of the XMLReaderIterator package.';
$replace = ' * XMLReaderIterator <http://git.io/xmlreaderiterator>';
$length = strlen($search);
if ($length !== strlen($replace)) {
echo "ERROR: Search and replace must have the same length.\n";
$errors++;
break;
}
$pos = strpos($buffer, $search);
if (!$pos) {
echo "ERROR: Unable to find search string in first 512 bytes.\n";
$errors++;
break;
}
$buffer = substr_replace($buffer, $replace, $pos, $length);
$bytesWritten = stream_put_contents($concatenateFileHandle, $buffer, 0, $pos + $length);
if (false === $bytesWritten) {
echo "ERROR: Failed to put first 512 bytes into stream.\n";
$errors++;
break;
}
} while (false);
fclose($concatenateFileHandle);
$buffer = file_get_contents($concatenateFile);
$search = " * @license AGPL-3.0 <http://spdx.org/licenses/AGPL-3.0>\n */";
$replace = " * @license AGPL-3.0 <http://spdx.org/licenses/AGPL-3.0>\n * @version $version\n */";
$pos = strpos($buffer, $search);
if (!$pos) {
echo "ERROR: Unable to find search string in buffer.\n";
$errors++;
return;
}
$buffer = substr_replace($buffer, $replace, $pos, strlen($search));
if (!is_string($buffer)) {
echo "ERROR: Failed to replace in buffer.\n";
$errors++;
return;
}
$bytesWritten = file_put_contents($concatenateFile, $buffer);
if (false === $bytesWritten) {
echo "ERROR: Failed to write back to file.\n";
$errors++;
return;
}
}
/**
* @param $errors
* @param $buildDir
* @param $concatenateDir
*/
function build_make_clean(&$errors, $buildDir, $concatenateDir)
{
if (is_dir($buildDir)) {
deltree($buildDir);
}
if (is_dir($buildDir)) {
printf("ERROR: cannot clean buildDir %s .\n", cwdname($buildDir));
$errors++;
} else {
mkdir($buildDir);
mkdir($concatenateDir);
}
}
/**
* @param $handle
*
* @return bool
*/
function fseek_first_empty_line($handle)
{
$lastLine = 0;
while (false !== $line = fgets($handle)) {
if ('' === rtrim($line, "\r\n")) {
break;
}
$lastLine += strlen($line);
}
if ($line === false) {
return false;
}
return !fseek($handle, $lastLine);
}
/**
* shorten pathname realtive to cwd
*
* @param $path
*
* @return string
*/
function cwdname($path)
{
static $base;
$base || $base = realpath('.');
$result = realpath($path);
if (substr($result, 0, strlen($base)) === $base) {
$result = '.' . substr($result, strlen($base));
} else {
echo "INFO: File '$path' not relative to cwd. Please verify.\n";
}
return strtr($result, '\\', '/');
}
/**
* copy files from one directory into another.
*
* @param string $sourceDir
* @param string $targetDir
*/
function copy_dir_to_dir($sourceDir, $targetDir)
{
foreach (new DirectoryIterator($sourceDir) as $file) {
if (!$file->isFile()) {
continue;
}
copy_file_to_dir($file->getPathname(), $targetDir);
}
}
/**
* copy file into directory
*
* @param string $file
* @param string $targetDir
*
* @return bool
*/
function copy_file_to_dir($file, $targetDir)
{
$target = rtrim($targetDir, '/\\') . '/' . basename($file);
if (realpath($file) === realpath($target)) {
echo "INFO: source and target in copy_to_dir() are the same.\n";
return true; // already copied
}
$result = copy($file, $target);
if ($result) {
printf("INFO: copied %s to %s.\n", cwdname($file), cwdname($target));
}
return $result;
}
/**
* deltree() - delete a directory incl. subdirectories and files therein.
*
* implemented as a stack so that no recursion is necessar and
* traversal is fast.
*
* @param $path
*/
function deltree($path)
{
if (!is_dir($path) || is_link($path)) {
echo "ERROR: given path rejected by deltree.\n";
return;
}
$stack = array($path);
$rmdirStack = array();
while ($stack) {
$path = array_pop($stack);
$it = new DirectoryIterator($path);
foreach ($it as $file) {
/* @var $file DirectoryIterator */
if ($file->isDot()) {
continue;
}
$localPath = $path . '/' . $file->getBasename();
if ($file->isDir()) {
$stack[] = $localPath;
} elseif ($file->isLink() || $file->isFile()) {
$result = unlink($localPath);
if (!$result) {
echo "ERROR: Failed to delete file '$localPath'. Expecting more problems.\n";
}
} else {
printf(
"ERROR: Unknown processing for %s [%s] (%s) isDot: %d\n",
$file,
get_class($file),
$localPath,
$file->isDir()
);
}
}
unset($file, $it);
array_unshift($rmdirStack, $path);
}
clearstatcache(true);
foreach ($rmdirStack as $path) {
chmod($path, 0777);
clearstatcache(true, $path);
$result = @rmdir($path);
if (!$result) {
echo "ERROR: Failed to delete directory '$path'. Skipping rest.\n";
break;
}
}
}
/**
* @param resource $handle destination stream
* @param string $data data to write
* @param int $offset offset in destination stream if specified
* @param int $maxlength specify bytes to write if specified
*
* @return bool|int
* @internal param string $string string to write
*/
function stream_put_contents($handle, $data, $offset = null, $maxlength = null)
{
if (!is_resource($handle) or 'stream' !== get_resource_type($handle)) {
trigger_error('Destination is not a stream resource type.');
return false;
}
$length = strlen($data);
if (null !== $maxlength) {
$length = max(0, (int) $maxlength);
}
if (null !== $offset) {
if (-1 === fseek($handle, $offset)) {
trigger_error('Unable to seek.');
return false;
}
}
return fwrite($handle, $data, $length);
}