Jump to content
America's Comeback

Installing PhpStorm on Rocky 9 Linux


Recommended Posts

Posted
#!/bin/bash

set -e  # Exit immediately if a command exits with a non-zero status
[ "${DEBUG:-0}" == "1" ] && set -x  # Enable debug mode
set -u  # Exit if an unset variable is used
set -C  # Prevent existing files from being overwritten using '>'
[ "${VERBOSE:-0}" == "1" ] && set -v  # Print shell input lines as read

# Check for root privileges
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run with sudo"
   exit 1
fi

# Get the actual user who ran sudo
ACTUAL_USER="${SUDO_USER:-$USER}"
ACTUAL_HOME=$(getent passwd "$ACTUAL_USER" | cut -d: -f6)

PHPSTORM_HOME="${ACTUAL_HOME}/.jetbrains/phpstorm"
PHPSTORM_SESSIONS="${PHPSTORM_HOME}/sessions"
PHPSTORM_LOGS="${PHPSTORM_HOME}/logs"

safe_exit() {
    local msg="${1:-UnexpectedError}"
    echo "${msg}"
    exit 1
}

# Help
if [[ "${1:-}" == "-h" || "${1:-}" == "help" || "${1:-}" == "--help" ]]; then
    cat << EOF
Usage: ${0##*/} [version] [options]

This script installs PhpStorm, an IDE by JetBrains for PHP development. It downloads
the specified version of PhpStorm, verifies its checksum, and sets up a convenient
execution shim.

Arguments:
  version           Specify the version of PhpStorm to download, e.g., '2024.3'.
                   If not provided, defaults to the latest available version.

Options:
  -h, --help, help  Display this help and exit.

Example:
  ${0##*/} 2024.3    # Install PhpStorm version 2024.3
  ${0##*/}           # Install the latest version of PhpStorm

EOF
    exit 1
fi

# Version handling
default_version="2024.3"
version="${1:-"${default_version}"}"
version_regex='^[0-9]{4}\.[0-9]{1,2}$'
if [[ ! $version =~ $version_regex ]]; then
    echo "Invalid version specified. Installing default version ${default_version}."
    read -r -p "Do you want to install PhpStorm ${default_version}? (y/n): " choice
    case "${choice}" in
        [Yy]*)
            version="${default_version}"
            ;;
        *)
            safe_exit "Installation cancelled."
            ;;
    esac
fi

# Create required directories with proper ownership
install -d -m 755 -o "$ACTUAL_USER" -g "$ACTUAL_USER" \
    "${PHPSTORM_HOME}" "${PHPSTORM_SESSIONS}" "${PHPSTORM_LOGS}"

# Download and verify PhpStorm
cd "${PHPSTORM_HOME}"

phpstorm_tar="PhpStorm-${version}.tar.gz"
checksum_file="${phpstorm_tar}.sha256"
checksum_url="https://download.jetbrains.com/webide/${checksum_file}"
tar_url="https://download-cdn.jetbrains.com/webide/${phpstorm_tar}"

# Download checksum
if [[ ! -f "${checksum_file}" ]]; then
    curl -f -L "${checksum_url}" -o "${checksum_file}" || safe_exit "Failed to download ${checksum_url}"
    chown "$ACTUAL_USER:$ACTUAL_USER" "${checksum_file}"
fi

# Download tarball
if [[ ! -f "${phpstorm_tar}" ]]; then
    curl -f -L "${tar_url}" -o "${phpstorm_tar}" || safe_exit "Failed to download ${tar_url}"
    chown "$ACTUAL_USER:$ACTUAL_USER" "${phpstorm_tar}"
fi

# Verify checksum
local_checksum="$(sha256sum "${phpstorm_tar}" | awk '{print $1}')"
upstream_checksum="$(awk '{print $1}' "${checksum_file}")"

if [[ "${upstream_checksum}" != "${local_checksum}" ]]; then
    echo "DANGER! Checksums do not match!"
    echo "${phpstorm_tar} = ${local_checksum}"
    echo "${checksum_url} = ${upstream_checksum}"
    rm -f "${checksum_file}" "${phpstorm_tar}"
    exit 1
fi

# Extract PhpStorm
tar -zxf "${phpstorm_tar}"
rm -f "${phpstorm_tar}" "${checksum_file}"

# Create latest symlink
phpstorm_dir=$(find . -maxdepth 1 -type d -name "PhpStorm-*" | head -n 1)
[[ -L "latest" ]] && rm -f "latest"
ln -s "${phpstorm_dir}" "latest"

# Fix ownership of all installed files
chown -R "$ACTUAL_USER:$ACTUAL_USER" "${PHPSTORM_HOME}"

# Create the shim file first in a temporary location
TMP_SHIM=$(mktemp)
[ -f "${TMP_SHIM}"  ] && sudo rm -rf "${TMP_SHIM}"
cat > "${TMP_SHIM}" << 'EOF'
#!/bin/bash

PHPSTORM_HOME="${HOME}/.jetbrains/phpstorm"
PHPSTORM_SCRIPT="${PHPSTORM_HOME}/latest/bin/phpstorm.sh"
PHPSTORM_SESSIONS="${PHPSTORM_HOME}/sessions"
PHPSTORM_LOGS="${PHPSTORM_HOME}/logs"

# Ensure directories exist
mkdir -p "${PHPSTORM_SESSIONS}" "${PHPSTORM_LOGS}"

# Clean old logs (90+ days or 1GB+ files older than 7 days)
cleanup_logs() {
    find "${PHPSTORM_LOGS}" -type f -name '*.log' -mtime +90 -delete
    large_files=$(find "${PHPSTORM_LOGS}" -type f -name '*.log' -mtime +7 -size +1G)
    if [ -n "$large_files" ]; then
        echo "Found large log files older than 7 days and larger than 1GB:"
        echo "$large_files"
        read -t 12 -p "Delete these files? (yes/no): " response
        if [ "$response" = "yes" ]; then
            echo "$large_files" | xargs rm -f
            echo "Large log files deleted."
        fi
    fi
}

cleanup_logs

# Launch PhpStorm
LOG_FILE="${PHPSTORM_LOGS}/$(date +'%Y.%m.%d.%H.%M.%S').log"
nohup "${PHPSTORM_SCRIPT}" "$@" > "${LOG_FILE}" 2>&1 &
EOF

# Move the shim to its final location with proper permissions
install -m 755 "${TMP_SHIM}" "/usr/local/bin/phpstorm"
rm -f "${TMP_SHIM}"

echo "PhpStorm ${version} has been installed successfully!"
echo "You can start PhpStorm by running 'phpstorm' from any directory."

 

×
×
  • Create New...