From a4229edd6fdba03785f9b7542f731498a2680dc1 Mon Sep 17 00:00:00 2001 From: Timofey Date: Wed, 10 Aug 2022 21:52:07 +0300 Subject: [PATCH] Add read project lists --- src/clippy/clippy.cpp | 29 +++++++++++++++++-- src/clippy/clippy.hpp | 9 +++++- src/clippy/project_list.cpp | 57 +++++++++++++++++++++++++++++++++++++ src/clippy/project_list.hpp | 27 ++++++++++++++++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 src/clippy/project_list.cpp create mode 100644 src/clippy/project_list.hpp diff --git a/src/clippy/clippy.cpp b/src/clippy/clippy.cpp index c17e7d4..6854848 100644 --- a/src/clippy/clippy.cpp +++ b/src/clippy/clippy.cpp @@ -1,16 +1,25 @@ #include +#include #include #include +#include #include +#include void Clippy::Run(const std::vector& args) { - TryExecuteClippyCommand(args); + auto result = TryExecuteClippyCommand(args); + if (result) { + result->Execute(); + return; + } } -bool Clippy::TryExecuteClippyCommand(const std::vector& args) { +Clippy::TargetPtr Clippy::TryExecuteClippyCommand( + const std::vector& args) { using namespace utils::parametres; + using namespace clippy::targets; if (CheckPatternParametres(args, Parameter::Skip, "help", Parameter::Anything)) { std::cout << "Hello I'm clippy" << std::endl; @@ -19,7 +28,21 @@ bool Clippy::TryExecuteClippyCommand(const std::vector& args) { std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " "; } std::cout << "}" << std::endl; + + return std::make_unique(); } - 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(p.value().configuration_file); + } else { + return std::make_unique(projects_); + } + } + + return nullptr; } diff --git a/src/clippy/clippy.hpp b/src/clippy/clippy.hpp index a074c29..a944267 100644 --- a/src/clippy/clippy.hpp +++ b/src/clippy/clippy.hpp @@ -1,12 +1,19 @@ #pragma once +#include +#include + #include #include +#include class Clippy { public: void Run(const std::vector& args); + using TargetPtr = std::unique_ptr; private: - bool TryExecuteClippyCommand(const std::vector& args); + TargetPtr TryExecuteClippyCommand(const std::vector& args); + + ProjectList projects_; }; diff --git a/src/clippy/project_list.cpp b/src/clippy/project_list.cpp new file mode 100644 index 0000000..7b29f75 --- /dev/null +++ b/src/clippy/project_list.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +#include +#include + + + + + + +#include + +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 ProjectList::GetCurrentProject() { + auto current_path = std::filesystem::current_path(); + + std::optional 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; +} diff --git a/src/clippy/project_list.hpp b/src/clippy/project_list.hpp new file mode 100644 index 0000000..be5d369 --- /dev/null +++ b/src/clippy/project_list.hpp @@ -0,0 +1,27 @@ +#pragma once +#include + +#include +#include +#include + +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& GetProjects() const { return projects_; } + + std::optional GetCurrentProject(); + + private: + std::vector projects_; +};