53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
![]() |
#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;
|
||
|
}
|