clippy-terminal/src/clippy/target.hpp

74 lines
1.5 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
#include <iostream>
#include <functional>
namespace clippy::targets {
class Target {
public:
virtual void Execute() = 0;
};
class EmptyTarget : public Target {
public:
void Execute() override {}
};
class OpenProjectConfig : public Target {
public:
OpenProjectConfig(Config config) : config_(config) {}
2022-08-10 21:50:50 +03:00
void Execute() override { config_.Edit(); }
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
}
private:
ProjectList& projects_;
};
class RunShellScript : public Target {
public:
RunShellScript(std::vector<std::string> commands)
: commands_(std::move(commands)) {}
void Execute() override {
cppshell::Shell s;
for (auto& i : commands_) {
std::cout << "-> " << i << std::endl;
s.Execute(i);
if (int err = s.GetExitCodeLastCommand(); err != 0) {
std::cout << "ERROR" << std::endl;
}
}
}
private:
std::vector<std::string> commands_;
};
2022-08-10 21:50:50 +03:00
} // namespace clippy::targets