Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(installer): add additional shell config #555

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 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,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
#
Expand All @@ -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
Expand All @@ -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 ""
Expand Down
16 changes: 12 additions & 4 deletions cargo-dist/tests/gallery/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down
63 changes: 56 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,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
#
Expand All @@ -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
Expand All @@ -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 ""
Expand Down
63 changes: 56 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,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
#
Expand All @@ -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
Expand All @@ -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 ""
Expand Down
Loading
Loading