Skip to content

Commit 66c9351

Browse files
committed
cufetchpm: some WIP
1 parent fffa32d commit 66c9351

File tree

12 files changed

+250
-28
lines changed

12 files changed

+250
-28
lines changed

cufetchpm/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ NAME = cufetchpm
2424
TARGET ?= $(NAME)
2525
OLDVERSION = 0.0.0
2626
VERSION = 0.0.1
27-
SRC = $(wildcard src/*.cpp ../src/util.cpp)
27+
SRC = $(wildcard src/*.cpp src/manager/*.cpp ../src/util.cpp)
2828
OBJ = $(SRC:.cpp=.o)
2929
LDFLAGS += -L./$(BUILDDIR)/fmt -lfmt
3030
CXXFLAGS ?= -mtune=generic -march=native
31-
CXXFLAGS += -fvisibility=hidden -I../include -std=c++20 $(VARS) -DVERSION=\"$(VERSION)\"
31+
CXXFLAGS += -fvisibility=hidden -I../include -Iinclude -std=c++20 $(VARS) -DVERSION=\"$(VERSION)\"
3232

3333
all: fmt toml $(TARGET)
3434

cufetchpm/compile_flags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-I../include
2+
-Iinclude
23
-Wall
34
-Wextra
45
-Wpedantic

cufetchpm/include/manifest.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <optional>
2+
#include <string>
3+
#include <string_view>
4+
#include <utility>
5+
#include <vector>
6+
#include "libcufetch/common.hh"
7+
#include "toml++/toml.hpp"
8+
9+
struct manifest_t
10+
{
11+
std::string name;
12+
std::string license;
13+
std::string description;
14+
std::string output_dir;
15+
std::vector<std::string> authors;
16+
std::vector<std::string> build;
17+
};
18+
19+
const char* const MANIFEST_NAME = "cufetchpm.toml";
20+
21+
class CManifest {
22+
public:
23+
CManifest(const std::string_view path);
24+
CManifest(toml::table&& tbl) : m_tbl(tbl){}
25+
CManifest(const toml::table& tbl) : m_tbl(std::move(tbl)){}
26+
CManifest &operator=(CManifest &&) = default;
27+
CManifest &operator=(const CManifest &) = default;
28+
~CManifest() = default;
29+
30+
manifest_t get_plugin(const std::string_view name);
31+
private:
32+
toml::table m_tbl;
33+
bool m_is_state = true;
34+
35+
template <typename T>
36+
T getValue(const std::string_view name, const std::string_view value) const
37+
{
38+
const std::optional<T>& ret = m_tbl[name][value].value<T>();
39+
return ret.value_or(UNKNOWN);
40+
}
41+
42+
std::vector<std::string> getValueArrayStr(const std::string_view name, const std::string_view value) const
43+
{
44+
std::vector<std::string> ret;
45+
46+
// https://stackoverflow.com/a/78266628
47+
if (const toml::array* array_it = m_tbl[name][value].as_array())
48+
{
49+
array_it->for_each([&ret](auto&& el) {
50+
if (const toml::value<std::string>* str_elem = el.as_string())
51+
ret.push_back((*str_elem)->data());
52+
});
53+
54+
return ret;
55+
}
56+
return {};
57+
}
58+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <array>
2+
#include <filesystem>
3+
#include <string>
4+
#include <string_view>
5+
#include <utility>
6+
7+
#include "stateManager.hpp"
8+
#include "toml++/toml.hpp"
9+
#include "util.hpp"
10+
11+
constexpr std::array<std::string_view, 1> dependencies = {"git"};
12+
13+
class PluginManager
14+
{
15+
public:
16+
PluginManager(const StateManager& state) : m_state(state){}
17+
PluginManager(StateManager&& state) : m_state(std::move(state)){}
18+
19+
void create_cache_dir();
20+
void add_repo_plugins(const std::string& repo);
21+
bool add_plugin(const std::string&);
22+
bool has_deps();
23+
24+
private:
25+
const StateManager& m_state;
26+
const std::filesystem::path m_cache_path{getHomeCacheDir()/"cufetchpm"/"plugins"};
27+
toml::table m_manifest;
28+
};

cufetchpm/include/stateManager.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <filesystem>
2+
3+
#include "toml++/toml.hpp"
4+
#include "util.hpp"
5+
6+
class StateManager
7+
{
8+
public:
9+
StateManager();
10+
StateManager(StateManager&&) = default;
11+
StateManager(const StateManager&) = default;
12+
~StateManager() = default;
13+
14+
toml::table get_state() { return m_state; }
15+
16+
private:
17+
const std::filesystem::path m_path{getConfigDir()/"plugins"/"state.toml"};
18+
toml::table m_state;
19+
};

cufetchpm/src/add.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

cufetchpm/src/main.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#include <cstring>
22
#include <string_view>
33

4-
#include "libcufetch/common.hpp"
54
#include "fmt/base.h"
5+
#include "libcufetch/common.hh"
66

77
bool download_git(const std::string_view url);
8-
9-
int main (int argc, char *argv[])
10-
{
11-
if (argc > 1 && strcmp(argv[1], "add") == 0)
12-
{
13-
if (argc == 2)
14-
die("please insert a git url to clone");
15-
16-
if (download_git(argv[2]))
17-
info("plugin downloaded successfully");
18-
}
19-
else
20-
{
21-
fmt::println("usage: cufetchpm add <url>");
22-
}
23-
return 0;
24-
}

cufetchpm/src/manifest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "manifest.hpp"
2+
3+
#include "libcufetch/common.hh"
4+
#include "util.hpp"
5+
6+
CManifest::CManifest(const std::string_view path) : m_is_state(false)
7+
{
8+
try
9+
{
10+
this->m_tbl = toml::parse_file(path);
11+
}
12+
catch (const toml::parse_error& err)
13+
{
14+
die(_("Failed to parse state file at '{}':\n"
15+
"{}\n"
16+
"\t(error occurred at line {} column {})"),
17+
path, err.description(),
18+
err.source().begin.line, err.source().begin.column);
19+
}
20+
}
21+
22+
manifest_t CManifest::get_plugin(const std::string_view name)
23+
{
24+
if (!m_tbl[name].is_table())
25+
die("Couldn't find such plugin '{}' in manifest", name);
26+
27+
return {
28+
name.data(),
29+
getValue<std::string>(name, "license"),
30+
getValue<std::string>(name, "description"),
31+
getValue<std::string>(name, "output-dir"),
32+
getValueArrayStr(name, "authors"),
33+
getValueArrayStr(name, "build"),
34+
};
35+
}

cufetchpm/src/pluginManager.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "pluginManager.hpp"
2+
#include <cstdio>
3+
#include <filesystem>
4+
#include <string>
5+
#include <string_view>
6+
#include <vector>
7+
#include "libcufetch/common.hh"
8+
#include "libcufetch/fmt/ranges.h"
9+
#include "util.hpp"
10+
11+
bool PluginManager::has_deps()
12+
{
13+
for (const std::string_view bin : dependencies) // expand in the future
14+
{
15+
if (which(bin) == UNKNOWN)
16+
return false;
17+
}
18+
19+
return true;
20+
}
21+
22+
void PluginManager::create_cache_dir()
23+
{
24+
if (!std::filesystem::exists(m_cache_path))
25+
std::filesystem::create_directories(m_cache_path);
26+
}
27+
28+
void PluginManager::add_repo_plugins(const std::string& repo)
29+
{
30+
if (!has_deps())
31+
die("Not all dependencies have been installed. You'll need to install {}", fmt::join(dependencies, ", "));
32+
33+
const std::filesystem::path& working_dir = (m_cache_path/std::tmpnam(nullptr));
34+
std::filesystem::create_directories(working_dir);
35+
36+
if (!taur_exec({"git", "clone", "--recursive", repo, working_dir.string()}, false))
37+
die("Failed to clone at directory '{}'", working_dir.string());
38+
}

cufetchpm/src/stateManager.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <filesystem>
2+
3+
#include "fmt/os.h"
4+
#include "libcufetch/common.hh"
5+
#include "stateManager.hpp"
6+
7+
StateManager::StateManager()
8+
{
9+
if (!std::filesystem::exists(m_path))
10+
{
11+
auto f = fmt::output_file(m_path.string(), fmt::file::WRONLY | fmt::file::TRUNC | fmt::file::CREATE);
12+
f.print(R"(
13+
# AUTOGENERATED FILE. DO NOT EDIT THIS FILE.
14+
# YOU GONNA MESS SHIT UP. unless you know what you doing ofc
15+
)");
16+
f.close();
17+
}
18+
19+
try
20+
{
21+
if (m_state.empty())
22+
m_state = toml::parse_file(m_path.string());
23+
}
24+
catch (const toml::parse_error& err)
25+
{
26+
die(_("Failed to parse state file at '{}':\n"
27+
"{}\n"
28+
"\t(error occurred at line {} column {})"),
29+
m_path.string(), err.description(),
30+
err.source().begin.line, err.source().begin.column);
31+
}
32+
}

0 commit comments

Comments
 (0)