Skip to content

Commit

Permalink
feat(installer): add additional shell config
Browse files Browse the repository at this point in the history
~/.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.
  • Loading branch information
mistydemeo committed Nov 29, 2023
1 parent ceeca6f commit 13410b1
Show file tree
Hide file tree
Showing 23 changed files with 667 additions and 161 deletions.
36 changes: 29 additions & 7 deletions cargo-dist/templates/installer/installer.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand All @@ -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 [ -z "$_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
Expand All @@ -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 ""
Expand Down
36 changes: 29 additions & 7 deletions cargo-dist/tests/snapshots/akaikatana_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand All @@ -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 [ -z "$_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
Expand All @@ -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 ""
Expand Down
36 changes: 29 additions & 7 deletions cargo-dist/tests/snapshots/akaikatana_musl.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand All @@ -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 [ -z "$_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
Expand All @@ -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 ""
Expand Down
36 changes: 29 additions & 7 deletions cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand All @@ -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 [ -z "$_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
Expand All @@ -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 ""
Expand Down
36 changes: 29 additions & 7 deletions cargo-dist/tests/snapshots/axolotlsay_abyss.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand All @@ -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 [ -z "$_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
Expand All @@ -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 ""
Expand Down
Loading

0 comments on commit 13410b1

Please sign in to comment.