From 65155a002ca5658c9bc18ddbffd6d452eeb457ef Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Fri, 30 Apr 2021 23:52:23 +0200 Subject: [PATCH] Add `link` utility to link Symfony UX packages to existing Symfony project --- .gitignore | 2 ++ README.md | 15 +++++++++ composer.json | 7 +++++ link | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 composer.json create mode 100755 link diff --git a/.gitignore b/.gitignore index b1150e7ab6b..75eb9de11ad 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ node_modules yarn.lock yarn-error.log +/composer.lock +/vendor diff --git a/README.md b/README.md index 434da9bc1d9..a0367236b7a 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,18 @@ do in JavaScript could be done streamlined as a UX package. We have some ideas and we will release more packages in the coming days. The rest is on you: let's create an amazing ecosystem together! + +## Contributing + +If you want to test your code in an existing project that uses Symfony UX packages, +you can use the `link` utility provided in this Git repository (that you have to clone). +This tool scans the `vendor/` directory of your project, finds Symfony UX packages it uses, +and replaces them by symbolic links to the ones in the Git repository. + +```shell +# Install required dependencies +$ composer install + +# And link Symfony UX packages to your project +$ php link /path/to/your/project +``` diff --git a/composer.json b/composer.json new file mode 100644 index 00000000000..abca8ffc469 --- /dev/null +++ b/composer.json @@ -0,0 +1,7 @@ +{ + "name": "symfony/ux", + "license": "MIT", + "require-dev": { + "symfony/filesystem": "^5.2" + } +} diff --git a/link b/link new file mode 100755 index 00000000000..52fbd1d2d6c --- /dev/null +++ b/link @@ -0,0 +1,86 @@ +#!/usr/bin/env php + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +require __DIR__.'/vendor/autoload.php'; + +use Symfony\Component\Filesystem\Filesystem; + +/** + * Links dependencies of a project to a local clone of the main symfony/symfony GitHub repository. + * + * @author Kévin Dunglas + */ + +$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/symfony")) { + echo 'Links dependencies of a project to a local clone of the main symfony/ux 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); +} + +$sfPackages = array(); + +$filesystem = new Filesystem(); +$directories = glob(__DIR__.'/src/*', GLOB_ONLYDIR | GLOB_NOSORT); + +foreach ($directories as $dir) { + if ($filesystem->exists($composer = "$dir/composer.json")) { + $sfPackages[json_decode(file_get_contents($composer))->name] = $dir; + } +} + +foreach (glob("$pathToProject/vendor/symfony/*", GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { + $package = 'symfony/'.basename($dir); + + if (!isset($sfPackages[$package])) { + continue; + } + + if ($rollback) { + $filesystem->remove($dir); + echo "\"$package\" has been rollback from \"$sfPackages[$package]\".".PHP_EOL; + continue; + } + + if (!$copy && is_link($dir)) { + echo "\"$package\" is already a symlink, skipping.".PHP_EOL; + continue; + } + + $sfDir = ('\\' === DIRECTORY_SEPARATOR || $copy) ? $sfPackages[$package] : $filesystem->makePathRelative($sfPackages[$package], dirname(realpath($dir))); + + $filesystem->remove($dir); + + if ($copy) { + $filesystem->mirror($sfDir, $dir); + echo "\"$package\" has been copied from \"$sfPackages[$package]\".".PHP_EOL; + } else { + $filesystem->symlink($sfDir, $dir); + echo "\"$package\" has been linked to \"$sfPackages[$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; +}