diff --git a/cargo-dist/templates/installer/installer.sh.j2 b/cargo-dist/templates/installer/installer.sh.j2 index d60b4c41f..ad2c344ba 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,10 +307,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -322,8 +346,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +398,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/gallery/dist.rs b/cargo-dist/tests/gallery/dist.rs index 4137833de..da76fcb12 100644 --- a/cargo-dist/tests/gallery/dist.rs +++ b/cargo-dist/tests/gallery/dist.rs @@ -370,13 +370,18 @@ impl DistResult { let app_home = tempdir.join(format!(".{app_name}")); let _output = script.output_checked(|cmd| { cmd.env("HOME", &tempdir) + .env("ZDOTDIR", &tempdir) .env("MY_ENV_VAR", &app_home) .env_remove("CARGO_HOME") })?; // we could theoretically look at the above output and parse out the `source` line... // Check that the script wrote files where we expected - let rcfile = tempdir.join(".profile"); + let rcfiles = &[ + tempdir.join(".profile"), + tempdir.join(".bash_profile"), + tempdir.join(".zshrc"), + ]; let expected_bin_dir = Utf8PathBuf::from(expected_bin_dir); let bin_dir = tempdir.join(&expected_bin_dir); let env_dir = if expected_bin_dir @@ -390,7 +395,9 @@ impl DistResult { let env_script = env_dir.join("env"); assert!(bin_dir.exists(), "bin dir wasn't created"); - assert!(rcfile.exists(), ".profile wasn't created"); + for rcfile in rcfiles { + assert!(rcfile.exists(), "{} wasn't created", rcfile); + } assert!(env_script.exists(), "env script wasn't created"); // Check that all the binaries work @@ -411,9 +418,10 @@ impl DistResult { let test_script_text = format!( r#"#!/bin/sh - . {rcfile} + . {} which {bin_name} - "# + "#, + rcfiles.first().expect("rcfiles was empty?!") ); LocalAsset::write_new(&test_script_text, &test_script_path)?; std::fs::set_permissions(&test_script_path, std::fs::Permissions::from_mode(0o755)) diff --git a/cargo-dist/tests/snapshots/akaikatana_basic.snap b/cargo-dist/tests/snapshots/akaikatana_basic.snap index d5ac78439..6307dd2c1 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..03e3a2196 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,10 +300,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -315,8 +339,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +391,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..6307dd2c1 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..ebd6b22fc 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..f288bbfbf 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..b076d6406 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..77021a50b 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..71ec86e8f 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,10 +300,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -315,8 +339,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +391,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..a919a816b 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,10 +300,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -315,8 +339,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +391,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..e08722a5f 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..0323b1b99 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..346f24335 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..4d7d62057 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..8952fa374 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,10 +290,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -305,8 +329,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +381,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..6282974af 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_env_subdir.snap b/cargo-dist/tests/snapshots/install_path_env_subdir.snap index 396eb5e27..d8eadf1de 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_env_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap index 016e04107..045679f1d 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_env_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap index 9d640a28c..738bd1337 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_home_subdir_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap index 979d9d985..634a32ad6 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_home_subdir_min.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap index 1014a1e74..a945f1468 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_home_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap index 1e9ab67dc..326e5babf 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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/install_path_home_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap index 15852afb4..9cb5b3bd0 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,10 +273,34 @@ 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 } +print_home_for_script() { + local script="$1" + + local _home + case "$script" in + # zsh has a special ZDOTDIR directory, which if set + # should be considered instead of $HOME + .zsh*) + if [ -n "$ZDOTDIR" ]; then + _home="$ZDOTDIR" + else + _home="$HOME" + fi + ;; + *) + _home="$HOME" + ;; + esac + + echo "$_home" +} + add_install_dir_to_path() { # Edit rcfiles ($HOME/.profile) to add install_dir to $PATH # @@ -288,8 +312,33 @@ 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 + local _home + + # Find the first file in the array that exists and choose + # that as our target to write to + for _rcfile_relative in $_rcfiles; do + _home="$(print_home_for_script "$_rcfile_relative")" + 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 [ -z "${_target:-}" ]; then + local _rcfile_relative + _rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')" + _home="$(print_home_for_script "$_rcfile_relative")" + _target="$_home/$_rcfile_relative" + 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 +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 ""