Add read project lists
This commit is contained in:
parent
fe9e887061
commit
a4229edd6f
|
@ -1,16 +1,25 @@
|
|||
#include <clippy/clippy.hpp>
|
||||
#include <clippy/target.hpp>
|
||||
|
||||
#include <utils/parametres.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
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 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<std::string>& args) {
|
|||
std::cout << args[i] << (i + 1 == args.size() ? "" : ",") << " ";
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <clippy/project_list.hpp>
|
||||
#include <clippy/target.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class Clippy {
|
||||
public:
|
||||
void Run(const std::vector<std::string>& args);
|
||||
|
||||
using TargetPtr = std::unique_ptr<clippy::targets::Target>;
|
||||
private:
|
||||
bool TryExecuteClippyCommand(const std::vector<std::string>& args);
|
||||
TargetPtr TryExecuteClippyCommand(const std::vector<std::string>& args);
|
||||
|
||||
ProjectList projects_;
|
||||
};
|
||||
|
|
57
src/clippy/project_list.cpp
Normal file
57
src/clippy/project_list.cpp
Normal 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;
|
||||
}
|
27
src/clippy/project_list.hpp
Normal file
27
src/clippy/project_list.hpp
Normal 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_;
|
||||
};
|
Loading…
Reference in a new issue