|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +function ask(string $question, string $default = ''): string |
| 5 | +{ |
| 6 | + $answer = readline($question.($default ? " ({$default})" : null).': '); |
| 7 | + |
| 8 | + if (! $answer) { |
| 9 | + return $default; |
| 10 | + } |
| 11 | + |
| 12 | + return $answer; |
| 13 | +} |
| 14 | + |
| 15 | +function confirm(string $question, bool $default = false): bool |
| 16 | +{ |
| 17 | + $answer = ask($question.' ('.($default ? 'Y/n' : 'y/N').')'); |
| 18 | + |
| 19 | + if (! $answer) { |
| 20 | + return $default; |
| 21 | + } |
| 22 | + |
| 23 | + return strtolower($answer) === 'y'; |
| 24 | +} |
| 25 | + |
| 26 | +function writeln(string $line): void |
| 27 | +{ |
| 28 | + echo $line.PHP_EOL; |
| 29 | +} |
| 30 | + |
| 31 | +function run(string $command): string |
| 32 | +{ |
| 33 | + return trim((string) shell_exec($command)); |
| 34 | +} |
| 35 | + |
| 36 | +function str_after(string $subject, string $search): string |
| 37 | +{ |
| 38 | + $pos = strrpos($subject, $search); |
| 39 | + |
| 40 | + if ($pos === false) { |
| 41 | + return $subject; |
| 42 | + } |
| 43 | + |
| 44 | + return substr($subject, $pos + strlen($search)); |
| 45 | +} |
| 46 | + |
| 47 | +function slugify(string $subject): string |
| 48 | +{ |
| 49 | + return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-')); |
| 50 | +} |
| 51 | + |
| 52 | +function title_case(string $subject): string |
| 53 | +{ |
| 54 | + return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject))); |
| 55 | +} |
| 56 | + |
| 57 | +function title_snake(string $subject, string $replace = '_'): string |
| 58 | +{ |
| 59 | + return str_replace(['-', '_'], $replace, $subject); |
| 60 | +} |
| 61 | + |
| 62 | +function replace_in_file(string $file, array $replacements): void |
| 63 | +{ |
| 64 | + $contents = file_get_contents($file); |
| 65 | + |
| 66 | + file_put_contents( |
| 67 | + $file, |
| 68 | + str_replace( |
| 69 | + array_keys($replacements), |
| 70 | + array_values($replacements), |
| 71 | + $contents |
| 72 | + ) |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +function remove_prefix(string $prefix, string $content): string |
| 77 | +{ |
| 78 | + if (str_starts_with($content, $prefix)) { |
| 79 | + return substr($content, strlen($prefix)); |
| 80 | + } |
| 81 | + |
| 82 | + return $content; |
| 83 | +} |
| 84 | + |
| 85 | +function remove_composer_deps(array $names) |
| 86 | +{ |
| 87 | + $data = json_decode(file_get_contents(__DIR__.'/composer.json'), true); |
| 88 | + |
| 89 | + foreach ($data['require-dev'] as $name => $version) { |
| 90 | + if (in_array($name, $names, true)) { |
| 91 | + unset($data['require-dev'][$name]); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 96 | +} |
| 97 | + |
| 98 | +function remove_composer_script($scriptName) |
| 99 | +{ |
| 100 | + $data = json_decode(file_get_contents(__DIR__.'/composer.json'), true); |
| 101 | + |
| 102 | + foreach ($data['scripts'] as $name => $script) { |
| 103 | + if ($scriptName === $name) { |
| 104 | + unset($data['scripts'][$name]); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 110 | +} |
| 111 | + |
| 112 | +function remove_readme_paragraphs(string $file): void |
| 113 | +{ |
| 114 | + $contents = file_get_contents($file); |
| 115 | + |
| 116 | + file_put_contents( |
| 117 | + $file, |
| 118 | + preg_replace('/<!--delete-->.*<!--\/delete-->/s', '', $contents) ?: $contents |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +function safeUnlink(string $filename) |
| 123 | +{ |
| 124 | + if (file_exists($filename) && is_file($filename)) { |
| 125 | + unlink($filename); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function determineSeparator(string $path): string |
| 130 | +{ |
| 131 | + return str_replace('/', DIRECTORY_SEPARATOR, $path); |
| 132 | +} |
| 133 | + |
| 134 | +function replaceForWindows(): array |
| 135 | +{ |
| 136 | + return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i vendor | findstr /v /i '.basename(__FILE__).' | findstr /r /i /M /F:/ ":author :vendor :package VendorName skeleton migration_table_name vendor_name vendor_slug author@domain.com"')); |
| 137 | +} |
| 138 | + |
| 139 | +function replaceForAllOtherOSes(): array |
| 140 | +{ |
| 141 | + return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|migration_table_name|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__))); |
| 142 | +} |
| 143 | + |
| 144 | +$gitName = run('git config user.name'); |
| 145 | +$authorName = ask('Author name', $gitName); |
| 146 | + |
| 147 | +$gitEmail = run('git config user.email'); |
| 148 | +$authorEmail = ask('Author email', $gitEmail); |
| 149 | + |
| 150 | +$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; |
| 151 | +$usernameGuess = dirname($usernameGuess); |
| 152 | +$usernameGuess = basename($usernameGuess); |
| 153 | +$authorUsername = ask('Author username', $usernameGuess); |
| 154 | + |
| 155 | +$vendorName = ask('Vendor name', $authorUsername); |
| 156 | +$vendorSlug = slugify($vendorName); |
| 157 | +$vendorNamespace = ucwords($vendorName); |
| 158 | +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); |
| 159 | + |
| 160 | +$currentDirectory = getcwd(); |
| 161 | +$folderName = basename($currentDirectory); |
| 162 | + |
| 163 | +$packageName = ask('Package name', $folderName); |
| 164 | +$packageSlug = slugify($packageName); |
| 165 | +$packageSlugWithoutPrefix = remove_prefix('laravel-', $packageSlug); |
| 166 | + |
| 167 | +$className = title_case($packageName); |
| 168 | +$className = ask('Class name', $className); |
| 169 | +$variableName = lcfirst($className); |
| 170 | +$description = ask('Package description', "This is my package {$packageSlug}"); |
| 171 | + |
| 172 | +$usePhpStan = confirm('Enable PhpStan?', true); |
| 173 | +$useLaravelPint = confirm('Enable Laravel Pint?', true); |
| 174 | +$useDependabot = confirm('Enable Dependabot?', true); |
| 175 | +$useLaravelRay = confirm('Use Ray for debugging?', true); |
| 176 | +$useUpdateChangelogWorkflow = confirm('Use automatic changelog updater workflow?', true); |
| 177 | + |
| 178 | +writeln('------'); |
| 179 | +writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); |
| 180 | +writeln("Vendor : {$vendorName} ({$vendorSlug})"); |
| 181 | +writeln("Package : {$packageSlug} <{$description}>"); |
| 182 | +writeln("Namespace : {$vendorNamespace}\\{$className}"); |
| 183 | +writeln("Class name : {$className}"); |
| 184 | +writeln('---'); |
| 185 | +writeln('Packages & Utilities'); |
| 186 | +writeln('Use Laravel/Pint : '.($useLaravelPint ? 'yes' : 'no')); |
| 187 | +writeln('Use Larastan/PhpStan : '.($usePhpStan ? 'yes' : 'no')); |
| 188 | +writeln('Use Dependabot : '.($useDependabot ? 'yes' : 'no')); |
| 189 | +writeln('Use Ray App : '.($useLaravelRay ? 'yes' : 'no')); |
| 190 | +writeln('Use Auto-Changelog : '.($useUpdateChangelogWorkflow ? 'yes' : 'no')); |
| 191 | +writeln('------'); |
| 192 | + |
| 193 | +writeln('This script will replace the above values in all relevant files in the project directory.'); |
| 194 | + |
| 195 | +if (! confirm('Modify files?', true)) { |
| 196 | + exit(1); |
| 197 | +} |
| 198 | + |
| 199 | +$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); |
| 200 | + |
| 201 | +foreach ($files as $file) { |
| 202 | + replace_in_file($file, [ |
| 203 | + ':author_name' => $authorName, |
| 204 | + ':author_username' => $authorUsername, |
| 205 | + 'author@domain.com' => $authorEmail, |
| 206 | + ':vendor_name' => $vendorName, |
| 207 | + ':vendor_slug' => $vendorSlug, |
| 208 | + 'VendorName' => $vendorNamespace, |
| 209 | + ':package_name' => $packageName, |
| 210 | + ':package_slug' => $packageSlug, |
| 211 | + ':package_slug_without_prefix' => $packageSlugWithoutPrefix, |
| 212 | + 'Skeleton' => $className, |
| 213 | + 'skeleton' => $packageSlug, |
| 214 | + 'migration_table_name' => title_snake($packageSlug), |
| 215 | + 'variable' => $variableName, |
| 216 | + ':package_description' => $description, |
| 217 | + ]); |
| 218 | + |
| 219 | + match (true) { |
| 220 | + str_contains($file, determineSeparator('src/Skeleton.php')) => rename($file, determineSeparator('./src/'.$className.'.php')), |
| 221 | + str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/'.$className.'ServiceProvider.php')), |
| 222 | + str_contains($file, determineSeparator('src/Facades/Skeleton.php')) => rename($file, determineSeparator('./src/Facades/'.$className.'.php')), |
| 223 | + str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/'.$className.'Command.php')), |
| 224 | + str_contains($file, determineSeparator('database/migrations/create_skeleton_table.php.stub')) => rename($file, determineSeparator('./database/migrations/create_'.title_snake($packageSlugWithoutPrefix).'_table.php.stub')), |
| 225 | + str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/'.$packageSlugWithoutPrefix.'.php')), |
| 226 | + str_contains($file, 'README.md') => remove_readme_paragraphs($file), |
| 227 | + default => [], |
| 228 | + }; |
| 229 | +} |
| 230 | + |
| 231 | +if (! $useLaravelPint) { |
| 232 | + safeUnlink(__DIR__.'/.github/workflows/fix-php-code-style-issues.yml'); |
| 233 | + safeUnlink(__DIR__.'/pint.json'); |
| 234 | +} |
| 235 | + |
| 236 | +if (! $usePhpStan) { |
| 237 | + safeUnlink(__DIR__.'/phpstan.neon.dist'); |
| 238 | + safeUnlink(__DIR__.'/phpstan-baseline.neon'); |
| 239 | + safeUnlink(__DIR__.'/.github/workflows/phpstan.yml'); |
| 240 | + |
| 241 | + remove_composer_deps([ |
| 242 | + 'phpstan/extension-installer', |
| 243 | + 'phpstan/phpstan-deprecation-rules', |
| 244 | + 'phpstan/phpstan-phpunit', |
| 245 | + 'nunomaduro/larastan', |
| 246 | + ]); |
| 247 | + |
| 248 | + remove_composer_script('phpstan'); |
| 249 | +} |
| 250 | + |
| 251 | +if (! $useDependabot) { |
| 252 | + safeUnlink(__DIR__.'/.github/dependabot.yml'); |
| 253 | + safeUnlink(__DIR__.'/.github/workflows/dependabot-auto-merge.yml'); |
| 254 | +} |
| 255 | + |
| 256 | +if (! $useLaravelRay) { |
| 257 | + remove_composer_deps(['spatie/laravel-ray']); |
| 258 | +} |
| 259 | + |
| 260 | +if (! $useUpdateChangelogWorkflow) { |
| 261 | + safeUnlink(__DIR__.'/.github/workflows/update-changelog.yml'); |
| 262 | +} |
| 263 | + |
| 264 | +confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); |
| 265 | + |
| 266 | +confirm('Let this script delete itself?', true) && unlink(__FILE__); |
0 commit comments