Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 421 lines (342 sloc) 9.92 KB
#!/bin/sh
# shellcheck disable=SC1090,SC1091,SC2039
# SC2236: zsh doesn't handle `-n` flag in place of `! -z` correctly in POSIX
# mode when using `[` instead of `[[`.
# Activate koopa.
# Updated 2019-09-18.
# Dropped support for ksh because it doesn't support `local` variables scoped
# inside of functions.
# Pre-flight checks {{{1
# ==============================================================================
# Never activate for specific admin accounts.
[ "$(id -u)" -eq 0 ] && return 0
whoami | grep -Eq "\b(admin)\b" && return 0
# Check for interactive shell.
echo "$-" | grep -q "i" && interactive=1 || interactive=0
# Detect if koopa is already active and being requested inside a subshell.
if [ -n "${KOOPA_HOME:-}" ]
then
subshell=1
else
subshell=0
fi
# Detect force mode.
# Disabled by default.
[ -z "${force:-}" ] && force=0
case "$force" in
1|true)
force=1
;;
*)
force=0
;;
esac
# Skip activation for non-interactive sessions.
[ "$force" -eq 0 ] && [ "$interactive" -eq 0 ] && return 0
# Enable test mode {{{1
# ==============================================================================
[ -z "${test:-}" ] && test=0
[ -n "${KOOPA_TEST:-}" ] && test=1
case "$test" in
1|true)
test=1
;;
*)
test=0
;;
esac
if [ "$test" -eq 1 ]
then
printf "Test mode enabled.\n"
printf "Setting strict shell mode.\n"
set -o errexit
set -o nounset
if [ -n "${BASH_VERSION:-}" ]
then
set -o errtrace
set -o pipefail
set -o posix
set -o xtrace
fi
force=1
export KOOPA_TEST=1
fi
# Functions {{{1
# ==============================================================================
# Fake realpath support, if necessary.
# Note that `readlink -f` doesn't work on macOS.
#
# See also:
# - https://github.com/bcbio/bcbio-nextgen/blob/master/tests/run_tests.sh
#
# Updated 2019-06-26.
if ! command -v realpath >/dev/null
then
realpath() {
if [ "$(uname -s)" = "Darwin" ]
then
perl -MCwd -e 'print Cwd::abs_path shift' "$1"
else
readlink -f "$@"
fi
}
fi
# Usage {{{1
# ==============================================================================
usage() {
cat << EOF
usage: activate [-h]
Activate koopa.
supported shells: bash, zsh
optional arguments:
-h, --help
Show this help message and exit.
optional variables:
extra=1 [true]
Enable extra shell and OS-specific configuration.
minimal=0 [false]
Minimal mode. Simply export koopa programs into PATH.
Skip additional program and shell configuration.
Internally, this sets extra=0, programs=1.
programs=1 [true]
Enable automatic configuration of supported programs.
optional variables, for debugging:
force=0 [false]
Force reactivation, even if koopa is already active.
test=0 [false]
Enable verbose test mode. Used for Travis CI checks.
This will also automatically set 'force=1'.
EOF
}
# Parse arguments {{{1
# ==============================================================================
for i in "$@"
do
case "$i" in
-h|--help)
usage
exit
;;
*)
>&2 printf "Error: Invalid argument '%s'\n" "$1"
exit 1
;;
esac
done
unset -f usage
# Locate koopa home {{{1
# ==============================================================================
if [ -n "${BASH_VERSION:-}" ]
then
KOOPA_SHELL="bash"
# shellcheck disable=SC2039
# SC2039: In POSIX sh, array references are undefined.
file="${BASH_SOURCE[0]}"
elif [ -n "${ZSH_VERSION:-}" ]
then
KOOPA_SHELL="zsh"
# This covers both "_src_etc_profile" and "_src_etc_profile_d".
case "$0" in
_src_etc_profile*)
ZSHRC=0
;;
*)
ZSHRC=1
;;
esac
export ZSHRC
# Note that sourcing in `/etc/profile` doesn't return script path in `$0`,
# which is commonly recommended online in place of `$BASH_SOURCE`. `$0` in
# this case instead returns `_src_etc_profile`.
# https://stackoverflow.com/a/23259585/3911732
file="${(%):-%N}"
else
>&2 printf "Error: Failed to detect supported shell.\n"
>&2 printf "Supported: bash, zsh.\n\n"
>&2 printf " SHELL: %s\n" "$SHELL"
>&2 printf " 0: %s\n" "$0"
>&2 printf " -: %s\n" "$-"
return 1
fi
export KOOPA_SHELL
if [ ! -f "$file" ]
then
>&2 printf "Error: Failed to detect absolute path of activation script.\n"
return 1
fi
# Note that running realpath on the file instead of the directory will properly
# resolve `~/.config/koopa/activate` in Mike's default `~/.shrc` config file.
KOOPA_HOME="$(dirname "$(realpath "$file")")"
export KOOPA_HOME
# Load shared functions.
. "${KOOPA_HOME}/system/include/functions.sh"
# Get location of activation scripts.
activate_dir="${KOOPA_HOME}/system/activate"
# Subshell handling {{{1
# ==============================================================================
# Force subshell reload inside:
# - Interactive HPC slurm job.
# - Emacs terminal.
# - Tmux session.
# - Vim terminal.
if [ "$interactive" -eq 1 ]
then
if [ "${KOOPA_SHELL:-}" = "zsh" ] ||
[ -n "${SLURM_JOB_ID:-}" ] || \
[ "${TERM:-}" = "eterm-color" ] || \
[ -n "${TMUX:-}" ] || \
[ -n "${VIM_TERMINAL:-}" ]
then
force=1
fi
fi
# Early return if koopa is already active.
[ "$force" -eq 0 ] && [ "$subshell" -eq 1 ] && return 0
# Optional configuration {{{1
# ==============================================================================
# Detect extra mode.
# Enabled by default.
[ -z "${extra:-}" ] && extra=1
case "$extra" in
1|true)
extra=1
;;
*)
extra=0
;;
esac
# Detect programs mode.
# Enabled by default.
[ -z "${programs:-}" ] && programs=1
case "$programs" in
1|true)
programs=1
;;
*)
programs=0
;;
esac
# Detect minimal mode.
# Disabled by default.
[ -z "${minimal:-}" ] && minimal=0
case "$minimal" in
1|true)
minimal=1
;;
*)
minimal=0
;;
esac
# Disable other options when running in minimal mode.
if [ "$minimal" -eq 1 ]
then
extra=0
programs=0
fi
# Basic shell configuration {{{1
# ==============================================================================
if [ "$subshell" -eq 0 ]
then
. "${activate_dir}/preflight.sh"
. "${activate_dir}/xdg.sh"
. "${activate_dir}/path.sh"
_koopa_update_xdg_config
fi
. "${activate_dir}/general.sh"
# Program configuration {{{1
# ==============================================================================
if [ "$programs" -eq 1 ]
then
programs_dir="${activate_dir}/programs"
if [ "$subshell" -eq 0 ]
then
. "${programs_dir}/rust.sh"
. "${programs_dir}/aspera.sh"
. "${programs_dir}/ensembl-perl-api.sh"
. "${programs_dir}/bcbio.sh"
fi
. "${programs_dir}/perlbrew.sh"
. "${programs_dir}/rbenv.sh"
. "${programs_dir}/conda.sh"
. "${programs_dir}/virtualenv.sh"
unset -v programs programs_dir
fi
# Extra shell configuration {{{1
# ==============================================================================
if [ "$extra" -eq 1 ]
then
# Shell-specific configuration.
if [ "$KOOPA_SHELL" = "bash" ]
then
bash_dir="${activate_dir}/shell/bash"
. "${bash_dir}/shopt.sh"
. "${bash_dir}/prompt.sh"
unset -v bash_dir
elif [ "$KOOPA_SHELL" = "zsh" ]
then
zsh_dir="${activate_dir}/shell/zsh"
if [ "$subshell" -eq 0 ]
then
. "${zsh_dir}/fpath.sh"
fi
if [ "$ZSHRC" -eq 1 ]
then
. "${zsh_dir}/oh-my-zsh.sh"
. "${zsh_dir}/autojump.sh"
fi
. "${zsh_dir}/prompt.sh"
. "${zsh_dir}/setopt.sh"
. "${zsh_dir}/bindkey.sh"
unset -v zsh_dir
fi
# Run these after shell-specific configuration.
. "${activate_dir}/set.sh"
. "${activate_dir}/aliases.sh"
. "${activate_dir}/secrets.sh"
. "${activate_dir}/ssh-key.sh"
. "${activate_dir}/umask.sh"
# Platform-specific configuration.
if _koopa_is_darwin
then
darwin_dir="${activate_dir}/os/darwin"
if [ "$subshell" -eq 0 ]
then
. "${darwin_dir}/exports.sh"
. "${darwin_dir}/programs/homebrew.sh"
fi
. "${darwin_dir}/aliases.sh"
. "${darwin_dir}/grc-colors.sh"
unset -v darwin_dir
fi
# Today bucket.
if [ "$subshell" -eq 0 ]
then
_koopa_today_bucket
fi
fi
# Post-flight checks and clean-up {{{1
# ==============================================================================
_koopa_disk_check
unset -v activate_dir extra file force interactive minimal subshell
if [ "$KOOPA_SHELL" = "zsh" ]
then
unset -v ZSHRC
fi
# Checking declared values in Bash:
# - `declare -F`: Display function names.
# - `declare -p`: Display the attributes and value of each name.
# - `declare -x`: Display exported values.
# Disable test mode.
if [ "$test" -eq 1 ]
then
set +o errexit
set +o nounset
if [ -n "${BASH_VERSION:-}" ]
then
set -o errtrace
set -o posix
set -o pipefail
fi
printf "Activation was successful.\n"
fi
unset -v test
You can’t perform that action at this time.