Add change config file and run config file

This commit is contained in:
Timofey 2022-08-11 16:43:53 +03:00
parent 77a2ee9989
commit 51188e9f8e
9 changed files with 273 additions and 22 deletions

View file

@ -4,14 +4,72 @@
#include <fstream>
#include <mutex>
#include <random>
#include <chrono>
#include <iostream>
Config ProjectList::GetNewConfig(
const std::filesystem::path& config_directory) {
std::lock_guard guard(lock_file_);
void ProjectList::LoadFrom(const std::string& path) {
utils::filesystem::LockFile lock(path);
std::lock_guard guard(lock);
LoadWithouLock();
std::ifstream in(path);
std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count());
auto RandomSymbol = [&rnd]() {
int n = rnd() % (26 + 26 + 10);
if (n < 26) {
return 'a' + n;
} else if (n < 26 + 26) {
return 'A' + n - 26;
} else {
return '0' + n - 26 - 26;
}
};
auto GenerateFilename = [&rnd, &RandomSymbol]() {
std::string filename;
for (size_t i = 0; i < 6; ++i) {
filename += RandomSymbol();
}
return filename;
};
auto filename = GenerateFilename();
while (true) {
bool exist_config = false;
for (auto& project : projects_) {
if (filename == project.configuration_file.filename()) {
exist_config = true;
break;
}
}
if (!exist_config) {
break;
}
filename = GenerateFilename();
}
auto path_to_config = config_directory / filename;
std::ofstream out(path_, std::ios::ate);
out << std::filesystem::current_path() << " " << path_to_config << std::endl;
out.close();
projects_.emplace_back(std::filesystem::current_path(), path_to_config);
return {path_to_config};
}
void ProjectList::Load() {
std::lock_guard guard(lock_file_);
LoadWithouLock();
}
void ProjectList::LoadWithouLock() {
std::ifstream in(path_);
Project current;