clippy-terminal/src/clippy/clippy.cpp

92 lines
2.4 KiB
C++
Raw Normal View History

2022-08-10 10:53:23 +03:00
#include <clippy/clippy.hpp>
2022-08-10 21:52:07 +03:00
#include <clippy/target.hpp>
2022-08-10 10:53:23 +03:00
2022-08-10 12:02:25 +03:00
#include <utils/parametres.hpp>
#include <utils/config_path.hpp>
2022-08-10 12:02:25 +03:00
2022-08-10 10:53:23 +03:00
#include <iostream>
2022-08-10 21:52:07 +03:00
#include <memory>
2022-08-10 10:53:23 +03:00
#include <vector>
2022-08-10 21:52:07 +03:00
#include <filesystem>
2022-08-10 10:53:23 +03:00
#include <rang.hpp>
2022-08-10 10:53:23 +03:00
void Clippy::Run(const std::vector<std::string>& args) {
if (auto result = TryExecuteClippyCommand(args); result) {
result->Execute();
return;
}
if (auto result = GetScriptTarget(args); result) {
2022-08-10 21:52:07 +03:00
result->Execute();
return;
}
std::cout << rang::bg::red << rang::style::bold << "Unsupported parameters { ";
for (size_t i = 1; i < args.size(); ++i) {
std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " ";
}
std::cout << "}" << rang::bg::reset << rang::style::reset << std::endl;
2022-08-10 11:04:37 +03:00
}
2022-08-10 21:52:07 +03:00
Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
const std::vector<std::string>& args) {
2022-08-10 12:02:25 +03:00
using namespace utils::parametres;
2022-08-10 21:52:07 +03:00
using namespace clippy::targets;
2022-08-10 12:02:25 +03:00
if (CheckPatternParametres(args, Parameter::Skip, "help",
Parameter::Anything)) {
2022-08-10 11:04:37 +03:00
std::cout << "Hello I'm clippy" << std::endl;
std::cout << "Parametres: { ";
for (size_t i = 0; i < args.size(); ++i) {
std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " ";
}
std::cout << "}" << std::endl;
2022-08-10 21:52:07 +03:00
return std::make_unique<EmptyTarget>();
}
if (CheckPatternParametres(args, Parameter::Skip, "cfg",
Parameter::Anything)) {
LoadProjects();
auto p = projects_->GetCurrentProject();
2022-08-10 21:52:07 +03:00
if (p.has_value()) {
return std::make_unique<OpenProjectConfig>(p->GetConfig());
2022-08-10 21:52:07 +03:00
} else {
return std::make_unique<CreateProjectConfig>(projects_.value());
2022-08-10 21:52:07 +03:00
}
2022-08-10 10:53:23 +03:00
}
2022-08-10 11:04:37 +03:00
2022-08-11 21:51:04 +03:00
if (CheckPatternParametres(args, Parameter::Skip, "crt",
Parameter::Anything)) {
LoadProjects();
return std::make_unique<CreateProjectConfig>(projects_.value());
}
2022-08-10 21:52:07 +03:00
return nullptr;
2022-08-10 10:53:23 +03:00
}
Clippy::TargetPtr Clippy::GetScriptTarget(
const std::vector<std::string>& args) {
LoadProjects();
auto p = projects_->GetCurrentProject();
if (!p.has_value() || args.size() < 2) {
return nullptr;
}
auto cfg = p->GetConfig();
2022-10-29 09:38:26 +03:00
auto result = cfg.GetTarget(args[1]);
if (result && enable_aliases_) {
2022-10-29 09:38:26 +03:00
result->PushFront("shopt -s expand_aliases");
}
2022-10-29 09:38:26 +03:00
return result;
}
void Clippy::LoadProjects() {
if (!projects_.has_value()) {
projects_.emplace(utils::GetProjectDirectory() / "projects");
}
}