handle MOBINI after cwd

added origin of ini for inis command
This commit is contained in:
isanae
2020-05-22 13:58:47 -04:00
parent 94bd99f38d
commit ef104d17cc
3 changed files with 55 additions and 34 deletions
+1 -11
View File
@@ -1060,17 +1060,7 @@ clipp::group inis_command::do_group()
int inis_command::do_run()
{
const auto r = prepare_options(true);
if (r != 0)
return r;
u8cout << "\nhigher number overrides lower\n";
const auto v = inis();
for (std::size_t i=0; i<v.size(); ++i)
u8cout << (i + 1) << ") " << path_to_utf8(v[i]) << "\n";
return 0;
return prepare_options(true);
}
std::string inis_command::do_doc()
+50 -20
View File
@@ -831,47 +831,45 @@ void set_special_options()
std::vector<fs::path> find_inis(
bool auto_detect, const std::vector<std::string>& from_cl, bool verbose)
{
std::vector<fs::path> v;
// the string is just for verbose
std::vector<std::pair<std::string, fs::path>> v;
auto add_or_move_up = [&](fs::path p)
// adds a path to the vector; if the path already exists, moves it to
// the last element
auto add_or_move_up = [&](std::string where, fs::path p)
{
for (auto itor=v.begin(); itor!=v.end(); ++itor)
{
if (fs::equivalent(p, *itor))
if (fs::equivalent(p, itor->second))
{
auto pair = std::move(*itor);
v.erase(itor);
v.push_back(p);
v.push_back({where + ", was " + pair.first, p});
return;
}
}
v.push_back(p);
v.push_back({where, p});
};
// auto detect from exe directory and cwd
fs::path master;
// auto detect from exe directory
if (auto_detect)
{
if (verbose)
u8cout << "root is " << path_to_utf8(find_root()) << "\n";
const auto master = find_in_root(master_ini_filename());
master = find_in_root(master_ini_filename());
if (verbose)
u8cout << "found master " << master_ini_filename() << "\n";
v.push_back(master);
const auto in_cwd = fs::current_path() / master_ini_filename();
if (fs::exists(in_cwd) && !fs::equivalent(in_cwd, master))
{
if (verbose)
u8cout << "also found in cwd " << path_to_utf8(in_cwd) << "\n";
v.push_back(fs::canonical(in_cwd));
}
v.push_back({"master", master});
}
// MOBINI environment variable
if (auto e=this_env::get_opt("MOBINI"))
{
@@ -895,10 +893,28 @@ std::vector<fs::path> find_inis(
if (verbose)
u8cout << "ini from env: " << path_to_utf8(p) << "\n";
add_or_move_up(p);
add_or_move_up("env", p);
}
}
// auto detect from the current directory
if (auto_detect)
{
MOB_ASSERT(!master.empty());
const auto in_cwd = fs::current_path() / master_ini_filename();
if (fs::exists(in_cwd) && !fs::equivalent(in_cwd, master))
{
if (verbose)
u8cout << "also found in cwd " << path_to_utf8(in_cwd) << "\n";
v.push_back({"cwd", fs::canonical(in_cwd)});
}
}
// command line
for (auto&& i : from_cl)
{
auto p = fs::path(i);
@@ -913,10 +929,24 @@ std::vector<fs::path> find_inis(
if (verbose)
u8cout << "ini from command line: " << path_to_utf8(p) << "\n";
add_or_move_up(p);
add_or_move_up("cl", p);
}
return v;
if (verbose)
{
u8cout << "\nhigher number overrides lower\n";
for (std::size_t i=0; i<v.size(); ++i)
{
u8cout
<< (i + 1) << ") "
<< path_to_utf8(v[i].second) << " (" << v[i].first << ")\n";
}
}
return map(v, [&](auto&& p){ return p.second; });
}
void init_options(
+4 -3
View File
@@ -534,10 +534,11 @@ template <
}
template <class F>
std::vector<std::string> map(const std::vector<std::string>& v, F&& f)
template <class F, class T>
auto map(const std::vector<T>& v, F&& f)
{
std::vector<std::string> out;
using mapped_type = decltype(f(std::declval<T>()));
std::vector<mapped_type> out;
for (auto&& e : v)
out.push_back(f(e));