-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure.php
131 lines (99 loc) · 3.44 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
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
#!/usr/bin/env php
<?php
function ask(string $question, string $default = ''): string
{
$answer = readline($question . ($default ? " ({$default})" : null) . ': ');
if (! $answer) {
return $default;
}
return $answer;
}
function confirm(string $question, bool $default = false): bool
{
$answer = ask($question .' (' . ($default ? 'Y/n' : 'y/N') . ')');
if (! $answer) {
return $default;
}
return strtolower($answer) === 'y';
}
function writeln(string $line): void
{
echo $line.PHP_EOL;
}
function slugify(string $subject): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-'));
}
function titleCase(string $subject, bool $removeSpaces = false): string
{
$value = ucwords(str_replace(['-', '_'], ' ', $subject));
return $removeSpaces ? str_replace(' ', '', $value) : $value;
}
function replaceInFile(string $file, array $replacements): void
{
$contents = file_get_contents($file);
file_put_contents($file, str_replace(
array_keys($replacements),
array_values($replacements),
$contents
));
}
function separator(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
function run(string $command): string
{
return trim((string) shell_exec($command));
}
function getReplaceableFiles(): array
{
$namedFiles = explode(PHP_EOL, run('find ./* -name "pressbooks-plugin-scaffold*"'));
$contentFiles = explode(PHP_EOL, run('grep -E -r -l -i "PressbooksPluginScaffold|pressbooks-plugin-scaffold|Pressbooks Plugin Scaffold" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__)));
return array_unique(
array_merge($namedFiles, $contentFiles)
);
}
// --------------- SCRIPT START --------------- //
$currentDirectory = getcwd();
$pluginName = ask('Plugin name', titleCase(basename($currentDirectory), removeSpaces: false));
$kebabCaseName = slugify($pluginName);
$titleCaseName = ask('Namespace name', titleCase($pluginName, removeSpaces: true));
$description = ask('Plugin description', "This is my plugin {$kebabCaseName}");
writeln('------');
writeln("Plugin name : {$pluginName}");
writeln("Plugin : {$kebabCaseName} <{$description}>");
writeln("Namespace : Pressbooks\\{$titleCaseName}");
writeln("Classname : {$titleCaseName}");
writeln("Root file name : {$kebabCaseName}");
writeln('------');
writeln('This script will replace the above values in all relevant files in the project directory.');
if (! confirm('Modify files?', true)) {
exit(1);
}
$files = getReplaceableFiles();
foreach ($files as $file) {
if (str_contains($file, 'README.md')) {
$contents = file_get_contents($file);
file_put_contents(
$file,
<<<TEXT
# {$pluginName}
{$description}
TEXT
);
} else {
replaceInFile($file, [
'pressbooks-plugin-scaffold' => $kebabCaseName,
'PressbooksPluginScaffold' => $titleCaseName,
'Pressbooks Plugin Scaffold' => $pluginName,
'A scaffold for Pressbooks plugins.' => $description,
]);
}
if (str_contains($file, 'pressbooks-plugin-scaffold')) {
rename($file, str_replace('pressbooks-plugin-scaffold', $kebabCaseName, $file));
}
}
confirm('Execute `composer install` & `npm install`?') && run('composer install & npm install');
confirm('Let this script delete itself?', true) && unlink(__FILE__);
writeln('Script completed.');