diff --git a/src/clippy/clippy.cpp b/src/clippy/clippy.cpp index 9bd4fb2..d67f08b 100644 --- a/src/clippy/clippy.cpp +++ b/src/clippy/clippy.cpp @@ -28,12 +28,10 @@ void Clippy::Run(const std::vector& args) { std::cout << "}" << rang::bg::reset << rang::style::reset << std::endl; } -Clippy::TargetPtr 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)) { + if (CheckPatternParametres(args, Parameter::Skip, "help", Parameter::Anything)) { std::cout << "Hello I'm clippy" << std::endl; std::cout << "Parametres: { "; for (size_t i = 0; i < args.size(); ++i) { @@ -44,8 +42,7 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand( return std::make_unique(); } - if (CheckPatternParametres(args, Parameter::Skip, "cfg", - Parameter::Anything)) { + if (CheckPatternParametres(args, Parameter::Skip, "cfg", Parameter::Anything)) { LoadProjects(); auto p = projects_->GetCurrentProject(); @@ -56,17 +53,42 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand( } } - if (CheckPatternParametres(args, Parameter::Skip, "crt", - Parameter::Anything)) { + if (CheckPatternParametres(args, Parameter::Skip, "crt", Parameter::Anything)) { LoadProjects(); return std::make_unique(projects_.value()); } + if (CheckPatternParametres(args, Parameter::Skip, "prj", Parameter::Anything)) { + if (args.size() != 3) { + return nullptr; + } + std::string name_project = args[2]; + + LoadProjects(); + + for (auto& project : projects_->GetProjects()) { + if (project.name == name_project) { + std::cout << "Find: " << project.root_project << " " << project.configuration_file << std::endl; + } + } + } + + if (CheckPatternParametres(args, Parameter::Skip, "list", Parameter::Nothing)) { + LoadProjects(); + + return std::make_unique(projects_.value()); + } + + if (CheckPatternParametres(args, Parameter::Skip, "setname", Parameter::Anything)) { + LoadProjects(); + + return std::make_unique(projects_.value(), args[2]); + } + return nullptr; } -Clippy::TargetPtr Clippy::GetScriptTarget( - const std::vector& args) { +Clippy::TargetPtr Clippy::GetScriptTarget(const std::vector& args) { LoadProjects(); auto p = projects_->GetCurrentProject(); diff --git a/src/clippy/project_list.cpp b/src/clippy/project_list.cpp index e2ad9f3..f8f6c3a 100644 --- a/src/clippy/project_list.cpp +++ b/src/clippy/project_list.cpp @@ -1,17 +1,22 @@ #include #include +#include +#include #include +#include #include #include #include #include -Config ProjectList::GetNewConfig( - const std::filesystem::path& config_directory) { +#include +#include + +Config ProjectList::GetNewConfig(const std::filesystem::path& config_directory) { std::lock_guard guard(lock_file_); - LoadWithouLock(); + LoadWithoutLock(); std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count()); @@ -52,24 +57,21 @@ Config ProjectList::GetNewConfig( } auto path_to_config = config_directory / filename; - - std::ofstream out(path_, std::ios::app); - out << std::filesystem::current_path() << " " << path_to_config << std::endl; - out.close(); - projects_.emplace_back(std::filesystem::current_path(), path_to_config); + SaveConfig(); + return {path_to_config, std::filesystem::current_path()}; } void ProjectList::Load() { std::lock_guard guard(lock_file_); - LoadWithouLock(); + LoadWithoutLock(); } -void ProjectList::LoadWithouLock() { - std::ifstream in(path_); +void ProjectList::OldLoadConfig(const std::string& data) { + std::stringstream in(data); Project current; @@ -78,15 +80,77 @@ void ProjectList::LoadWithouLock() { } } -std::optional ProjectList::GetCurrentProject() { +void ProjectList::SaveConfig() { + jsoncons::json result; + + result["version"] = "0.1"; + result["projects"] = std::vector(); + + for (auto& project : projects_) { + jsoncons::json current; + current["path_to_config"] = std::string(project.configuration_file); + current["path_root_project"] = std::string(project.root_project); + + std::cout << (bool)project.name << std::endl; + if (project.name && !project.name.value().empty()) { + current["name"] = project.name.value(); + } + + result["projects"].emplace_back(current); + } + + std::ofstream out(path_); + out << result.to_string(); +} + +void ProjectList::LoadConfig(const jsoncons::json& data) { + if (data["version"] != "0.1") { + throw std::logic_error("unsupported version config"); + } + + Project current; + for (auto& project : data["projects"].array_range()) { + current.configuration_file = project["path_to_config"].as(); + current.root_project = project["path_root_project"].as(); + if (project.contains("name")) { + current.name = project["name"].as(); + } else { + current.name = std::nullopt; + } + + projects_.emplace_back(current); + } +} + +void ProjectList::LoadWithoutLock() { + auto data = utils::LoadFile(path_); + + try { + LoadConfig(jsoncons::json::parse(data)); + } catch (...) { + std::cout << "I can't read project lists. Try fix it?"; + + std::string result; + std::getline(std::cin, result); + + if (result == "y") { + OldLoadConfig(data); + SaveConfig(); + } + } +} + +Project* ProjectList::GetCurrentProject_() { auto current_path = std::filesystem::current_path(); - std::optional result; - auto UpdateResult = [&result](Project p) { - if (!result.has_value()) { - result = p; + Project* result = nullptr; + auto UpdateResult = [&result](Project& p) { + if (!result) { + result = &p; } else { - result = std::max(p, result.value()); + if (*result > p) { + result = &p; + } } }; diff --git a/src/clippy/project_list.hpp b/src/clippy/project_list.hpp index 5cefefe..bf6094f 100644 --- a/src/clippy/project_list.hpp +++ b/src/clippy/project_list.hpp @@ -1,12 +1,15 @@ #pragma once #include +#include #include #include #include #include +#include + struct Project { Project() {} @@ -15,10 +18,13 @@ struct Project { std::strong_ordering operator<=>(const Project&) const = default; - Config GetConfig() { return Config{configuration_file, root_project}; } + Config GetConfig() { + return Config{configuration_file, root_project}; + } std::filesystem::path root_project; std::filesystem::path configuration_file; + std::optional name; }; class ProjectList { @@ -29,13 +35,38 @@ class ProjectList { Config GetNewConfig(const std::filesystem::path& config_directory); - const std::vector& GetProjects() const { return projects_; } + const std::vector& GetProjects() const { + return projects_; + } - std::optional GetCurrentProject(); + std::optional GetCurrentProject() { + auto* p = GetCurrentProject_(); + if (p) { + return *p; + } else { + return std::nullopt; + } + } + + void SetNameCurrentProject(const std::string& name) { + auto* p = GetCurrentProject_(); + if (p) { + p->name = name; + SaveConfig(); + } else { + throw std::logic_error("Not exists current project"); + } + } private: + Project* GetCurrentProject_(); + + void OldLoadConfig(const std::string&); + void SaveConfig(); + void Load(); - void LoadWithouLock(); + void LoadConfig(const jsoncons::json&); + void LoadWithoutLock(); private: std::filesystem::path path_; diff --git a/src/clippy/target.hpp b/src/clippy/target.hpp index 996b03f..a8a46fa 100644 --- a/src/clippy/target.hpp +++ b/src/clippy/target.hpp @@ -32,7 +32,9 @@ class OpenProjectConfig : public Target { public: OpenProjectConfig(Config config) : config_(config) {} - void Execute() override { config_.Edit(); } + void Execute() override { + config_.Edit(); + } ~OpenProjectConfig() override {} private: @@ -76,15 +78,13 @@ class RunShellScript : public Target { cppshell::Shell s("bash", tmp_path); for (auto& command : commands_) { - std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset - << rang::bg::reset << " " << command << rang::style::reset << std::endl; + std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset << rang::bg::reset + << " " << command << rang::style::reset << std::endl; s.Execute(command); if (int err = s.GetExitCodeLastCommand(); err != 0) { - std::cout << rang::fg::red << rang::style::bold - << "Command exit with code " << err << rang::fg::reset + std::cout << rang::fg::red << rang::style::bold << "Command exit with code " << err << rang::fg::reset << rang::style::reset << std::endl; - std::cout << rang::fg::blue << rang::style::bold - << "Continue execution? [Y/n] " << rang::fg::reset + std::cout << rang::fg::blue << rang::style::bold << "Continue execution? [Y/n] " << rang::fg::reset << rang::style::reset; std::string result; @@ -103,4 +103,33 @@ class RunShellScript : public Target { private: std::deque commands_; }; + +class PrintListProjects : public Target { + public: + PrintListProjects(ProjectList& projects) : projects_(projects) {} + + void Execute() override { + for (auto& project : projects_.GetProjects()) { + std::cout << std::setw(10) << project.name.value_or("-") << " " << std::setw(50) << project.root_project << " " + << std::setw(70) << project.configuration_file << std::endl; + } + } + + private: + ProjectList& projects_; +}; + +class SetNameProject : public Target { + public: + SetNameProject(ProjectList& projects, std::string new_name) + : projects_(projects), new_name_(std::move(new_name)) {} + + void Execute() override { + projects_.SetNameCurrentProject(new_name_); + } + + private: + ProjectList& projects_; + std::string new_name_; +}; } // namespace clippy::targets diff --git a/src/main.cpp b/src/main.cpp index 267527e..239901f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,10 @@ #include +#include + +#include + int main(int argc, char* argv[]) { std::signal(SIGPIPE, SIG_IGN); diff --git a/src/utils/filesystem.hpp b/src/utils/filesystem.hpp new file mode 100644 index 0000000..e65acd2 --- /dev/null +++ b/src/utils/filesystem.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +namespace utils { +inline std::string LoadFile(const std::filesystem::path& file) { + std::ifstream in(file); + + std::string result; + std::string tmp; + + while (std::getline(in, tmp)) { + result += tmp; + result += "\n"; + } + + return result; +} + +} // namespace utils