|
| 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 | +function getGitHubApiEndpoint(string $endpoint): ?stdClass |
| 145 | +{ |
| 146 | + try { |
| 147 | + $curl = curl_init("https://api.github.com/{$endpoint}"); |
| 148 | + curl_setopt_array($curl, [ |
| 149 | + CURLOPT_RETURNTRANSFER => true, |
| 150 | + CURLOPT_FOLLOWLOCATION => true, |
| 151 | + CURLOPT_HTTPGET => true, |
| 152 | + CURLOPT_HTTPHEADER => [ |
| 153 | + 'User-Agent: spatie-configure-script/1.0', |
| 154 | + ], |
| 155 | + ]); |
| 156 | + |
| 157 | + $response = curl_exec($curl); |
| 158 | + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 159 | + |
| 160 | + curl_close($curl); |
| 161 | + |
| 162 | + if ($statusCode === 200) { |
| 163 | + return json_decode($response); |
| 164 | + } |
| 165 | + } catch (Exception $e) { |
| 166 | + // ignore |
| 167 | + } |
| 168 | + |
| 169 | + return null; |
| 170 | +} |
| 171 | + |
| 172 | +function searchCommitsForGitHubUsername(): string |
| 173 | +{ |
| 174 | + $authorName = strtolower(trim(shell_exec('git config user.name'))); |
| 175 | + |
| 176 | + $committersRaw = shell_exec("git log --author='@users.noreply.github.com' --pretty='%an:%ae' --reverse"); |
| 177 | + $committersLines = explode("\n", $committersRaw ?? ''); |
| 178 | + $committers = array_filter(array_map(function ($line) use ($authorName) { |
| 179 | + $line = trim($line); |
| 180 | + [$name, $email] = explode(':', $line) + [null, null]; |
| 181 | + |
| 182 | + return [ |
| 183 | + 'name' => $name, |
| 184 | + 'email' => $email, |
| 185 | + 'isMatch' => strtolower($name) === $authorName && ! str_contains($name, '[bot]'), |
| 186 | + ]; |
| 187 | + }, $committersLines), fn ($item) => $item['isMatch']); |
| 188 | + |
| 189 | + if (empty($committers)) { |
| 190 | + return ''; |
| 191 | + } |
| 192 | + |
| 193 | + $firstCommitter = reset($committers); |
| 194 | + |
| 195 | + return explode('@', $firstCommitter['email'])[0] ?? ''; |
| 196 | +} |
| 197 | + |
| 198 | +function guessGitHubUsernameUsingCli() |
| 199 | +{ |
| 200 | + try { |
| 201 | + if (preg_match('/ogged in to github\.com as ([a-zA-Z-_]+).+/', shell_exec('gh auth status -h github.com 2>&1'), $matches)) { |
| 202 | + return $matches[1]; |
| 203 | + } |
| 204 | + } catch (Exception $e) { |
| 205 | + // ignore |
| 206 | + } |
| 207 | + |
| 208 | + return ''; |
| 209 | +} |
| 210 | + |
| 211 | +function guessGitHubUsername(): string |
| 212 | +{ |
| 213 | + $username = searchCommitsForGitHubUsername(); |
| 214 | + if (! empty($username)) { |
| 215 | + return $username; |
| 216 | + } |
| 217 | + |
| 218 | + $username = guessGitHubUsernameUsingCli(); |
| 219 | + if (! empty($username)) { |
| 220 | + return $username; |
| 221 | + } |
| 222 | + |
| 223 | + // fall back to using the username from the git remote |
| 224 | + $remoteUrl = shell_exec('git config remote.origin.url'); |
| 225 | + $remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl))); |
| 226 | + |
| 227 | + return $remoteUrlParts[1] ?? ''; |
| 228 | +} |
| 229 | + |
| 230 | +function guessGitHubVendorInfo($authorName, $username): array |
| 231 | +{ |
| 232 | + $remoteUrl = shell_exec('git config remote.origin.url'); |
| 233 | + $remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl))); |
| 234 | + |
| 235 | + $response = getGitHubApiEndpoint("orgs/{$remoteUrlParts[1]}"); |
| 236 | + |
| 237 | + if ($response === null) { |
| 238 | + return [$authorName, $username]; |
| 239 | + } |
| 240 | + |
| 241 | + return [$response->name ?? $authorName, $response->login ?? $username]; |
| 242 | +} |
| 243 | + |
| 244 | +$gitName = run('git config user.name'); |
| 245 | +$authorName = ask('Author name', $gitName); |
| 246 | + |
| 247 | +$gitEmail = run('git config user.email'); |
| 248 | +$authorEmail = ask('Author email', $gitEmail); |
| 249 | +$authorUsername = ask('Author username', guessGitHubUsername()); |
| 250 | + |
| 251 | +$guessGitHubVendorInfo = guessGitHubVendorInfo($authorName, $authorUsername); |
| 252 | + |
| 253 | +$vendorName = ask('Vendor name', $guessGitHubVendorInfo[0]); |
| 254 | +$vendorUsername = ask('Vendor username', $guessGitHubVendorInfo[1] ?? slugify($vendorName)); |
| 255 | +$vendorSlug = slugify($vendorUsername); |
| 256 | + |
| 257 | +$vendorNamespace = str_replace('-', '', ucwords($vendorName)); |
| 258 | +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); |
| 259 | + |
| 260 | +$currentDirectory = getcwd(); |
| 261 | +$folderName = basename($currentDirectory); |
| 262 | + |
| 263 | +$packageName = ask('Package name', $folderName); |
| 264 | +$packageSlug = slugify($packageName); |
| 265 | +$packageSlugWithoutPrefix = remove_prefix('laravel-', $packageSlug); |
| 266 | + |
| 267 | +$className = title_case($packageName); |
| 268 | +$className = ask('Class name', $className); |
| 269 | +$variableName = lcfirst($className); |
| 270 | +$description = ask('Package description', "This is my package {$packageSlug}"); |
| 271 | + |
| 272 | +$usePhpStan = confirm('Enable PhpStan?', true); |
| 273 | +$useLaravelPint = confirm('Enable Laravel Pint?', true); |
| 274 | +$useDependabot = confirm('Enable Dependabot?', true); |
| 275 | +$useLaravelRay = confirm('Use Ray for debugging?', true); |
| 276 | +$useUpdateChangelogWorkflow = confirm('Use automatic changelog updater workflow?', true); |
| 277 | + |
| 278 | +writeln('------'); |
| 279 | +writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); |
| 280 | +writeln("Vendor : {$vendorName} ({$vendorSlug})"); |
| 281 | +writeln("Package : {$packageSlug} <{$description}>"); |
| 282 | +writeln("Namespace : {$vendorNamespace}\\{$className}"); |
| 283 | +writeln("Class name : {$className}"); |
| 284 | +writeln('---'); |
| 285 | +writeln('Packages & Utilities'); |
| 286 | +writeln('Use Laravel/Pint : '.($useLaravelPint ? 'yes' : 'no')); |
| 287 | +writeln('Use Larastan/PhpStan : '.($usePhpStan ? 'yes' : 'no')); |
| 288 | +writeln('Use Dependabot : '.($useDependabot ? 'yes' : 'no')); |
| 289 | +writeln('Use Ray App : '.($useLaravelRay ? 'yes' : 'no')); |
| 290 | +writeln('Use Auto-Changelog : '.($useUpdateChangelogWorkflow ? 'yes' : 'no')); |
| 291 | +writeln('------'); |
| 292 | + |
| 293 | +writeln('This script will replace the above values in all relevant files in the project directory.'); |
| 294 | + |
| 295 | +if (! confirm('Modify files?', true)) { |
| 296 | + exit(1); |
| 297 | +} |
| 298 | + |
| 299 | +$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); |
| 300 | + |
| 301 | +foreach ($files as $file) { |
| 302 | + replace_in_file($file, [ |
| 303 | + ':author_name' => $authorName, |
| 304 | + ':author_username' => $authorUsername, |
| 305 | + 'author@domain.com' => $authorEmail, |
| 306 | + ':vendor_name' => $vendorName, |
| 307 | + ':vendor_slug' => $vendorSlug, |
| 308 | + 'VendorName' => $vendorNamespace, |
| 309 | + ':package_name' => $packageName, |
| 310 | + ':package_slug' => $packageSlug, |
| 311 | + ':package_slug_without_prefix' => $packageSlugWithoutPrefix, |
| 312 | + 'Skeleton' => $className, |
| 313 | + 'skeleton' => $packageSlug, |
| 314 | + 'migration_table_name' => title_snake($packageSlug), |
| 315 | + 'variable' => $variableName, |
| 316 | + ':package_description' => $description, |
| 317 | + ]); |
| 318 | + |
| 319 | + match (true) { |
| 320 | + str_contains($file, determineSeparator('src/Skeleton.php')) => rename($file, determineSeparator('./src/'.$className.'.php')), |
| 321 | + str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/'.$className.'ServiceProvider.php')), |
| 322 | + str_contains($file, determineSeparator('src/Facades/Skeleton.php')) => rename($file, determineSeparator('./src/Facades/'.$className.'.php')), |
| 323 | + str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/'.$className.'Command.php')), |
| 324 | + str_contains($file, determineSeparator('database/migrations/create_skeleton_table.php.stub')) => rename($file, determineSeparator('./database/migrations/create_'.title_snake($packageSlugWithoutPrefix).'_table.php.stub')), |
| 325 | + str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/'.$packageSlugWithoutPrefix.'.php')), |
| 326 | + str_contains($file, 'README.md') => remove_readme_paragraphs($file), |
| 327 | + default => [], |
| 328 | + }; |
| 329 | +} |
| 330 | + |
| 331 | +if (! $useLaravelPint) { |
| 332 | + safeUnlink(__DIR__.'/.github/workflows/fix-php-code-style-issues.yml'); |
| 333 | + safeUnlink(__DIR__.'/pint.json'); |
| 334 | +} |
| 335 | + |
| 336 | +if (! $usePhpStan) { |
| 337 | + safeUnlink(__DIR__.'/phpstan.neon.dist'); |
| 338 | + safeUnlink(__DIR__.'/phpstan-baseline.neon'); |
| 339 | + safeUnlink(__DIR__.'/.github/workflows/phpstan.yml'); |
| 340 | + |
| 341 | + remove_composer_deps([ |
| 342 | + 'phpstan/extension-installer', |
| 343 | + 'phpstan/phpstan-deprecation-rules', |
| 344 | + 'phpstan/phpstan-phpunit', |
| 345 | + 'larastan/larastan', |
| 346 | + ]); |
| 347 | + |
| 348 | + remove_composer_script('phpstan'); |
| 349 | +} |
| 350 | + |
| 351 | +if (! $useDependabot) { |
| 352 | + safeUnlink(__DIR__.'/.github/dependabot.yml'); |
| 353 | + safeUnlink(__DIR__.'/.github/workflows/dependabot-auto-merge.yml'); |
| 354 | +} |
| 355 | + |
| 356 | +if (! $useLaravelRay) { |
| 357 | + remove_composer_deps(['spatie/laravel-ray']); |
| 358 | +} |
| 359 | + |
| 360 | +if (! $useUpdateChangelogWorkflow) { |
| 361 | + safeUnlink(__DIR__.'/.github/workflows/update-changelog.yml'); |
| 362 | +} |
| 363 | + |
| 364 | +confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); |
| 365 | + |
| 366 | +confirm('Let this script delete itself?', true) && unlink(__FILE__); |
0 commit comments