init project
This commit is contained in:
commit
821ea7eed0
16 changed files with 950 additions and 0 deletions
78
src/projects/project_list.cpp
Normal file
78
src/projects/project_list.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "projects/project_list.hpp"
|
||||
|
||||
ProjectList::ProjectList() {}
|
||||
|
||||
const std::deque<Project>& ProjectList::GetProjects() {
|
||||
return projects_;
|
||||
}
|
||||
|
||||
void ProjectList::AddProject(Project&& project) {
|
||||
bool exists =
|
||||
std::find_if(projects_.begin(), projects_.end(), [&project](const Project& another) {
|
||||
return another.GetName() == project.GetName();
|
||||
}) != projects_.end();
|
||||
|
||||
if (exists) {
|
||||
throw std::logic_error("Project with same name exists");
|
||||
}
|
||||
|
||||
projects_.push_back(std::move(project));
|
||||
}
|
||||
|
||||
const Project& ProjectList::GetProject(const std::filesystem::path& path) const {
|
||||
size_t index = 0, prefix = 0;
|
||||
for (size_t i = 0; i < projects_.size(); ++i) {
|
||||
const auto& project = projects_[i];
|
||||
if (project.IsSubDirectory(path)) {
|
||||
if (prefix < project.GetPath().string().size()) {
|
||||
index = i;
|
||||
prefix = project.GetPath().string().size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (prefix == 0) {
|
||||
throw std::logic_error("Project not exists");
|
||||
}
|
||||
|
||||
return projects_[index];
|
||||
}
|
||||
|
||||
void ProjectList::LoadFromYaml(const std::string& file_path) {
|
||||
try {
|
||||
YAML::Node yaml = YAML::LoadFile(file_path);
|
||||
for (const auto& project : yaml["projects"]) {
|
||||
std::string path = project["path"].as<std::string>();
|
||||
std::string path_to_scripts = project["path_to_scripts"].as<std::string>();
|
||||
std::string name = project["name"].as<std::string>();
|
||||
projects_.emplace_back(path, path_to_scripts, name);
|
||||
}
|
||||
} catch (YAML::Exception& e) {
|
||||
std::cerr << "Error loading YAML file: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectList::SaveToYaml(const std::string& file_path) const {
|
||||
YAML::Emitter yaml;
|
||||
yaml << YAML::BeginMap;
|
||||
yaml << YAML::Key << "projects";
|
||||
yaml << YAML::Value << YAML::BeginSeq;
|
||||
for (const auto& project : projects_) {
|
||||
yaml << YAML::BeginMap;
|
||||
yaml << YAML::Key << "path";
|
||||
yaml << YAML::Value << project.GetPath();
|
||||
yaml << YAML::Key << "path_to_scripts";
|
||||
yaml << YAML::Value << project.GetPathToScripts();
|
||||
yaml << YAML::Key << "name";
|
||||
yaml << YAML::Value << project.GetName();
|
||||
yaml << YAML::EndMap;
|
||||
}
|
||||
yaml << YAML::EndSeq;
|
||||
yaml << YAML::EndMap;
|
||||
|
||||
std::ofstream output(file_path);
|
||||
output << yaml.c_str();
|
||||
output.close();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue