Skip to content

Instantly share code, notes, and snippets.

@mralaminahamed
Forked from DarkaOnLine/php-storm-eap-update.sh
Last active October 2, 2024 14:33
Show Gist options
  • Select an option

  • Save mralaminahamed/15d4d0484d6c88a11a101caae35064c7 to your computer and use it in GitHub Desktop.

Select an option

Save mralaminahamed/15d4d0484d6c88a11a101caae35064c7 to your computer and use it in GitHub Desktop.
This script automates the process of updating PhpStorm EAP version. It downloads the latest EAP build and installs it on your system.
#!/usr/bin/env bash
#####################################################################
# PhpStorm EAP Updater Script
#
# This script automates the process of updating PhpStorm EAP version.
# It fetches the latest EAP build information, allows the user to choose
# the download format, and installs it on your system.
#
# Usage:
# sudo ./update_phpstorm_eap.sh [-d <install_dir>]
#
# Options:
# -d <install_dir> Specify a custom installation directory
# -h Display this help message
#
# Requirements:
# - This script must be run with root privileges (sudo)
# - curl and wget must be installed on the system
#
# Author: [Your Name]
# Version: 1.2
# Last Updated: [Current Date]
#####################################################################
set -euo pipefail
IFS=$'\n\t'
# Default configuration
readonly DEFAULT_INSTALL_DIR="/opt/PhpStorm-EAP"
readonly EAP_PAGE_URL="https://www.jetbrains.com/phpstorm/nextversion/"
readonly TEMP_DIR="/tmp/PhpStorm-EAP-temp"
readonly LOG_FILE="/var/log/phpstorm_eap_updater.log"
# Color codes for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly NC='\033[0m' # No Color
# Function to log messages
log_message() {
local level=$1
local message=$2
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [$level] $message" >> "$LOG_FILE"
}
# Function to display colored output
print_message() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Function to display usage information
usage() {
cat << EOF
Usage: $0 [-d <install_dir>]
-d <install_dir> Specify the installation directory
-h Display this help message
EOF
exit 1
}
# Function to check if script is run as root
check_root() {
if [[ $EUID -ne 0 ]]; then
print_message "$RED" "Error: This script must be run as root"
log_message "ERROR" "Script execution attempted without root privileges"
exit 1
fi
}
# Function to check required tools
check_requirements() {
local tools=("wget" "curl")
for tool in "${tools[@]}"; do
if ! command -v "$tool" &> /dev/null; then
print_message "$RED" "Error: $tool is not installed. Please install it and try again."
log_message "ERROR" "$tool not found on the system"
exit 1
fi
done
}
# Function to fetch latest EAP build information
fetch_eap_info() {
print_message "$YELLOW" "Fetching latest EAP build information..."
log_message "INFO" "Fetching EAP info from $EAP_PAGE_URL"
local eap_page
eap_page=$(curl -s "$EAP_PAGE_URL")
# Extract build number and release date
local build_info
build_info=$(echo "$eap_page" | grep -oP '202\d+\.\d+ eap build \d+\.\d+\.\d+.*?Released: [A-Za-z]+ \d+, 202\d' | head -n 1)
if [[ -z "$build_info" ]]; then
print_message "$RED" "Error: Could not fetch latest EAP build information"
log_message "ERROR" "Failed to fetch EAP build information"
exit 1
fi
echo "$build_info"
}
# Function to get available download formats
get_download_formats() {
local eap_page=$1
local formats
formats=$(echo "$eap_page" | grep -oP '\.tar\.gz \([^)]+\)' | sort -u)
echo "$formats"
}
# Function to prompt user for download format choice
choose_download_format() {
local formats=("$@")
print_message "$YELLOW" "Available download formats:"
for i in "${!formats[@]}"; do
echo "[$((i+1))] ${formats[i]}"
done
local choice
while true; do
read -rp "Enter the number of your chosen format: " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#formats[@]}" ]; then
break
fi
print_message "$RED" "Invalid choice. Please enter a number between 1 and ${#formats[@]}."
done
echo "${formats[$((choice-1))]}"
}
# Function to get download URL for chosen format
get_download_url() {
local eap_page=$1
local chosen_format=$2
local url
url=$(echo "$eap_page" | grep -oP "https://download\.jetbrains\.com/webide/PhpStorm-[^\"]+\.tar\.gz" | grep "$chosen_format" | head -n 1)
echo "$url"
}
# Function to download and extract PhpStorm
download_and_extract() {
local url=$1
print_message "$YELLOW" "Downloading PhpStorm from $url"
log_message "INFO" "Downloading PhpStorm from $url"
mkdir -p "$TEMP_DIR"
if ! wget --no-check-certificate -O "$TEMP_DIR/phpstorm.tar.gz" "$url"; then
print_message "$RED" "Error: Failed to download PhpStorm"
log_message "ERROR" "Failed to download PhpStorm from $url"
exit 1
fi
print_message "$YELLOW" "Extracting PhpStorm"
log_message "INFO" "Extracting PhpStorm"
tar -xzf "$TEMP_DIR/phpstorm.tar.gz" -C "$TEMP_DIR"
}
# Function to update PhpStorm installation
update_phpstorm() {
local install_dir=$1
print_message "$YELLOW" "Removing old PhpStorm installation"
log_message "INFO" "Removing old PhpStorm installation from $install_dir"
rm -rf "$install_dir"
print_message "$YELLOW" "Installing new PhpStorm"
log_message "INFO" "Installing new PhpStorm to $install_dir"
mv "$TEMP_DIR"/PhpStorm-* "$install_dir"
}
# Main script execution
main() {
local install_dir="$DEFAULT_INSTALL_DIR"
# Parse command-line options
while getopts ":d:h" opt; do
case $opt in
d) install_dir="$OPTARG" ;;
h) usage ;;
\?) print_message "$RED" "Invalid option -$OPTARG"; usage ;;
:) print_message "$RED" "Option -$OPTARG requires an argument"; usage ;;
esac
done
check_root
check_requirements
local eap_info
eap_info=$(fetch_eap_info)
print_message "$GREEN" "Latest EAP build: $eap_info"
local eap_page
eap_page=$(curl -s "$EAP_PAGE_URL")
local formats
mapfile -t formats < <(get_download_formats "$eap_page")
local chosen_format
chosen_format=$(choose_download_format "${formats[@]}")
local download_url
download_url=$(get_download_url "$eap_page" "$chosen_format")
if [[ -z "$download_url" ]]; then
print_message "$RED" "Error: Could not find download URL for chosen format"
log_message "ERROR" "Failed to find download URL for format: $chosen_format"
exit 1
fi
download_and_extract "$download_url"
update_phpstorm "$install_dir"
print_message "$YELLOW" "Cleaning up temporary files"
log_message "INFO" "Cleaning up temporary files"
rm -rf "$TEMP_DIR"
print_message "$GREEN" "PhpStorm EAP has been successfully updated!"
log_message "INFO" "PhpStorm EAP update completed successfully"
}
# Trap for cleanup on script exit
trap 'rm -rf "$TEMP_DIR"' EXIT
# Execute main function
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment