Add read project lists

This commit is contained in:
Timofey 2022-08-10 21:52:07 +03:00
parent fe9e887061
commit a4229edd6f
4 changed files with 118 additions and 4 deletions

View file

@ -0,0 +1,57 @@
#include <clippy/project_list.hpp>
#include <filesystem>
#include <utils/lock_file.hpp>
#include <fstream>
#include <mutex>
#include <iostream>
void ProjectList::LoadFrom(const std::string& path) {
utils::filesystem::LockFile lock(path);
std::lock_guard guard(lock);
std::ifstream in(path);
Project current;
while (in >> current.root_project >> current.configuration_file) {
projects_.push_back(current);
}
}
std::optional<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;
} else {
result = std::max(p, result.value());
}
};
for (auto& project : projects_) {
auto prefix_current_path = current_path;
if (project.root_project == "/") {
UpdateResult(project);
continue;
}
while (prefix_current_path != "/") {
if (prefix_current_path == project.root_project) {
UpdateResult(project);
}
prefix_current_path = prefix_current_path.parent_path();
}
}
return result;
}