From 524446eef6ebc7214d4b585a5b41948b43c26212 Mon Sep 17 00:00:00 2001 From: Florian Merle Date: Sun, 27 Apr 2025 20:40:57 +0200 Subject: [PATCH] Add link script --- composer.json | 1 + link | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100755 link diff --git a/composer.json b/composer.json index 075290b2..deac5a7a 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/config": "^6.4 || ^7.0", "symfony/dependency-injection": "^6.4 || ^7.0", "symfony/expression-language": "^6.4 || ^7.0", + "symfony/filesystem": "^6.4 || ^7.0", "symfony/http-client": "^6.4 || ^7.0", "symfony/http-kernel": "^6.4 || ^7.0", "symfony/security-bundle": "^6.4 || ^7.0", diff --git a/link b/link new file mode 100755 index 00000000..b2181a23 --- /dev/null +++ b/link @@ -0,0 +1,97 @@ +#!/usr/bin/env php + + * See https://github.com/symfony/symfony/blob/7.3/link + * + * (c) Fabien Potencier + */ + +$copy = false !== $k = array_search('--copy', $argv, true); +$copy && array_splice($argv, $k, 1); +$rollback = false !== $k = array_search('--rollback', $argv, true); +$rollback && array_splice($argv, $k, 1); +$pathToProject = $argv[1] ?? getcwd(); + +if (!is_dir("$pathToProject/vendor/sylius")) { + echo 'Links dependencies of a project to a local clone of the main Sylius/Stack GitHub repository.'.PHP_EOL.PHP_EOL; + echo "Usage: $argv[0] /path/to/the/project".PHP_EOL; + echo ' Use `--copy` to copy dependencies instead of symlink'.PHP_EOL.PHP_EOL; + echo ' Use `--rollback` to rollback'.PHP_EOL.PHP_EOL; + echo "The directory \"$pathToProject\" does not exist or the dependencies are not installed, did you forget to run \"composer install\" in your project?".PHP_EOL; + exit(1); +} + +$stackPackages = []; + +$filesystem = new Filesystem(); +$directories = glob(__DIR__.'/src/*', GLOB_ONLYDIR | GLOB_NOSORT); + +foreach ($directories as $dir) { + if ($filesystem->exists($composer = "$dir/composer.json")) { + $stackPackages[json_decode($filesystem->readFile($composer), flags: JSON_THROW_ON_ERROR)->name] = $dir; + } +} + +foreach (glob("$pathToProject/vendor/sylius/*", GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { + $package = 'sylius/'.basename($dir); + + if (!isset($stackPackages[$package])) { + continue; + } + + if ($rollback) { + $filesystem->remove($dir); + echo "\"$package\" has been rollback from \"$stackPackages[$package]\".".PHP_EOL; + continue; + } + + if (!$copy && is_link($dir)) { + echo "\"$package\" is already a symlink, skipping.".PHP_EOL; + continue; + } + + $sfDir = ('\\' === DIRECTORY_SEPARATOR || $copy) ? $stackPackages[$package] : $filesystem->makePathRelative($stackPackages[$package], dirname(realpath($dir))); + + $filesystem->remove($dir); + + if ($copy) { + $filesystem->mirror($sfDir, $dir); + echo "\"$package\" has been copied from \"$stackPackages[$package]\".".PHP_EOL; + } else { + $filesystem->symlink($sfDir, $dir); + echo "\"$package\" has been linked to \"$stackPackages[$package]\".".PHP_EOL; + } +} + +foreach (glob("$pathToProject/var/cache/*", GLOB_NOSORT) as $cacheDir) { + $filesystem->remove($cacheDir); +} + +if ($rollback) { + echo PHP_EOL."Rollback done, do not forget to run \"composer install\" in your project \"$pathToProject\".".PHP_EOL; +}