-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathready.php
351 lines (278 loc) · 9.41 KB
/
ready.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
<?php
declare(strict_types = 1);
use GuzzleHttp\Client;
use function Laravel\Prompts\{error, info, multiselect, select, text};
use Symfony\Component\Process\Process;
$env = environment();
if (!file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
exit("Please, run \"composer install\" before running this script." . PHP_EOL);
}
if ($env['APP_ENV'] === 'production') {
exit("For safety, you cannot run this script in production." . PHP_EOL);
}
require $autoload;
$actions = [
'executePintPreparation' => '[Dev. Tool] Install Laravel Pint',
'executeLaraStanPreparation' => '[Dev. Tool] Install LaraStan',
'executeLaravelDebugBarPreparation' => '[Dev. Tool] Install Laravel DebugBar',
'executeIdeHelperPreparation' => '[Dev. Tool] Install Laravel IDE Helper',
'executeClockWorkPreparation' => '[Dev. Tool] Install Clock Work',
'executeLivewirePreparation' => 'Install Livewire',
'executeCommentsRemoval' => 'Remove Unnecessary Laravel Comments',
'executeCaptainHook' => 'Creating captain hook',
];
$type = select('What do you want to do?', [
'packages' => 'Install Dev. Tools Packages',
'project' => 'Prepare New Project',
]);
if ($type === 'packages') {
$packages = collect($actions)
->filter(fn ($key) => str_contains($key, '[Dev. Tool]'))
->mapWithKeys(fn ($value, $key) => [$key => str_replace('[Dev. Tool] ', '', $value)])
->toArray();
$steps = multiselect('Select the packages:', $packages, scroll: 20, required: true);
} else {
$actions = collect($actions)
->map(fn ($value) => str_replace('[Dev. Tool] ', '', $value))
->toArray();
$steps = multiselect('What do you want to do?', $actions, scroll: 20, required: true);
}
/** Steps Execution */
foreach ($steps as $step) {
info($step . "...");
try {
if (($result = $step()) !== true) {
throw new Exception($result);
}
info($step . " ✅") . PHP_EOL;
} catch (Exception $exception) {
error($exception->getMessage()) . PHP_EOL;
result($exception->getMessage());
}
}
/** End Steps Execution */
function environment(): array
{
$response = [];
$handle = fopen(".env", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$arrayLine = explode("=", $line);
$first = array_shift($arrayLine);
if (!empty($first)) {
$response[$first] = implode("=", $arrayLine);
}
}
fclose($handle);
}
return $response;
}
function executePintPreparation(): bool|string
{
try {
if (verifyInstallDependency("laravel/pint") && ($status = executeCommand(
"composer require laravel/pint --dev"
)) !== true) {
return $status;
}
$endpoint = text(
'What address is your pint configuration?',
default: 'https://raw.githubusercontent.com/bhcosta90/laravel-ready/main/pint.json'
);
$content = (new Client())->get($endpoint);
file_put_contents(
'pint.json',
$content->getBody()->getContents()
);
$composer = json_decode(file_get_contents('composer.json'));
$composer->scripts->format = './vendor/bin/pint';
file_put_contents('composer.json', json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
function executeLaraStanPreparation(): bool|string
{
try {
if (verifyInstallDependency("larastan/larastan") && ($status = executeCommand(
"composer require larastan/larastan --dev"
)) !== true) {
return $status;
}
$endpoint = text(
'What address is your lara stan configuration?',
default: 'https://raw.githubusercontent.com/bhcosta90/laravel-ready/main/phpstan.neon'
);
$content = (new Client())->get($endpoint);
file_put_contents(
'phpstan.neon',
$content->getBody()->getContents()
);
$composer = json_decode(file_get_contents('composer.json'));
$composer->scripts->analyse = './vendor/bin/phpstan analyse';
file_put_contents('composer.json', json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
function executeLaravelDebugBarPreparation(): bool|string
{
if (verifyInstallDependency("barryvdh/laravel-debugbar")) {
return executeCommand("composer require barryvdh/laravel-debugbar --dev");
}
return 'Laravel debugbar is already installed';
}
function executeIdeHelperPreparation(): bool|string
{
if (verifyInstallDependency("barryvdh/laravel-ide-helper")) {
return executeCommand("composer require barryvdh/laravel-ide-helper --dev");
}
return 'Laravel ide helper is already installed';
}
function executeLivewirePreparation(): bool|string
{
if (!verifyInstallDependency('livewire/livewire')) {
return 'Livewire is already installed in a different version.';
}
$livewire = select('Select Livewire version:', [
'livewire/livewire:^3.0' => 'Livewire [3.x]',
'livewire/livewire:^2.0' => 'Livewire [2.x]',
]);
try {
if (($result = executeCommand("composer require $livewire")) !== true) {
return $result;
}
if ($livewire === 'livewire/livewire:^2.0') {
return true;
}
return executeAlpineJsRemovalPreparation();
} catch (Exception $e) {
return $e->getMessage();
}
}
function executeAlpineJsRemovalPreparation(): bool|string
{
try {
if (($status = executeCommand("npm remove alpinejs")) !== true) {
return $status;
}
unlink(__DIR__ . '/resources/js/app.js');
file_put_contents(__DIR__ . '/resources/js/app.js', "import './bootstrap';");
return executeCommand("npm run build");
} catch (Exception $e) {
return $e->getMessage();
}
}
function executeCommentsRemoval(): bool
{
$path = text("Which folder do you want to remove comments from?", default: 'app');
$extension = text("Which extension do you want to remove comments from?", default: 'php');
return executeCommand(
'find "' . __DIR__ . '/' . $path . '" -type f -name "*.' . $extension . '" -exec sh -c \'sed "/\/\*\*/,/\*\//d" "$0" > "$0.tmp" && mv "$0.tmp" "$0"\' {} \;'
);
}
function executeCaptainHook()
{
if (!is_dir('.git')) {
executeCommand('git init');
}
$executeWhen = select('What type of execution do you wanna do??', [
'pre-push',
'pre-commit',
], scroll: 20, required: true);
$executionSteps = multiselect('What do you want to do?', [
'test',
'lara stan',
'pint',
], scroll: 20, required: true);
$actions = [];
foreach ($executionSteps as $step) {
switch ($step) {
case 'lara stan':
$actions[] = "./vendor/bin/phpstan analyse";
break;
case 'test':
$actions[] = !verifyInstallDependency('pestphp/pest')
? "./vendor/bin/pest --parallel"
: "./vendor/bin/phpunit";
break;
case 'pint':
$actions[] = "./vendor/bin/pint --test";
break;
}
}
$content = "";
foreach ($actions as $key => $action) {
if ($key > 0) {
$content .= "\n";
}
$content .= " {\"action\": \"{$action}\"},";
}
$content = substr($content, 0, -1);
if (verifyInstallDependency('captainhook/captainhook') && executeCommand(
"composer require captainhook/captainhook --dev"
) !== true) {
return false;
}
if (verifyInstallDependency('captainhook/captainhook') && executeCommand(
"composer require captainhook/captainhook-phar --dev"
) !== true) {
return false;
}
$content = <<<CONTENT
{
"{$executeWhen}": {
"enabled": true,
"actions": [
{$content}
]
}
}
CONTENT;
file_put_contents('captainhook.json', $content);
if (executeCommand("./vendor/bin/captainhook install -f -s") !== true) {
return false;
}
return true;
}
function executeClockWorkPreparation(){
if (verifyInstallDependency("itsgoingd/clockwork")) {
return executeCommand("composer require itsgoingd/clockwork --dev");
}
return 'Clock work is already installed';
}
function result(string $message): void
{
file_put_contents('storage/logs/laravel-ready.log', $message . PHP_EOL, FILE_APPEND);
}
function executeCommand(string $command): bool|string
{
try {
Process::fromShellCommandline("$command")
->setTty(false)
->setTimeout(null)
->run();
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
function verifyInstallDependency(string $package): bool
{
$composer = json_decode(file_get_contents('composer.json'));
return !(array_key_exists($package, (array) $composer->require) || array_key_exists(
$package,
(array) $composer->{'require-dev'}
));
}
$type = select('Do you want to remove this file?', [
true => 'True',
false => 'False',
]);
if ($type) {
info("Your project is ready to be used! 🚀 Deleting script in 5 seconds...");
sleep(5);
unlink(__FILE__);
}