-
Notifications
You must be signed in to change notification settings - Fork 2
/
build
executable file
·637 lines (547 loc) · 17.2 KB
/
build
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/env php
<?php
use function Laravel\Prompts\{confirm, info, warning};
use function Laravel\Prompts\{multiselect, select, spin, suggest, text};
require_once __DIR__.'/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Helper functions
|--------------------------------------------------------------------------
|
| Functions used to help keep things DRY.
|
*/
function run($command): string
{
return trim(string: (string) shell_exec(command: $command));
}
function slugify(string $text): string
{
$text = str_replace(search: ' ', replace: '-', subject: $text);
$text = preg_replace(pattern: '/[^A-Za-z0-9\-]/', replacement: '', subject: $text);
$text = preg_replace(pattern: '/-+/', replacement: '-', subject: $text);
$text = trim($text, characters: '-');
return strtolower($text);
}
function camelCase(string $string): string
{
return preg_replace_callback(pattern: '/[-_](.)/', callback: function ($matches) {
return strtoupper($matches[1]);
}, subject: $string);
}
function kebabCase(string $string): string
{
return strtolower(preg_replace(pattern: '/([a-zA-Z])(?=[A-Z])/', replacement: '$1-', subject: $string));
}
function replaceInFile($search, $replace, $filename): void
{
file_put_contents(
$filename,
str_replace($search, $replace, file_get_contents($filename))
);
}
function addComposerData(array $data, string $filePath = 'composer.json'): bool
{
// Check if the file exists and is readable
if (! is_readable($filePath) || ! is_writable($filePath)) {
return false;
}
file_put_contents(filename: $filePath, data: '{}');
// Read and decode the existing composer.json file
$composerData = json_decode(json: file_get_contents($filePath), associative: true);
if ($composerData === null && json_last_error() !== JSON_ERROR_NONE) {
return false; // Invalid JSON
}
// Merge the new data
$composerData = array_merge_recursive($composerData, $data);
// Write the updated data back to composer.json
$result = file_put_contents(
filename: $filePath,
data: json_encode($composerData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
return ($result !== false);
}
/*
|--------------------------------------------------------------------------
| Prompt the user for package details
| --------------------------------------------------------------------------
|
| Prompts the user for the package details needed before scaffolding begins.
|
*/
/**
* Prompt the user to enter the name of the package maintainer
* Suggests: it gets the name from git config
*/
$packageAuthorName = suggest(
label: 'What is the package author\'s name?',
options: fn () => [run(command: 'git config user.name')],
required: true,
);
/**
* Prompt the user to enter the email of the package maintainer
* Suggests: it gets the email from git config
* Validation: it must be a valid email address
*/
$email = suggest(
label: 'What is the package author\'s email?',
options: fn () => [run(command: 'git config user.email')],
required: true,
validate: fn ($email) => match (true) {
! filter_var($email, FILTER_VALIDATE_EMAIL) => 'The email must be a valid email address',
default => null,
},
);
/**
* Prompt the user to enter the name of the package author
*/
$username = text(
label: 'What is your VCS username?',
required: true,
hint: 'This will be your VCS provider username.',
);
$username = slugify($username);
/**
* Prompt the user to enter the vendor namespace of the package
* Suggests: the vendor name
* Validation: it must be alphanumeric
*/
$vendorName = suggest(
label: 'What namespace should the package use?',
options: fn () => [str_replace(search: '-', replace: '', subject: ucwords($username))],
placeholder: 'Consider: '.ucwords($username),
required: true,
validate: fn ($vendor) => match (true) {
! preg_match(pattern: '/^[A-Za-z0-9\-]+$/i', subject: $vendor) => 'Vendor namespace must be alphanumeric',
! preg_match(pattern: '/^[A-Z]/i', subject: $vendor) => 'Vendor namespace must be capitalized',
default => null,
},
);
$vendorName = ucwords($vendorName);
$vendorNameSlug = slugify($vendorName);
/**
* Prompt the user to enter the name of the package
* Validation: it must be alphanumeric
*/
$packageName = suggest(
label: 'What name would you like to give your package?',
options: [basename(getcwd())],
required: true,
validate: fn ($value) => match (true) {
! preg_match(pattern: '/^[A-Za-z0-9\-\s]+$/i', subject: $value) => 'Package name must be alphanumeric',
default => null,
},
);
$packageName = slugify($packageName);
/**
* Prompt the user to enter the package description
*/
$packageDescription = text(
label: 'Describe what your package tries to accomplish',
required: true,
);
/**
* Prompt the user for the packages' Class name
* Suggests: the package name
* Validation: it must be alphanumeric
*/
$className = suggest(
label: 'Choose a class name for your package',
options: fn () => [str_replace(search: ' ', replace: '', subject: ucwords(string: str_replace(['-', '_'], replace: ' ', subject: $packageName)))],
required: true,
validate: fn ($value) => match (true) {
! preg_match(pattern: '/^[A-Za-z0-9\-]+$/i', subject: $value) => 'Class name must be alphanumeric',
default => null,
},
);
$className = ucwords($className);
/**
* Prompt for the minimum PHP version the package supports
*/
$phpVersion = select(
label: 'What is the minimum PHP version your package supports?',
options: [
'8.3',
'8.2',
'8.1',
],
default: '8.2',
);
/**
* prompt for the Laravel version the package supports
*/
$laravelVersion = select(
label: 'What is the minimum Laravel version your package supports?',
options: [
'11',
'10',
],
default: '10',
);
/**
* Prompt for which testing framework to use
*/
$testingFramework = select(
label: 'Select a Testing Framework',
options: [
'Pest',
'PHPUnit',
],
default: 'Pest',
);
/**
* Prompt the user to enable some extra dependencies and actions
* - Dependabot
* - Update CHANGELOG
* - Pint
* - PHPStan
* - Rector
*/
$enabledFeatures = multiselect(
label: 'Which extra features do you want enabled?',
options: [
'Dependabot',
'Update CHANGELOG',
'Pint',
'PHPStan',
'Rector',
],
default: [
'Dependabot',
'Update CHANGELOG',
],
);
/**
* If PHPStan is selected as an extra feature, see if Larastan should be enabled too
*/
if (isset($enabledFeatures[3])) {
$enableLarastan = confirm(label: 'Do you want to enable Larastan?');
}
/*
|--------------------------------------------------------------------------
| Scaffold the package
|--------------------------------------------------------------------------
|
| Scaffold the package using the details provided by the user.
|
*/
/**
* Check the git remote origin, and make sure it matches the package name.
*/
$gitRemoteOrigin = run(command: 'git config --get remote.origin.url');
if (str_contains(haystack: $gitRemoteOrigin, needle: 'skeleton.git')) {
$parts = explode(separator: '/', string: $gitRemoteOrigin);
$parts[count($parts) - 1] = $packageName.'.git';
$gitRemoteOrigin = implode(separator: '/', array: $parts);
run(command: "git remote set-url origin $gitRemoteOrigin");
}
// Add the source folders
$directories = [
'config',
'database' => [
'factories',
'migrations',
'seeders',
],
'resources' => [
'views',
],
'routes',
'src' => [
'Facades',
],
'tests',
];
// loop over $directories and create the folders
foreach ($directories as $key => $value) {
if (is_array($value)) {
foreach ($value as $subDir) {
$dirPath = $key.'/'.$subDir;
if (! file_exists($dirPath)) {
mkdir(directory: $dirPath, recursive: true);
}
}
} elseif (! file_exists($value)) {
mkdir(directory: $value, recursive: true);
}
}
// Add the config file
touch(filename: "config/$packageName.php");
file_put_contents(
filename: "config/$packageName.php",
data: "<?php\n\nreturn [\n\n];\n"
);
// Add the LICENCE file
replaceInFile(
search: [':year', ':fullName'],
replace: [date('Y'), $packageAuthorName],
filename: 'LICENSE',
);
if (! is_dir($directory = '.github')) {
mkdir(directory: $directory);
mkdir(directory: $directory.'/workflows');
mkdir(directory: $directory.'/ISSUE_TEMPLATE');
}
// Does the User want to use Dependabot?
if (in_array(needle: 'Dependabot', haystack: $enabledFeatures)) {
rename(from: 'stubs/dependabot.yml.stub', to: '.github/dependabot.yml');
rename(from: 'stubs/dependabot-auto-merge.yml.stub', to: '.github/workflows/dependabot-auto-merge.yml');
}
// Does the User want to use the "Update CHANGELOG" workflow?
if (in_array(needle: 'Update CHANGELOG', haystack: $enabledFeatures)) {
rename(from: 'stubs/update-changelog.yml.stub', to: '.github/workflows/update-changelog.yml');
}
/**
* Add needed files for submitting issues to the repo.
*/
rename(from: 'stubs/bug_report.yml.stub', to: '.github/ISSUE_TEMPLATE/bug_report.yml');
/**
* Add some meta data and main requirements.
*/
$composerDataStructure = [
'name' => "$vendorNameSlug/$packageName",
'description' => "$packageDescription",
'keywords' => [
'laravel',
"$packageName",
],
"homepage" => "https://github.com/$vendorNameSlug/$packageName",
"license" => "MIT",
"authors" => [
[
"name" => "$packageAuthorName",
"email" => "$email",
],
],
"require" => [
"php" => "^$phpVersion",
"illuminate/support" => "^$laravelVersion.0",
"spatie/laravel-package-tools" => "^1.14",
],
"require-dev" => [
"nunomaduro/collision" => "^7.0",
],
];
/**
* Add the development requirements.
*/
// Add a testing framework. These are the defaults.
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"orchestra/testbench" => "^8.0",
"phpunit/phpunit" => "^10.0",
]
);
// At this point, $packageName needs to be ucfirst
$packageName = ucfirst(camelCase(string: $packageName));
// Add the Facade
touch(filename: "src/Facades/$className.php");
$packageNameKebab = kebabCase($packageName);
$facadeCode = <<<EOT
<?php
namespace $vendorName\\$packageName\\Facades;
use Illuminate\Support\Facades\Facade;
class $className extends Facade
{
protected static function getFacadeAccessor(): string
{
return '$packageNameKebab';
}
}
EOT;
file_put_contents(
filename: "src/Facades/$className.php",
data: $facadeCode,
);
// Add the Service Provider
touch(filename: "src/{$className}ServiceProvider.php");
$serviceProviderCode = <<<EOT
<?php
namespace $vendorName\\$packageName;
use Illuminate\Support\ServiceProvider;
class {$className}ServiceProvider extends ServiceProvider
{
public function boot(): void
{
// ...
}
}
EOT;
file_put_contents(
filename: "src/{$className}ServiceProvider.php",
data: $serviceProviderCode,
);
rename(from: 'stubs/Arch.php.stub', to: 'tests/Arch.php');
rename(from: 'stubs/TestCase.php.stub', to: 'tests/TestCase.php');
replaceInFile(
search: [':vendorName', ':packageName'],
replace: [$vendorName, $packageName],
filename: 'tests/TestCase.php',
);
if ($testingFramework === 'PHPUnit') {
rename(from: 'stubs/run-tests.yml.stub', to: '.github/workflows/run-tests.yml');
replaceInFile(
search: [':phpVersion', ':laravelVersion'],
replace: [$phpVersion, $laravelVersion],
filename: '.github/workflows/run-tests.yml',
);
}
// Does the User want to use Pest?
if ($testingFramework === 'Pest') {
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"pestphp/pest" => "^2.0",
"pestphp/pest-plugin-arch" => "^2.0",
"pestphp/pest-plugin-laravel" => "^2.0",
]
);
touch(filename: 'tests/Pest.php');
file_put_contents(
filename: 'tests/Pest.php',
data: "<?php\n\nuse $vendorName\\$packageName\\Tests\TestCase;\n\nuses(TestCase::class)->in(__DIR__);\n"
);
rename(from: 'stubs/run-pest.yml.stub', to: '.github/workflows/run-pest.yml');
replaceInFile(
search: [':phpVersion', ':laravelVersion'],
replace: [$phpVersion, $laravelVersion],
filename: '.github/workflows/run-pest.yml',
);
}
// Does the User want to use Pint?
if (in_array(needle: 'Pint', haystack: $enabledFeatures)) {
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"laravel/pint" => "^1.0",
]
);
rename(from: 'stubs/run-linter.yml.stub', to: '.github/workflows/run-linter.yml');
}
// Does the User want to use PHPStan and Larastan?
if (in_array(needle: 'PHPStan', haystack: $enabledFeatures)) {
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"phpstan/phpstan" => "^1.0",
]
);
if (isset($enableLarastan)) {
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"nunomaduro/larastan" => "^2.0",
]
);
}
rename(from: 'stubs/phpstan.neon.stub', to: 'phpstan.neon');
rename(from: 'stubs/static-analysis.yml.stub', to: '.github/workflows/static-analysis.yml');
file_put_contents(
filename: 'phpstan.neon',
data: "includes: - ./vendor/nunomaduro/larastan/extension.neon\n\n".file_get_contents(filename: 'phpstan.neon')
);
replaceInFile(
search: [':phpVersion'],
replace: [$phpVersion],
filename: '.github/workflows/static-analysis.yml',
);
}
// Does the User want to use Rector?
if (in_array(needle: 'Rector', haystack: $enabledFeatures)) {
$composerDataStructure['require-dev'] = array_merge(
$composerDataStructure['require-dev'],
[
"driftingly/rector-laravel" => "^0.24",
"rectorphp/rector" => "^0.18",
]
);
rename(from: 'stubs/rector.php.stub', to: 'rector.php');
replaceInFile(
search: ':laravelVersion',
replace: $laravelVersion,
filename: 'rector.php',
);
}
/**
* Add autoload/autoload-dev data
*/
$composerDataStructure['autoload'] = [
'psr-4' => [
"$vendorName\\$packageName\\" => 'src/',
"$vendorName\\$packageName\\Database\\Factories\\" => 'database/factories/',
],
];
$composerDataStructure['autoload-dev'] = [
'psr-4' => [
"$vendorName\\$packageName\\Tests\\" => 'tests/',
],
];
/**
* Add scripts
*/
$composerDataStructure['scripts'] = [
'analyze' => 'vendor/bin/phpstan analyse',
'lint' => 'vendor/bin/pint',
'format' => 'vendor/bin/rector process',
'post-autoload-dump' => '@php ./vendor/bin/testbench package:discover --ansi',
'test' => 'vendor/bin/phpunit',
'pest' => 'vendor/bin/pest',
];
/**
* Add config data
*/
$composerDataStructure['config'] = [
'allow-plugins' => [
'pestphp/pest-plugin' => true,
],
'sort-packages' => true,
];
/**
* Add extra data
*/
$composerDataStructure['extra'] = [
'laravel' => [
'providers' => [
"$vendorName\\$packageName\\{$className}ServiceProvider",
],
'aliases' => [
"$className" => "$vendorName\\$packageName\\Facades\\$className",
],
],
'minimum-stability' => 'stable',
'prefer-stable' => true,
];
addComposerData(data: $composerDataStructure);
/*
|--------------------------------------------------------------------------
| Install the dependencies
|--------------------------------------------------------------------------
|
| Everything is done; now it's time to install the dependencies.
|
*/
$confirmInstall = confirm(label: 'Are you ready to install the dependencies?');
if ($confirmInstall) {
spin(callback: function () use ($username, $packageName, $laravelVersion, $testingFramework) {
run(command: 'composer update --quiet --no-interaction');
rename(from: 'stubs/PULL_REQUEST_TEMPLATE.md.stub', to: '.github/PULL_REQUEST_TEMPLATE.md');
rename(from: 'stubs/README.md.stub', to: 'README.md');
replaceInFile(
search: [':username', ':packageName', ':laravelVersion', ':which-test'],
replace: [$username, kebabCase($packageName), $laravelVersion, $testingFramework === 'Pest' ? 'pest' : 'tests'],
filename: 'README.md',
);
}, message: 'Installing dependencies...');
}
$deleteInstaller = confirm(label: 'Do you want to delete the installer?');
if ($deleteInstaller) {
unlink(filename: 'build');
warning(message: 'The installer has been deleted');
}
$deleteStubs = confirm(label: 'Do you want to delete the stubs?');
if ($deleteStubs) {
run(command: 'rm -rf stubs');
warning(message: 'The stubs folder has been deleted');
}
info(message: 'Installation complete! You\'re all set to start building your package.');