From 48f25d4baa4b106342cbf681f14db8e3293682b7 Mon Sep 17 00:00:00 2001 From: Timofey Khoruzhii Date: Sun, 16 Apr 2023 00:57:57 +0300 Subject: [PATCH] update config --- src/clippy/project_list.cpp | 74 ++++++++++++++++++++++++++++++++++--- src/clippy/project_list.hpp | 6 +++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/clippy/project_list.cpp b/src/clippy/project_list.cpp index 5409a42..cf4aa2e 100644 --- a/src/clippy/project_list.cpp +++ b/src/clippy/project_list.cpp @@ -14,6 +14,9 @@ #include #include +#include "yaml-cpp/node/parse.h" + +#include Config ProjectList::GetNewConfig(const std::filesystem::path& config_directory) { std::lock_guard guard(lock_file_); @@ -52,7 +55,14 @@ void UpdateField(jsoncons::json& data, const std::string& field, std::optional +void UpdateField(YAML::Node& data, const std::string& field, std::optional member) { + if (member) { + data[field] = static_cast(member.value()); + } +} + +void ProjectList::SaveConfig(tag_json) { jsoncons::json result; result["version"] = "0.1"; @@ -73,11 +83,52 @@ void ProjectList::SaveConfig() { out << result.to_string(); } +void ProjectList::SaveConfig() { + YAML::Node result; + result["version"] = "0.2"; + + for (auto& project : projects_) { + YAML::Node current; + + UpdateField(current, "name", project.name); + current["path_root_project"] = std::string(project.root_project); + current["path_to_config"] = std::string(project.configuration_file); + UpdateField(current, "open_script", project.open_script); + + result["projects"].push_back(current); + } + + std::ofstream out(path_); + out << result; +} + template std::optional GetOptionalField(const jsoncons::json& data, std::string field) { return data.contains(field) ? std::make_optional(data[field].as()) : std::nullopt; } +template +std::optional GetOptionalField(const YAML::Node& data, std::string field) { + return data[field].IsDefined() ? std::make_optional(data[field].as()) : std::nullopt; +} + +void ProjectList::LoadConfig(const YAML::Node& data) { + if (data["version"].as() != "0.2") { + throw std::logic_error("unsupport version"); + } + + Project current; + for (const auto& project : data["projects"]) { + current.configuration_file = project["path_to_config"].as(); + current.root_project = project["path_root_project"].as(); + + current.name = GetOptionalField(project, "name"); + current.open_script = GetOptionalField(project, "open_script"); + + projects_.emplace_back(current); + } +} + void ProjectList::LoadConfig(const jsoncons::json& data) { if (data["version"] != "0.1") { throw std::logic_error("unsupported version config"); @@ -97,18 +148,31 @@ void ProjectList::LoadConfig(const jsoncons::json& data) { void ProjectList::LoadWithoutLock() { projects_.clear(); - auto data = utils::filesystem::LoadFile(path_); try { - LoadConfig(jsoncons::json::parse(data)); + LoadConfig(YAML::LoadFile(path_)); } catch (...) { - std::cout << "I can't read project lists. Try fix it?"; + auto data = utils::filesystem::LoadFile(path_); + + std::cout << "I can't parse yaml. Try read json?" << std::endl; std::string result; std::getline(std::cin, result); if (result == "y") { - OldLoadConfig(data); + try { + LoadConfig(jsoncons::json::parse(data)); + } catch (...) { + std::cout << "I can't read project lists. Try fix it?" << std::endl; + + std::string result; + std::getline(std::cin, result); + + if (result == "y") { + OldLoadConfig(data); + SaveConfig(); + } + } SaveConfig(); } } diff --git a/src/clippy/project_list.hpp b/src/clippy/project_list.hpp index b65d4db..fe1ba71 100644 --- a/src/clippy/project_list.hpp +++ b/src/clippy/project_list.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "utils/config_path.hpp" #include "utils/editor.hpp" #include "utils/filesystem.hpp" @@ -96,10 +97,15 @@ class ProjectList { Project* GetProjectByName_(const std::string&); void OldLoadConfig(const std::string&); + + struct tag_json {}; + void SaveConfig(tag_json); + void SaveConfig(); void Load(); void LoadConfig(const jsoncons::json&); + void LoadConfig(const YAML::Node&); void LoadWithoutLock(); private: