From eb4202b02e5b90b02b4938d13bb54057383b8e80 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 4 Nov 2020 20:40:27 -0500 Subject: [PATCH] aliases --- mob.ini | 5 ++-- src/commands.cpp | 59 ++++++++++++++++++++++++------------------ src/commands.h | 6 +++++ src/conf.cpp | 6 ++++- src/tasks/task.cpp | 64 ++++++++++++++++++++++++++++++++++++++++------ src/tasks/task.h | 5 +++- src/utility.cpp | 49 +++++++++++++++++++++++++++++++++++ src/utility.h | 3 ++- 8 files changed, 159 insertions(+), 38 deletions(-) diff --git a/mob.ini b/mob.ini index 15a815f..6274651 100644 --- a/mob.ini +++ b/mob.ini @@ -38,13 +38,14 @@ git_shallow = false git_shallow = false [installer:task] +enabled = false git_shallow = false [translations:task] enabled = false -[installer:task] -enabled = false +[aliases] +plugins = check_fnis bsapacker bsa_extractor diagnose_basic installer_* plugin_python preview_base preview_bsa tool_* game_* [tools] sevenz = 7z.exe diff --git a/src/commands.cpp b/src/commands.cpp index d7d5392..1f56561 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -184,23 +184,7 @@ void command::set_task_enabled_flags(const std::vector& names) bool failed = false; for (auto&& pattern : names) - { - const auto tasks = find_tasks(pattern); - - if (tasks.empty()) - { - u8cout << "no task matching '" << pattern<< "' found\n"; - - u8cout << "valid tasks:\n"; - for (auto&& t : get_all_tasks()) - u8cout << " - " << join(t->names(), ", ") << "\n"; - - failed = true; - } - - for (auto&& t : tasks) - common.options.push_back(t->name() + ":task/enabled=true"); - } + common.options.push_back(pattern + ":task/enabled=true"); if (failed) throw bailed(); @@ -598,6 +582,9 @@ clipp::group list_command::do_group() (clipp::option("-a", "--all") >> all_) % "shows all the tasks, including pseudo parallel tasks", + (clipp::option("-i", "--aliases") >> aliases_) + % "shows only aliases", + (clipp::opt_values( clipp::match::prefix_not("-"), "task", tasks_)) % "with -a; when given, acts like the tasks given to `build` and " @@ -607,20 +594,31 @@ clipp::group list_command::do_group() int list_command::do_run() { - if (all_) + if (aliases_) { - if (!tasks_.empty()) - set_task_enabled_flags(tasks_); - - load_options(); - dump(get_top_level_tasks(), 0); + dump_aliases(); } else { - for (auto&& t : get_all_tasks()) - u8cout << " - " << join(t->names(), ", ") << "\n"; + if (all_) + { + if (!tasks_.empty()) + set_task_enabled_flags(tasks_); + + load_options(); + dump(get_top_level_tasks(), 0); + + u8cout << "\n\naliases:\n"; + dump_aliases(); + } + else + { + for (auto&& t : get_all_tasks()) + u8cout << " - " << join(t->names(), ", ") << "\n"; + } } + return 0; } @@ -641,6 +639,17 @@ void list_command::dump(const std::vector& v, std::size_t indent) const } } +void list_command::dump_aliases() const +{ + const auto v = get_all_aliases(); + if (v.empty()) + return; + + for (auto&& [k, patterns] : v) + u8cout << " - " << k << ": " << join(patterns, ", ") << "\n"; +} + + std::string list_command::do_doc() { return diff --git a/src/commands.h b/src/commands.h index a557b68..a49e404 100644 --- a/src/commands.h +++ b/src/commands.h @@ -160,9 +160,15 @@ protected: private: bool all_ = false; + bool aliases_ = false; std::vector tasks_; void dump(const std::vector& v, std::size_t indent) const; + + void dump_tasks( + const std::vector& v, std::size_t indent, bool recurse) const; + + void dump_aliases() const; }; diff --git a/src/conf.cpp b/src/conf.cpp index 2accbe7..f7a84c3 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -767,7 +767,11 @@ void parse_section( if (k.empty()) ini_error(ini, i, "bad line '" + line + "'"); - if (task.empty()) + if (section == "aliases") + { + add_alias(k, split_quoted(v, " ")); + } + else if (task.empty()) { if (add) conf::add_global(section, k, v); diff --git a/src/tasks/task.cpp b/src/tasks/task.cpp index 7836ad4..d45594d 100644 --- a/src/tasks/task.cpp +++ b/src/tasks/task.cpp @@ -12,6 +12,7 @@ class interrupted {}; static std::vector> g_tasks; static std::vector g_all_tasks; static std::atomic g_interrupt = false; +static alias_map g_aliases; std::mutex task::interrupt_mutex_; @@ -40,7 +41,7 @@ std::vector get_top_level_tasks() return v; } -std::vector find_tasks(const std::string& pattern) +std::vector find_tasks_by_pattern(const std::string& pattern) { std::vector tasks; @@ -66,21 +67,50 @@ std::vector find_tasks(const std::string& pattern) return tasks; } -task* find_task(const std::string& pattern) +std::vector find_tasks_by_alias(const std::string& pattern) { + std::vector v; + + auto itor = g_aliases.find(pattern); + if (itor == g_aliases.end()) + return v; + + for (auto&& a : itor->second) + { + const auto temp = find_tasks_by_pattern(a); + v.insert(v.end(), temp.begin(), temp.end()); + } + + return v; +} + +std::vector find_tasks(const std::string& pattern) +{ + std::vector tasks; + for (auto&& t : g_all_tasks) { if (pattern == "super" && t->is_super()) - return t; - - for (auto&& n : t->names()) { - if (mob::glob_match(pattern, n)) - return t; + tasks.push_back(t); + } + else + { + for (auto&& n : t->names()) + { + if (mob::glob_match(pattern, n)) + { + tasks.push_back(t); + break; + } + } } } - return nullptr; + if (tasks.empty()) + tasks = find_tasks_by_alias(pattern); + + return tasks; } void run_all_tasks() @@ -132,6 +162,24 @@ bool is_super_task(const std::string& name) return false; } +void add_alias(std::string name, std::vector names) +{ + auto itor = g_aliases.find(name); + if (itor != g_aliases.end()) + { + gcx().warning(context::generic, "alias {} already exists", name); + return; + } + + g_aliases.emplace(std::move(name), std::move(names)); +} + +const alias_map& get_all_aliases() +{ + return g_aliases; +} + + std::string to_string(task::clean c) { diff --git a/src/tasks/task.h b/src/tasks/task.h index 63e36fd..fad22eb 100644 --- a/src/tasks/task.h +++ b/src/tasks/task.h @@ -24,11 +24,14 @@ Task& add_task(Args&&... args) void run_all_tasks(); bool is_super_task(const std::string& name); std::vector find_tasks(const std::string& pattern); -task* find_task(const std::string& pattern); std::vector get_all_tasks(); std::vector get_top_level_tasks(); +using alias_map = std::map>; +void add_alias(std::string name, std::vector patterns); +const alias_map& get_all_aliases(); + class task_conf_holder { diff --git a/src/utility.cpp b/src/utility.cpp index 8d9535f..cc4ef01 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -383,6 +383,55 @@ std::vector split(const std::string& s, const std::string& seps) return v; } +std::vector split_quoted(const std::string& s, const std::string& seps) +{ + std::vector v; + bool q = false; + std::string token; + + for (std::size_t i=0; i void trim_impl(std::basic_string& s, std::basic_string_view what) { diff --git a/src/utility.h b/src/utility.h index e4528bd..6bf9b58 100644 --- a/src/utility.h +++ b/src/utility.h @@ -336,7 +336,8 @@ T join(const std::vector& v, const Sep& sep) return s; } -std::vector split(const std::string& s, const std::string& sep); +std::vector split(const std::string& s, const std::string& seps); +std::vector split_quoted(const std::string& s, const std::string& seps); std::string pad_right(std::string s, std::size_t n, char c=' '); std::string pad_left(std::string s, std::size_t n, char c=' ');