|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +$gitName = run('git config user.name'); |
| 5 | +$authorName = ask('Author name', $gitName); |
| 6 | + |
| 7 | +$gitEmail = run('git config user.email'); |
| 8 | +$authorEmail = ask('Author email', $gitEmail); |
| 9 | + |
| 10 | +$usernameGuess = explode(':', run('git config remote.origin.url'))[1] ?? ''; |
| 11 | +if ($usernameGuess !== '') { |
| 12 | + $usernameGuess = dirname($usernameGuess); |
| 13 | + $usernameGuess = basename($usernameGuess); |
| 14 | +} |
| 15 | +$authorUsername = ask('Author username', $usernameGuess); |
| 16 | + |
| 17 | +$vendorName = ask('Vendor name', $authorUsername); |
| 18 | +$vendorSlug = slugify($vendorName); |
| 19 | +$vendorNamespace = str_replace('-', '', ucwords($vendorName)); |
| 20 | +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); |
| 21 | + |
| 22 | +$currentDirectory = getcwd(); |
| 23 | +$folderName = basename($currentDirectory); |
| 24 | + |
| 25 | +$packageName = ask('Package name', $folderName); |
| 26 | +$packageSlug = slugify($packageName); |
| 27 | +$packageSlugWithoutPrefix = removePrefix('laracord-', $packageSlug); |
| 28 | + |
| 29 | +$className = titleCase($packageName); |
| 30 | +$className = ask('Class name', $className); |
| 31 | +$variableName = lcfirst($className); |
| 32 | +$description = ask('Package description', "This is my package $packageSlug"); |
| 33 | + |
| 34 | +writeln("\r"); |
| 35 | +writeln('------'); |
| 36 | +writeln("Author : \e[0;36m$authorName ($authorUsername, $authorEmail)\e[0m"); |
| 37 | +writeln("Vendor : \e[0;36m$vendorName ($vendorSlug)\e[0m"); |
| 38 | +writeln('Package : ' . "\e[0;36m" . $packageSlug . ($description ? " <{$description}>" : '') . "\e[0m"); |
| 39 | +writeln("Namespace : \e[0;36m$vendorNamespace\\$className\e[0m"); |
| 40 | +writeln("Class name : \e[0;36m$className\e[0m"); |
| 41 | +writeln('------'); |
| 42 | +writeln("\r"); |
| 43 | +writeln('This script will replace the above values in all relevant files in the project directory.'); |
| 44 | +writeln("\r"); |
| 45 | + |
| 46 | +if (! confirm('Modify files?', true)) { |
| 47 | + exit(1); |
| 48 | +} |
| 49 | + |
| 50 | +$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); |
| 51 | + |
| 52 | +foreach ($files as $file) { |
| 53 | + replaceInFile($file, [ |
| 54 | + ':author_name' => $authorName, |
| 55 | + ':author_username' => $authorUsername, |
| 56 | + 'author@domain.com' => $authorEmail, |
| 57 | + ':vendor_name' => $vendorName, |
| 58 | + ':vendor_slug' => $vendorSlug, |
| 59 | + 'VendorName' => $vendorNamespace, |
| 60 | + ':package_name' => $packageName, |
| 61 | + ':package_slug' => $packageSlug, |
| 62 | + ':package_slug_without_prefix' => $packageSlugWithoutPrefix, |
| 63 | + 'Skeleton' => $className, |
| 64 | + 'skeleton' => $packageSlug, |
| 65 | + 'migration_table_name' => titleSnake($packageSlug), |
| 66 | + 'variable' => $variableName, |
| 67 | + ':package_description' => $description, |
| 68 | + ]); |
| 69 | + |
| 70 | + match (true) { |
| 71 | + str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/' . $className . 'ServiceProvider.php')), |
| 72 | + str_contains($file, determineSeparator('src/SkeletonPlugin.php')) => rename($file, determineSeparator('./src/' . $className . 'Plugin.php')), |
| 73 | + str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/' . $className . 'Command.php')), |
| 74 | + str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/' . $packageSlugWithoutPrefix . '.php')), |
| 75 | + str_contains($file, 'README.md') => removeTag($file, 'delete'), |
| 76 | + default => [], |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +confirm('Execute `composer install`?') && run('composer install'); |
| 81 | + |
| 82 | +if (confirm('Let this script delete itself?', true)) { |
| 83 | + unlink(__FILE__); |
| 84 | +} |
| 85 | + |
| 86 | +function ask(string $question, string $default = ''): string |
| 87 | +{ |
| 88 | + $def = $default ? "\e[0;33m ($default)" : ''; |
| 89 | + $answer = readline("\e[0;32m" . $question . $def . ": \e[0m"); |
| 90 | + |
| 91 | + if (! $answer) { |
| 92 | + return $default; |
| 93 | + } |
| 94 | + |
| 95 | + return $answer; |
| 96 | +} |
| 97 | + |
| 98 | +function confirm(string $question, bool $default = false): bool |
| 99 | +{ |
| 100 | + $answer = ask($question, ($default ? 'Y/n' : 'y/N')); |
| 101 | + |
| 102 | + if (strtolower($answer) === 'y/n') { |
| 103 | + return $default; |
| 104 | + } |
| 105 | + |
| 106 | + return strtolower($answer) === 'y'; |
| 107 | +} |
| 108 | + |
| 109 | +function writeln(string $line): void |
| 110 | +{ |
| 111 | + echo $line . PHP_EOL; |
| 112 | +} |
| 113 | + |
| 114 | +function run(string $command): string |
| 115 | +{ |
| 116 | + return trim((string) shell_exec($command)); |
| 117 | +} |
| 118 | + |
| 119 | +function slugify(string $subject): string |
| 120 | +{ |
| 121 | + return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-')); |
| 122 | +} |
| 123 | + |
| 124 | +function titleCase(string $subject): string |
| 125 | +{ |
| 126 | + return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject))); |
| 127 | +} |
| 128 | + |
| 129 | +function titleSnake(string $subject, string $replace = '_'): string |
| 130 | +{ |
| 131 | + return str_replace(['-', '_'], $replace, $subject); |
| 132 | +} |
| 133 | + |
| 134 | +function replaceInFile(string $file, array $replacements): void |
| 135 | +{ |
| 136 | + $contents = file_get_contents($file); |
| 137 | + |
| 138 | + file_put_contents( |
| 139 | + $file, |
| 140 | + str_replace( |
| 141 | + array_keys($replacements), |
| 142 | + array_values($replacements), |
| 143 | + $contents |
| 144 | + ) |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +function removePrefix(string $prefix, string $content): string |
| 149 | +{ |
| 150 | + if (str_starts_with($content, $prefix)) { |
| 151 | + return substr($content, strlen($prefix)); |
| 152 | + } |
| 153 | + |
| 154 | + return $content; |
| 155 | +} |
| 156 | + |
| 157 | +function removeComposerDeps(array $names, string $location): void |
| 158 | +{ |
| 159 | + $data = json_decode(file_get_contents(__DIR__ . '/composer.json'), true); |
| 160 | + |
| 161 | + foreach ($data[$location] as $name => $version) { |
| 162 | + if (in_array($name, $names, true)) { |
| 163 | + unset($data[$location][$name]); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + file_put_contents(__DIR__ . '/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 168 | +} |
| 169 | + |
| 170 | +function removeNpmDeps(array $names, string $location): void |
| 171 | +{ |
| 172 | + $data = json_decode(file_get_contents(__DIR__ . '/package.json'), true); |
| 173 | + |
| 174 | + foreach ($data[$location] as $name => $version) { |
| 175 | + if (in_array($name, $names, true)) { |
| 176 | + unset($data[$location][$name]); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + file_put_contents(__DIR__ . '/package.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | |
| 181 | + JSON_UNESCAPED_UNICODE)); |
| 182 | +} |
| 183 | + |
| 184 | +function removeTag(string $file, string $tag): void |
| 185 | +{ |
| 186 | + $contents = file_get_contents($file); |
| 187 | + |
| 188 | + file_put_contents( |
| 189 | + $file, |
| 190 | + preg_replace('/<!--' . $tag . '-->.*<!--\/' . $tag . '-->/s', '', $contents) ?: $contents |
| 191 | + ); |
| 192 | +} |
| 193 | + |
| 194 | +function safeUnlink(string $filename): void |
| 195 | +{ |
| 196 | + if (file_exists($filename) && is_file($filename)) { |
| 197 | + unlink($filename); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +function determineSeparator(string $path): string |
| 202 | +{ |
| 203 | + return str_replace('/', DIRECTORY_SEPARATOR, $path); |
| 204 | +} |
| 205 | + |
| 206 | +function replaceForWindows(): array |
| 207 | +{ |
| 208 | + 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"')); |
| 209 | +} |
| 210 | + |
| 211 | +function replaceForAllOtherOSes(): array |
| 212 | +{ |
| 213 | + return explode(PHP_EOL, run('find ./* ./.github/* -name "vendor" -type d -prune \ |
| 214 | + -o -name "configure.php" -prune \ |
| 215 | + -o -type f -print0 | xargs -0 grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|migration_table_name|vendor_name|vendor_slug|author@domain.com"')); |
| 216 | +} |
| 217 | + |
| 218 | +function removeDirectory($dir): void |
| 219 | +{ |
| 220 | + if (is_dir($dir)) { |
| 221 | + $objects = scandir($dir); |
| 222 | + |
| 223 | + foreach ($objects as $object) { |
| 224 | + if ($object != '.' && $object != '..') { |
| 225 | + if (filetype($dir . '/' . $object) == 'dir') { |
| 226 | + removeDirectory($dir . '/' . $object); |
| 227 | + } else { |
| 228 | + unlink($dir . '/' . $object); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + rmdir($dir); |
| 234 | + } |
| 235 | +} |
0 commit comments