From 65a02b9a78065f829e67d32b60131ffcee06ce93 Mon Sep 17 00:00:00 2001 From: David Lukes Date: Tue, 11 Oct 2022 13:25:56 +0200 Subject: [PATCH] Support for mamba init fish --- mamba/mamba/mamba_shell_init.py | 36 ++++++++++++++++---------- mamba/mamba/shell_templates/mamba.fish | 24 +++++++++++++++++ mamba/setup.py | 1 + 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 mamba/mamba/shell_templates/mamba.fish diff --git a/mamba/mamba/mamba_shell_init.py b/mamba/mamba/mamba_shell_init.py index d97611e3ba..965ce69c4d 100644 --- a/mamba/mamba/mamba_shell_init.py +++ b/mamba/mamba/mamba_shell_init.py @@ -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): @@ -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: @@ -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"] diff --git a/mamba/mamba/shell_templates/mamba.fish b/mamba/mamba/shell_templates/mamba.fish new file mode 100644 index 0000000000..9c62706faa --- /dev/null +++ b/mamba/mamba/shell_templates/mamba.fish @@ -0,0 +1,24 @@ +# Based on conda.fish which is licensed as below +# +# Copyright (C) 2012 Anaconda, Inc +# SPDX-License-Identifier: BSD-3-Clause + +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 diff --git a/mamba/setup.py b/mamba/setup.py index 793f75e8d3..263acefbb4 100644 --- a/mamba/setup.py +++ b/mamba/setup.py @@ -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(