Skip to content

Commit 946a190

Browse files
committed
cufetchpm: actually build projects and add to state.toml + (almost)final version of the manifest
1 parent 17db8ca commit 946a190

9 files changed

Lines changed: 143 additions & 71 deletions

File tree

cufetchpm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SRC = $(wildcard src/*.cpp src/manager/*.cpp ../src/util.cpp)
2828
OBJ = $(SRC:.cpp=.o)
2929
LDLIBS += $(BUILDDIR)/libfmt.a $(BUILDDIR)/libtiny-process-library.a
3030
CXXFLAGS ?= -mtune=generic -march=native
31-
CXXFLAGS += -fvisibility=hidden -I../include -I../src/libs/ -Iinclude -std=c++20 $(VARS) -DVERSION=\"$(VERSION)\"
31+
CXXFLAGS += -fvisibility=hidden -I../include -I../include/libs/ -Iinclude -std=c++20 $(VARS) -DVERSION=\"$(VERSION)\"
3232

3333
all: fmt toml tpl $(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+
-I../include/libs
23
-I../src/libs/
34
-Iinclude
45
-Wall

cufetchpm/include/manifest.hpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,57 @@
66
#include <string_view>
77
#include <utility>
88
#include <vector>
9+
910
#include "libcufetch/common.hh"
1011
#include "toml++/toml.hpp"
1112

12-
struct manifest_t
13+
struct plugin_t
1314
{
15+
// The plugin name.
1416
std::string name;
15-
std::string license;
17+
18+
// The plugin description.
1619
std::string description;
20+
21+
// The plugin build directory,
22+
// where we'll retrive the built plugin
1723
std::string output_dir;
24+
25+
// The plugin multiple SPDX License Identifier (MIT, GPL-2.0, ...)
26+
// NOTE: it doesn't actually check if they are correct or not.
27+
std::vector<std::string> licenses;
28+
29+
// Which plugins can be conflicting by name / modules.
30+
// TODO: choose if check either name or git url.
31+
std::vector<std::string> conflicts;
32+
33+
// The plugin authors.
1834
std::vector<std::string> authors;
35+
36+
// A list of commands to be executed for building the plugin.
37+
// Kinda like a Makefile target instructions.
38+
// Each command will be executed from a different shell session.
1939
std::vector<std::string> build_steps;
40+
41+
// A list of root modules that the plugin will be used for querying its modules
42+
std::vector<std::string> prefixes;
2043
};
2144

2245
const char* const MANIFEST_NAME = "cufetchpm.toml";
2346

24-
class CManifest {
47+
class CManifest
48+
{
2549
public:
2650
CManifest(const std::string_view path);
27-
CManifest(toml::table&& tbl) : m_tbl(tbl){}
28-
CManifest(const toml::table& tbl) : m_tbl(std::move(tbl)){}
29-
CManifest &operator=(CManifest &&) = default;
30-
CManifest &operator=(const CManifest &) = default;
31-
~CManifest() = default;
51+
CManifest(toml::table&& tbl) : m_tbl(std::move(tbl)) {}
52+
CManifest(const toml::table& tbl) : m_tbl(tbl) {}
3253

33-
manifest_t get_plugin(const std::string_view name);
34-
std::vector<manifest_t> get_all_plugins();
54+
plugin_t get_plugin(const std::string_view name);
55+
std::vector<plugin_t> get_all_plugins();
3556

3657
private:
3758
toml::table m_tbl;
38-
bool m_is_state = true;
59+
bool m_is_state = true;
3960

4061
template <typename T>
4162
T getValue(const std::string_view name, const std::string_view value) const
@@ -62,4 +83,4 @@ class CManifest {
6283
}
6384
};
6485

65-
#endif // !_MANIFEST_HPP_;
86+
#endif // !_MANIFEST_HPP_;

cufetchpm/include/pluginManager.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,40 @@
1111
#include "toml++/toml.hpp"
1212
#include "util.hpp"
1313

14-
constexpr std::array<std::string_view, 1> dependencies = {"git"}; // expand in the future, maybe
14+
constexpr std::array<std::string_view, 1> dependencies = { "git" }; // expand in the future, maybe
1515

1616
#define BOLD_COLOR(x) (fmt::emphasis::bold | fmt::fg(fmt::rgb(x)))
1717

1818
template <typename... Args>
1919
void success(const std::string_view fmt, Args&&... args) noexcept
2020
{
2121
fmt::print(BOLD_COLOR((fmt::color::green)), "SUCCESS:\033[0m {}\n",
22-
fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...));
22+
fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...));
2323
}
2424

2525
template <typename... Args>
2626
void status(const std::string_view fmt, Args&&... args) noexcept
2727
{
2828
fmt::print(BOLD_COLOR((fmt::color::cadet_blue)), "status:\033[0m {} ...\n",
29-
fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...));
29+
fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...));
3030
}
3131

3232
#undef BOLD_COLOR
3333

3434
class PluginManager
3535
{
3636
public:
37-
PluginManager(const StateManager& state) : m_state(state){}
38-
PluginManager(StateManager&& state) : m_state(std::move(state)){}
37+
PluginManager(const StateManager& state) : m_state(state) {}
38+
PluginManager(StateManager&& state) : m_state(std::move(state)) {}
3939

4040
void add_repo_plugins(const std::string& repo);
4141
bool add_plugin(const std::string&);
4242
bool has_deps();
4343

4444
private:
45-
const StateManager& m_state;
46-
const std::filesystem::path m_cache_path{getHomeCacheDir()/"cufetchpm"/"plugins"};
47-
toml::table m_manifest;
45+
StateManager m_state;
46+
const std::filesystem::path m_cache_path{ getHomeCacheDir() / "cufetchpm" / "plugins" };
47+
toml::table m_manifest;
4848
};
4949

50-
5150
#endif

cufetchpm/include/stateManager.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <filesystem>
55

6+
#include "manifest.hpp"
67
#include "toml++/toml.hpp"
78
#include "util.hpp"
89

@@ -14,10 +15,11 @@ class StateManager
1415
StateManager(const StateManager&) = default;
1516
~StateManager() = default;
1617

18+
void add_new_plugin(const plugin_t& manifest);
1719
toml::table get_state() { return m_state; }
1820

1921
private:
20-
const std::filesystem::path m_path{getHomeCacheDir()/"cufetchpm"/"state.toml"};
22+
const std::filesystem::path m_path{ getHomeCacheDir() / "cufetchpm" / "state.toml" };
2123
toml::table m_state;
2224
};
2325

cufetchpm/src/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "stateManager.hpp"
21
#include "pluginManager.hpp"
2+
#include "stateManager.hpp"
33

4-
int main (int argc, char *argv[])
4+
int main(int argc, char* argv[])
55
{
6-
std::filesystem::create_directories({getHomeCacheDir()/"cufetchpm"/"plugins"});
7-
StateManager state;
8-
PluginManager man(state);
6+
std::filesystem::create_directories({ getHomeCacheDir() / "cufetchpm" / "plugins" });
7+
StateManager state;
8+
PluginManager man(std::move(state));
99
man.add_repo_plugins(argv[2]);
1010
return 0;
1111
}

cufetchpm/src/manifest.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#include "manifest.hpp"
2+
23
#include <algorithm>
34
#include <cctype>
45
#include <vector>
56

67
#include "libcufetch/common.hh"
78
#include "util.hpp"
89

9-
static bool validManifestName(const std::string_view n) {
10-
return std::ranges::all_of(n, [](const unsigned char c) { return (isalnum(c) || c == '-' || c == '_' || c == '='); });
10+
static bool validManifestName(const std::string_view n)
11+
{
12+
return std::ranges::all_of(n,
13+
[](const unsigned char c) { return (isalnum(c) || c == '-' || c == '_' || c == '='); });
1114
}
1215

1316
CManifest::CManifest(const std::string_view path) : m_is_state(false)
@@ -21,48 +24,48 @@ CManifest::CManifest(const std::string_view path) : m_is_state(false)
2124
die(_("Failed to parse state file at '{}':\n"
2225
"{}\n"
2326
"\t(error occurred at line {} column {})"),
24-
path, err.description(),
25-
err.source().begin.line, err.source().begin.column);
27+
path, err.description(), err.source().begin.line, err.source().begin.column);
2628
}
2729
}
2830

29-
std::vector<manifest_t> CManifest::get_all_plugins()
31+
std::vector<plugin_t> CManifest::get_all_plugins()
3032
{
31-
std::vector<manifest_t> plugins;
33+
std::vector<plugin_t> plugins;
3234
for (auto const& [name, _] : m_tbl)
3335
{
3436
if (name.str() == "repository")
3537
continue;
3638

3739
if (!validManifestName(name.str()))
3840
{
39-
warn("Plugin '{}' has an invalid name. Only alphanumeric and '-', '_', '=' are allowed in the name", name.str());
41+
warn("Plugin '{}' has an invalid name. Only alphanumeric and '-', '_', '=' are allowed in the name",
42+
name.str());
4043
continue;
4144
}
4245

43-
plugins.push_back({
44-
name.data(),
45-
getValue<std::string>(name, "license"),
46-
getValue<std::string>(name, "description"),
47-
getValue<std::string>(name, "output-dir"),
48-
getValueArrayStr(name, "authors"),
49-
getValueArrayStr(name, "build-steps"),
50-
});
46+
plugins.push_back({ .name = name.data(),
47+
.description = getValue<std::string>(name, "description"),
48+
.output_dir = getValue<std::string>(name, "license"),
49+
.licenses = getValueArrayStr(name, "licenses"),
50+
.conflicts = getValueArrayStr(name, "conflicts"),
51+
.authors = getValueArrayStr(name, "authors"),
52+
.build_steps = getValueArrayStr(name, "build-steps"),
53+
.prefixes = getValueArrayStr(name, "prefixes") });
5154
}
5255
return plugins;
5356
}
5457

55-
manifest_t CManifest::get_plugin(const std::string_view name)
58+
plugin_t CManifest::get_plugin(const std::string_view name)
5659
{
5760
if (!m_tbl[name].is_table())
5861
die("Couldn't find such plugin '{}' in manifest", name);
5962

60-
return {
61-
name.data(),
62-
getValue<std::string>(name, "license"),
63-
getValue<std::string>(name, "description"),
64-
getValue<std::string>(name, "output-dir"),
65-
getValueArrayStr(name, "authors"),
66-
getValueArrayStr(name, "build-steps"),
67-
};
63+
return { .name = name.data(),
64+
.description = getValue<std::string>(name, "description"),
65+
.output_dir = getValue<std::string>(name, "license"),
66+
.licenses = getValueArrayStr(name, "licenses"),
67+
.conflicts = getValueArrayStr(name, "conflicts"),
68+
.authors = getValueArrayStr(name, "authors"),
69+
.build_steps = getValueArrayStr(name, "build-steps"),
70+
.prefixes = getValueArrayStr(name, "prefixes") };
6871
}

cufetchpm/src/pluginManager.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "pluginManager.hpp"
2+
23
#include <cstdio>
34
#include <cstdlib>
45
#include <filesystem>
@@ -7,25 +8,21 @@
78
#include <string_view>
89
#include <vector>
910

10-
#include "tiny-process-library/process.hpp"
1111
#include "fmt/format.h"
12-
#include "manifest.hpp"
1312
#include "libcufetch/common.hh"
14-
//#include "fmt/ranges.h"
13+
#include "manifest.hpp"
14+
#include "tiny-process-library/process.hpp"
15+
// #include "fmt/ranges.h"
1516

1617
using namespace TinyProcessLib;
1718

18-
std::random_device rd;
19-
std::mt19937 gen(rd());
20-
std::uniform_int_distribution<> dist(0, 999999);
21-
2219
bool PluginManager::has_deps()
2320
{
2421
for (const std::string_view bin : dependencies)
2522
{
2623
Process proc(fmt::format("command -v {}", bin),
2724
"",
28-
[](const char*, size_t) {}, // discard stdout
25+
[](const char*, size_t) {}, // discard stdout
2926
[](const char*, size_t) {}); // discard stderr
3027
if (proc.get_exit_status() != 0)
3128
return false;
@@ -37,7 +34,7 @@ bool PluginManager::has_deps()
3734
void PluginManager::add_repo_plugins(const std::string& repo)
3835
{
3936
if (!has_deps())
40-
die("Not all dependencies have been installed. You'll need to install git"); // fmt::join(dependencies, ", "));
37+
die("Not all dependencies have been installed. You'll need to install git"); // fmt::join(dependencies, ", "));
4138

4239
std::string repo_name;
4340
{
@@ -55,11 +52,15 @@ void PluginManager::add_repo_plugins(const std::string& repo)
5552
return;
5653
}
5754

55+
static std::random_device rd;
56+
static std::mt19937 gen(rd());
57+
static std::uniform_int_distribution<> dist(100000, 999999);
58+
5859
const std::filesystem::path& working_dir = m_cache_path / ("plugin_" + std::to_string(dist(gen)));
5960
std::filesystem::create_directories(working_dir);
6061

6162
status("Cloning repository '{}' at '{}'", repo, working_dir.string());
62-
if (Process({"git", "clone", "--recursive", repo, working_dir.string()}).get_exit_status() != 0)
63+
if (Process({ "git", "clone", "--recursive", repo, working_dir.string() }).get_exit_status() != 0)
6364
{
6465
std::filesystem::remove_all(working_dir);
6566
die("Failed to clone at directory '{}'", working_dir.string());
@@ -70,26 +71,31 @@ void PluginManager::add_repo_plugins(const std::string& repo)
7071
CManifest manifest(MANIFEST_NAME);
7172

7273
status("Querying all plugins declared from the manifest");
73-
const std::vector<manifest_t>& plugins = manifest.get_all_plugins();
74+
const std::vector<plugin_t>& plugins = manifest.get_all_plugins();
7475
if (plugins.empty())
7576
{
7677
std::filesystem::remove_all(working_dir);
7778
die("Looks like there are no plugins to build with '{}'", repo_name);
7879
}
7980
success("Queried the plugins");
8081

81-
for (const manifest_t& plugin : plugins)
82+
for (const plugin_t& plugin : plugins)
8283
{
8384
status("Trying to build plugin '{}'", plugin.name);
8485
for (const std::string& bs : plugin.build_steps)
8586
{
86-
Process process(bs, "", nullptr);
87-
if (process.get_exit_status() != 0)
88-
{
87+
if (Process(bs, "").get_exit_status() != 0)
88+
{
8989
std::filesystem::remove_all(working_dir);
9090
die("Failed to build plugin '{}' from '{}'", plugin.name, repo);
9191
}
9292
}
93-
success("Successfully built '{}'", plugin.name);
93+
success("Successfully built '{}' into '{}'", plugin.name, plugin.output_dir);
94+
m_state.add_new_plugin(plugin);
9495
}
96+
97+
const std::filesystem::path& new_plugin_path = (m_cache_path / repo_name);
98+
success("Plugins Successfully built! Moving it to '{}'...", new_plugin_path.string());
99+
std::filesystem::create_directories(new_plugin_path);
100+
std::filesystem::rename(working_dir, new_plugin_path);
95101
}

0 commit comments

Comments
 (0)