| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Commands; |
| 4: | |
| 5: | use InvalidArgumentException; |
| 6: | use Illuminate\Support\Facades\File; |
| 7: | use Illuminate\Console\Command; |
| 8: | |
| 9: | class BaseGenerator extends Command |
| 10: | { |
| 11: | |
| 12: | protected function assertAppExists(string $app): bool |
| 13: | { |
| 14: | return File::exists(base_path("ui5/{$app}")); |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | protected function parseCamelCasePair(string $input): array |
| 27: | { |
| 28: | if (!str_contains($input, '/')) { |
| 29: | throw new InvalidArgumentException('Please use the format AppName/ObjectName (e.g. Projects/CutOff).'); |
| 30: | } |
| 31: | |
| 32: | [$app, $object] = explode('/', $input, 2); |
| 33: | |
| 34: | foreach (['App name' => $app, 'Object name' => $object] as $label => $value) { |
| 35: | $this->assertCamelCase($label, $value); |
| 36: | } |
| 37: | |
| 38: | return [$app, $object]; |
| 39: | } |
| 40: | |
| 41: | protected function assertCamelCase(string $label, string $input): void |
| 42: | { |
| 43: | if (!preg_match('/^[A-Z][a-zA-Z0-9]+$/', $input)) { |
| 44: | throw new InvalidArgumentException("{$label} must be CamelCase (e.g. ProjectKpi, CutOff)."); |
| 45: | } |
| 46: | } |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | protected function compileStub(string $stubName, array $replacements): string |
| 64: | { |
| 65: | $stub = File::get(__DIR__ . "/../../resources/stubs/$stubName"); |
| 66: | |
| 67: | foreach ($replacements as $key => $value) { |
| 68: | $stub = str_replace("{{ $key }}", $value, $stub); |
| 69: | } |
| 70: | |
| 71: | return $stub; |
| 72: | } |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | protected function copyDistAssets(string $sourcePath, string $targetPath, array $toCopy): void |
| 81: | { |
| 82: | $source = $sourcePath; |
| 83: | $target = $targetPath . '../resources/ui5/'; |
| 84: | |
| 85: | File::ensureDirectoryExists($target); |
| 86: | File::ensureDirectoryExists($target . 'i18n'); |
| 87: | |
| 88: | foreach ($toCopy as $file) { |
| 89: | $from = "{$source}/{$file}"; |
| 90: | $to = $target . $file; |
| 91: | if (file_exists($from)) { |
| 92: | File::copy($from, $to); |
| 93: | if ($this->output->isVerbose()) { |
| 94: | $this->info("✓ {$file}"); |
| 95: | } |
| 96: | } |
| 97: | } |
| 98: | } |
| 99: | } |
| 100: | |