|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ArtisanBuild\Kibble\Commands; |
| 6 | + |
| 7 | +use ArtisanBuild\Kibble\Actions\KibbleGitIgnore; |
| 8 | +use Illuminate\Console\Command; |
| 9 | + |
| 10 | +class LinkCommand extends Command |
| 11 | +{ |
| 12 | + protected $signature = 'kibble:link {repository}'; |
| 13 | + |
| 14 | + protected $description = 'Add a Kibble monorepo to your composer project'; |
| 15 | + |
| 16 | + public function handle(): int |
| 17 | + { |
| 18 | + app(KibbleGitIgnore::class)(); |
| 19 | + |
| 20 | + if (! file_exists('kibble.json')) { |
| 21 | + file_put_contents( |
| 22 | + filename: 'kibble.json', |
| 23 | + data: json_encode( |
| 24 | + value: [], |
| 25 | + flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
| 26 | + ).PHP_EOL |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + $kibble = json_decode(file_get_contents('kibble.json'), true); |
| 31 | + |
| 32 | + $repository = $this->argument('repository'); |
| 33 | + |
| 34 | + if (! isset($kibble[$repository])) { |
| 35 | + if (! $this->confirm( |
| 36 | + question: "{$repository} repository not found in kibble.json. Would you like to add it?", |
| 37 | + default: true, |
| 38 | + )) { |
| 39 | + return Command::FAILURE; |
| 40 | + } |
| 41 | + |
| 42 | + $path = $this->ask('Enter the path to the repository (without trailing slash)'); |
| 43 | + |
| 44 | + $path = str_ends_with((string) $path, '/*') ? $path : $path.'/*'; |
| 45 | + |
| 46 | + // "dogfood": { |
| 47 | + // "type": "path", |
| 48 | + // "url": "/Users/gopher/Code/artisan-build/dogfood/packages/*", |
| 49 | + // "options": { |
| 50 | + // "symlink": true |
| 51 | + // } |
| 52 | + // }, |
| 53 | + $kibble[$repository] = [ |
| 54 | + 'type' => 'path', |
| 55 | + 'url' => $path, |
| 56 | + 'options' => [ |
| 57 | + 'symlink' => true, |
| 58 | + ], |
| 59 | + ]; |
| 60 | + |
| 61 | + file_put_contents( |
| 62 | + filename: 'kibble.json', |
| 63 | + data: json_encode( |
| 64 | + value: $kibble, |
| 65 | + flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
| 66 | + ).PHP_EOL |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Now start work on the composer.json file. |
| 72 | + */ |
| 73 | + $composer_json = file_get_contents('composer.json'); |
| 74 | + $trailing_newline = str_ends_with($composer_json, "\n"); |
| 75 | + |
| 76 | + $composer = json_decode($composer_json, true); |
| 77 | + |
| 78 | + if (! isset($composer['repositories'])) { |
| 79 | + $composer['repositories'] = []; |
| 80 | + } |
| 81 | + |
| 82 | + if ( |
| 83 | + isset($composer['repositories'][$repository]) && |
| 84 | + $composer['repositories'][$repository] === $kibble[$repository] |
| 85 | + ) { |
| 86 | + $this->info("{$repository} already in composer.json"); |
| 87 | + |
| 88 | + return Command::SUCCESS; |
| 89 | + } |
| 90 | + |
| 91 | + if ( |
| 92 | + isset($composer['repositories'][$repository]) && |
| 93 | + $composer['repositories'][$repository] !== $kibble[$repository] |
| 94 | + ) { |
| 95 | + $this->error("{$repository} already in composer.json but with different configuration."); |
| 96 | + |
| 97 | + return Command::FAILURE; |
| 98 | + } |
| 99 | + |
| 100 | + $composer['repositories'][$repository] = $kibble[$repository]; |
| 101 | + |
| 102 | + file_put_contents( |
| 103 | + filename: 'composer.json', |
| 104 | + data: json_encode( |
| 105 | + value: $composer, |
| 106 | + flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
| 107 | + ).($trailing_newline ? "\n" : '') |
| 108 | + ); |
| 109 | + |
| 110 | + $this->info("{$repository} added to composer.json"); |
| 111 | + |
| 112 | + return Command::SUCCESS; |
| 113 | + } |
| 114 | +} |
0 commit comments