From d162759107934d1492cfc2bece21c8d17d73e2ac Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Wed, 2 Oct 2024 15:06:53 +0200 Subject: [PATCH] Win activate (#3489) * Fixed activation scripts for cmd.exe * Fixed get_PATH on Windows * linter --- libmamba/data/activate.bat | 12 +----------- libmamba/data/mamba_hook.bat | 16 +++++----------- libmamba/src/core/activation.cpp | 18 ++++++++++++------ libmamba/src/core/shell_init.cpp | 19 ++++++++++++++----- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/libmamba/data/activate.bat b/libmamba/data/activate.bat index eb52a9eb5f..0b63e3ee9b 100644 --- a/libmamba/data/activate.bat +++ b/libmamba/data/activate.bat @@ -2,14 +2,4 @@ @REM SPDX-License-Identifier: BSD-3-Clause @CALL "%~dp0..\condabin\mamba_hook.bat" - -@REM Replaced by mamba executable with the MAMBA_EXE variable pointing to the correct location. -__MAMBA_INSERT_MAMBA_EXE__ - -@REM We need to know the name of the executable, either mamba or micromamba -@REM Get the base filename of MAMBA_EXE -@FOR %%A in ("%MAMBA_EXE%") do set "__mamba_filename=%%~nxA" -@REM Remove .exe extension from the filename -@SET "__mamba_name=!__mamba_filename:%~x1=!" - -!__mamba_name! activate %* +__MAMBA_INSERT_EXE_NAME__ activate %* diff --git a/libmamba/data/mamba_hook.bat b/libmamba/data/mamba_hook.bat index 7a635f1a1e..0c314eca81 100644 --- a/libmamba/data/mamba_hook.bat +++ b/libmamba/data/mamba_hook.bat @@ -13,15 +13,9 @@ __MAMBA_INSERT_MAMBA_EXE__ @SET __mambabin_dir= @SET __mamba_root= -@echo off -@REM We need to define an alias with the same name as the executable to be called by the user. -@REM Get the base filename of MAMBA_EXE -@FOR %%A in ("%MAMBA_EXE%") do ( - @set "__mamba_filename=%%~nxA" - @REM Remove .exe extension from the filename - @SET "__mamba_name=!__mamba_filename:%~x1=!" - @REM Define correct alias depending on the executable name - @set "__mamba_cmd=call ""%MAMBA_BAT%"" $*" - @DOSKEY !__mamba_name!=!__mamba_cmd! -) +@REM @DOSKEY does not work with delayed evaluation +@REM @DOSKEY after the first usage of a macro whose name is defined with a variable +@REM Therefore no magic here, just grep and replace when generating the final file +@DOSKEY __MAMBA_INSERT_EXE_NAME__="%MAMBA_BAT%" $* + @SET CONDA_SHLVL=0 diff --git a/libmamba/src/core/activation.cpp b/libmamba/src/core/activation.cpp index 0e4039601f..f18485bab3 100644 --- a/libmamba/src/core/activation.cpp +++ b/libmamba/src/core/activation.cpp @@ -228,13 +228,20 @@ namespace mamba std::vector Activator::get_PATH() { std::vector path; + std::vector strings{}; + if (m_env.find("PATH") != m_env.end()) { - auto strings = util::split(m_env["PATH"], util::pathsep()); - for (auto& s : strings) - { - path.push_back(s); - } + strings = util::split(m_env["PATH"], util::pathsep()); + } + // On Windows, the variable can be Path and not PATH + else if (m_env.find("Path") != m_env.end()) + { + strings = util::split(m_env["Path"], util::pathsep()); + } + for (auto& s : strings) + { + path.push_back(s); } return path; } @@ -267,7 +274,6 @@ namespace mamba std::vector final_path = get_path_dirs(prefix); final_path.insert(final_path.end(), path_list.begin(), path_list.end()); final_path.erase(std::unique(final_path.begin(), final_path.end()), final_path.end()); - std::string result = util::join(util::pathsep(), final_path).string(); return result; } diff --git a/libmamba/src/core/shell_init.cpp b/libmamba/src/core/shell_init.cpp index bffe508eb7..d4d6744d92 100644 --- a/libmamba/src/core/shell_init.cpp +++ b/libmamba/src/core/shell_init.cpp @@ -686,6 +686,7 @@ namespace mamba void init_root_prefix_cmdexe(const Context&, const fs::u8path& root_prefix) { fs::u8path exe = get_self_exe_path(); + fs::u8path exe_name = exe.stem(); try { @@ -697,7 +698,7 @@ namespace mamba // Maybe the prefix isn't writable. No big deal, just keep going. } - std::ofstream mamba_bat_f = open_ofstream(root_prefix / "condabin" / "mamba.bat"); + // mamba.bat std::string mamba_bat_contents(data_mamba_bat); util::replace_all( mamba_bat_contents, @@ -709,14 +710,16 @@ namespace mamba std::string("__MAMBA_INSERT_MAMBA_EXE__"), "@SET \"MAMBA_EXE=" + exe.string() + "\"" ); - + std::ofstream mamba_bat_f = open_ofstream(root_prefix / "condabin" / "mamba.bat"); mamba_bat_f << mamba_bat_contents; + + // _mamba_activate.bat std::ofstream _mamba_activate_bat_f = open_ofstream( root_prefix / "condabin" / "_mamba_activate.bat" ); _mamba_activate_bat_f << data__mamba_activate_bat; - + // condabin/activate.bat std::string activate_bat_contents(data_activate_bat); util::replace_all( activate_bat_contents, @@ -728,22 +731,28 @@ namespace mamba std::string("__MAMBA_INSERT_MAMBA_EXE__"), "@SET \"MAMBA_EXE=" + exe.string() + "\"" ); - - + util::replace_all( + activate_bat_contents, + std::string("__MAMBA_INSERT_EXE_NAME__"), + exe_name.string() + ); std::ofstream condabin_activate_bat_f = open_ofstream( root_prefix / "condabin" / "activate.bat" ); condabin_activate_bat_f << activate_bat_contents; + // Scripts/activate.bat std::ofstream scripts_activate_bat_f = open_ofstream(root_prefix / "Scripts" / "activate.bat"); scripts_activate_bat_f << activate_bat_contents; + // mamba_hook.bat std::string hook_content = data_mamba_hook_bat; util::replace_all( hook_content, std::string("__MAMBA_INSERT_MAMBA_EXE__"), "@SET \"MAMBA_EXE=" + exe.string() + "\"" ); + util::replace_all(hook_content, std::string("__MAMBA_INSERT_EXE_NAME__"), exe_name.string()); std::ofstream mamba_hook_bat_f = open_ofstream(root_prefix / "condabin" / "mamba_hook.bat"); mamba_hook_bat_f << hook_content;