97 lines
2.3 KiB
C++
97 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <clippy/project_list.hpp>
|
|
#include <clippy/config.hpp>
|
|
|
|
#include <utils/editor.hpp>
|
|
#include <utils/config_path.hpp>
|
|
|
|
#define SIGPIPE_ALWAYS_IGNORE
|
|
#include <cppshell/shell.hpp>
|
|
#undef SIGPIPE_ALWAYS_IGNORE
|
|
|
|
#include <rang.hpp>
|
|
|
|
#include <iostream>
|
|
#include <ranges>
|
|
#include <functional>
|
|
|
|
namespace clippy::targets {
|
|
class Target {
|
|
public:
|
|
virtual void Execute() = 0;
|
|
virtual ~Target() = 0;
|
|
};
|
|
|
|
class EmptyTarget : public Target {
|
|
public:
|
|
void Execute() override {}
|
|
~EmptyTarget() override {}
|
|
};
|
|
|
|
class OpenProjectConfig : public Target {
|
|
public:
|
|
OpenProjectConfig(Config config) : config_(config) {}
|
|
|
|
void Execute() override { config_.Edit(); }
|
|
~OpenProjectConfig() override {}
|
|
|
|
private:
|
|
Config config_;
|
|
};
|
|
|
|
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();
|
|
}
|
|
~CreateProjectConfig() override {}
|
|
|
|
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& 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) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
~RunShellScript() override {}
|
|
|
|
private:
|
|
std::vector<std::string> commands_;
|
|
};
|
|
} // namespace clippy::targets
|