|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +function ask(string $question, string $default = ''): string { |
| 5 | + $answer = readline($question . ($default ? " ({$default})" : null) . ': '); |
| 6 | + |
| 7 | + if (! $answer) { |
| 8 | + return $default; |
| 9 | + } |
| 10 | + |
| 11 | + return $answer; |
| 12 | +} |
| 13 | + |
| 14 | +function confirm(string $question, bool $default = false): bool { |
| 15 | + $answer = ask($question . ' (' . ($default ? 'Y/n' : 'y/N') . ')'); |
| 16 | + |
| 17 | + if (! $answer) { |
| 18 | + return $default; |
| 19 | + } |
| 20 | + |
| 21 | + return strtolower($answer) === 'y'; |
| 22 | +} |
| 23 | + |
| 24 | +function writeln(string $line): void { |
| 25 | + echo $line . PHP_EOL; |
| 26 | +} |
| 27 | + |
| 28 | +function run(string $command): string { |
| 29 | + return trim(shell_exec($command)); |
| 30 | +} |
| 31 | + |
| 32 | +function str_after(string $subject, string $search): string { |
| 33 | + $pos = strrpos($subject, $search); |
| 34 | + |
| 35 | + if ($pos === false) { |
| 36 | + return $subject; |
| 37 | + } |
| 38 | + |
| 39 | + return substr($subject, $pos + strlen($search)); |
| 40 | +} |
| 41 | + |
| 42 | +function slugify(string $subject): string { |
| 43 | + return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-')); |
| 44 | +} |
| 45 | + |
| 46 | +function title_case(string $subject): string { |
| 47 | + return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject))); |
| 48 | +} |
| 49 | + |
| 50 | +function replace_in_file(string $file, array $replacements): void { |
| 51 | + $contents = file_get_contents($file); |
| 52 | + |
| 53 | + file_put_contents( |
| 54 | + $file, |
| 55 | + str_replace( |
| 56 | + array_keys($replacements), |
| 57 | + array_values($replacements), |
| 58 | + $contents |
| 59 | + ) |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +$gitName = run('git config user.name'); |
| 64 | +$authorName = ask('Author name', $gitName); |
| 65 | + |
| 66 | +$gitEmail = run('git config user.email'); |
| 67 | +$authorEmail = ask('Author email', $gitEmail); |
| 68 | + |
| 69 | +$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; |
| 70 | +$usernameGuess = dirname($usernameGuess); |
| 71 | +$usernameGuess = basename($usernameGuess); |
| 72 | +$authorUsername = ask('Author username', $usernameGuess); |
| 73 | + |
| 74 | +$vendorName = ask('Vendor name', $authorUsername); |
| 75 | +$vendorSlug = slugify($vendorName); |
| 76 | +$vendorNamespace = ucwords($vendorName); |
| 77 | +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); |
| 78 | + |
| 79 | +$currentDirectory = getcwd(); |
| 80 | +$folderName = basename($currentDirectory); |
| 81 | + |
| 82 | +$packageName = ask('Package name', $folderName); |
| 83 | +$packageSlug = slugify($packageName); |
| 84 | + |
| 85 | +$className = title_case($packageName); |
| 86 | +$className = ask('Class name', $className); |
| 87 | +$description = ask('Package description', "This is my package {$packageSlug}"); |
| 88 | + |
| 89 | +writeln('------'); |
| 90 | +writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); |
| 91 | +writeln("Vendor : {$vendorName} ({$vendorSlug})"); |
| 92 | +writeln("Package : {$packageSlug} <{$description}>"); |
| 93 | +writeln("Namespace : {$vendorNamespace}\\{$className}"); |
| 94 | +writeln("Class name : {$className}"); |
| 95 | +writeln('------'); |
| 96 | + |
| 97 | +writeln('This script will replace the above values in all relevant files in the project directory.'); |
| 98 | + |
| 99 | +if (! confirm('Modify files?', true)) { |
| 100 | + exit(1); |
| 101 | +} |
| 102 | + |
| 103 | +$files = explode( PHP_EOL, run( 'grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|[email protected]" --exclude-dir=vendor ./* ./.github/* | grep -v ' . basename( __FILE__))); |
| 104 | + |
| 105 | +foreach ($files as $file) { |
| 106 | + replace_in_file($file, [ |
| 107 | + ':author_name' => $authorName, |
| 108 | + ':author_username' => $authorUsername, |
| 109 | + '[email protected]' => $authorEmail, |
| 110 | + ':vendor_name' => $vendorName, |
| 111 | + ':vendor_slug' => $vendorSlug, |
| 112 | + 'VendorName' => $vendorNamespace, |
| 113 | + ':package_name' => $packageName, |
| 114 | + ':package_slug' => $packageSlug, |
| 115 | + 'Skeleton' => $className, |
| 116 | + ':package_description' => $description, |
| 117 | + ]); |
| 118 | + |
| 119 | + match (true) { |
| 120 | + str_contains($file, 'src/Skeleton.php') => rename($file, './src/' . $className . '.php'), |
| 121 | + str_contains($file, 'src/SkeletonServiceProvider.php') => rename($file, './src/' . $className . 'ServiceProvider.php'), |
| 122 | + str_contains($file, 'src/SkeletonFacade.php') => rename($file, './src/' . $className . 'Facade.php'), |
| 123 | + str_contains($file, 'src/Commands/SkeletonCommand.php') => rename($file, './src/Commands/' . $className . 'Command.php'), |
| 124 | + default => [], |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); |
| 129 | + |
| 130 | +confirm('Let this script delete itself?', true) && unlink(__FILE__); |
0 commit comments