From a21430a7a540442a0aac28df521365f4ab7513d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Misty=20De=20M=C3=A9o?= Date: Tue, 7 Nov 2023 16:07:36 -0800 Subject: [PATCH] feat(installer): add additional shell config ~/.profile isn't sourced in many circumstances; we need to write this to a large number of locations to make sure it gets considered by bash and zsh under the largest number of circumstances. We have three sets of configuration files, covering generic POSIX, bash, and zsh. We pick the first file that exists in each set, and if none exists, we create the first in the list. Fixes #547. --- .../templates/installer/installer.sh.j2 | 36 +++++++++++++++---- .../tests/snapshots/akaikatana_basic.snap | 36 +++++++++++++++---- .../tests/snapshots/akaikatana_musl.snap | 36 +++++++++++++++---- .../akaikatana_repo_with_dot_git.snap | 36 +++++++++++++++---- .../tests/snapshots/axolotlsay_abyss.snap | 36 +++++++++++++++---- .../snapshots/axolotlsay_abyss_only.snap | 36 +++++++++++++++---- .../tests/snapshots/axolotlsay_basic.snap | 36 +++++++++++++++---- .../snapshots/axolotlsay_edit_existing.snap | 36 +++++++++++++++---- .../tests/snapshots/axolotlsay_musl.snap | 36 +++++++++++++++---- .../snapshots/axolotlsay_musl_no_gnu.snap | 36 +++++++++++++++---- .../axolotlsay_no_homebrew_publish.snap | 36 +++++++++++++++---- .../axolotlsay_ssldotcom_windows_sign.snap | 36 +++++++++++++++---- ...xolotlsay_ssldotcom_windows_sign_prod.snap | 36 +++++++++++++++---- .../axolotlsay_user_publish_job.snap | 36 +++++++++++++++---- .../snapshots/install_path_cargo_home.snap | 36 +++++++++++++++---- .../snapshots/install_path_env_no_subdir.snap | 36 +++++++++++++++---- .../snapshots/install_path_env_subdir.snap | 36 +++++++++++++++---- .../install_path_env_subdir_space.snap | 36 +++++++++++++++---- .../install_path_env_subdir_space_deeper.snap | 36 +++++++++++++++---- .../install_path_home_subdir_deeper.snap | 36 +++++++++++++++---- .../install_path_home_subdir_min.snap | 36 +++++++++++++++---- .../install_path_home_subdir_space.snap | 36 +++++++++++++++---- ...install_path_home_subdir_space_deeper.snap | 36 +++++++++++++++---- 23 files changed, 667 insertions(+), 161 deletions(-) diff --git a/cargo-dist/templates/installer/installer.sh.j2 b/cargo-dist/templates/installer/installer.sh.j2 index d60b4c41f..eef00b9b5 100644 --- a/cargo-dist/templates/installer/installer.sh.j2 +++ b/cargo-dist/templates/installer/installer.sh.j2 @@ -48,7 +48,7 @@ then unpacks the binaries and installs them to {% if install_path.kind == "Cargo {{ error("unimplemented install_path format: " ~ install_path.kind) }} {%- endif %} -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: {{ app_name }}-installer.sh [OPTIONS] @@ -307,7 +307,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -322,8 +324,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -349,14 +371,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/akaikatana_basic.snap b/cargo-dist/tests/snapshots/akaikatana_basic.snap index d5ac78439..ee8c3ef21 100644 --- a/cargo-dist/tests/snapshots/akaikatana_basic.snap +++ b/cargo-dist/tests/snapshots/akaikatana_basic.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/mistydemeo/akaikatana-repack/releases/download/v0.2.0 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: akaikatana-repack-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/akaikatana_musl.snap b/cargo-dist/tests/snapshots/akaikatana_musl.snap index 4154ddb2d..58f921acb 100644 --- a/cargo-dist/tests/snapshots/akaikatana_musl.snap +++ b/cargo-dist/tests/snapshots/akaikatana_musl.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/mistydemeo/akaikatana-repack/releases/download/v0.2.0 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: akaikatana-repack-installer.sh [OPTIONS] @@ -300,7 +300,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -315,8 +317,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -342,14 +364,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap b/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap index d5ac78439..ee8c3ef21 100644 --- a/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap +++ b/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/mistydemeo/akaikatana-repack/releases/download/v0.2.0 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: akaikatana-repack-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_abyss.snap b/cargo-dist/tests/snapshots/axolotlsay_abyss.snap index 0b2cdd200..4ffbd053c 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_abyss.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_abyss.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://fake.axo.dev/faker/axolotlsay/fake-id-do-not-upload then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap b/cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap index aae2cc50a..36cefaca7 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://fake.axo.dev/faker/axolotlsay/fake-id-do-not-upload then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_basic.snap b/cargo-dist/tests/snapshots/axolotlsay_basic.snap index 1a270b6ec..0e28cdc51 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_basic.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_basic.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap b/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap index 55e633a44..92582ede9 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_musl.snap b/cargo-dist/tests/snapshots/axolotlsay_musl.snap index ce09f627d..14c0bff90 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_musl.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_musl.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -300,7 +300,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -315,8 +317,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -342,14 +364,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_musl_no_gnu.snap b/cargo-dist/tests/snapshots/axolotlsay_musl_no_gnu.snap index 202cb06d8..ed3ec0b5c 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_musl_no_gnu.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_musl_no_gnu.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -300,7 +300,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -315,8 +317,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -342,14 +364,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_no_homebrew_publish.snap b/cargo-dist/tests/snapshots/axolotlsay_no_homebrew_publish.snap index deeb45f91..b7daa07b1 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_no_homebrew_publish.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_no_homebrew_publish.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign.snap b/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign.snap index 4aa6075ff..3d364e1a4 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign_prod.snap b/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign_prod.snap index 3ee6c8bca..2cbaa8869 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign_prod.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_ssldotcom_windows_sign_prod.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap index 04e2cc7f1..89472e1bc 100644 --- a/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap +++ b/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_cargo_home.snap b/cargo-dist/tests/snapshots/install_path_cargo_home.snap index d2eb25d35..a714527b1 100644 --- a/cargo-dist/tests/snapshots/install_path_cargo_home.snap +++ b/cargo-dist/tests/snapshots/install_path_cargo_home.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$CARGO_HOME/bin (\$HOME/.cargo/bin) -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -290,7 +290,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -305,8 +307,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -332,14 +354,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap b/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap index 0e202d6ab..b5633fdd2 100644 --- a/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap +++ b/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$MY_ENV_VAR/ -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir.snap b/cargo-dist/tests/snapshots/install_path_env_subdir.snap index 396eb5e27..d25d05c71 100644 --- a/cargo-dist/tests/snapshots/install_path_env_subdir.snap +++ b/cargo-dist/tests/snapshots/install_path_env_subdir.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$MY_ENV_VAR/bin -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap index 016e04107..e0c9f6e2f 100644 --- a/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap +++ b/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$MY_ENV_VAR/My Axolotlsay Documents -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap index 9d640a28c..c9d302320 100644 --- a/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap +++ b/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$MY_ENV_VAR/My Axolotlsay Documents/bin -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap index 979d9d985..49b0acb82 100644 --- a/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap +++ b/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$HOME/.axolotlsay/bins -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap index 1014a1e74..7fc3b0bdc 100644 --- a/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap +++ b/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$HOME/.axolotlsay -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap index 1e9ab67dc..e4ce3be8c 100644 --- a/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap +++ b/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$HOME/My Axolotlsay Documents -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say "" diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap index 15852afb4..403d75027 100644 --- a/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap +++ b/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap @@ -45,7 +45,7 @@ This script detects what platform you're on and fetches an appropriate archive f https://github.com/axodotdev/axolotlsay/releases/download/v0.2.1 then unpacks the binaries and installs them to \$HOME/My Axolotlsay Documents/bin -It will then add that dir to PATH by adding the appropriate line to \$HOME/.profile +It will then add that dir to PATH by adding the appropriate line to your shell profiles. USAGE: axolotlsay-installer.sh [OPTIONS] @@ -273,7 +273,9 @@ install() { say "everything's installed!" if [ "0" = "$NO_MODIFY_PATH" ]; then - add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".profile" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".bash_profile .bash_login .bashrc" + add_install_dir_to_path "$_install_dir_expr" "$_env_script_path" "$_env_script_path_expr" ".zshrc .zshenv" fi } @@ -288,8 +290,28 @@ add_install_dir_to_path() { local _install_dir_expr="$1" local _env_script_path="$2" local _env_script_path_expr="$3" + local _rcfiles="$4" + if [ -n "${HOME:-}" ]; then - local _rcfile="$HOME/.profile" + local _target + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + local _rcfile="$HOME/$_rcfile_relative" + + if [ -f "$_rcfile" ]; then + _target="$_rcfile" + break + fi + done + + # If we didn't find anything, pick the first entry in the + # list as the default to create and write to + if [ -n "$_target" ]; then + _target="$(echo "$_rcfiles" | awk '{ print $1 }')" + fi + # `source x` is an alias for `. x`, and the latter is more portable/actually-posix. # This apparently comes up a lot on freebsd. It's easy enough to always add # the more robust line to rcfiles, but when telling the user to apply the change @@ -315,14 +337,14 @@ add_install_dir_to_path() { # (on error we want to create the file, which >> conveniently does) # # We search for both kinds of line here just to do the right thing in more cases. - if ! grep -F "$_robust_line" "$_rcfile" > /dev/null 2>/dev/null && \ - ! grep -F "$_pretty_line" "$_rcfile" > /dev/null 2>/dev/null + if ! grep -F "$_robust_line" "$_target" > /dev/null 2>/dev/null && \ + ! grep -F "$_pretty_line" "$_target" > /dev/null 2>/dev/null then # If the script now exists, add the line to source it to the rcfile # (This will also create the rcfile if it doesn't exist) if [ -f "$_env_script_path" ]; then - say_verbose "adding $_robust_line to $_rcfile" - ensure echo "$_robust_line" >> "$_rcfile" + say_verbose "adding $_robust_line to $_target" + ensure echo "$_robust_line" >> "$_target" say "" say "To add $_install_dir_expr to your PATH, either restart your shell or run:" say ""