Last active
April 11, 2025 18:38
-
-
Save yuki777/6244823b8aa8cf4457e97e6407ada5ad to your computer and use it in GitHub Desktop.
Link the php installed by Brew to ~/.phpenv/versions
This file contains hidden or 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 bash | |
set -au | |
# Check cellar dir | |
cellarDir=$(brew --prefix)/Cellar | |
if [ ! -d "$cellarDir" ]; then | |
echo "Not found. brew package path $cellarDir :(" | |
echo | |
exit 1 | |
fi | |
# Unlink all link files | |
find ~/.phpenv/versions -type l -exec unlink {} \; | |
parentDirs=$(find $cellarDir/php* -type d -maxdepth 0) | |
echo "# Link the php installed by Brew to ~/.phpenv/versions" | |
for parentDir in $parentDirs | |
do | |
childDirs=$(find "$parentDir"/* -type d -maxdepth 0) | |
for childDir in $childDirs | |
do | |
phpPath=$childDir/bin/php | |
if [ ! -f "$phpPath" ]; then | |
continue | |
fi | |
major=$($phpPath -d error_reporting=0 -r 'echo PHP_MAJOR_VERSION;') | |
minor=$($phpPath -d error_reporting=0 -r 'echo PHP_MINOR_VERSION;') | |
patch=$($phpPath -d error_reporting=0 -r 'echo PHP_RELEASE_VERSION;') | |
# link major.minor | |
linkCommand="ln -s $childDir $HOME/.phpenv/versions/$major.$minor" | |
echo "$linkCommand" | |
$linkCommand | |
# link major.minor.patch | |
linkCommand="ln -s $childDir $HOME/.phpenv/versions/$major.$minor.$patch" | |
echo "$linkCommand" | |
$linkCommand | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment