|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Updates the composer.json files to use the local version of the Symfony AI packages. |
| 5 | + */ |
| 6 | + |
| 7 | +require __DIR__.'/../vendor/autoload.php'; |
| 8 | + |
| 9 | +use Symfony\Component\Finder\Finder; |
| 10 | + |
| 11 | +$finder = (new Finder()) |
| 12 | + ->in([__DIR__.'/../src/*/', __DIR__.'/../src/*/src/Bridge/*/']) |
| 13 | + ->depth(0) |
| 14 | + ->name('composer.json') |
| 15 | +; |
| 16 | + |
| 17 | +// 1. Find all AI packages |
| 18 | +$aiPackages = []; |
| 19 | +foreach ($finder as $composerFile) { |
| 20 | + $json = file_get_contents($composerFile->getPathname()); |
| 21 | + if (null === $packageData = json_decode($json, true)) { |
| 22 | + passthru(sprintf('composer validate %s', $composerFile->getPathname())); |
| 23 | + exit(1); |
| 24 | + } |
| 25 | + |
| 26 | + if (str_starts_with($composerFile->getPathname(), __DIR__ . '/../src/')) { |
| 27 | + $packageName = $packageData['name']; |
| 28 | + |
| 29 | + $aiPackages[$packageName] = [ |
| 30 | + 'path' => realpath($composerFile->getPath()), |
| 31 | + ]; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// 2. Update all composer.json files from the repository, to use the local version of the AI packages |
| 36 | +foreach ($finder as $composerFile) { |
| 37 | + $json = file_get_contents($composerFile->getPathname()); |
| 38 | + if (null === $packageData = json_decode($json, true)) { |
| 39 | + passthru(sprintf('composer validate %s', $composerFile->getPathname())); |
| 40 | + exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + $repositories = $packageData['repositories'] ?? []; |
| 44 | + |
| 45 | + foreach ($aiPackages as $packageName => $packageInfo) { |
| 46 | + if (isset($packageData['require'][$packageName]) |
| 47 | + || isset($packageData['require-dev'][$packageName]) |
| 48 | + ) { |
| 49 | + $repositories[] = [ |
| 50 | + 'type' => 'path', |
| 51 | + 'url' => $packageInfo['path'], |
| 52 | + ]; |
| 53 | + $key = isset($packageData['require'][$packageName]) ? 'require' : 'require-dev'; |
| 54 | + $packageData[$key][$packageName] = '@dev'; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if ($repositories) { |
| 59 | + $packageData['repositories'] = $repositories; |
| 60 | + } |
| 61 | + |
| 62 | + $json = json_encode($packageData, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE); |
| 63 | + file_put_contents($composerFile->getPathname(), $json."\n"); |
| 64 | +} |
0 commit comments