From 11c998a14833ed897127764447c7a907d1ee1b5f Mon Sep 17 00:00:00 2001 From: Nidal Bakir <29513372+Nidal-Bakir@users.noreply.github.com> Date: Tue, 1 Oct 2024 20:15:25 +0300 Subject: [PATCH] feat: automatically add `~/.fvm_flutter/bin` to the PATH var in `install.sh` and introduce `update.sh` (#700) --- scripts/install.sh | 197 ++++++++++++++++++++++++++++++++++--------- scripts/uninstall.sh | 31 +++---- scripts/update.sh | 116 +++++++++++++++++++++++++ 3 files changed, 289 insertions(+), 55 deletions(-) create mode 100755 scripts/update.sh diff --git a/scripts/install.sh b/scripts/install.sh index 5ce5c2a8..18225e14 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -11,27 +11,35 @@ ARCH="$(uname -m)" # Map to FVM naming case "$OS" in - Linux*) OS='linux' ;; - Darwin*) OS='macos' ;; - *) log_message "Unsupported OS"; exit 1 ;; +Linux*) OS='linux' ;; +Darwin*) OS='macos' ;; +*) + log_message "Unsupported OS" + exit 1 + ;; esac case "$ARCH" in - x86_64) ARCH='x64' ;; - arm64|aarch64) ARCH='arm64' ;; - armv7l) ARCH='arm' ;; - *) log_message "Unsupported architecture"; exit 1 ;; + x86_64) ARCH='x64' ;; + arm64|aarch64) ARCH='arm64' ;; + armv7l) ARCH='arm' ;; + *) log_message "Unsupported architecture"; exit 1 ;; esac # Terminal colors setup -Color_Off='\033[0m' # Reset -Green='\033[0;32m' # Green -Red='\033[0;31m' +Color_Off='\033[0m' # Reset +Green='\033[0;32m' # Green +Red='\033[0;31m' +Bold_White='\033[1m' # Bold White success() { log_message "${Green}$1${Color_Off}" } +info_bold() { + log_message "${Bold_White}$1${Color_Off}" +} + error() { log_message "${Red}error: $1${Color_Off}" >&2 exit 1 @@ -42,42 +50,40 @@ log_message "Detected OS: $OS" log_message "Detected Architecture: $ARCH" # Check for curl -if ! command -v curl &> /dev/null; then +if ! command -v curl &>/dev/null; then error "curl is required but not installed." fi # Get installed FVM version if exists INSTALLED_FVM_VERSION="" -if command -v fvm &> /dev/null; then +if command -v fvm &>/dev/null; then INSTALLED_FVM_VERSION=$(fvm --version 2>&1) || error "Failed to fetch installed FVM version." fi # Define the URL of the FVM binary if [ -z "$1" ]; then - FVM_VERSION=$(curl -s https://api.github.com/repos/leoafarias/fvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') - if [ -z "$FVM_VERSION" ]; then - error "Failed to fetch latest FVM version." - fi + FVM_VERSION=$(curl -s https://api.github.com/repos/leoafarias/fvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + if [ -z "$FVM_VERSION" ]; then + error "Failed to fetch latest FVM version." + fi else - FVM_VERSION="$1" + FVM_VERSION="$1" fi log_message "Installing FVM version $FVM_VERSION." # Setup installation directory and symlink FVM_DIR="$HOME/.fvm_flutter" -FMV_DIR_BIN="$FVM_DIR/bin" -SYMLINK_TARGET="/usr/local/bin/fvm" - +FVM_DIR_BIN="$FVM_DIR/bin" # Create FVM directory if it doesn't exist mkdir -p "$FVM_DIR" || error "Failed to create FVM directory: $FVM_DIR." # Check if FVM_DIR exists, and if it does delete it -if [ -d "$FMV_DIR_BIN" ]; then +if [ -d "$FVM_DIR_BIN" ]; then log_message "FVM bin directory already exists. Removing it." - if ! rm -rf "$FMV_DIR_BIN"; then - error "Failed to remove existing FVM directory: $FMV_DIR_BIN." + if ! rm -rf "$FVM_DIR_BIN"; then + error "Failed to remove existing FVM directory: $FVM_DIR_BIN." fi fi @@ -87,7 +93,6 @@ if ! curl -L "$URL" -o fvm.tar.gz; then error "Download failed. Check your internet connection and URL: $URL" fi - # Extract binary to the new location if ! tar xzf fvm.tar.gz -C "$FVM_DIR" 2>&1; then error "Extraction failed. Check permissions and tar.gz file integrity." @@ -99,27 +104,139 @@ if ! rm -f fvm.tar.gz; then fi # Rename FVM_DIR/fvm to FVM_DIR/bin -if ! mv "$FVM_DIR/fvm" "$FMV_DIR_BIN"; then +if ! mv "$FVM_DIR/fvm" "$FVM_DIR_BIN"; then error "Failed to move fvm to bin directory." fi -# Create a symlink -if ! ln -sf "$FMV_DIR_BIN/fvm" "$SYMLINK_TARGET"; then - # verify if linux and try with sudo - if [ "$OS" == "linux" ]; then - echo -e "Trying to create symlink with sudo..." - if ! sudo ln -sf "$FMV_DIR_BIN/fvm" "$SYMLINK_TARGET"; then - error "Failed to create symlink" - fi +tildify() { + if [[ $1 = $HOME/* ]]; then + local replacement=\~/ + + echo "${1/$HOME\//$replacement}" else - error "Failed to create symlink" + echo "$1" fi -fi +} + +tilde_FVM_DIR_BIN=$(tildify "$FVM_DIR_BIN") +refresh_command='' + +case $(basename "$SHELL") in +fish) + commands=( + "set --export PATH $FVM_DIR_BIN \$PATH" + ) + + fish_config=$HOME/.config/fish/config.fish + tilde_fish_config=$(tildify "$fish_config") + + if [[ -w $fish_config ]]; then + { + echo -e '\n# FVM' + + for command in "${commands[@]}"; do + echo "$command" + done + } >>"$fish_config" + + log_message "Added \"$tilde_FVM_DIR_BIN\" to \$PATH in \"$tilde_fish_config\"" + refresh_command="source $tilde_fish_config" + + else + log_message "Manually add the directory to $tilde_fish_config (or similar):" + + for command in "${commands[@]}"; do + info_bold " $command" + done + fi + ;; +zsh) + commands=( + "export PATH=\"$FVM_DIR_BIN:\$PATH\"" + ) + + zsh_config=$HOME/.zshrc + tilde_zsh_config=$(tildify "$zsh_config") + + if [[ -w $zsh_config ]]; then + { + echo -e '\n# FVM' + + for command in "${commands[@]}"; do + echo "$command" + done + } >>"$zsh_config" + + log_message "Added \"$tilde_FVM_DIR_BIN\" to \$PATH in \"$tilde_zsh_config\"" + refresh_command="source $zsh_config" + + else + log_message "Manually add the directory to $tilde_zsh_config (or similar):" + + for command in "${commands[@]}"; do + info_bold " $command" + done + fi + ;; +bash) + commands=( + "export PATH=$FVM_DIR_BIN:\$PATH" + ) + + bash_configs=( + "$HOME/.bashrc" + "$HOME/.bash_profile" + ) + + if [[ ${XDG_CONFIG_HOME:-} ]]; then + bash_configs+=( + "$XDG_CONFIG_HOME/.bash_profile" + "$XDG_CONFIG_HOME/.bashrc" + "$XDG_CONFIG_HOME/bash_profile" + "$XDG_CONFIG_HOME/bashrc" + ) + fi + + set_manually=true + for bash_config in "${bash_configs[@]}"; do + tilde_bash_config=$(tildify "$bash_config") + + if [[ -w $bash_config ]]; then + { + echo -e '\n# FVM' + + for command in "${commands[@]}"; do + echo "$command" + done + } >>"$bash_config" + + log_message "Added \"$tilde_FVM_DIR_BIN\" to \$PATH in \"$tilde_bash_config\"" + refresh_command="source $bash_config" + set_manually=false + break + fi + done + + if [[ $set_manually = true ]]; then + log_message "Manually add the directory to $tilde_bash_config (or similar):" + + for command in "${commands[@]}"; do + info_bold " $command" + done + fi + ;; +*) + log_message 'Manually add the directory to ~/.bashrc (or similar):' + info_bold " export PATH=\"$FVM_DIR_BIN:\$PATH\"" + ;; +esac + +echo +log_message "To get started, run:" +echo -# Verify installation -if ! command -v fvm &> /dev/null; then - error "Installation verification failed. FVM may not be in PATH or failed to execute." +if [[ $refresh_command ]]; then + info_bold " $refresh_command" fi -INSTALLED_FVM_VERSION=$(fvm --version 2>&1) || error "Failed to verify installed FVM version." -success "FVM $INSTALLED_FVM_VERSION installed successfully." +info_bold " fvm --help" diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index 2024af9b..265adc3c 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -1,15 +1,21 @@ #!/bin/bash -# Define the FVM directory and binary path -FVM_DIR="$HOME/.fvm_flutter" -BIN_LINK="/usr/local/bin/fvm" +# Detect OS +OS="$(uname -s)" -# Check if FVM is installed -if ! command -v fvm &> /dev/null -then - echo "FVM is not installed. Exiting." +# Map to FVM naming +case "$OS" in +Linux*) OS='linux' ;; +Darwin*) OS='macos' ;; +*) + log_message "Unsupported OS" exit 1 -fi + ;; +esac + +# Define the FVM directory and binary path +FVM_DIR="$HOME/.fvm_flutter" +FVM_DIR_BIN="$FVM_DIR/bin" # Remove the FVM binary echo "Uninstalling FVM..." @@ -18,15 +24,10 @@ rm -rf "$FVM_DIR" || { exit 1 } -# Remove the symlink -rm -f "$BIN_LINK" || { - echo "Failed to remove FVM symlink: $BIN_LINK." - exit 1 -} +echo "You can remove \"export PATH=$FVM_DIR_BIN:\$PATH\" from your ~/.bashrc, ~/.zshrc (or similar)" # Check if uninstallation was successful -if command -v fvm &> /dev/null -then +if command -v fvm &>/dev/null; then echo "Uninstallation failed. Please try again later." exit 1 fi diff --git a/scripts/update.sh b/scripts/update.sh new file mode 100755 index 00000000..e09a93bd --- /dev/null +++ b/scripts/update.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash + +# Function to log messages with date and time +log_message() { + echo -e "$1" +} + +# Detect OS and architecture +OS="$(uname -s)" +ARCH="$(uname -m)" + +# Map to FVM naming +case "$OS" in +Linux*) OS='linux' ;; +Darwin*) OS='macos' ;; +*) + log_message "Unsupported OS" + exit 1 + ;; +esac + +case "$ARCH" in +x86_64) ARCH='x64' ;; +arm64) ARCH='arm64' ;; +armv7l) ARCH='arm' ;; +*) + log_message "Unsupported architecture" + exit 1 + ;; +esac + +# Terminal colors setup +Color_Off='\033[0m' # Reset +Green='\033[0;32m' # Green +Red='\033[0;31m' +Bold_White='\033[1m' # Bold White + +success() { + log_message "${Green}$1${Color_Off}" +} + +info_bold() { + log_message "${Bold_White}$1${Color_Off}" +} + +error() { + log_message "${Red}error: $1${Color_Off}" >&2 + exit 1 +} + +# Log detected OS and architecture +log_message "Detected OS: $OS" +log_message "Detected Architecture: $ARCH" + +# Check for curl +if ! command -v curl &>/dev/null; then + error "curl is required but not installed." +fi + +# Get installed FVM version if exists +INSTALLED_FVM_VERSION="" +if command -v fvm &>/dev/null; then + INSTALLED_FVM_VERSION=$(fvm --version 2>&1) || error "Failed to fetch installed FVM version." +fi + +# Define the URL of the FVM binary +if [ -z "$1" ]; then + FVM_VERSION=$(curl -s https://api.github.com/repos/leoafarias/fvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + if [ -z "$FVM_VERSION" ]; then + error "Failed to fetch latest FVM version." + fi +else + FVM_VERSION="$1" +fi + +log_message "Installing FVM version $FVM_VERSION." + +# Setup installation directory and symlink +FVM_DIR="$HOME/.fvm_flutter" +FVM_DIR_BIN="$FVM_DIR/bin" + +# Create FVM directory if it doesn't exist +mkdir -p "$FVM_DIR" || error "Failed to create FVM directory: $FVM_DIR." + +# Check if FVM_DIR exists, and if it does delete it +if [ -d "$FVM_DIR_BIN" ]; then + log_message "FVM bin directory already exists. Removing it." + if ! rm -rf "$FVM_DIR_BIN"; then + error "Failed to remove existing FVM directory: $FVM_DIR_BIN." + fi +fi + +# Download FVM +URL="https://github.com/leoafarias/fvm/releases/download/$FVM_VERSION/fvm-$FVM_VERSION-$OS-$ARCH.tar.gz" +if ! curl -L "$URL" -o fvm.tar.gz; then + error "Download failed. Check your internet connection and URL: $URL" +fi + +# Extract binary to the new location +if ! tar xzf fvm.tar.gz -C "$FVM_DIR" 2>&1; then + error "Extraction failed. Check permissions and tar.gz file integrity." +fi + +# Cleanup +if ! rm -f fvm.tar.gz; then + error "Failed to cleanup" +fi + +# Rename FVM_DIR/fvm to FVM_DIR/bin +if ! mv "$FVM_DIR/fvm" "$FVM_DIR_BIN"; then + error "Failed to move fvm to bin directory." +fi + +echo +success "FVM updated to $FVM_VERSION successfully" +echo