move to json; add name for projects

This commit is contained in:
Timofey Khoruzhii 2022-12-28 14:58:14 +03:00
parent dbd34e0817
commit a2186366b8
6 changed files with 210 additions and 38 deletions

View file

@ -28,12 +28,10 @@ void Clippy::Run(const std::vector<std::string>& args) {
std::cout << "}" << rang::bg::reset << rang::style::reset << std::endl; std::cout << "}" << rang::bg::reset << rang::style::reset << std::endl;
} }
Clippy::TargetPtr Clippy::TryExecuteClippyCommand( Clippy::TargetPtr Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) {
const std::vector<std::string>& args) {
using namespace utils::parametres; using namespace utils::parametres;
using namespace clippy::targets; 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;
std::cout << "Parametres: { "; std::cout << "Parametres: { ";
for (size_t i = 0; i < args.size(); ++i) { for (size_t i = 0; i < args.size(); ++i) {
@ -44,8 +42,7 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
return std::make_unique<EmptyTarget>(); return std::make_unique<EmptyTarget>();
} }
if (CheckPatternParametres(args, Parameter::Skip, "cfg", if (CheckPatternParametres(args, Parameter::Skip, "cfg", Parameter::Anything)) {
Parameter::Anything)) {
LoadProjects(); LoadProjects();
auto p = projects_->GetCurrentProject(); auto p = projects_->GetCurrentProject();
@ -56,17 +53,42 @@ Clippy::TargetPtr Clippy::TryExecuteClippyCommand(
} }
} }
if (CheckPatternParametres(args, Parameter::Skip, "crt", if (CheckPatternParametres(args, Parameter::Skip, "crt", Parameter::Anything)) {
Parameter::Anything)) {
LoadProjects(); LoadProjects();
return std::make_unique<CreateProjectConfig>(projects_.value()); return std::make_unique<CreateProjectConfig>(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<PrintListProjects>(projects_.value());
}
if (CheckPatternParametres(args, Parameter::Skip, "setname", Parameter::Anything)) {
LoadProjects();
return std::make_unique<SetNameProject>(projects_.value(), args[2]);
}
return nullptr; return nullptr;
} }
Clippy::TargetPtr Clippy::GetScriptTarget( Clippy::TargetPtr Clippy::GetScriptTarget(const std::vector<std::string>& args) {
const std::vector<std::string>& args) {
LoadProjects(); LoadProjects();
auto p = projects_->GetCurrentProject(); auto p = projects_->GetCurrentProject();

View file

@ -1,17 +1,22 @@
#include <clippy/project_list.hpp> #include <clippy/project_list.hpp>
#include <filesystem> #include <filesystem>
#include <optional>
#include <sstream>
#include <utils/lock_file.hpp> #include <utils/lock_file.hpp>
#include <utils/filesystem.hpp>
#include <fstream> #include <fstream>
#include <mutex> #include <mutex>
#include <random> #include <random>
#include <chrono> #include <chrono>
Config ProjectList::GetNewConfig( #include <jsoncons/json.hpp>
const std::filesystem::path& config_directory) { #include <jsoncons_ext/cbor/cbor.hpp>
Config ProjectList::GetNewConfig(const std::filesystem::path& config_directory) {
std::lock_guard guard(lock_file_); std::lock_guard guard(lock_file_);
LoadWithouLock(); LoadWithoutLock();
std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count()); 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; 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); projects_.emplace_back(std::filesystem::current_path(), path_to_config);
SaveConfig();
return {path_to_config, std::filesystem::current_path()}; return {path_to_config, std::filesystem::current_path()};
} }
void ProjectList::Load() { void ProjectList::Load() {
std::lock_guard guard(lock_file_); std::lock_guard guard(lock_file_);
LoadWithouLock(); LoadWithoutLock();
} }
void ProjectList::LoadWithouLock() { void ProjectList::OldLoadConfig(const std::string& data) {
std::ifstream in(path_); std::stringstream in(data);
Project current; Project current;
@ -78,15 +80,77 @@ void ProjectList::LoadWithouLock() {
} }
} }
std::optional<Project> ProjectList::GetCurrentProject() { void ProjectList::SaveConfig() {
jsoncons::json result;
result["version"] = "0.1";
result["projects"] = std::vector<std::string>();
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<std::string>();
current.root_project = project["path_root_project"].as<std::string>();
if (project.contains("name")) {
current.name = project["name"].as<std::string>();
} 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(); auto current_path = std::filesystem::current_path();
std::optional<Project> result; Project* result = nullptr;
auto UpdateResult = [&result](Project p) { auto UpdateResult = [&result](Project& p) {
if (!result.has_value()) { if (!result) {
result = p; result = &p;
} else { } else {
result = std::max(p, result.value()); if (*result > p) {
result = &p;
}
} }
}; };

View file

@ -1,12 +1,15 @@
#pragma once #pragma once
#include <clippy/config.hpp> #include <clippy/config.hpp>
#include <stdexcept>
#include <utils/lock_file.hpp> #include <utils/lock_file.hpp>
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include <optional> #include <optional>
#include <jsoncons/json.hpp>
struct Project { struct Project {
Project() {} Project() {}
@ -15,10 +18,13 @@ struct Project {
std::strong_ordering operator<=>(const Project&) const = default; 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 root_project;
std::filesystem::path configuration_file; std::filesystem::path configuration_file;
std::optional<std::string> name;
}; };
class ProjectList { class ProjectList {
@ -29,13 +35,38 @@ class ProjectList {
Config GetNewConfig(const std::filesystem::path& config_directory); Config GetNewConfig(const std::filesystem::path& config_directory);
const std::vector<Project>& GetProjects() const { return projects_; } const std::vector<Project>& GetProjects() const {
return projects_;
}
std::optional<Project> GetCurrentProject(); std::optional<Project> 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: private:
Project* GetCurrentProject_();
void OldLoadConfig(const std::string&);
void SaveConfig();
void Load(); void Load();
void LoadWithouLock(); void LoadConfig(const jsoncons::json&);
void LoadWithoutLock();
private: private:
std::filesystem::path path_; std::filesystem::path path_;

View file

@ -32,7 +32,9 @@ class OpenProjectConfig : public Target {
public: public:
OpenProjectConfig(Config config) : config_(config) {} OpenProjectConfig(Config config) : config_(config) {}
void Execute() override { config_.Edit(); } void Execute() override {
config_.Edit();
}
~OpenProjectConfig() override {} ~OpenProjectConfig() override {}
private: private:
@ -76,15 +78,13 @@ class RunShellScript : public Target {
cppshell::Shell s("bash", tmp_path); cppshell::Shell s("bash", tmp_path);
for (auto& command : commands_) { for (auto& command : commands_) {
std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset std::cout << rang::fg::green << rang::style::bold << rang::bgB::blue << "->" << rang::fg::reset << rang::bg::reset
<< rang::bg::reset << " " << command << rang::style::reset << std::endl; << " " << command << rang::style::reset << std::endl;
s.Execute(command); s.Execute(command);
if (int err = s.GetExitCodeLastCommand(); err != 0) { if (int err = s.GetExitCodeLastCommand(); err != 0) {
std::cout << rang::fg::red << rang::style::bold std::cout << rang::fg::red << rang::style::bold << "Command exit with code " << err << rang::fg::reset
<< "Command exit with code " << err << rang::fg::reset
<< rang::style::reset << std::endl; << rang::style::reset << std::endl;
std::cout << rang::fg::blue << rang::style::bold std::cout << rang::fg::blue << rang::style::bold << "Continue execution? [Y/n] " << rang::fg::reset
<< "Continue execution? [Y/n] " << rang::fg::reset
<< rang::style::reset; << rang::style::reset;
std::string result; std::string result;
@ -103,4 +103,33 @@ class RunShellScript : public Target {
private: private:
std::deque<std::string> commands_; std::deque<std::string> 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 } // namespace clippy::targets

View file

@ -6,6 +6,10 @@
#include <csignal> #include <csignal>
#include <utils/filesystem.hpp>
#include <jsoncons/json.hpp>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::signal(SIGPIPE, SIG_IGN); std::signal(SIGPIPE, SIG_IGN);

22
src/utils/filesystem.hpp Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <string>
#include <filesystem>
#include <fstream>
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