Skip to content

Commit

Permalink
Allow mamba name in sh shells
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Mar 11, 2024
1 parent 18ea170 commit 2d3cbac
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ find_package(Python3 COMPONENTS Interpreter)

set(
SHELL_SCRIPTS
micromamba.sh
mamba.sh
micromamba.csh
micromamba.bat
activate.bat
Expand Down
37 changes: 22 additions & 15 deletions libmamba/data/micromamba.sh → libmamba/data/mamba.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: BSD-3-Clause

__mamba_exe() (
"$MAMBA_EXE" "${@}"
"${MAMBA_EXE}" "${@}"
)

__mamba_hashr() {
Expand All @@ -16,13 +16,13 @@ __mamba_hashr() {
}

__mamba_xctivate() {
\local ask_conda
ask_conda="$(PS1="${PS1:-}" __mamba_exe shell "${@}" --shell bash)" || \return
\eval "${ask_conda}"
\local ask_mamba
ask_mamba="$(PS1="${PS1:-}" __mamba_exe shell "${@}" --shell bash)" || \return
\eval "${ask_mamba}"
__mamba_hashr
}

micromamba() {
__mamba_wrap() {
\local cmd="${1-__missing__}"
case "${cmd}" in
activate|reactivate|deactivate)
Expand All @@ -36,8 +36,8 @@ micromamba() {
__mamba_exe "${@}" || \return

# remove leftover backup file on Windows
if [ -f "$MAMBA_EXE.bkup" ]; then
rm -f "$MAMBA_EXE.bkup"
if [ -f "${MAMBA_EXE}.bkup" ]; then
rm -f "${MAMBA_EXE}.bkup"
fi
;;
*)
Expand All @@ -46,16 +46,23 @@ micromamba() {
esac
}


# We need to define a function with the same name as the executable to be called by the user.
# There is no way to register it dynamically without relying on hacks or eval.
__exe_name="$(basename "${MAMBA_EXE}")"
__exe_name="${__exe_name%.*}"
if [[ "${__exe_name}" == "micromamba" ]]; then
micromamba() { __mamba_wrap "${@}"; }
elif [[ "${__exe_name}" == "mamba" ]]; then
mamba() { __mamba_wrap "${@}"; }
else
echo "Error unknow MAMBA_EXE: \"${MAMBA_EXE}\", filename must be mamba or micromamba" 1>&2
fi


if [ -z "${CONDA_SHLVL+x}" ]; then
\export CONDA_SHLVL=0
# In dev-mode MAMBA_EXE is python.exe and on Windows
# it is in a different relative location to condabin.
if [ -n "${_CE_CONDA+x}" ] && [ -n "${WINDIR+x}" ]; then
PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}"
else
PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}"
fi
\export PATH
\export PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}"

# We're not allowing PS1 to be unbound. It must at least be set.
# However, we're not exporting it, which can cause problems when starting a second shell
Expand Down
2 changes: 1 addition & 1 deletion libmamba/include/mamba/core/shell_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "mamba/fs/filesystem.hpp"

extern const char data_micromamba_sh[];
extern const char data_mamba_sh[];
extern const char data_micromamba_csh[];
extern const char data_micromamba_bat[];
extern const char data_activate_bat[];
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ namespace mamba

fs::u8path PosixActivator::hook_source_path()
{
return m_context.prefix_params.root_prefix / "etc" / "profile.d" / "micromamba.sh";
return m_context.prefix_params.root_prefix / "etc" / "profile.d" / "mamba.sh";
}

/*********************************
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/core/shell_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,9 @@ namespace mamba

if (shell == "zsh" || shell == "bash" || shell == "posix")
{
std::string contents = data_micromamba_sh;
std::string contents = data_mamba_sh;
// Using /unix/like/paths on Unix shell (even on Windows)
util::replace_all(contents, "$MAMBA_EXE", util::path_to_posix(exe.string()));
util::replace_all(contents, "${MAMBA_EXE}", util::path_to_posix(exe.string()));
return contents;
}
else if (shell == "csh")
Expand Down Expand Up @@ -817,7 +817,7 @@ namespace mamba
// Maybe the prefix isn't writable. No big deal, just keep going.
}
std::ofstream sh_file = open_ofstream(sh_source_path);
sh_file << data_micromamba_sh;
sh_file << data_mamba_sh;
}
else if (shell == "csh")
{
Expand Down

0 comments on commit 2d3cbac

Please sign in to comment.