Last active
November 11, 2023 08:08
-
-
Save GromNaN/020dd50fc3ed71d31ebc548bb75845e7 to your computer and use it in GitHub Desktop.
composer-link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$pwd = getcwd(); | |
$args = $_SERVER['argv']; | |
if (count($args) !== 2) { | |
echo "Replace any composer package from a project with a symlink to an external directory.\n"; | |
echo "Usage: composer-link <path-to-package>\n"; | |
exit(1); | |
} | |
$packagePath = $args[1]; | |
if (in_array($packagePath, ['.', '..'])) { | |
echo "Invalid package path: $packagePath\n"; | |
exit(1); | |
} | |
if (!str_starts_with($packagePath, '/')) { | |
$packagePath = $pwd.'/'.$packagePath; | |
} | |
if (!is_dir($packagePath)) { | |
echo "Package path does not exist: $packagePath\n"; | |
exit(1); | |
} | |
$packageJsonPath = $packagePath . '/composer.json'; | |
if (!is_file($packageJsonPath)) { | |
echo "Package path does not contain a composer.json file: $packageJsonPath\n"; | |
exit(1); | |
} | |
$packageJson = json_decode(file_get_contents($packageJsonPath), true); | |
if (!isset($packageJson['name'])) { | |
echo "Package composer.json does not contain a name: $packageJsonPath\n"; | |
exit(1); | |
} | |
$packageName = $packageJson['name']; | |
$installed = require $pwd . '/vendor/composer/installed.php'; | |
if (!isset($installed['versions'][$packageName])) { | |
echo "Package is not installed: $packageName\n"; | |
echo "Run `composer require $packageName` before\n"; | |
exit(1); | |
} | |
$installPath = $installed['versions'][$packageName]['install_path']; | |
if (file_exists($installPath)) { | |
exec("rm -rf '$installPath'"); | |
} | |
echo "Linking $packagePath to $installPath\n"; | |
symlink($packagePath, $installPath); | |
echo "\nTo revert to the original package at the locked version instead of the symlink, run:\n"; | |
echo "composer reinstall $packageName\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment