Skip to content

Commit c96d84a

Browse files
committed
add link command
1 parent 64a4b60 commit c96d84a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

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/ai 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)