clippy-terminal/src/clippy/target.hpp

98 lines
2.3 KiB
C++
Raw Normal View History

2022-08-10 21:50:50 +03:00
#pragma once
#include <clippy/project_list.hpp>
#include <clippy/config.hpp>
2022-08-10 21:50:50 +03:00
#include <utils/editor.hpp>
#include <utils/config_path.hpp>
#define SIGPIPE_ALWAYS_IGNORE
#include <cppshell/shell.hpp>
#undef SIGPIPE_ALWAYS_IGNORE
2022-08-10 21:50:50 +03:00
2022-08-11 21:42:53 +03:00
#include <rang.hpp>
2022-08-10 21:50:50 +03:00
#include <iostream>
2022-08-11 21:42:53 +03:00
#include <ranges>
2022-08-10 21:50:50 +03:00
#include <functional>
namespace clippy::targets {
class Target {
public:
virtual void Execute() = 0;
2022-10-28 23:49:52 +03:00
virtual ~Target() = 0;
2022-08-10 21:50:50 +03:00
};
class EmptyTarget : public Target {
public:
void Execute() override {}
2022-10-28 23:49:52 +03:00
~EmptyTarget() override {}
2022-08-10 21:50:50 +03:00
};
class OpenProjectConfig : public Target {
public:
OpenProjectConfig(Config config) : config_(config) {}
2022-08-10 21:50:50 +03:00
void Execute() override { config_.Edit(); }
2022-10-28 23:49:52 +03:00
~OpenProjectConfig() override {}
2022-08-10 21:50:50 +03:00
private:
Config config_;
2022-08-10 21:50:50 +03:00
};
class CreateProjectConfig : public Target {
public:
CreateProjectConfig(ProjectList& projects) : projects_(projects) {}
void Execute() override {
auto scripts_path = utils::GetProjectDirectory() / "scripts";
std::filesystem::create_directories(scripts_path);
auto config = projects_.GetNewConfig(scripts_path);
config.Edit();
2022-08-10 21:50:50 +03:00
}
2022-10-28 23:49:52 +03:00
~CreateProjectConfig() override {}
2022-08-10 21:50:50 +03:00
private:
ProjectList& projects_;
};
class RunShellScript : public Target {
public:
RunShellScript(std::vector<std::string> commands)
: commands_(std::move(commands)) {}
void Execute() override {
cppshell::Shell s;
2022-08-11 21:42:53 +03:00
for (auto& command : commands_) {
std::cout << rang::fg::green << rang::style::bold << "-> "
<< rang::fg::reset << command << rang::style::reset
<< std::endl;
s.Execute(command);
if (int err = s.GetExitCodeLastCommand(); err != 0) {
2022-08-11 21:42:53 +03:00
std::cout << rang::fg::red << rang::style::bold
<< "Command exit with code " << err << rang::fg::reset
<< rang::style::reset << std::endl;
std::cout << rang::fg::blue << rang::style::bold
<< "Continue execution? [Y/n] " << rang::fg::reset
<< rang::style::reset;
std::string result;
std::getline(std::cin, result);
if (result == "" || result == "y") {
continue;
} else {
return;
}
}
}
}
2022-10-28 23:49:52 +03:00
~RunShellScript() override {}
private:
std::vector<std::string> commands_;
};
2022-08-10 21:50:50 +03:00
} // namespace clippy::targets