-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
171 lines (130 loc) · 4.73 KB
/
setup.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
#!/bin/php
<?php
declare(strict_types=1);
// Since this is a PHP repository template, using PHP for a wizard instead of
// bash makes reading it a bit easier and more cross-platform compatible.
//
// Since this is a one-time script without any composer dependencies,
// it's also not as pretty it can be with fancy libraries.
function execOrFail(string $command): string
{
$output = exec($command, $lines, $return);
if ($return !== 0) {
throw new RuntimeException("Could execute command: '$command' (exit code $return)");
}
return $output;
}
/** Detect as many variables from the git repository as possible. */
function detectGitSettings(): array
{
$name = '';
$repo = '';
echo '- Detecting git remote URL...';
$origin = execOrFail('git remote get-url origin');
if (preg_match('/github\.com[\/:](\w+)\/([^.\/]+)/i', $origin, $matches) !== false) {
[, $name, $repo] = $matches;
echo 'OK';
} else {
echo 'NO MATCH';
}
echo PHP_EOL;
echo '- Detecting author email...';
$mail = execOrFail('git config user.email || git config --global user.email || echo');
echo 'OK' . PHP_EOL;
return ['name' => $name, 'repo' => $repo, 'email' => $mail];
}
function getDisplayName(string $str): string
{
$str = ucwords(preg_replace('/[^a-z]+/i', ' ', $str));
return trim(preg_replace('/\s{2,}/', '', $str));
}
function getTitleCase(string $str): string
{
return str_replace(' ', '', getDisplayName($str));
}
/** Grabs user input and allows script cancellation */
function getInput(string $label, ?string $default = ''): string
{
echo "> $label"
. ($default !== '' ? " [default: $default]" : '')
. ': ';
$input = fgets(STDIN);
if ($input === false) {
// Signal interrupt (e.g. by CTRL+C)
exit(1);
}
$input = trim($input);
if ($input !== '') {
return $input;
}
if ($default === '') {
return getInput($label, $default);
}
return $default;
}
function replaceInFolder(array $vars)
{
// Allow different variants of the variables
$patterns = [];
foreach ($vars as $key => $value) {
$patterns["_{$key}_"] = $value;
$patterns[":{$key}"] = $value;
}
// Grab files from git so we get a clean list instead of ignores folders and the like
$files = rtrim(exec('git ls-tree --full-tree -r --full-name --name-only -z HEAD'), "\x00");
$files = explode("\x00", $files);
foreach ($files as $entry) {
echo " ./$entry" . PHP_EOL;
$newFileName = strtr($entry, $patterns);
file_put_contents($newFileName, strtr(file_get_contents($entry), $patterns));
if ($newFileName !== $entry) {
unlink($entry);
}
}
}
function runWizard()
{
echo 'Welcome to the setup assistant of this package template.' . PHP_EOL;
echo 'This wizard will ask a few questions to fill in the placeholders.' . PHP_EOL;
echo '(you can use CTRL+C to stop at any time).' . PHP_EOL;
echo PHP_EOL;
$gitSettings = detectGitSettings();
$vars = [
'date_year' => date('Y'),
];
echo PHP_EOL;
$vars['author_name'] = getInput('Github user or organization name', $gitSettings['name']);
$vars['author_mail'] = getInput('Author email address', $gitSettings['email']);
$vars['package_name'] = getInput('Github repository name', $gitSettings['repo']);
$vars['package_description'] = getInput('Project description (short sentence)');
$vars['display_name'] = getInput('Display name', getDisplayName($vars['package_name']));
$vars['composer_tags'] = getInput('List of tags (separated by comma)', 'laravel');
$vars['vendor_name'] = getInput('Composer Package vendor', getTitleCase($vars['author_name']));
$vars['vendor_package'] = getInput('Composer Package vendor', getTitleCase($vars['package_name']));
echo PHP_EOL;
foreach ($vars as $key => $value) {
printf(' %-20.20s %s' . PHP_EOL, $key, $value);
}
echo PHP_EOL;
if (strtolower(getInput('Are the above settings ok? (y/N)')) !== 'y') {
exit(1);
}
$vars['composer_tags'] = implode('","', array_map('trim', explode(',', $vars['composer_tags'])));
echo PHP_EOL;
echo '- Replacing variables:' . PHP_EOL;
replaceInFolder($vars);
rename(__DIR__ . '/.github/workflows/ci.yml.dist', __DIR__ . '/.github/workflows/ci.yml');
file_put_contents(
__DIR__ . '/README.md',
strstr(file_get_contents(__DIR__ . '/README.md'), '<!--README_START-->', false),
);
unlink(__FILE__);
echo PHP_EOL . 'Don\'t forget to add the codecov token!' . PHP_EOL;
echo PHP_EOL . 'OK.' . PHP_EOL;
}
// Off we go
try {
runWizard();
} catch (Exception $e) {
echo "ERROR: {$e->getMessage()}" . PHP_EOL;
}