update config
This commit is contained in:
parent
1b655704ee
commit
48f25d4baa
|
@ -14,6 +14,9 @@
|
|||
|
||||
#include <jsoncons/json.hpp>
|
||||
#include <jsoncons_ext/cbor/cbor.hpp>
|
||||
#include "yaml-cpp/node/parse.h"
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
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<T
|
|||
}
|
||||
}
|
||||
|
||||
void ProjectList::SaveConfig() {
|
||||
template <typename U, typename T>
|
||||
void UpdateField(YAML::Node& data, const std::string& field, std::optional<T> member) {
|
||||
if (member) {
|
||||
data[field] = static_cast<U>(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<std::string>(current, "name", project.name);
|
||||
current["path_root_project"] = std::string(project.root_project);
|
||||
current["path_to_config"] = std::string(project.configuration_file);
|
||||
UpdateField<std::string>(current, "open_script", project.open_script);
|
||||
|
||||
result["projects"].push_back(current);
|
||||
}
|
||||
|
||||
std::ofstream out(path_);
|
||||
out << result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> GetOptionalField(const jsoncons::json& data, std::string field) {
|
||||
return data.contains(field) ? std::make_optional(data[field].as<std::string>()) : std::nullopt;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> GetOptionalField(const YAML::Node& data, std::string field) {
|
||||
return data[field].IsDefined() ? std::make_optional(data[field].as<std::string>()) : std::nullopt;
|
||||
}
|
||||
|
||||
void ProjectList::LoadConfig(const YAML::Node& data) {
|
||||
if (data["version"].as<std::string>() != "0.2") {
|
||||
throw std::logic_error("unsupport version");
|
||||
}
|
||||
|
||||
Project current;
|
||||
for (const auto& project : data["projects"]) {
|
||||
current.configuration_file = project["path_to_config"].as<std::string>();
|
||||
current.root_project = project["path_root_project"].as<std::string>();
|
||||
|
||||
current.name = GetOptionalField<std::string>(project, "name");
|
||||
current.open_script = GetOptionalField<std::string>(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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <optional>
|
||||
|
||||
#include <jsoncons/json.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#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:
|
||||
|
|
Loading…
Reference in a new issue