diff --git a/installer/client/cli/utils.py b/installer/client/cli/utils.py index c78ef46..4cc2771 100644 --- a/installer/client/cli/utils.py +++ b/installer/client/cli/utils.py @@ -355,13 +355,9 @@ class Alias: self.config_files = [] home_directory = os.path.expanduser("~") self.patterns = os.path.join(home_directory, ".config/fabric/patterns") - if os.path.exists(os.path.join(home_directory, ".bashrc")): - self.config_files.append(os.path.join(home_directory, ".bashrc")) - if os.path.exists(os.path.join(home_directory, ".zshrc")): - self.config_files.append(os.path.join(home_directory, ".zshrc")) - if os.path.exists(os.path.join(home_directory, ".bash_profile")): + if os.path.exists(os.path.join(home_directory, ".config/fabric/fabric-bootstrap.inc")): self.config_files.append(os.path.join( - home_directory, ".bash_profile")) + home_directory, ".config/fabric/fabric-bootstrap.inc")) self.remove_all_patterns() self.add_patterns() print('Aliases added successfully. Please restart your terminal to use them.') @@ -584,10 +580,9 @@ class Setup: user_home = os.path.expanduser("~") sh_config = None # Check for shell configuration files - if os.path.exists(os.path.join(user_home, ".bashrc")): - sh_config = os.path.join(user_home, ".bashrc") - elif os.path.exists(os.path.join(user_home, ".zshrc")): - sh_config = os.path.join(user_home, ".zshrc") + if os.path.exists(os.path.join(user_home, ".config/fabric/fabric-bootstrap.inc")): + sh_config = os.path.join( + user_home, ".config/fabric/fabric-bootstrap.inc") else: print("No environment file found.") if sh_config: @@ -628,10 +623,9 @@ class Setup: user_home = os.path.expanduser("~") sh_config = None # Check for shell configuration files - if os.path.exists(os.path.join(user_home, ".bashrc")): - sh_config = os.path.join(user_home, ".bashrc") - elif os.path.exists(os.path.join(user_home, ".zshrc")): - sh_config = os.path.join(user_home, ".zshrc") + if os.path.exists(os.path.join(user_home, ".config/fabric/fabric-bootstrap.inc")): + sh_config = os.path.join( + user_home, ".config/fabric/fabric-bootstrap.inc") if sh_config: with open(sh_config, "r") as f: diff --git a/setup.sh b/setup.sh index a5af045..4b51e7b 100755 --- a/setup.sh +++ b/setup.sh @@ -13,59 +13,55 @@ poetry install # List of commands to check and add or update alias for # Add 'yt' and 'ts' to the list of commands -commands=("fabric" "fabric-api" "fabric-webui" "ts", "yt") +commands=("fabric" "fabric-api" "fabric-webui" "ts" "yt") + +# Path to the bootstrap file +bootstrap_file="$HOME/.config/fabric/fabric-bootstrap.inc" + +# Ensure the directory for the bootstrap file exists +mkdir -p "$(dirname "$bootstrap_file")" + +# Start the bootstrap file with a shebang if it doesn't already exist +if [ ! -f "$bootstrap_file" ]; then + echo "#!/bin/bash" > "$bootstrap_file" +fi # List of shell configuration files to update config_files=("$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile") -# Initialize an array to hold the paths of the sourced files -source_commands=() - for config_file in "${config_files[@]}"; do # Check if the configuration file exists if [ -f "$config_file" ]; then - echo "Updating $config_file" - for cmd in "${commands[@]}"; do - # Get the path of the command - CMD_PATH=$(poetry run which $cmd 2>/dev/null) + echo "Checking $config_file" + + # Ensure the bootstrap script is sourced from the shell configuration file + source_line="if [ -f \"$bootstrap_file\" ]; then . \"$bootstrap_file\"; fi" + if ! grep -qF -- "$source_line" "$config_file"; then + echo -e "\n# Load custom aliases\n$source_line" >> "$config_file" + echo "Added source command for $bootstrap_file in $config_file." + fi + sed -i '/alias fabric=/d' "$config_file" + sed -i '/fabric --pattern/d' "$config_file" - # Check if CMD_PATH is empty - if [ -z "$CMD_PATH" ]; then - echo "Command $cmd not found in the current Poetry environment." - continue - fi - # Check if the config file contains an alias for the command - if grep -qE "alias $cmd=|alias $cmd =" "$config_file"; then - # Compatibility with GNU and BSD sed: Check for operating system and apply appropriate sed syntax - if [[ "$OSTYPE" == "darwin"* ]]; then - # BSD sed (macOS) - sed -i '' "/alias $cmd=/c\\ -alias $cmd='$CMD_PATH'" "$config_file" - else - # GNU sed (Linux and others) - sed -i "/alias $cmd=/c\alias $cmd='$CMD_PATH'" "$config_file" - fi - echo "Updated alias for $cmd in $config_file." - else - # If not, add the alias to the config file - echo -e "\nalias $cmd='$CMD_PATH'" >>"$config_file" - echo "Added alias for $cmd to $config_file." - fi - done - # Add to source_commands array - source_commands+=("$config_file") else echo "$config_file does not exist." fi done -# Provide instruction to source the updated files -if [ ${#source_commands[@]} -ne 0 ]; then - echo "To apply the changes, please run the following command(s) in your terminal:" - for file in "${source_commands[@]}"; do - echo "source $file" - done -else - echo "No configuration files were updated. No need to source." -fi +# Add aliases to the bootstrap file +for cmd in "${commands[@]}"; do + CMD_PATH=$(poetry run which $cmd 2>/dev/null) + if [ -z "$CMD_PATH" ]; then + echo "Command $cmd not found in the current Poetry environment." + continue + fi + + # Check if the alias already exists in the bootstrap file + if ! grep -qF "alias $cmd=" "$bootstrap_file"; then + echo "alias $cmd='$CMD_PATH'" >> "$bootstrap_file" + echo "Added alias for $cmd to $bootstrap_file." + fi +done + +echo "Setup completed. Please restart your terminal or source your configuration files to apply changes."