Skip to content

Commit 5f6d7ad

Browse files
committed
minor #89 Add link utility to link Symfony UX packages to existing Symfony project (Kocal)
This PR was squashed before being merged into the main branch. Discussion ---------- Add `link` utility to link Symfony UX packages to existing Symfony project | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Tickets | Fix #40 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This PR add a new `link` utility to link Symfony UX packages to an existing Symfony project. It's literally a copy/paste from [the `link` utility from `symfony/symfony`](https://github.com/symfony/symfony/blob/5.x/link) but with different directories and an explicit dependency on Symfony Filesystem component. Demo: ![image](https://user-images.githubusercontent.com/2103975/116757933-67583f00-aa0f-11eb-92d8-3e2d2673ae6e.png) ![image](https://user-images.githubusercontent.com/2103975/116758130-c0c06e00-aa0f-11eb-94fd-962f2b0f942c.png) Commits ------- 65155a0 Add `link` utility to link Symfony UX packages to existing Symfony project
2 parents be76b0b + 65155a0 commit 5f6d7ad

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
node_modules
33
yarn.lock
44
yarn-error.log
5+
/composer.lock
6+
/vendor

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ do in JavaScript could be done streamlined as a UX package.
6363

6464
We have some ideas and we will release more packages in the coming days. The rest
6565
is on you: let's create an amazing ecosystem together!
66+
67+
## Contributing
68+
69+
If you want to test your code in an existing project that uses Symfony UX packages,
70+
you can use the `link` utility provided in this Git repository (that you have to clone).
71+
This tool scans the `vendor/` directory of your project, finds Symfony UX packages it uses,
72+
and replaces them by symbolic links to the ones in the Git repository.
73+
74+
```shell
75+
# Install required dependencies
76+
$ composer install
77+
78+
# And link Symfony UX packages to your project
79+
$ php link /path/to/your/project
80+
```

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "symfony/ux",
3+
"license": "MIT",
4+
"require-dev": {
5+
"symfony/filesystem": "^5.2"
6+
}
7+
}

link

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* This file is part of the Symfony package.
6+
*
7+
* (c) Fabien Potencier <fabien@symfony.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
require __DIR__.'/vendor/autoload.php';
14+
15+
use Symfony\Component\Filesystem\Filesystem;
16+
17+
/**
18+
* Links dependencies of a project to a local clone of the main symfony/symfony GitHub repository.
19+
*
20+
* @author Kévin Dunglas <dunglas@gmail.com>
21+
*/
22+
23+
$copy = false !== $k = array_search('--copy', $argv, true);
24+
$copy && array_splice($argv, $k, 1);
25+
$rollback = false !== $k = array_search('--rollback', $argv, true);
26+
$rollback && array_splice($argv, $k, 1);
27+
$pathToProject = $argv[1] ?? getcwd();
28+
29+
if (!is_dir("$pathToProject/vendor/symfony")) {
30+
echo 'Links dependencies of a project to a local clone of the main symfony/ux GitHub repository.'.PHP_EOL.PHP_EOL;
31+
echo "Usage: $argv[0] /path/to/the/project".PHP_EOL;
32+
echo ' Use `--copy` to copy dependencies instead of symlink'.PHP_EOL.PHP_EOL;
33+
echo ' Use `--rollback` to rollback'.PHP_EOL.PHP_EOL;
34+
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;
35+
exit(1);
36+
}
37+
38+
$sfPackages = array();
39+
40+
$filesystem = new Filesystem();
41+
$directories = glob(__DIR__.'/src/*', GLOB_ONLYDIR | GLOB_NOSORT);
42+
43+
foreach ($directories as $dir) {
44+
if ($filesystem->exists($composer = "$dir/composer.json")) {
45+
$sfPackages[json_decode(file_get_contents($composer))->name] = $dir;
46+
}
47+
}
48+
49+
foreach (glob("$pathToProject/vendor/symfony/*", GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
50+
$package = 'symfony/'.basename($dir);
51+
52+
if (!isset($sfPackages[$package])) {
53+
continue;
54+
}
55+
56+
if ($rollback) {
57+
$filesystem->remove($dir);
58+
echo "\"$package\" has been rollback from \"$sfPackages[$package]\".".PHP_EOL;
59+
continue;
60+
}
61+
62+
if (!$copy && is_link($dir)) {
63+
echo "\"$package\" is already a symlink, skipping.".PHP_EOL;
64+
continue;
65+
}
66+
67+
$sfDir = ('\\' === DIRECTORY_SEPARATOR || $copy) ? $sfPackages[$package] : $filesystem->makePathRelative($sfPackages[$package], dirname(realpath($dir)));
68+
69+
$filesystem->remove($dir);
70+
71+
if ($copy) {
72+
$filesystem->mirror($sfDir, $dir);
73+
echo "\"$package\" has been copied from \"$sfPackages[$package]\".".PHP_EOL;
74+
} else {
75+
$filesystem->symlink($sfDir, $dir);
76+
echo "\"$package\" has been linked to \"$sfPackages[$package]\".".PHP_EOL;
77+
}
78+
}
79+
80+
foreach (glob("$pathToProject/var/cache/*", GLOB_NOSORT) as $cacheDir) {
81+
$filesystem->remove($cacheDir);
82+
}
83+
84+
if ($rollback) {
85+
echo PHP_EOL."Rollback done, do not forget to run \"composer install\" in your project \"$pathToProject\".".PHP_EOL;
86+
}

0 commit comments

Comments
 (0)