Skip to content

Commit

Permalink
feat: automatically add ~/.fvm_flutter/bin to the PATH var in `inst…
Browse files Browse the repository at this point in the history
…all.sh` and introduce `update.sh` (#700)
  • Loading branch information
Nidal-Bakir authored Oct 1, 2024
1 parent 9a4a661 commit 11c998a
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 55 deletions.
197 changes: 157 additions & 40 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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."
Expand All @@ -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"
31 changes: 16 additions & 15 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -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..."
Expand All @@ -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
Expand Down
Loading

0 comments on commit 11c998a

Please sign in to comment.