move to json; add name for projects
This commit is contained in:
parent
dbd34e0817
commit
a2186366b8
|
@ -28,12 +28,10 @@ void Clippy::Run(const std::vector<std::string>& args) {
|
|||
std::cout << "}" << rang::bg::reset << rang::style::reset << std::endl;
|
||||
}
|
||||
|
||||
Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
|
||||
const std::vector<std::string>& args) {
|
||||
Clippy::TargetPtr Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) {
|
||||
using namespace utils::parametres;
|
||||
using namespace clippy::targets;
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "help",
|
||||
Parameter::Anything)) {
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "help", Parameter::Anything)) {
|
||||
std::cout << "Hello I'm clippy" << std::endl;
|
||||
std::cout << "Parametres: { ";
|
||||
for (size_t i = 0; i < args.size(); ++i) {
|
||||
|
@ -44,8 +42,7 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
|
|||
return std::make_unique<EmptyTarget>();
|
||||
}
|
||||
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "cfg",
|
||||
Parameter::Anything)) {
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "cfg", Parameter::Anything)) {
|
||||
LoadProjects();
|
||||
auto p = projects_->GetCurrentProject();
|
||||
|
||||
|
@ -56,17 +53,42 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
|
|||
}
|
||||
}
|
||||
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "crt",
|
||||
Parameter::Anything)) {
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "crt", Parameter::Anything)) {
|
||||
LoadProjects();
|
||||
return std::make_unique<CreateProjectConfig>(projects_.value());
|
||||
}
|
||||
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "prj", Parameter::Anything)) {
|
||||
if (args.size() != 3) {
|
||||
return nullptr;
|
||||
}
|
||||
std::string name_project = args[2];
|
||||
|
||||
LoadProjects();
|
||||
|
||||
for (auto& project : projects_->GetProjects()) {
|
||||
if (project.name == name_project) {
|
||||
std::cout << "Find: " << project.root_project << " " << project.configuration_file << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "list", Parameter::Nothing)) {
|
||||
LoadProjects();
|
||||
|
||||
return std::make_unique<PrintListProjects>(projects_.value());
|
||||
}
|
||||
|
||||
if (CheckPatternParametres(args, Parameter::Skip, "setname", Parameter::Anything)) {
|
||||
LoadProjects();
|
||||
|
||||
return std::make_unique<SetNameProject>(projects_.value(), args[2]);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Clippy::TargetPtr Clippy::GetScriptTarget(
|
||||
const std::vector<std::string>& args) {
|
||||
Clippy::TargetPtr Clippy::GetScriptTarget(const std::vector<std::string>& args) {
|
||||
LoadProjects();
|
||||
auto p = projects_->GetCurrentProject();
|
||||
|
||||
|
|
|
@ -1,17 +1,22 @@
|
|||
#include <clippy/project_list.hpp>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <utils/lock_file.hpp>
|
||||
#include <utils/filesystem.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
|
||||
Config ProjectList::GetNewConfig(
|
||||
const std::filesystem::path& config_directory) {
|
||||
#include <jsoncons/json.hpp>
|
||||
#include <jsoncons_ext/cbor/cbor.hpp>
|
||||
|
||||
Config ProjectList::GetNewConfig(const std::filesystem::path& config_directory) {
|
||||
std::lock_guard guard(lock_file_);
|
||||
|
||||
LoadWithouLock();
|
||||
LoadWithoutLock();
|
||||
|
||||
std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count());
|
||||
|
||||
|
@ -52,24 +57,21 @@ Config ProjectList::GetNewConfig(
|
|||
}
|
||||
|
||||
auto path_to_config = config_directory / filename;
|
||||
|
||||
std::ofstream out(path_, std::ios::app);
|
||||
out << std::filesystem::current_path() << " " << path_to_config << std::endl;
|
||||
out.close();
|
||||
|
||||
projects_.emplace_back(std::filesystem::current_path(), path_to_config);
|
||||
|
||||
SaveConfig();
|
||||
|
||||
return {path_to_config, std::filesystem::current_path()};
|
||||
}
|
||||
|
||||
void ProjectList::Load() {
|
||||
std::lock_guard guard(lock_file_);
|
||||
|
||||
LoadWithouLock();
|
||||
LoadWithoutLock();
|
||||
}
|
||||
|
||||
void ProjectList::LoadWithouLock() {
|
||||
std::ifstream in(path_);
|
||||
void ProjectList::OldLoadConfig(const std::string& data) {
|
||||
std::stringstream in(data);
|
||||
|
||||
Project current;
|
||||
|
||||
|
@ -78,15 +80,77 @@ void ProjectList::LoadWithouLock() {
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<Project> ProjectList::GetCurrentProject() {
|
||||
void ProjectList::SaveConfig() {
|
||||
jsoncons::json result;
|
||||
|
||||
result["version"] = "0.1";
|
||||
result["projects"] = std::vector<std::string>();
|
||||
|
||||
for (auto& project : projects_) {
|
||||
jsoncons::json current;
|
||||
current["path_to_config"] = std::string(project.configuration_file);
|
||||
current["path_root_project"] = std::string(project.root_project);
|
||||
|
||||
std::cout << (bool)project.name << std::endl;
|
||||
if (project.name && !project.name.value().empty()) {
|
||||
current["name"] = project.name.value();
|
||||
}
|
||||
|
||||
result["projects"].emplace_back(current);
|
||||
}
|
||||
|
||||
std::ofstream out(path_);
|
||||
out << result.to_string();
|
||||
}
|
||||
|
||||
void ProjectList::LoadConfig(const jsoncons::json& data) {
|
||||
if (data["version"] != "0.1") {
|
||||
throw std::logic_error("unsupported version config");
|
||||
}
|
||||
|
||||
Project current;
|
||||
for (auto& project : data["projects"].array_range()) {
|
||||
current.configuration_file = project["path_to_config"].as<std::string>();
|
||||
current.root_project = project["path_root_project"].as<std::string>();
|
||||
if (project.contains("name")) {
|
||||
current.name = project["name"].as<std::string>();
|
||||
} else {
|
||||
current.name = std::nullopt;
|
||||
}
|
||||
|
||||
projects_.emplace_back(current);
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectList::LoadWithoutLock() {
|
||||
auto data = utils::LoadFile(path_);
|
||||
|
||||
try {
|
||||
LoadConfig(jsoncons::json::parse(data));
|
||||
} catch (...) {
|
||||
std::cout << "I can't read project lists. Try fix it?";
|
||||
|
||||
std::string result;
|
||||
std::getline(std::cin, result);
|
||||
|
||||
if (result == "y") {
|
||||
OldLoadConfig(data);
|
||||
SaveConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Project* ProjectList::GetCurrentProject_() {
|
||||
auto current_path = std::filesystem::current_path();
|
||||
|
||||
std::optional<Project> result;
|
||||
auto UpdateResult = [&result](Project p) {
|
||||
if (!result.has_value()) {
|
||||
result = p;
|
||||
Project* result = nullptr;
|
||||
auto UpdateResult = [&result](Project& p) {
|
||||
if (!result) {
|
||||
result = &p;
|
||||
} else {
|
||||
result = std::max(p, result.value());
|
||||
if (*result > p) {
|
||||
result = &p;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
#pragma once
|
||||
#include <clippy/config.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utils/lock_file.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
|
||||
#include <jsoncons/json.hpp>
|
||||
|
||||
struct Project {
|
||||
Project() {}
|
||||
|
||||
|
@ -15,10 +18,13 @@ struct Project {
|
|||
|
||||
std::strong_ordering operator<=>(const Project&) const = default;
|
||||
|
||||
Config GetConfig() { return Config{configuration_file, root_project}; }
|
||||
Config GetConfig() {
|
||||
return Config{configuration_file, root_project};
|
||||
}
|
||||
|
||||
std::filesystem::path root_project;
|
||||
std::filesystem::path configuration_file;
|
||||
std::optional<std::string> name;
|
||||
};
|
||||
|
||||
class ProjectList {
|
||||
|
@ -29,13 +35,38 @@ class ProjectList {
|
|||
|
||||
Config GetNewConfig(const std::filesystem::path& config_directory);
|
||||
|
||||
const std::vector<Project>& GetProjects() const { return projects_; }
|
||||
const std::vector<Project>& GetProjects() const {
|
||||
return projects_;
|
||||
}
|
||||
|
||||
std::optional<Project> GetCurrentProject();
|
||||
std::optional<Project> GetCurrentProject() {
|
||||
auto* p = GetCurrentProject_();
|
||||
if (p) {
|
||||
return *p;
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
void SetNameCurrentProject(const std::string& name) {
|
||||
auto* p = GetCurrentProject_();
|
||||
if (p) {
|
||||
p->name = name;
|
||||
SaveConfig();
|
||||
} else {
|
||||
throw std::logic_error("Not exists current project");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Project* GetCurrentProject_();
|
||||
|
||||
void OldLoadConfig(const std::string&);
|
||||
void SaveConfig();
|
||||
|
||||
void Load();
|
||||
void LoadWithouLock();
|
||||
void LoadConfig(const jsoncons::json&);
|
||||
void LoadWithoutLock();
|
||||
|
||||
private:
|
||||
std::filesystem::path path_;
|
||||
|
|
|
@ -32,7 +32,9 @@ class OpenProjectConfig : public Target {
|
|||
public:
|
||||
OpenProjectConfig(Config config) : config_(config) {}
|
||||
|
||||
void Execute() override { config_.Edit(); }
|
||||
void Execute() override {
|
||||
config_.Edit();
|
||||
}
|
||||
~OpenProjectConfig() override {}
|
||||
|
||||
private:
|
||||
|
@ -76,15 +78,13 @@ class RunShellScript : public Target {
|
|||
cppshell::Shell s("bash", tmp_path);
|
||||
|
||||
for (auto& command : commands_) {
|
||||
std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset
|
||||
<< rang::bg::reset << " " << command << rang::style::reset << std::endl;
|
||||
std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset << rang::bg::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
|
||||
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
|
||||
std::cout << rang::fg::blue << rang::style::bold << "Continue execution? [Y/n] " << rang::fg::reset
|
||||
<< rang::style::reset;
|
||||
|
||||
std::string result;
|
||||
|
@ -103,4 +103,33 @@ class RunShellScript : public Target {
|
|||
private:
|
||||
std::deque<std::string> commands_;
|
||||
};
|
||||
|
||||
class PrintListProjects : public Target {
|
||||
public:
|
||||
PrintListProjects(ProjectList& projects) : projects_(projects) {}
|
||||
|
||||
void Execute() override {
|
||||
for (auto& project : projects_.GetProjects()) {
|
||||
std::cout << std::setw(10) << project.name.value_or("-") << " " << std::setw(50) << project.root_project << " "
|
||||
<< std::setw(70) << project.configuration_file << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ProjectList& projects_;
|
||||
};
|
||||
|
||||
class SetNameProject : public Target {
|
||||
public:
|
||||
SetNameProject(ProjectList& projects, std::string new_name)
|
||||
: projects_(projects), new_name_(std::move(new_name)) {}
|
||||
|
||||
void Execute() override {
|
||||
projects_.SetNameCurrentProject(new_name_);
|
||||
}
|
||||
|
||||
private:
|
||||
ProjectList& projects_;
|
||||
std::string new_name_;
|
||||
};
|
||||
} // namespace clippy::targets
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
|
||||
#include <csignal>
|
||||
|
||||
#include <utils/filesystem.hpp>
|
||||
|
||||
#include <jsoncons/json.hpp>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
|
|
22
src/utils/filesystem.hpp
Normal file
22
src/utils/filesystem.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace utils {
|
||||
inline std::string LoadFile(const std::filesystem::path& file) {
|
||||
std::ifstream in(file);
|
||||
|
||||
std::string result;
|
||||
std::string tmp;
|
||||
|
||||
while (std::getline(in, tmp)) {
|
||||
result += tmp;
|
||||
result += "\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace utils
|
Loading…
Reference in a new issue