Skip to content

Commit

Permalink
Use factorized win_os.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Oct 25, 2023
1 parent 6f1b2af commit 6da3424
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
42 changes: 10 additions & 32 deletions libmamba/src/core/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "mamba/core/output.hpp"
#include "mamba/core/util_os.hpp"
#include "mamba/util/os_win.hpp"
#else
#include <pwd.h>
#include <sys/types.h>
Expand All @@ -31,35 +32,6 @@ extern "C"

namespace mamba::env
{
#ifdef _WIN32
namespace
{
const std::map<std::string, KNOWNFOLDERID> knownfolders = {
{ "programs", FOLDERID_Programs }, { "profile", FOLDERID_Profile },
{ "documents", FOLDERID_Documents }, { "roamingappdata", FOLDERID_RoamingAppData },
{ "programdata", FOLDERID_ProgramData }, { "localappdata", FOLDERID_LocalAppData },
};

auto get_known_folder(const std::string& id) -> fs::u8path
{
wchar_t* localAppData;
HRESULT hres;

hres = SHGetKnownFolderPath(knownfolders.at(id), KF_FLAG_DONT_VERIFY, nullptr, &localAppData);

if (FAILED(hres))
{
throw std::runtime_error("Could not retrieve known folder");
}

std::wstring tmp(localAppData);
fs::u8path res(tmp);
CoTaskMemFree(localAppData);
return res;
}
}
#endif

std::optional<std::string> get(const std::string& key)
{
#ifdef _WIN32
Expand Down Expand Up @@ -311,7 +283,9 @@ namespace mamba::env
if (maybe_user_config_dir.empty())
{
#ifdef _WIN32
maybe_user_config_dir = get_known_folder("roamingappdata");
maybe_user_config_dir = util::get_windows_known_user_folder(
util::WindowsKnowUserFolder::RoamingAppData
);
#else
maybe_user_config_dir = home_directory() / ".config";
#endif
Expand All @@ -325,7 +299,9 @@ namespace mamba::env
if (maybe_user_data_dir.empty())
{
#ifdef _WIN32
maybe_user_data_dir = get_known_folder("roamingappdata");
maybe_user_config_dir = util::get_windows_known_user_folder(
util::WindowsKnowUserFolder::RoamingAppData
);
#else
maybe_user_data_dir = home_directory() / ".local" / "share";
#endif
Expand All @@ -339,7 +315,9 @@ namespace mamba::env
if (maybe_user_cache_dir.empty())
{
#ifdef _WIN32
maybe_user_cache_dir = get_known_folder("localappdata");
maybe_user_config_dir = util::get_windows_known_user_folder(
util::WindowsKnowUserFolder::LocalAppData
);
#else
maybe_user_cache_dir = home_directory() / ".cache";
#endif
Expand Down
16 changes: 13 additions & 3 deletions libmamba/src/core/menuinst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifdef _WIN32
#include <shlobj.h>
#include <windows.h>

#include "mamba/util/os_win.hpp"
#endif

#include "mamba/core/context.hpp"
Expand Down Expand Up @@ -240,8 +242,12 @@ namespace mamba
};

#ifdef _WIN32
vars["${PERSONALDIR}"] = to_forward_slash(win::get_folder("documents"));
vars["${USERPROFILE}"] = to_forward_slash(win::get_folder("profile"));
vars["${PERSONALDIR}"] = to_forward_slash(
util::get_windows_known_user_folder(util::WindowsKnowUserFolder::Documents)
);
vars["${USERPROFILE}"] = to_forward_slash(
util::get_windows_known_user_folder(util::WindowsKnowUserFolder::Profile)
);
#endif

for (auto& [key, val] : vars)
Expand Down Expand Up @@ -304,7 +310,11 @@ namespace mamba
{ cwp_path.string(), target_prefix.string(), env_pyw.string() }
);

fs::u8path target_dir = win::get_folder("programs") / menu_name;
fs::u8path target_dir = util::get_windows_known_user_folder(
util::WindowsKnowUserFolder::Programs
)
/ menu_name;

if (!fs::exists(target_dir))
{
fs::create_directories(target_dir);
Expand Down

0 comments on commit 6da3424

Please sign in to comment.