Skip to content

Commit

Permalink
feat: support ZDOTDIR in installer
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Dec 4, 2023
1 parent dabcf3e commit 5ed9108
Show file tree
Hide file tree
Showing 24 changed files with 530 additions and 46 deletions.
25 changes: 23 additions & 2 deletions cargo-dist/templates/installer/installer.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -332,7 +350,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -345,7 +365,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
1 change: 1 addition & 0 deletions cargo-dist/tests/gallery/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ 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")
})?;
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_musl.snap
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -325,7 +343,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -338,7 +358,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_abyss.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
25 changes: 23 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ install() {
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*)
_home="$(zsh -c 'if [ -n "$ZDOTDIR" ]; then echo $ZDOTDIR; else echo $HOME; fi')"
;;
*)
_home="$HOME"
;;
esac

echo "$_home"
}

add_install_dir_to_path() {
# Edit rcfiles ($HOME/.profile) to add install_dir to $PATH
#
Expand All @@ -315,7 +333,9 @@ add_install_dir_to_path() {
# 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"
local _home
_home=$(print_home_for_script "$_rcfile_relative")
local _rcfile="$_home/$_rcfile_relative"

if [ -f "$_rcfile" ]; then
_target="$_rcfile"
Expand All @@ -328,7 +348,8 @@ add_install_dir_to_path() {
if [ -z "${_target:-}" ]; then
local _rcfile_relative
_rcfile_relative="$(echo "$_rcfiles" | awk '{ print $1 }')"
_target="$HOME/$_rcfile_relative"
_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.
Expand Down
Loading

0 comments on commit 5ed9108

Please sign in to comment.