Skip to content

Commit

Permalink
Win activate (#3489)
Browse files Browse the repository at this point in the history
* Fixed activation scripts for cmd.exe

* Fixed get_PATH on Windows

* linter
  • Loading branch information
JohanMabille authored Oct 2, 2024
1 parent 45c437a commit d162759
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
12 changes: 1 addition & 11 deletions libmamba/data/activate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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 %*
16 changes: 5 additions & 11 deletions libmamba/data/mamba_hook.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 12 additions & 6 deletions libmamba/src/core/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,20 @@ namespace mamba
std::vector<fs::u8path> Activator::get_PATH()
{
std::vector<fs::u8path> path;
std::vector<std::string> 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;
}
Expand Down Expand Up @@ -267,7 +274,6 @@ namespace mamba
std::vector<fs::u8path> 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;
}
Expand Down
19 changes: 14 additions & 5 deletions libmamba/src/core/shell_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit d162759

Please sign in to comment.