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

@ -1,16 +1,25 @@
#include <clippy/clippy.hpp> #include <clippy/clippy.hpp>
#include <clippy/target.hpp>
#include <utils/parametres.hpp> #include <utils/parametres.hpp>
#include <iostream> #include <iostream>
#include <memory>
#include <vector> #include <vector>
#include <filesystem>
void Clippy::Run(const std::vector<std::string>& args) { void Clippy::Run(const std::vector<std::string>& args) {
TryExecuteClippyCommand(args); auto result = TryExecuteClippyCommand(args);
if (result) {
result->Execute();
return;
}
} }
bool Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) { Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
const std::vector<std::string>& args) {
using namespace utils::parametres; using namespace utils::parametres;
using namespace clippy::targets;
if (CheckPatternParametres(args, Parameter::Skip, "help", if (CheckPatternParametres(args, Parameter::Skip, "help",
Parameter::Anything)) { Parameter::Anything)) {
std::cout << "Hello I'm clippy" << std::endl; std::cout << "Hello I'm clippy" << std::endl;
@ -19,7 +28,21 @@ bool Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) {
std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " "; std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " ";
} }
std::cout << "}" << std::endl; std::cout << "}" << std::endl;
return std::make_unique<EmptyTarget>();
} }
return false; if (CheckPatternParametres(args, Parameter::Skip, "cfg",
Parameter::Anything)) {
projects_.LoadFrom("test");
auto p = projects_.GetCurrentProject();
if (p.has_value()) {
return std::make_unique<OpenProjectConfig>(p.value().configuration_file);
} else {
return std::make_unique<CreateProjectConfig>(projects_);
}
}
return nullptr;
} }

View file

@ -1,12 +1,19 @@
#pragma once #pragma once
#include <clippy/project_list.hpp>
#include <clippy/target.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory>
class Clippy { class Clippy {
public: public:
void Run(const std::vector<std::string>& args); void Run(const std::vector<std::string>& args);
using TargetPtr = std::unique_ptr<clippy::targets::Target>;
private: private:
bool TryExecuteClippyCommand(const std::vector<std::string>& args); TargetPtr TryExecuteClippyCommand(const std::vector<std::string>& args);
ProjectList projects_;
}; };

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;
}

View file

@ -0,0 +1,27 @@
#pragma once
#include <clippy/config.hpp>
#include <vector>
#include <filesystem>
#include <optional>
struct Project {
std::strong_ordering operator<=>(const Project&) const = default;
std::filesystem::path root_project;
std::filesystem::path configuration_file;
};
class ProjectList {
public:
ProjectList() {}
void LoadFrom(const std::string& path);
const std::vector<Project>& GetProjects() const { return projects_; }
std::optional<Project> GetCurrentProject();
private:
std::vector<Project> projects_;
};