-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.php
75 lines (58 loc) · 2.21 KB
/
configure.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
<?php
function process_files($dir, $packageName, $namespace)
{
if (!is_dir($dir)) {
echo "* error: $dir is not a directory.\n";
return;
}
$files = scandir($dir);
foreach($files as $file) {
if (in_array($file, ['.', '..', '.git', '.github', '.gitignore', 'configure.php'])) {
continue;
}
$file = "$dir/$file";
$relativeFn = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file);
if (!is_file($file)) {
process_files($file, $packageName, $namespace);
continue;
}
if (basename($file) === basename(__FILE__)) {
continue;
}
$content = file_get_contents($file);
$changed = false;
if (strpos($content, 'permafrost-dev/package-skeleton') !== false) {
$content = str_replace('permafrost-dev/package-skeleton', "permafrost-dev/{$packageName}", $content);
$changed = true;
}
if (preg_match('~package-skeleton~', $content) === 1) {
$content = str_replace('package-skeleton', "{$packageName}", $content);
$changed = true;
}
if (preg_match('~Permafrost\\\\?Example(;|\\\\?)~', $content) === 1) {
$content = preg_replace('~(Permafrost\\\\?)Example~', "$1{$namespace}", $content);
$changed = true;
}
if (strpos($content, 'Permafrost\\\\Example') !== false) {
$content = str_replace('Permafrost\\\\Example', "Permafrost\\\\{$namespace}", $content);
$changed = true;
}
if ($changed) {
file_put_contents($file, $content);
echo "* writing file $relativeFn\n";
$changed = false;
}
}
}
function validate_input($packageName, $namespace)
{
if (empty($packageName) || empty($namespace)) {
echo "* error: package name and namespace must not be empty.\n";
exit(1);
}
}
$packageName = trim(readline('project name: '));
$namespace = trim(readline('namespace: '));
validate_input($packageName, $namespace);
process_files(__DIR__, $packageName, $namespace);
echo "* finished configuring new package.\n";