-
Notifications
You must be signed in to change notification settings - Fork 18
/
pear-package-helper.php
68 lines (56 loc) · 2.05 KB
/
pear-package-helper.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
#!/usr/bin/env php
<?php
//
// Copies all files mentioned in <contents> of package.xml to a temporary dir, enabling commented out
// require_once statements. The result may be packaged with "pear package".
//
// This is needed as <tasks:replace> tags in package.xml do not allow arbitrary substitutions, unfortunately.
//
$translations = [
'// pear-package-only ' => ''
];
function handleFile(SimpleXMLElement $file, $dirName)
{
global $translations;
if (false === ($pos = strpos($file['name'], '/'))) {
$targetDir = './.pear-package' . substr($dirName, 1);
} else {
$targetDir = './.pear-package' . substr($dirName, 1) . '/'
. substr($file['name'], 0, $pos);
if (!is_dir($targetDir)) {
echo "Creating {$targetDir}" . PHP_EOL;
mkdir($targetDir, 0777, true);
}
}
if (!preg_match('/.php$/', $file['name'])) {
echo "Copying {$dirName}/{$file['name']} to {$targetDir}" . PHP_EOL;
copy("{$dirName}/{$file['name']}", './.pear-package' . substr($dirName, 1) . '/' . $file['name']);
} else {
echo "Mangling {$dirName}/{$file['name']} and saving to {$targetDir}" . PHP_EOL;
$text = file_get_contents($dirName . '/' . $file['name']);
file_put_contents('./.pear-package' . substr($dirName, 1) . '/' . $file['name'], strtr($text, $translations));
}
}
function handleDir(SimpleXMLElement $dir, $dirName = null)
{
if (null === $dirName) {
$dirName = '.';
} else {
$dirName .= '/' . $dir['name'];
}
$targetDir = './.pear-package' . substr($dirName, 1);
if (!is_dir($targetDir)) {
echo "Creating {$targetDir}" . PHP_EOL;
mkdir($targetDir, 0777, true);
}
foreach ($dir->children() as $child) {
if ('dir' === $child->getName()) {
handleDir($child, $dirName);
} else {
handleFile($child, $dirName);
}
}
}
$package = simplexml_load_file('./package.xml');
handleDir($package->contents->dir);
copy('./package.xml', './.pear-package/package.xml');