Skip to content

Commit 4eb89e3

Browse files
author
Fszontagh
committed
Mod. external module detection, enhance module info and list, enhance ext. module naming
1 parent 8a1140d commit 4eb89e3

File tree

7 files changed

+172
-40
lines changed

7 files changed

+172
-40
lines changed

Modules/Curl/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ project(
1212

1313
find_package(CURL REQUIRED)
1414

15-
add_library(modules-${MODULE_NAME} SHARED)
16-
target_sources(modules-${MODULE_NAME}
15+
add_library(voidscript-module-${MODULE_NAME} SHARED)
16+
target_sources(voidscript-module-${MODULE_NAME}
1717
PRIVATE
1818
src/CurlModule.cpp
1919
src/PluginInit.cpp
2020
)
2121

22-
target_link_libraries(modules-${MODULE_NAME} PRIVATE curl)
22+
target_link_libraries(voidscript-module-${MODULE_NAME} PRIVATE curl)
2323

24-
install(TARGETS modules-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")
24+
install(TARGETS voidscript-module-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")

Modules/Format/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ project(
1414
find_package(fmt REQUIRED)
1515

1616

17-
add_library(modules-format SHARED)
18-
target_sources(modules-format
17+
add_library(voidscript-module-format SHARED)
18+
target_sources(voidscript-module-format
1919
PRIVATE
2020
src/FormatModule.cpp
2121
src/PluginInit.cpp
2222
)
2323

24-
target_link_libraries(modules-format PRIVATE fmt::fmt)
24+
target_link_libraries(voidscript-module-format PRIVATE fmt::fmt)
2525

26-
install(TARGETS modules-format DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-format")
26+
install(TARGETS voidscript-module-format DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-format")

Modules/Imagick/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
1717

1818
find_package(ImageMagick REQUIRED COMPONENTS Magick++)
1919

20-
add_library(modules-imagick SHARED)
21-
target_sources(modules-imagick
20+
add_library(voidscript-module-imagick SHARED)
21+
target_sources(voidscript-module-imagick
2222
PRIVATE
2323
src/ImagickModule.cpp
2424
src/PluginInit.cpp
2525
)
2626

27-
target_include_directories(modules-imagick PRIVATE ${ImageMagick_INCLUDE_DIRS})
28-
target_link_libraries(modules-imagick PRIVATE ${ImageMagick_LIBRARIES})
27+
target_include_directories(voidscript-module-imagick PRIVATE ${ImageMagick_INCLUDE_DIRS})
28+
target_link_libraries(voidscript-module-imagick PRIVATE ${ImageMagick_LIBRARIES})
2929

30-
install(TARGETS modules-imagick DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-imagick")
30+
install(TARGETS voidscript-module-imagick DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-imagick")

Modules/MariaDb/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ if (NOT MARIADB_INCLUDE_DIR OR NOT MARIADBCLIENT_LIB)
1717
message(FATAL "MariaDB client library or header not found")
1818
endif()
1919

20-
add_library(modules-mariadb SHARED)
21-
target_sources(modules-mariadb
20+
add_library(voidscript-module-mariadb SHARED)
21+
target_sources(voidscript-module-mariadb
2222
PRIVATE
2323
src/MariaDBModule.cpp
2424
src/PluginInit.cpp
2525
)
26-
target_include_directories(modules-mariadb PRIVATE ${MARIADB_INCLUDE_DIR})
27-
target_link_libraries(modules-mariadb PRIVATE voidscript ${MARIADBCLIENT_LIB})
26+
target_include_directories(voidscript-module-mariadb PRIVATE ${MARIADB_INCLUDE_DIR})
27+
target_link_libraries(voidscript-module-mariadb PRIVATE voidscript ${MARIADBCLIENT_LIB})
2828

2929

30-
install(TARGETS modules-mariadb DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT modules-mariadb)
30+
install(TARGETS voidscript-module-mariadb DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT modules-mariadb)

Modules/MongoDb/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ message(STATUS "BSONCXX_INCLUDE_DIRS: ${BSONCXX_INCLUDE_DIRS}")
2020
message(STATUS "MONGOCXX_LIBRARIES: ${MONGOCXX_LIBRARIES}")
2121
message(STATUS "BSONCXX_LIBRARIES: ${BSONCXX_LIBRARIES}")
2222

23-
add_library(modules-${MODULE_NAME} SHARED)
24-
target_sources(modules-${MODULE_NAME}
23+
add_library(voidscript-module-${MODULE_NAME} SHARED)
24+
target_sources(voidscript-module-${MODULE_NAME}
2525
PRIVATE
2626
src/MongoDBModule.cpp
2727
src/PluginInit.cpp
2828
)
29-
target_include_directories(modules-${MODULE_NAME} PRIVATE ${MONGOCXX_INCLUDE_DIRS} ${BSONCXX_INCLUDE_DIRS})
30-
target_link_libraries(modules-${MODULE_NAME} PRIVATE voidscript ${MONGOCXX_LIBRARIES} ${BSONCXX_LIBRARIES})
29+
target_include_directories(voidscript-module-${MODULE_NAME} PRIVATE ${MONGOCXX_INCLUDE_DIRS} ${BSONCXX_INCLUDE_DIRS})
30+
target_link_libraries(voidscript-module-${MODULE_NAME} PRIVATE voidscript ${MONGOCXX_LIBRARIES} ${BSONCXX_LIBRARIES})
3131

32-
install(TARGETS modules-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")
32+
install(TARGETS voidscript-module-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")

Modules/Xml2/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ project(
1515
)
1616

1717
find_package(LibXml2 REQUIRED)
18-
add_library(modules-${MODULE_NAME} SHARED)
19-
target_sources(modules-${MODULE_NAME}
18+
add_library(voidscript-module-${MODULE_NAME} SHARED)
19+
target_sources(voidscript-module-${MODULE_NAME}
2020
PRIVATE
2121
src/XmlModule.cpp
2222
src/PluginInit.cpp
2323
)
2424

25-
target_include_directories(modules-${MODULE_NAME} PRIVATE ${LIBXML2_INCLUDE_DIRS})
26-
target_link_libraries(modules-${MODULE_NAME} PRIVATE ${LIBXML2_LIBRARIES})
25+
target_include_directories(voidscript-module-${MODULE_NAME} PRIVATE ${LIBXML2_INCLUDE_DIRS})
26+
target_link_libraries(voidscript-module-${MODULE_NAME} PRIVATE ${LIBXML2_LIBRARIES})
2727

28-
install(TARGETS modules-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")
28+
install(TARGETS voidscript-module-${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME}/Modules COMPONENT "modules-${MODULE_NAME}")

cli/main.cpp

Lines changed: 144 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

33
#include <iostream>
44
#include <unordered_map>
5+
#include <unordered_set>
56
#include <vector>
7+
#include <fstream>
8+
#include <sstream>
9+
#include <filesystem>
10+
#include <algorithm>
611

712
#include "options.h"
813
#include "utils.h"
914
#include "VoidScript.hpp"
1015
#include "Symbols/SymbolContainer.hpp"
1116

17+
// Struct to hold module information
18+
struct ModuleInfo {
19+
std::string name;
20+
std::string path;
21+
std::string description;
22+
};
23+
1224
// Supported command-line parameters and descriptions
1325
const std::unordered_map<std::string, std::string> params = {
1426
{ "--help", "Print this help message" },
@@ -17,10 +29,60 @@ const std::unordered_map<std::string, std::string> params = {
1729
{ "--enable-tags", "Only parse tokens between PARSER_OPEN_TAG and PARSER_CLOSE_TAG when enabled" },
1830
{ "--suppress-tags-outside",
1931
"Suppress text outside PARSER_OPEN_TAG/PARSER_CLOSE_TAG when tag filtering is enabled" },
20-
{ "-m, --modules", "List loaded modules" },
32+
{ "-m, --modules", "List loaded modules with detailed information" },
33+
{ "--module-info", "Display detailed information about a specific module" },
2134
{ "-c, --command", "Execute script string instead of reading from file" },
2235
};
2336

37+
/**
38+
* @brief Scan Modules/ directory for external modules
39+
* @return Vector of external ModuleInfo
40+
*/
41+
static std::vector<ModuleInfo> scanExternalModules() {
42+
std::vector<ModuleInfo> modules;
43+
44+
// Determine modules directory (similar to VoidScript::loadPlugins)
45+
std::string modulesPath = "./Modules";
46+
47+
// Try to find the modules directory relative to executable if possible
48+
char exePath[1024];
49+
ssize_t count = readlink("/proc/self/exe", exePath, sizeof(exePath));
50+
if (count != -1) {
51+
exePath[count] = '\0';
52+
std::string binPath(exePath);
53+
size_t lastSlash = binPath.find_last_of("/\\");
54+
std::string binDir = (lastSlash != std::string::npos) ? binPath.substr(0, lastSlash) : ".";
55+
std::string altPath = binDir + "/Modules";
56+
if (utils::exists(altPath) && utils::is_directory(altPath)) {
57+
modulesPath = altPath;
58+
}
59+
}
60+
61+
// Scan modules directory if it exists
62+
if (utils::exists(modulesPath) && utils::is_directory(modulesPath)) {
63+
for (const auto& entry : std::filesystem::directory_iterator(modulesPath)) {
64+
if (entry.is_directory()) {
65+
std::string module_name = entry.path().filename().string();
66+
std::string lowercase_name = module_name;
67+
std::transform(lowercase_name.begin(), lowercase_name.end(), lowercase_name.begin(), ::tolower);
68+
std::string lib_name = "libvoidscript-module-" + lowercase_name + ".so";
69+
std::string module_path = modulesPath + "/" + module_name + "/" + lib_name;
70+
71+
// Try to get description from SymbolContainer (will be available after modules are loaded)
72+
auto symbolContainer = Symbols::SymbolContainer::instance();
73+
std::string description = symbolContainer->getModuleDescription(module_name);
74+
if (description.empty()) {
75+
description = "No description available.";
76+
}
77+
78+
modules.push_back({module_name, module_path, description});
79+
}
80+
}
81+
}
82+
83+
return modules;
84+
}
85+
2486
int main(int argc, char * argv[]) {
2587
std::string usage = "Usage: " + std::string(argv[0]);
2688
for (const auto & [key, value] : params) {
@@ -87,34 +149,104 @@ int main(int argc, char * argv[]) {
87149
suppressTagsOutside = true;
88150
} else if (a == "-m" || a == "--modules") {
89151
VoidScript voidscript("modules", false, false, false, false, false, false, std::vector<std::string>{});
90-
auto modules = Symbols::SymbolContainer::instance()->getModuleNames();
152+
auto symbolContainer = Symbols::SymbolContainer::instance();
153+
auto moduleNames = symbolContainer->getModuleNames();
91154

92-
// Define external modules based on their location in ./Modules/
93-
const std::vector<std::string> externalModules = {"Curl", "Format", "Imagick", "MariaDb", "Xml2"};
155+
// Scan external modules from directory
156+
std::vector<ModuleInfo> externalModules = scanExternalModules();
157+
std::unordered_set<std::string> externalModuleNames;
158+
for (const auto& mod : externalModules) {
159+
externalModuleNames.insert(mod.name);
160+
}
94161

95-
// Separate modules into built-in and external
96-
std::vector<std::string> builtin, external;
97-
for (const auto& module : modules) {
98-
if (std::find(externalModules.begin(), externalModules.end(), module) != externalModules.end()) {
99-
external.push_back(module);
162+
// Separate modules into built-in and external ModuleInfo
163+
std::vector<ModuleInfo> builtin, external;
164+
for (const auto& name : moduleNames) {
165+
std::string description = symbolContainer->getModuleDescription(name);
166+
if (description.empty()) {
167+
description = "No description available.";
168+
}
169+
if (externalModuleNames.count(name)) {
170+
// Get path from external modules
171+
for (const auto& extMod : externalModules) {
172+
if (extMod.name == name) {
173+
external.push_back({name, extMod.path, description});
174+
break;
175+
}
176+
}
100177
} else {
101-
builtin.push_back(module);
178+
builtin.push_back({name, "", description});
102179
}
103180
}
104181

105182
// Print built-in modules
106183
std::cout << "Built-in modules:\n";
107184
for (const auto& module : builtin) {
108-
std::cout << " " << module << "\n";
185+
std::cout << " Name: " << module.name << "\n";
186+
std::cout << " Path: " << (module.path.empty() ? "(built-in)" : module.path) << "\n";
187+
std::cout << " Description: " << module.description << "\n";
188+
std::cout << "\n";
109189
}
110190
std::cout << "\n";
111191

112192
// Print external modules
113193
std::cout << "External modules:\n";
114194
for (const auto& module : external) {
115-
std::cout << " " << module << "\n";
195+
std::cout << " Name: " << module.name << "\n";
196+
std::cout << " Path: " << module.path << "\n";
197+
std::cout << " Description: " << module.description << "\n";
198+
std::cout << "\n";
116199
}
117200
return 0;
201+
} else if (a == "--module-info") {
202+
// Next argument should be the module name
203+
if (i + 1 >= argc) {
204+
std::cerr << "Error: --module-info requires a module name\n";
205+
std::cerr << usage << "\n";
206+
return 1;
207+
}
208+
std::string moduleName = argv[++i];
209+
210+
VoidScript voidscript("module-info", false, false, false, false, false, false, std::vector<std::string>{});
211+
auto symbolContainer = Symbols::SymbolContainer::instance();
212+
auto moduleNames = symbolContainer->getModuleNames();
213+
214+
// Check if the module exists
215+
if (std::find(moduleNames.begin(), moduleNames.end(), moduleName) != moduleNames.end()) {
216+
std::string description = symbolContainer->getModuleDescription(moduleName);
217+
if (description.empty()) {
218+
description = "No description available.";
219+
}
220+
221+
std::cout << "Module Information:\n";
222+
std::cout << " Name: " << moduleName << "\n";
223+
224+
// Check if external
225+
std::vector<ModuleInfo> externalModules = scanExternalModules();
226+
bool isExternal = false;
227+
std::string path = "";
228+
for (const auto& mod : externalModules) {
229+
if (mod.name == moduleName) {
230+
path = mod.path;
231+
isExternal = true;
232+
break;
233+
}
234+
}
235+
236+
if (isExternal) {
237+
std::cout << " Path: " << path << "\n";
238+
std::cout << " Type: External\n";
239+
} else {
240+
std::cout << " Path: (built-in)\n";
241+
std::cout << " Type: Built-in\n";
242+
}
243+
std::cout << " Description: " << description << "\n";
244+
return 0;
245+
}
246+
247+
// Not found
248+
std::cerr << "Error: Module '" << moduleName << "' not found.\n";
249+
return 1;
118250
} else if (a == "-c" || a == "--command") {
119251
// Next argument should be the script content
120252
if (i + 1 >= argc) {

0 commit comments

Comments
 (0)