Skip to content

Commit

Permalink
Refactor install.sh to make phpvm binary executable and handle versio…
Browse files Browse the repository at this point in the history
…n fetching errors
  • Loading branch information
Thavarshan committed Oct 6, 2024
1 parent 7a2117d commit c5a7de0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 61 deletions.
60 changes: 1 addition & 59 deletions bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,6 @@
phpvm_echo >&2 'Failed to install Node.js dependencies. Please report this!'
exit 1
}

# Make the phpvm binary executable
phpvm_echo "=> Making phpvm binary executable"
chmod +x "$INSTALL_DIR/bin/phpvm" || {
phpvm_echo >&2 'Failed to set execute permissions on phpvm binary. Please report this!'
exit 1
}
}

phpvm_detect_profile() {
if [ "${PROFILE-}" = '/dev/null' ]; then
return
fi

if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
phpvm_echo "${PROFILE}"
return
fi

local SHELL_TYPE
SHELL_TYPE="$(basename "$SHELL")"

if [ "$SHELL_TYPE" = "bash" ]; then
if [ -f "$HOME/.bashrc" ]; then
phpvm_echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
phpvm_echo "$HOME/.bash_profile"
fi
elif [ "$SHELL_TYPE" = "zsh" ]; then
if [ -f "$HOME/.zshrc" ]; then
phpvm_echo "$HOME/.zshrc"
elif [ -f "$HOME/.zprofile" ]; then
phpvm_echo "$HOME/.zprofile"
fi
fi
}

inject_phpvm_config() {
Expand All @@ -128,37 +93,14 @@
PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")"

PHPVM_CONFIG_STR="
# Set up PHPVM environment
export PHPVM_DIR=\"${PROFILE_INSTALL_DIR}\"
# Only source phpvm if it's needed (for auto-switching versions)
phpvm_auto_switch_on_cd() {
local PHPVMRC_FILE=\"\$(phpvm_find_phpvmrc)\"
if [ -n \"\$PHPVMRC_FILE\" ]; then
local PHP_VERSION=\$(cat \"\$PHPVMRC_FILE\")
if [ -n \"\$PHP_VERSION\" ]; then
phpvm use \$PHP_VERSION
else
echo 'No PHP version specified in .phpvmrc'
fi
fi
}
# Update 'cd' command to automatically switch versions based on .phpvmrc
cd() {
builtin cd \"\$@\" || return
phpvm_auto_switch_on_cd
}
# Load PHPVM if necessary (this will allow phpvm to be invoked manually)
if [ -s \"\$PHPVM_DIR/index.js\" ]; then
export PATH=\"\$PHPVM_DIR/bin:\$PATH\"
fi
"

if [ -n "$PHPVM_PROFILE" ]; then
if ! command grep -qc 'phpvm_auto_switch_on_cd' "$PHPVM_PROFILE"; then
if ! command grep -qc '/phpvm/index.js' "$PHPVM_PROFILE"; then
phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE"
echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE"
else
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/phpvmrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ function findPHPVMRCFile(startDir = process.cwd()) {
* Automatically switch to the PHP version specified in the .phpvmrc file
*
* This function is meant to be called when the shell is started.
* It remains silent if no .phpvmrc file is found.
*/
async function autoSwitchPHPVersion() {
const phpvmrcPath = findPHPVMRCFile();

if (phpvmrcPath) {
const version = fs.readFileSync(phpvmrcPath, 'utf8').trim();
console.log(`Found .phpvmrc file. PHP version specified: ${version}`);

// Switch to the specified version
try {
await usePHPVersion(version); // Use your existing 'use' command logic
await usePHPVersion(version); // Use the existing 'use' command logic
console.log(`Switched to PHP ${version}`);
} catch (error) {
console.error(`Failed to switch to PHP ${version}: ${error.message}`);
}
} else {
// Silent if no .phpvmrc file is found
return;
}
}

Expand Down

0 comments on commit c5a7de0

Please sign in to comment.