Skip to content

Commit

Permalink
Support for mamba init fish
Browse files Browse the repository at this point in the history
  • Loading branch information
dlukes committed Oct 11, 2022
1 parent e502ae6 commit ca79d04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
36 changes: 23 additions & 13 deletions mamba/mamba/mamba_shell_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
SHELL_TEMPLATES = join(HERE, "shell_templates")

MAMBA_SNIPPET_SH = """
if [ -f "{mamba_sh_path}" ]; then
. "{mamba_sh_path}"
if [ -f "{mamba_source_path}" ]; then
. "{mamba_source_path}"
fi
"""
MAMBA_SNIPPET_FISH = """
if test -f "{mamba_source_path}"
source "{mamba_source_path}"
end
"""


def add_mamba_to_rcfile(file, conda_prefix):
Expand All @@ -30,14 +35,21 @@ def check_init_block(lines, start_i, prefix):
with open(file, "r") as fi:
current_content = fi.readlines()

mamba_sh_path = native_path_to_unix(
join(conda_prefix, "etc", "profile.d", "mamba.sh")
)
if file.endswith(".fish"):
snippet = MAMBA_SNIPPET_FISH
mamba_source_path = native_path_to_unix(
join(conda_prefix, "etc", "fish", "conf.d", "mamba.fish")
)
else:
snippet = MAMBA_SNIPPET_SH
mamba_source_path = native_path_to_unix(
join(conda_prefix, "etc", "profile.d", "mamba.sh")
)
new_content = []
for i, line in enumerate(current_content):
if line.startswith("# <<< conda initialize <<<"):
if check_init_block(current_content, i, native_path_to_unix(conda_prefix)):
new_content.append(MAMBA_SNIPPET_SH.format(mamba_sh_path=mamba_sh_path))
new_content.append(snippet.format(mamba_source_path=mamba_source_path))
new_content.append(line)

with open(file, "w") as fo:
Expand Down Expand Up @@ -96,13 +108,11 @@ def shell_init(args):
args.reverse,
)
for el in plan:
if el.get("function") == "init_sh_user":
args = el.get("kwargs")
target_path = args["target_path"]
conda_prefix = args["conda_prefix"]
add_mamba_to_rcfile(target_path, conda_prefix)
changed = True
if el.get("function") == "init_sh_system":
if el.get("function") in (
"init_sh_user",
"init_sh_system",
"init_fish_user",
):
args = el.get("kwargs")
target_path = args["target_path"]
conda_prefix = args["conda_prefix"]
Expand Down
19 changes: 19 additions & 0 deletions mamba/mamba/shell_templates/mamba.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set -gx MAMBA_EXE (dirname $CONDA_EXE)/mamba

function mamba --inherit-variable CONDA_EXE --inherit-variable MAMBA_EXE
if test (count $argv) -lt 1 || contains -- --help $argv
$MAMBA_EXE $argv
else
set -l cmd $argv[1]
set -e argv[1]
switch $cmd
case activate deactivate
eval ($CONDA_EXE shell -s fish $cmd $argv)
case install update upgrade remove uninstall
$MAMBA_EXE $cmd $argv || return $status
and eval ($CONDA_EXE shell -s fish reactivate)
case '*'
$MAMBA_EXE $cmd $argv
end
end
end
1 change: 1 addition & 0 deletions mamba/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

data_files = [
("etc/profile.d", ["mamba/shell_templates/mamba.sh"]),
("etc/fish/conf.d", ["mamba/shell_templates/mamba.fish"]),
]
if sys.platform == "win32":
data_files.append(
Expand Down

0 comments on commit ca79d04

Please sign in to comment.