Skip to content

Commit abaab30

Browse files
committed
cufetchpm: move state.toml to the config directory
plugins: only load plugins that have a supported OS library extension
1 parent cdf4b5e commit abaab30

File tree

7 files changed

+57
-38
lines changed

7 files changed

+57
-38
lines changed

cufetchpm/include/manifest.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class CManifest
7777
{ return m_repo.plugins; }
7878

7979
private:
80-
toml::table m_tbl;
81-
manifest_t m_repo;
80+
toml::table m_tbl;
81+
manifest_t m_repo;
8282

8383
void parse_manifest();
8484

cufetchpm/include/pluginManager.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class PluginManager
4242
bool has_deps();
4343

4444
private:
45-
StateManager m_state;
46-
fs::path m_config_path{ getConfigDir() / "plugins" };
47-
fs::path m_cache_path{ getHomeCacheDir() / "cufetchpm" / "plugins" };
45+
StateManager m_state;
46+
fs::path m_config_path{ getConfigDir() / "plugins" };
47+
fs::path m_cache_path{ getHomeCacheDir() / "cufetchpm" / "plugins" };
4848
};
4949

5050
#endif

cufetchpm/include/stateManager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class StateManager
2222
toml::table get_state() { return m_state; }
2323

2424
private:
25-
const fs::path m_path{ getHomeCacheDir() / "cufetchpm" / "state.toml" };
26-
toml::table m_state;
25+
const fs::path m_path{ getConfigDir() / "plugins" / "state.toml" };
26+
toml::table m_state;
2727
};
2828

2929
#endif

cufetchpm/src/main.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <cstring>
2+
23
#include "libcufetch/common.hh"
3-
#include "libs/switch_fnv1a.hpp"
44
#include "pluginManager.hpp"
55
#include "stateManager.hpp"
66

@@ -12,17 +12,18 @@
1212

1313
#include "getopt_port/getopt.h"
1414

15-
enum {
15+
static int op = 0;
16+
enum
17+
{
1618
INSTALL,
1719
REMOVE,
1820
LIST
1921
};
2022

21-
static struct operations_t
23+
static struct operations_install_t
2224
{
23-
int name;
2425
std::vector<std::string> args;
25-
} op;
26+
} install_op;
2627

2728
static void version()
2829
{
@@ -44,7 +45,7 @@ Manage plugins for customfetch.
4445
NOTE: the operations must be the first argument to pass
4546
4647
OPERATIONS:
47-
install - Install a new plugin repository. Takes as an argument the git url to be cloned.
48+
install - Install a new plugin repository. Takes as an argument the git url to be cloned.
4849
4950
GENERAL OPTIONS
5051
-h, --help Print this help menu.
@@ -76,19 +77,19 @@ static bool parseargs(int argc, char* argv[])
7677
{
7778
if (strncmp(argv[i], "install", 7) == 0 || strncmp(argv[i], "i", 1) == 0)
7879
{
79-
op.name = INSTALL;
80+
op = INSTALL;
8081
optind++;
8182
break;
8283
}
83-
if (strncmp(argv[i], "list", 4) == 0|| strncmp(argv[i], "l", 1) == 0)
84+
if (strncmp(argv[i], "list", 4) == 0 || strncmp(argv[i], "l", 1) == 0)
8485
{
85-
op.name = LIST;
86+
op = LIST;
8687
optind++;
8788
break;
8889
}
8990
if (strncmp(argv[i], "remove", 6) == 0 || strncmp(argv[i], "r", 1) == 0)
9091
{
91-
op.name = REMOVE;
92+
op = REMOVE;
9293
optind++;
9394
break;
9495
}
@@ -107,8 +108,12 @@ static bool parseargs(int argc, char* argv[])
107108
}
108109
}
109110

110-
for (int i = optind; i < argc; ++i)
111-
op.args.push_back(argv[i]);
111+
switch (op)
112+
{
113+
case INSTALL:
114+
for (int i = optind; i < argc; ++i)
115+
install_op.args.push_back(argv[i]);
116+
}
112117

113118
return true;
114119
}
@@ -119,18 +124,18 @@ int main(int argc, char* argv[])
119124
return -1;
120125

121126
fs::create_directories({ getHomeCacheDir() / "cufetchpm" / "plugins" });
122-
StateManager state;
123-
PluginManager plugin_manager(std::move(state));
124-
switch (op.name)
127+
switch (op)
125128
{
126-
case INSTALL:
127-
{
128-
if (op.args.size() != 1)
129-
die("Please provide a singular git url repository");
130-
plugin_manager.add_repo_plugins(op.args[0]); break;
131-
}
132-
default:
133-
warn("Not yet implemented");
129+
case INSTALL:
130+
{
131+
if (install_op.args.size() != 1)
132+
die("Please provide a singular git url repository");
133+
StateManager state;
134+
PluginManager plugin_manager(std::move(state));
135+
plugin_manager.add_repo_plugins(install_op.args[0]);
136+
break;
137+
}
138+
default: warn("Not yet implemented");
134139
}
135140

136141
return 0;

cufetchpm/src/stateManager.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static toml::table& ensure_table(toml::table& parent, std::string_view key)
3535
return *it->second.as_table();
3636
}
3737

38-
static toml::array vector_to_array(const std::vector<std::string> vec)
38+
static toml::array vector_to_array(const std::vector<std::string>& vec)
3939
{
4040
toml::array ret;
4141
for (const std::string& str : vec)
@@ -48,7 +48,7 @@ StateManager::StateManager()
4848
if (!fs::exists(m_path))
4949
{
5050
auto f = fmt::output_file(m_path.string(), fmt::file::WRONLY | fmt::file::TRUNC | fmt::file::CREATE);
51-
f.print(R"(# AUTOGENERATED FILE. DO NOT EDIT THIS FILE.
51+
f.print(R"(# AUTO-GENERATED FILE. DO NOT EDIT THIS FILE.
5252
# YOU GONNA MESS SHIT UP. unless you know what you doing ofc
5353
)");
5454
f.close();
@@ -77,19 +77,22 @@ void StateManager::add_new_repo(const CManifest& manifest)
7777
toml::array plugins_arr;
7878
for (const plugin_t& plugin : manifest.get_all_plugins())
7979
{
80-
toml::table entry{ { "name", plugin.name },
80+
// will be inserted in alphabetical order
81+
toml::table entry{ { "name", plugin.name },
8182
{ "description", plugin.description },
82-
{ "authors", vector_to_array(plugin.authors) },
83-
{ "licenses", vector_to_array(plugin.licenses) },
84-
{ "output-dir", plugin.output_dir },
85-
{ "conflicts", vector_to_array(plugin.conflicts) },
86-
{ "prefixes", vector_to_array(plugin.prefixes) } };
83+
{ "output-dir", plugin.output_dir },
84+
{ "authors", vector_to_array(plugin.authors) },
85+
{ "licenses", vector_to_array(plugin.licenses) },
86+
{ "conflicts", vector_to_array(plugin.conflicts) },
87+
{ "prefixes", vector_to_array(plugin.prefixes) } };
8788

8889
plugins_arr.push_back(std::move(entry));
8990
}
9091

9192
repo.insert_or_assign("plugins", std::move(plugins_arr));
9293
std::stringstream ss;
94+
ss << R"(# AUTO-GENERATED FILE. DO NOT EDIT THIS FILE.
95+
# YOU GONNA MESS SHIT UP. unless you know what you doing ofc")";
9396
ss << m_state;
9497

9598
if (!writeState(ss.str(), m_path))

include/util.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ struct byte_units_t
7878

7979
#define UNLOAD_LIBRARY(handle) dlclose(handle);
8080

81+
#if CF_WINDOWS
82+
constexpr char LIBRARY_EXTENSION[] = ".dll";
83+
#elif CF_MACOS
84+
constexpr char LIBRARY_EXTENSION[] = ".dylib";
85+
#else
86+
constexpr char LIBRARY_EXTENSION[] = ".so";
87+
#endif
88+
8189
inline bool is_live_mode = false;
8290

8391
/* https://stackoverflow.com/questions/874134/find-out-if-string-ends-with-another-string-in-c#874160

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ int main(int argc, char* argv[])
638638
std::filesystem::create_directories(pluginDir);
639639
for (const auto& entry : std::filesystem::recursive_directory_iterator{ pluginDir })
640640
{
641+
if (entry.is_regular_file() && entry.path().has_extension() && entry.path().extension() == LIBRARY_EXTENSION){}
642+
else {continue;}
643+
641644
debug("loading plugin at {}!", entry.path().string());
642645

643646
void* handle = LOAD_LIBRARY(std::filesystem::absolute(entry.path()).c_str());

0 commit comments

Comments
 (0)