|
| 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 | +function remove_prefix(string $prefix, string $content): string { |
| 64 | + if (str_starts_with($content, $prefix)) { |
| 65 | + return substr($content, strlen($prefix)); |
| 66 | + } |
| 67 | + |
| 68 | + return $content; |
| 69 | +} |
| 70 | + |
| 71 | +function remove_composer_deps(array $names) { |
| 72 | + $data = json_decode(file_get_contents(__DIR__.'/composer.json'), true); |
| 73 | + |
| 74 | + foreach($data['require-dev'] as $name => $version) { |
| 75 | + if (in_array($name, $names, true)) { |
| 76 | + unset($data['require-dev'][$name]); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 81 | +} |
| 82 | + |
| 83 | +function remove_composer_script($scriptName) { |
| 84 | + $data = json_decode(file_get_contents(__DIR__.'/composer.json'), true); |
| 85 | + |
| 86 | + foreach($data['scripts'] as $name => $script) { |
| 87 | + if ($scriptName === $name) { |
| 88 | + unset($data['scripts'][$name]); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 94 | +} |
| 95 | + |
| 96 | +function remove_readme_paragraphs(string $file): void { |
| 97 | + $contents = file_get_contents($file); |
| 98 | + |
| 99 | + file_put_contents( |
| 100 | + $file, |
| 101 | + preg_replace('/<!--delete-->.*<!--\/delete-->/s', '', $contents) ?: $contents |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +function safeUnlink(string $filename) { |
| 106 | + if (file_exists($filename) && is_file($filename)) { |
| 107 | + unlink($filename); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function determineSeparator(string $path): string { |
| 112 | + return str_replace('/', DIRECTORY_SEPARATOR, $path); |
| 113 | +} |
| 114 | + |
| 115 | +function replaceForWindows(): array { |
| 116 | + 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 vendor_name vendor_slug author@domain.com"')); |
| 117 | +} |
| 118 | + |
| 119 | +function replaceForAllOtherOSes(): array { |
| 120 | + return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v ' . basename(__FILE__))); |
| 121 | +} |
| 122 | + |
| 123 | +$gitName = run('git config user.name'); |
| 124 | +$authorName = ask('Author name', $gitName); |
| 125 | + |
| 126 | +$gitEmail = run('git config user.email'); |
| 127 | +$authorEmail = ask('Author email', $gitEmail); |
| 128 | + |
| 129 | +$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; |
| 130 | +$usernameGuess = dirname($usernameGuess); |
| 131 | +$usernameGuess = basename($usernameGuess); |
| 132 | +$authorUsername = ask('Author username', $usernameGuess); |
| 133 | + |
| 134 | +$vendorName = ask('Vendor name', $authorUsername); |
| 135 | +$vendorSlug = slugify($vendorName); |
| 136 | +$vendorNamespace = ucwords($vendorName); |
| 137 | +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); |
| 138 | + |
| 139 | +$currentDirectory = getcwd(); |
| 140 | +$folderName = basename($currentDirectory); |
| 141 | + |
| 142 | +$packageName = ask('Package name', $folderName); |
| 143 | +$packageSlug = slugify($packageName); |
| 144 | +$packageSlugWithoutPrefix = remove_prefix('laravel-', $packageSlug); |
| 145 | + |
| 146 | +$className = title_case($packageName); |
| 147 | +$className = ask('Class name', $className); |
| 148 | +$variableName = lcfirst($className); |
| 149 | +$description = ask('Package description', "This is my package {$packageSlug}"); |
| 150 | + |
| 151 | +$usePhpStan = confirm('Enable PhpStan?', true); |
| 152 | +$usePhpCsFixer = confirm('Enable PhpCsFixer?', true); |
| 153 | +$useUpdateChangelogWorkflow = confirm('Use automatic changelog updater workflow?', true); |
| 154 | + |
| 155 | +writeln('------'); |
| 156 | +writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); |
| 157 | +writeln("Vendor : {$vendorName} ({$vendorSlug})"); |
| 158 | +writeln("Package : {$packageSlug} <{$description}>"); |
| 159 | +writeln("Namespace : {$vendorNamespace}\\{$className}"); |
| 160 | +writeln("Class name : {$className}"); |
| 161 | +writeln("---"); |
| 162 | +writeln("Packages & Utilities"); |
| 163 | +writeln("Use PhpCsFixer : " . ($usePhpCsFixer ? 'yes' : 'no')); |
| 164 | +writeln("Use Larastan/PhpStan : " . ($usePhpStan ? 'yes' : 'no')); |
| 165 | +writeln("Use Auto-Changelog : " . ($useUpdateChangelogWorkflow ? 'yes' : 'no')); |
| 166 | +writeln('------'); |
| 167 | + |
| 168 | +writeln('This script will replace the above values in all relevant files in the project directory.'); |
| 169 | + |
| 170 | +if (! confirm('Modify files?', true)) { |
| 171 | + exit(1); |
| 172 | +} |
| 173 | + |
| 174 | +$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); |
| 175 | + |
| 176 | +foreach ($files as $file) { |
| 177 | + replace_in_file($file, [ |
| 178 | + ':author_name' => $authorName, |
| 179 | + ':author_username' => $authorUsername, |
| 180 | + 'author@domain.com' => $authorEmail, |
| 181 | + ':vendor_name' => $vendorName, |
| 182 | + ':vendor_slug' => $vendorSlug, |
| 183 | + 'VendorName' => $vendorNamespace, |
| 184 | + ':package_name' => $packageName, |
| 185 | + ':package_slug' => $packageSlug, |
| 186 | + ':package_slug_without_prefix' => $packageSlugWithoutPrefix, |
| 187 | + 'Skeleton' => $className, |
| 188 | + 'skeleton' => $packageSlug, |
| 189 | + 'variable' => $variableName, |
| 190 | + ':package_description' => $description, |
| 191 | + ]); |
| 192 | + |
| 193 | + match (true) { |
| 194 | + str_contains($file, determineSeparator('src/Skeleton.php')) => rename($file, determineSeparator('./src/' . $className . '.php')), |
| 195 | + str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/' . $className . 'ServiceProvider.php')), |
| 196 | + str_contains($file, determineSeparator('src/Facades/Skeleton.php')) => rename($file, determineSeparator('./src/Facades/' . $className . '.php')), |
| 197 | + str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/' . $className . 'Command.php')), |
| 198 | + str_contains($file, determineSeparator('database/migrations/create_skeleton_table.php.stub')) => rename($file, determineSeparator('./database/migrations/create_' . $packageSlugWithoutPrefix . '_table.php.stub')), |
| 199 | + str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/' . $packageSlugWithoutPrefix . '.php')), |
| 200 | + str_contains($file, 'README.md') => remove_readme_paragraphs($file), |
| 201 | + default => [], |
| 202 | + }; |
| 203 | +} |
| 204 | + |
| 205 | +if (! $usePhpCsFixer) { |
| 206 | + safeUnlink(__DIR__ . '/.php_cs.dist.php'); |
| 207 | + safeUnlink(__DIR__ . '/.github/workflows/php-cs-fixer.yml'); |
| 208 | +} |
| 209 | + |
| 210 | +if (! $usePhpStan) { |
| 211 | + safeUnlink(__DIR__ . '/phpstan.neon.dist'); |
| 212 | + safeUnlink(__DIR__ . '/phpstan-baseline.neon'); |
| 213 | + safeUnlink(__DIR__ . '/.github/workflows/phpstan.yml'); |
| 214 | + |
| 215 | + remove_composer_deps([ |
| 216 | + 'phpstan/extension-installer', |
| 217 | + 'phpstan/phpstan-deprecation-rules', |
| 218 | + 'phpstan/phpstan-phpunit', |
| 219 | + 'nunomaduro/larastan', |
| 220 | + ]); |
| 221 | + |
| 222 | + remove_composer_script('phpstan'); |
| 223 | +} |
| 224 | + |
| 225 | +if (! $useUpdateChangelogWorkflow) { |
| 226 | + safeUnlink(__DIR__ . '/.github/workflows/update-changelog.yml'); |
| 227 | +} |
| 228 | + |
| 229 | +confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); |
| 230 | + |
| 231 | +confirm('Let this script delete itself?', true) && unlink(__FILE__); |
0 commit comments